JWT-CPP v0.7.1
A header only library for creating and validating JSON Web Tokens (JWT) in C++
Loading...
Searching...
No Matches
traits.h
1#ifndef JWT_CPP_DANIELAPARKER_JSONCONS_TRAITS_H
2#define JWT_CPP_DANIELAPARKER_JSONCONS_TRAITS_H
3
4#define JWT_DISABLE_PICOJSON
5#define JSONCONS_NO_DEPRECATED
6
7#include "jsoncons/json.hpp"
8#include "jwt-cpp/jwt.h"
9
10#include <sstream>
11
12namespace jwt {
16 namespace traits {
19 // Needs at least https://github.com/danielaparker/jsoncons/commit/28c56b90ec7337f98a5b8942574590111a5e5831
20 static_assert(jsoncons::version().major > 0);
21
22 using json = jsoncons::json;
23 using value_type = json;
24
25 struct object_type {
26 using key_type = json::key_type;
27 using mapped_type = json;
28 using value_type = std::pair<const key_type, mapped_type>;
29 using size_type = size_t;
30 using iterator = json::object_iterator;
31 using const_iterator = json::const_object_iterator;
32
33 object_type() = default;
34 object_type(const object_type& o) : json_(o) {}
35 explicit object_type(const json& j) : json_(j) {}
36 explicit object_type(object_type&& o) noexcept : json_(std::move(o)) {}
37 ~object_type() = default;
38
39 object_type& operator=(const object_type& o) {
40 json_ = o.json_;
41 return *this;
42 }
43
44 object_type& operator=(object_type&& o) noexcept {
45 json_ = std::move(o.json_);
46 return *this;
47 }
48
49 // Add missing C++11 subscription operator
50 mapped_type& operator[](const key_type& key) { return json_[key]; }
51
52 // Add missing C++11 element access
53 const mapped_type& at(const key_type& key) const { return json_.at(key); }
54
55 // Add missing C++11 lookup method
56 size_type count(const key_type& key) const { return json_.count(key); }
57
58 iterator begin() { return json_.object_range().begin(); }
59 iterator end() { return json_.object_range().end(); }
60 const_iterator begin() const { return json_.object_range().cbegin(); }
61 const_iterator end() const { return json_.object_range().cend(); }
62 const_iterator cbegin() const { return json_.object_range().cbegin(); }
63 const_iterator cend() const { return json_.object_range().cend(); }
64
65 private:
66 json json_;
67 };
68
69 struct array_type {
70 using value_type = json;
71 using size_type = size_t;
72 using iterator = json::array_iterator;
73 using const_iterator = json::const_array_iterator;
74
75 array_type() = default;
76 array_type(const array_type& a) : json_(a) {}
77 explicit array_type(const json& j) : json_(j) {}
78 explicit array_type(array_type&& a) noexcept : json_(std::move(a)) {}
79 template<typename Iterator>
80 array_type(Iterator first, Iterator last) {
81 json_ = json::array();
82 for (auto it = first; it != last; ++it) {
83 json_.push_back(*it);
84 }
85 }
86 ~array_type() = default;
87
88 array_type& operator=(const array_type& o) {
89 json_ = o.json_;
90 return *this;
91 }
92
93 array_type& operator=(array_type&& o) noexcept {
94 json_ = std::move(o.json_);
95 return *this;
96 }
97
98 value_type& operator[](size_type index) { return json_[index]; }
99
100 const value_type& at(size_type index) const { return json_.at(index); }
101
102 value_type const& front() const { return json_.at(0); }
103
104 void push_back(const value_type& val) { json_.push_back(val); }
105
106 iterator begin() { return json_.array_range().begin(); }
107 iterator end() { return json_.array_range().end(); }
108 const_iterator begin() const { return json_.array_range().cbegin(); }
109 const_iterator end() const { return json_.array_range().cend(); }
110 const_iterator cbegin() const { return json_.array_range().cbegin(); }
111 const_iterator cend() const { return json_.array_range().cend(); }
112
113 private:
114 json json_;
115 };
116
117 using string_type = std::string; // current limitation of traits implementation
118 using number_type = double;
119 using integer_type = int64_t;
120 using boolean_type = bool;
121
122 static jwt::json::type get_type(const json& val) {
123 using jwt::json::type;
124
125 if (val.type() == jsoncons::json_type::bool_value) return type::boolean;
126 if (val.type() == jsoncons::json_type::int64_value) return type::integer;
127 if (val.type() == jsoncons::json_type::uint64_value) return type::integer;
128 if (val.type() == jsoncons::json_type::half_value) return type::number;
129 if (val.type() == jsoncons::json_type::double_value) return type::number;
130 if (val.type() == jsoncons::json_type::string_value) return type::string;
131 if (val.type() == jsoncons::json_type::array_value) return type::array;
132 if (val.type() == jsoncons::json_type::object_value) return type::object;
133
134 throw std::logic_error("invalid type");
135 }
136
137 static object_type as_object(const json& val) {
138 if (val.type() != jsoncons::json_type::object_value) throw std::bad_cast();
139 return object_type(val);
140 }
141
142 static array_type as_array(const json& val) {
143 if (val.type() != jsoncons::json_type::array_value) throw std::bad_cast();
144 return array_type(val);
145 }
146
147 static string_type as_string(const json& val) {
148 if (val.type() != jsoncons::json_type::string_value) throw std::bad_cast();
149 return val.as_string();
150 }
151
152 static number_type as_number(const json& val) {
153 if (get_type(val) != jwt::json::type::number) throw std::bad_cast();
154 return val.as_double();
155 }
156
157 static integer_type as_integer(const json& val) {
158 if (get_type(val) != jwt::json::type::integer) throw std::bad_cast();
159 return val.as<integer_type>();
160 }
161
162 static boolean_type as_boolean(const json& val) {
163 if (val.type() != jsoncons::json_type::bool_value) throw std::bad_cast();
164 return val.as_bool();
165 }
166
167 static bool parse(json& val, const std::string& str) {
168 val = json::parse(str);
169 return true;
170 }
171
172 static std::string serialize(const json& val) {
173 std::ostringstream os;
174 os << jsoncons::print(val);
175 return os.str();
176 }
177 };
178 } // namespace traits
179} // namespace jwt
180
181namespace jsoncons {
182 template<typename Json>
183 struct json_type_traits<Json, jwt::traits::danielaparker_jsoncons::object_type> {
184
185 using allocator_type = typename Json::allocator_type;
186
187 static bool is(const Json&) noexcept { return true; }
188
189 static jwt::traits::danielaparker_jsoncons::object_type as(const Json& j) {
191 for (const auto& item : j.object_range()) {
192 o[item.key()] = item.value();
193 }
194 return o;
195 }
196
197 static Json to_json(const jwt::traits::danielaparker_jsoncons::object_type& val) {
198 jsoncons::json j = jsoncons::json::object();
199 for (const auto& item : val) {
200 j[item.key()] = item.value();
201 }
202 return j;
203 }
204
205 static Json to_json(const jwt::traits::danielaparker_jsoncons::object_type& val, const allocator_type&) {
206 return to_json(val);
207 }
208 };
209
210 template<typename Json>
211 struct json_type_traits<Json, jwt::traits::danielaparker_jsoncons::array_type> {
212
213 using allocator_type = typename Json::allocator_type;
214
215 static bool is(const Json&) noexcept { return true; }
216
217 static jwt::traits::danielaparker_jsoncons::array_type as(const Json& j) {
219 for (const auto& item : j.array_range()) {
220 a.push_back(item);
221 }
222 return a;
223 }
224
225 static Json to_json(const jwt::traits::danielaparker_jsoncons::array_type& val) {
226 jsoncons::json a = jsoncons::json::array();
227 for (const auto& item : val) {
228 a.push_back(item);
229 }
230 return a;
231 }
232
233 static Json to_json(const jwt::traits::danielaparker_jsoncons::array_type& val, const allocator_type&) {
234 return to_json(val);
235 }
236 };
237} // namespace jsoncons
238
239#endif // JWT_CPP_DANIELAPARKER_JSONCONS_TRAITS_H
type
Categories for the various JSON types used in JWTs.
Definition jwt.h:2301
JSON Web Token.
Definition base.h:21
basic_claim's JSON trait implementation for jsoncons.
Definition traits.h:18