JWT-CPP v0.7.0
A header only library for creating and validating JSON Web Tokens (JWT) in C++
Loading...
Searching...
No Matches
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
7namespace jwt {
11 namespace traits {
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) {
27 for (Iterator it = begin; it != end; ++it) {
28 Json::Value value;
29 value = *it;
30 this->append(value);
31 }
32 }
33 ~array_type() = default;
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;
49 object_type(const object_type&) = default;
50 explicit object_type(const Json::Value& o) : Json::Value(o) {}
51 object_type(object_type&&) = default;
52 explicit object_type(Json::Value&& o) : Json::Value(o) {}
53 ~object_type() = default;
54 object_type& operator=(const object_type& o) = default;
55 object_type& operator=(object_type&& o) noexcept = default;
56
57 // Add missing C++11 element access
58 const mapped_type& at(const key_type& key) const {
59 Json::Value const* found = find(key.data(), key.data() + key.length());
60 if (!found) throw std::out_of_range("invalid key");
61 return *found;
62 }
63
64 size_type count(const key_type& key) const { return this->isMember(key) ? 1 : 0; }
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) {
69 using jwt::json::type;
70
71 if (val.isArray())
72 return type::array;
73 else if (val.isString())
74 return type::string;
75 // Order is important https://github.com/Thalhammer/jwt-cpp/pull/320#issuecomment-1865322511
76 else if (val.isInt())
77 return type::integer;
78 else if (val.isNumeric())
79 return type::number;
80 else if (val.isBool())
81 return type::boolean;
82 else if (val.isObject())
83 return type::object;
84
85 throw std::logic_error("invalid type");
86 }
87
88 static integer_type as_integer(const value_type& val) {
89 switch (val.type()) {
90 case Json::intValue: return val.asInt64();
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) {
107 if (!val.isString()) throw std::bad_cast();
108 return val.asString();
109 }
110
111 static object_type as_object(const value_type& val) {
112 if (!val.isObject()) throw std::bad_cast();
113 return object_type(val);
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) {
122 Json::CharReaderBuilder builder;
123 const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
124
125 return reader->parse(reinterpret_cast<const char*>(str.c_str()),
126 reinterpret_cast<const char*>(str.c_str() + str.size()), &val, nullptr);
127 }
128
129 static string_type serialize(const value_type& val) {
130 Json::StreamWriterBuilder builder;
131 builder["commentStyle"] = "None";
132 builder["indentation"] = "";
133 std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
134 return Json::writeString(builder, val);
135 }
136 };
137 } // namespace traits
138} // namespace jwt
139
140#endif
type
Categories for the various JSON types used in JWTs.
Definition jwt.h:2274
JSON Web Token.
Definition base.h:21
basic_claim's JSON trait implementation for jsoncpp
Definition traits.h:13