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