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_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.type() == jsoncons::json_type::bool_value) return type::boolean;
135 if (val.type() == jsoncons::json_type::int64_value) return type::integer;
136 if (val.type() == jsoncons::json_type::uint64_value) return type::integer;
137 if (val.type() == jsoncons::json_type::half_value) return type::number;
138 if (val.type() == jsoncons::json_type::double_value) return type::number;
139 if (val.type() == jsoncons::json_type::string_value) return type::string;
140 if (val.type() == jsoncons::json_type::array_value) return type::array;
141 if (val.type() == jsoncons::json_type::object_value) return type::object;
142
143 throw std::logic_error("invalid type");
144 }
145
146 static object_type as_object(const json& val) {
147 if (val.type() != jsoncons::json_type::object_value) throw std::bad_cast();
148 return object_type(val);
149 }
150
151 static array_type as_array(const json& val) {
152 if (val.type() != jsoncons::json_type::array_value) throw std::bad_cast();
153 return array_type(val);
154 }
155
156 static string_type as_string(const json& val) {
157 if (val.type() != jsoncons::json_type::string_value) throw std::bad_cast();
158 return val.as_string();
159 }
160
161 static number_type as_number(const json& val) {
162 if (get_type(val) != jwt::json::type::number) throw std::bad_cast();
163 return val.as_double();
164 }
165
166 static integer_type as_integer(const json& val) {
167 if (get_type(val) != jwt::json::type::integer) throw std::bad_cast();
168 return val.as<integer_type>();
169 }
170
171 static boolean_type as_boolean(const json& val) {
172 if (val.type() != jsoncons::json_type::bool_value) throw std::bad_cast();
173 return val.as_bool();
174 }
175
176 static bool parse(json& val, const std::string& str) {
177 val = json::parse(str);
178 return true;
179 }
180
181 static std::string serialize(const json& val) {
182 std::ostringstream os;
183 os << jsoncons::print(val);
184 return os.str();
185 }
186 };
187 } // namespace traits
188} // namespace jwt
189
190namespace jsoncons {
191 template<typename Json>
192 struct json_type_traits<Json, jwt::traits::danielaparker_jsoncons::object_type> {
193
194 using allocator_type = typename Json::allocator_type;
195
196 static bool is(const Json&) noexcept { return true; }
197
198 static jwt::traits::danielaparker_jsoncons::object_type as(const Json& j) {
200 for (const auto& item : j.object_range()) {
201 o[item.key()] = item.value();
202 }
203 return o;
204 }
205
206 static Json to_json(const jwt::traits::danielaparker_jsoncons::object_type& val) {
207 jsoncons::json j = jsoncons::json::object();
208 for (const auto& item : val) {
209 j[item.key()] = item.value();
210 }
211 return j;
212 }
213
214 static Json to_json(const jwt::traits::danielaparker_jsoncons::object_type& val, const allocator_type&) {
215 return to_json(val);
216 }
217 };
218
219 template<typename Json>
220 struct json_type_traits<Json, jwt::traits::danielaparker_jsoncons::array_type> {
221
222 using allocator_type = typename Json::allocator_type;
223
224 static bool is(const Json&) noexcept { return true; }
225
226 static jwt::traits::danielaparker_jsoncons::array_type as(const Json& j) {
228 for (const auto& item : j.array_range()) {
229 a.push_back(item);
230 }
231 return a;
232 }
233
234 static Json to_json(const jwt::traits::danielaparker_jsoncons::array_type& val) {
235 jsoncons::json a = jsoncons::json::array();
236 for (const auto& item : val) {
237 a.push_back(item);
238 }
239 return a;
240 }
241
242 static Json to_json(const jwt::traits::danielaparker_jsoncons::array_type& val, const allocator_type&) {
243 return to_json(val);
244 }
245 };
246} // namespace jsoncons
247
248#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