• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Thalhammer / jwt-cpp / 11876093722

17 Nov 2024 04:58AM UTC coverage: 94.974%. Remained the same
11876093722

push

github

web-flow
Ensure array_type has front() method + fix array_type on jsoncpp (#361)

* Use operator[] instead of front to support jsoncpp

Replace the `front()` with `[0]` as front is not supported by jsoncpp on
array types.

Signed-off-by: Omar Mohamed <mohamed.omar67492@gmail.com>

* Revert "Use operator[] instead of front to support jsoncpp"

This reverts commit dfa7d29da.

* Implement `front()` in jsoncpp array_type

Signed-off-by: Omar Mohamed <mohamed.omar67492@gmail.com>

* Check if array_type has a front() method

Signed-off-by: Omar Mohamed <mohamed.omar67492@gmail.com>

* Add `front()` to jsoncons array_type

* Remove non-const overloads of front()

Signed-off-by: Omar Mohamed <mohamed.omar67492@gmail.com>

* Fix check for front() in array_type

Signed-off-by: Omar Mohamed <mohamed.omar67492@gmail.com>

* Use std::decay

Signed-off-by: Omar Mohamed <mohamed.omar67492@gmail.com>

* fix example to print array value

* expose base class ctors

* update as_array traits helper

* fixup linter

---------

Signed-off-by: Omar Mohamed <mohamed.omar67492@gmail.com>
Co-authored-by: Chris Mc <prince.chrismc@gmail.com>

1285 of 1353 relevant lines covered (94.97%)

255.52 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

66.67
/include/jwt-cpp/traits/nlohmann-json/traits.h
1
#ifndef JWT_CPP_NLOHMANN_JSON_TRAITS_H
2
#define JWT_CPP_NLOHMANN_JSON_TRAITS_H
3

4
#include "jwt-cpp/jwt.h"
5
#include "nlohmann/json.hpp"
6

7
namespace jwt {
8
        /**
9
         * \brief Namespace containing all the json_trait implementations for a jwt::basic_claim.
10
        */
11
        namespace traits {
12
                /// basic_claim's JSON trait implementation for Modern C++ JSON
13
                struct nlohmann_json {
14
                        using json = nlohmann::json;
15
                        using value_type = json;
16
                        using object_type = json::object_t;
17
                        using array_type = json::array_t;
18
                        using string_type = std::string; // current limitation of traits implementation
19
                        using number_type = json::number_float_t;
20
                        using integer_type = json::number_integer_t;
21
                        using boolean_type = json::boolean_t;
22

23
                        static jwt::json::type get_type(const json& val) {
34✔
24
                                using jwt::json::type;
25

26
                                if (val.type() == json::value_t::boolean) return type::boolean;
34✔
27
                                // nlohmann internally tracks two types of integers
28
                                if (val.type() == json::value_t::number_integer) return type::integer;
34✔
29
                                if (val.type() == json::value_t::number_unsigned) return type::integer;
33✔
30
                                if (val.type() == json::value_t::number_float) return type::number;
25✔
31
                                if (val.type() == json::value_t::string) return type::string;
25✔
32
                                if (val.type() == json::value_t::array) return type::array;
8✔
33
                                if (val.type() == json::value_t::object) return type::object;
4✔
34

35
                                throw std::logic_error("invalid type");
×
36
                        }
37

38
                        static json::object_t as_object(const json& val) {
16✔
39
                                if (val.type() != json::value_t::object) throw std::bad_cast();
16✔
40
                                return val.get<json::object_t>();
16✔
41
                        }
42

43
                        static std::string as_string(const json& val) {
24✔
44
                                if (val.type() != json::value_t::string) throw std::bad_cast();
24✔
45
                                return val.get<std::string>();
24✔
46
                        }
47

48
                        static json::array_t as_array(const json& val) {
×
49
                                if (val.type() != json::value_t::array) throw std::bad_cast();
×
50
                                return val.get<json::array_t>();
×
51
                        }
52

53
                        static int64_t as_integer(const json& val) {
8✔
54
                                switch (val.type()) {
8✔
55
                                case json::value_t::number_integer:
8✔
56
                                case json::value_t::number_unsigned: return val.get<int64_t>();
8✔
57
                                default: throw std::bad_cast();
×
58
                                }
59
                        }
60

61
                        static bool as_boolean(const json& val) {
×
62
                                if (val.type() != json::value_t::boolean) throw std::bad_cast();
×
63
                                return val.get<bool>();
×
64
                        }
65

66
                        static double as_number(const json& val) {
×
67
                                if (val.type() != json::value_t::number_float) throw std::bad_cast();
×
68
                                return val.get<double>();
×
69
                        }
70

71
                        static bool parse(json& val, std::string str) {
16✔
72
                                val = json::parse(str.begin(), str.end());
16✔
73
                                return true;
16✔
74
                        }
75

76
                        static std::string serialize(const json& val) { return val.dump(); }
14✔
77
                };
78
        } // namespace traits
79
} // namespace jwt
80

81
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc