JWT-CPP v0.7.2
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#include <initializer_list>
7
8namespace jwt {
12 namespace traits {
15 using value_type = Json::Value;
16 using string_type = std::string;
17 class array_type : public Json::Value {
18 public:
19 using value_type = Json::Value;
20
21 array_type() = default;
22 array_type(const array_type&) = default;
23 explicit array_type(const Json::Value& o) : Json::Value(o) {}
24 array_type(array_type&&) = default;
25 explicit array_type(Json::Value&& o) : Json::Value(o) {}
26 array_type(std::initializer_list<value_type> init) {
27 for (auto const& v : init) {
28 Json::Value value;
29 value = v;
30 this->append(value);
31 }
32 }
33 template<typename Iterator>
34 array_type(Iterator begin, Iterator end) {
35 for (Iterator it = begin; it != end; ++it) {
36 Json::Value value;
37 value = *it;
38 this->append(value);
39 }
40 }
41 ~array_type() = default;
42 array_type& operator=(const array_type& o) = default;
43 array_type& operator=(array_type&& o) noexcept = default;
44
45 value_type const& front() const { return this->operator[](0U); }
46 };
47 using number_type = double;
48 using integer_type = Json::Value::Int;
49 using boolean_type = bool;
50 class object_type : public Json::Value {
51 public:
52 using key_type = std::string;
53 using mapped_type = Json::Value;
54 using size_type = size_t;
55
56 object_type() : Json::Value(Json::objectValue) {}
57 object_type(const object_type&) = default;
58 explicit object_type(const Json::Value& o) : Json::Value(o) {}
59 object_type(object_type&&) = default;
60 explicit object_type(Json::Value&& o) : Json::Value(o) {}
61 ~object_type() = default;
62 object_type& operator=(const object_type& o) = default;
63 object_type& operator=(object_type&& o) noexcept = default;
64
65 // Add missing C++11 element access
66 const mapped_type& at(const key_type& key) const {
67 Json::Value const* found = find(key.data(), key.data() + key.length());
68 if (!found) throw std::out_of_range("invalid key");
69 return *found;
70 }
71
72 size_type count(const key_type& key) const { return this->isMember(key) ? 1 : 0; }
73 };
74
75 // Translation between the implementation notion of type, to the jwt::json::type equivilant
76 static jwt::json::type get_type(const value_type& val) {
77 using jwt::json::type;
78
79 if (val.isArray())
80 return type::array;
81 else if (val.isString())
82 return type::string;
83 // Order is important https://github.com/Thalhammer/jwt-cpp/pull/320#issuecomment-1865322511
84 else if (val.isInt())
85 return type::integer;
86 else if (val.isNumeric())
87 return type::number;
88 else if (val.isBool())
89 return type::boolean;
90 else if (val.isObject())
91 return type::object;
92
93 throw std::logic_error("invalid type");
94 }
95
96 static integer_type as_integer(const value_type& val) {
97 switch (val.type()) {
98 case Json::intValue: return val.asInt64();
99 case Json::uintValue: return static_cast<integer_type>(val.asUInt64());
100 default: throw std::bad_cast();
101 }
102 }
103
104 static boolean_type as_boolean(const value_type& val) {
105 if (!val.isBool()) throw std::bad_cast();
106 return val.asBool();
107 }
108
109 static number_type as_number(const value_type& val) {
110 if (!val.isNumeric()) throw std::bad_cast();
111 return val.asDouble();
112 }
113
114 static string_type as_string(const value_type& val) {
115 if (!val.isString()) throw std::bad_cast();
116 return val.asString();
117 }
118
119 static object_type as_object(const value_type& val) {
120 if (!val.isObject()) throw std::bad_cast();
121 return object_type(val);
122 }
123
124 static array_type as_array(const value_type& val) {
125 if (!val.isArray()) throw std::bad_cast();
126 return array_type(val);
127 }
128
129 static bool parse(value_type& val, string_type str) {
130 Json::CharReaderBuilder builder;
131 const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
132
133 return reader->parse(reinterpret_cast<const char*>(str.c_str()),
134 reinterpret_cast<const char*>(str.c_str() + str.size()), &val, nullptr);
135 }
136
137 static string_type serialize(const value_type& val) {
138 Json::StreamWriterBuilder builder;
139 builder["commentStyle"] = "None";
140 builder["indentation"] = "";
141 std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
142 return Json::writeString(builder, val);
143 }
144 };
145 } // namespace traits
146} // namespace jwt
147
148#endif
type
Categories for the various JSON types used in JWTs.
Definition jwt.h:2314
Namespace containing all the json_trait implementations for a jwt::basic_claim.
Definition traits.h:14
JSON Web Token.
Definition base.h:21
basic_claim's JSON trait implementation for jsoncpp
Definition traits.h:14