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

Thalhammer / jwt-cpp / 11876093722

17 Nov 2024 04:58AM CUT 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

77.27
/include/jwt-cpp/traits/open-source-parsers-jsoncpp/traits.h
1
#ifndef JWT_CPP_JSONCPP_TRAITS_H
2
#define JWT_CPP_JSONCPP_TRAITS_H
3

4
#include "jwt-cpp/jwt.h"
5
#include "json/json.h"
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 jsoncpp
13
                struct open_source_parsers_jsoncpp {
14
                        using value_type = Json::Value;
15
                        using string_type = std::string;
16
                        class array_type : public Json::Value {
17
                        public:
18
                                using value_type = Json::Value;
19

20
                                array_type() = default;
21
                                array_type(const array_type&) = default;
22
                                explicit array_type(const Json::Value& o) : Json::Value(o) {}
×
23
                                array_type(array_type&&) = default;
24
                                explicit array_type(Json::Value&& o) : Json::Value(o) {}
25
                                template<typename Iterator>
26
                                array_type(Iterator begin, Iterator end) {
3✔
27
                                        for (Iterator it = begin; it != end; ++it) {
10✔
28
                                                Json::Value value;
7✔
29
                                                value = *it;
7✔
30
                                                this->append(value);
7✔
31
                                        }
32
                                }
3✔
33
                                ~array_type() = default;
3✔
34
                                array_type& operator=(const array_type& o) = default;
35
                                array_type& operator=(array_type&& o) noexcept = default;
36

37
                                value_type const& front() const { return this->operator[](0U); }
38
                        };
39
                        using number_type = double;
40
                        using integer_type = Json::Value::Int;
41
                        using boolean_type = bool;
42
                        class object_type : public Json::Value {
43
                        public:
44
                                using key_type = std::string;
45
                                using mapped_type = Json::Value;
46
                                using size_type = size_t;
47

48
                                object_type() = default;
24✔
49
                                object_type(const object_type&) = default;
5✔
50
                                explicit object_type(const Json::Value& o) : Json::Value(o) {}
14✔
51
                                object_type(object_type&&) = default;
14✔
52
                                explicit object_type(Json::Value&& o) : Json::Value(o) {}
53
                                ~object_type() = default;
57✔
54
                                object_type& operator=(const object_type& o) = default;
55
                                object_type& operator=(object_type&& o) noexcept = default;
14✔
56

57
                                // Add missing C++11 element access
58
                                const mapped_type& at(const key_type& key) const {
25✔
59
                                        Json::Value const* found = find(key.data(), key.data() + key.length());
25✔
60
                                        if (!found) throw std::out_of_range("invalid key");
25✔
61
                                        return *found;
25✔
62
                                }
63

64
                                size_type count(const key_type& key) const { return this->isMember(key) ? 1 : 0; }
69✔
65
                        };
66

67
                        // Translation between the implementation notion of type, to the jwt::json::type equivilant
68
                        static jwt::json::type get_type(const value_type& val) {
34✔
69
                                using jwt::json::type;
70

71
                                if (val.isArray())
34✔
72
                                        return type::array;
4✔
73
                                else if (val.isString())
30✔
74
                                        return type::string;
17✔
75
                                // Order is important https://github.com/Thalhammer/jwt-cpp/pull/320#issuecomment-1865322511
76
                                else if (val.isInt())
13✔
77
                                        return type::integer;
9✔
78
                                else if (val.isNumeric())
4✔
79
                                        return type::number;
×
80
                                else if (val.isBool())
4✔
81
                                        return type::boolean;
×
82
                                else if (val.isObject())
4✔
83
                                        return type::object;
4✔
84

85
                                throw std::logic_error("invalid type");
×
86
                        }
87

88
                        static integer_type as_integer(const value_type& val) {
8✔
89
                                switch (val.type()) {
8✔
90
                                case Json::intValue: return val.asInt64();
8✔
91
                                case Json::uintValue: return static_cast<integer_type>(val.asUInt64());
×
92
                                default: throw std::bad_cast();
×
93
                                }
94
                        }
95

96
                        static boolean_type as_boolean(const value_type& val) {
×
97
                                if (!val.isBool()) throw std::bad_cast();
×
98
                                return val.asBool();
×
99
                        }
100

101
                        static number_type as_number(const value_type& val) {
×
102
                                if (!val.isNumeric()) throw std::bad_cast();
×
103
                                return val.asDouble();
×
104
                        }
105

106
                        static string_type as_string(const value_type& val) {
20✔
107
                                if (!val.isString()) throw std::bad_cast();
20✔
108
                                return val.asString();
20✔
109
                        }
110

111
                        static object_type as_object(const value_type& val) {
14✔
112
                                if (!val.isObject()) throw std::bad_cast();
14✔
113
                                return object_type(val);
14✔
114
                        }
115

116
                        static array_type as_array(const value_type& val) {
×
117
                                if (!val.isArray()) throw std::bad_cast();
×
118
                                return array_type(val);
×
119
                        }
120

121
                        static bool parse(value_type& val, string_type str) {
14✔
122
                                Json::CharReaderBuilder builder;
14✔
123
                                const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
14✔
124

125
                                return reader->parse(reinterpret_cast<const char*>(str.c_str()),
28✔
126
                                                                         reinterpret_cast<const char*>(str.c_str() + str.size()), &val, nullptr);
42✔
127
                        }
14✔
128

129
                        static string_type serialize(const value_type& val) {
14✔
130
                                Json::StreamWriterBuilder builder;
14✔
131
                                builder["commentStyle"] = "None";
14✔
132
                                builder["indentation"] = "";
14✔
133
                                std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
14✔
134
                                return Json::writeString(builder, val);
28✔
135
                        }
14✔
136
                };
137
        } // namespace traits
138
} // namespace jwt
139

140
#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