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

Thalhammer / jwt-cpp / 7290989515

21 Dec 2023 04:53PM UTC coverage: 94.988% (-1.0%) from 96.01%
7290989515

push

github

web-flow
Update tests to include jsoncpp trait (#320)

* update tests to run jsoncpp

* fix order for type checking

Co-authored-by: cjserio <cjserio@users.noreply.github.com>

* linter should be more specific on on default for trait

* fix linter

* output folder name

* use filepath for linter processing

---------

Co-authored-by: cjserio <cjserio@users.noreply.github.com>

1 of 2 new or added lines in 1 file covered. (50.0%)

1156 of 1217 relevant lines covered (94.99%)

825.29 hits per line

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

76.56
/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
        namespace traits {
9
                struct open_source_parsers_jsoncpp {
10
                        using value_type = Json::Value;
11
                        using string_type = std::string;
12
                        class array_type : public Json::Value {
13
                        public:
14
                                using value_type = Json::Value;
15

16
                                array_type() = default;
17
                                array_type(const array_type&) = default;
18
                                explicit array_type(const Json::Value& o) : Json::Value(o) {}
×
19
                                array_type(array_type&&) = default;
20
                                explicit array_type(Json::Value&& o) : Json::Value(o) {}
21
                                template<typename Iterator>
22
                                array_type(Iterator begin, Iterator end) {
3✔
23
                                        for (Iterator it = begin; it != end; ++it) {
10✔
24
                                                Json::Value value;
7✔
25
                                                value = *it;
7✔
26
                                                this->append(value);
7✔
27
                                        }
28
                                }
3✔
29
                                ~array_type() = default;
3✔
30
                                array_type& operator=(const array_type& o) = default;
31
                                array_type& operator=(array_type&& o) noexcept = default;
32
                        };
33
                        using number_type = double;
34
                        using integer_type = Json::Value::Int;
35
                        using boolean_type = bool;
36
                        class object_type : public Json::Value {
37
                        public:
38
                                using key_type = std::string;
39
                                using mapped_type = Json::Value;
40
                                using size_type = size_t;
41

42
                                object_type() = default;
20✔
43
                                object_type(const object_type&) = default;
4✔
44
                                explicit object_type(const Json::Value& o) : Json::Value(o) {}
12✔
45
                                object_type(object_type&&) = default;
12✔
46
                                explicit object_type(Json::Value&& o) : Json::Value(o) {}
47
                                ~object_type() = default;
48✔
48
                                object_type& operator=(const object_type& o) = default;
49
                                object_type& operator=(object_type&& o) noexcept = default;
12✔
50

51
                                // Add missing C++11 element access
52
                                const mapped_type& at(const key_type& key) const {
21✔
53
                                        Json::Value const* found = find(key.data(), key.data() + key.length());
21✔
54
                                        if (!found) throw std::out_of_range("invalid key");
21✔
55
                                        return *found;
21✔
56
                                }
57

58
                                size_type count(const key_type& key) const { return this->isMember(key) ? 1 : 0; }
60✔
59
                        };
60

61
                        // Translation between the implementation notion of type, to the jwt::json::type equivilant
62
                        static jwt::json::type get_type(const value_type& val) {
29✔
63
                                using jwt::json::type;
64

65
                                if (val.isArray())
29✔
66
                                        return type::array;
4✔
67
                                else if (val.isString())
25✔
68
                                        return type::string;
14✔
69
                                // Order is important https://github.com/Thalhammer/jwt-cpp/pull/320#issuecomment-1865322511
70
                                else if (val.isInt())
11✔
71
                                        return type::integer;
7✔
72
                                else if (val.isNumeric())
4✔
NEW
73
                                        return type::number;
×
74
                                else if (val.isBool())
4✔
75
                                        return type::boolean;
×
76
                                else if (val.isObject())
4✔
77
                                        return type::object;
4✔
78

79
                                throw std::logic_error("invalid type");
×
80
                        }
81

82
                        static integer_type as_integer(const value_type& val) {
6✔
83
                                switch (val.type()) {
6✔
84
                                case Json::intValue: return val.asInt64();
6✔
85
                                case Json::uintValue: return static_cast<integer_type>(val.asUInt64());
×
86
                                default: throw std::bad_cast();
×
87
                                }
88
                        }
89

90
                        static boolean_type as_boolean(const value_type& val) {
×
91
                                if (!val.isBool()) throw std::bad_cast();
×
92
                                return val.asBool();
×
93
                        }
94

95
                        static number_type as_number(const value_type& val) {
×
96
                                if (!val.isNumeric()) throw std::bad_cast();
×
97
                                return val.asDouble();
×
98
                        }
99

100
                        static string_type as_string(const value_type& val) {
17✔
101
                                if (!val.isString()) throw std::bad_cast();
17✔
102
                                return val.asString();
17✔
103
                        }
104

105
                        static object_type as_object(const value_type& val) {
12✔
106
                                if (!val.isObject()) throw std::bad_cast();
12✔
107
                                return object_type(val);
12✔
108
                        }
109

110
                        static array_type as_array(const value_type& val) {
×
111
                                if (!val.isArray()) throw std::bad_cast();
×
112
                                return array_type(val);
×
113
                        }
114

115
                        static bool parse(value_type& val, string_type str) {
12✔
116
                                Json::Reader reader;
12✔
117
                                return reader.parse(str, val);
24✔
118
                        }
12✔
119

120
                        static string_type serialize(const value_type& val) {
12✔
121
                                Json::StreamWriterBuilder builder;
12✔
122
                                builder["commentStyle"] = "None";
12✔
123
                                builder["indentation"] = "";
12✔
124
                                std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
12✔
125
                                return Json::writeString(builder, val);
24✔
126
                        }
12✔
127
                };
128
        } // namespace traits
129
} // namespace jwt
130

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

© 2026 Coveralls, Inc