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 using number_type = double;
38 using integer_type = Json::Value::Int;
39 using boolean_type = bool;
40 class object_type : public Json::Value {
41 public:
42 using key_type = std::string;
43 using mapped_type = Json::Value;
44 using size_type = size_t;
45
46 object_type() = default;
47 object_type(const object_type&) = default;
48 explicit object_type(const Json::Value& o) : Json::Value(o) {}
49 object_type(object_type&&) = default;
50 explicit object_type(Json::Value&& o) : Json::Value(o) {}
51 ~object_type() = default;
52 object_type& operator=(const object_type& o) = default;
53 object_type& operator=(object_type&& o) noexcept = default;
54
55 // Add missing C++11 element access
56 const mapped_type& at(const key_type& key) const {
57 Json::Value const* found = find(key.data(), key.data() + key.length());
58 if (!found) throw std::out_of_range("invalid key");
59 return *found;
60 }
61
62 size_type count(const key_type& key) const { return this->isMember(key) ? 1 : 0; }
63 };
64
65 // Translation between the implementation notion of type, to the jwt::json::type equivilant
66 static jwt::json::type get_type(const value_type& val) {
67 using jwt::json::type;
68
69 if (val.isArray())
70 return type::array;
71 else if (val.isString())
72 return type::string;
73 // Order is important https://github.com/Thalhammer/jwt-cpp/pull/320#issuecomment-1865322511
74 else if (val.isInt())
75 return type::integer;
76 else if (val.isNumeric())
77 return type::number;
78 else if (val.isBool())
79 return type::boolean;
80 else if (val.isObject())
81 return type::object;
82
83 throw std::logic_error("invalid type");
84 }
85
86 static integer_type as_integer(const value_type& val) {
87 switch (val.type()) {
88 case Json::intValue: return val.asInt64();
89 case Json::uintValue: return static_cast<integer_type>(val.asUInt64());
90 default: throw std::bad_cast();
91 }
92 }
93
94 static boolean_type as_boolean(const value_type& val) {
95 if (!val.isBool()) throw std::bad_cast();
96 return val.asBool();
97 }
98
99 static number_type as_number(const value_type& val) {
100 if (!val.isNumeric()) throw std::bad_cast();
101 return val.asDouble();
102 }
103
104 static string_type as_string(const value_type& val) {
105 if (!val.isString()) throw std::bad_cast();
106 return val.asString();
107 }
108
109 static object_type as_object(const value_type& val) {
110 if (!val.isObject()) throw std::bad_cast();
111 return object_type(val);
112 }
113
114 static array_type as_array(const value_type& val) {
115 if (!val.isArray()) throw std::bad_cast();
116 return array_type(val);
117 }
118
119 static bool parse(value_type& val, string_type str) {
120 Json::Reader reader;
121 return reader.parse(str, val);
122 }
123
124 static string_type serialize(const value_type& val) {
125 Json::StreamWriterBuilder builder;
126 builder["commentStyle"] = "None";
127 builder["indentation"] = "";
128 std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
129 return Json::writeString(builder, val);
130 }
131 };
132 } // namespace traits
133} // namespace jwt
134
135#endif
type
Categories for the various JSON types used in JWTs.
Definition jwt.h:2034
JSON Web Token.
Definition base.h:21
basic_claim's JSON trait implementation for jsoncpp
Definition traits.h:13