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#define JWT_DISABLE_PICOJSON
2#define JSONCONS_NO_DEPRECATED
3
4#include "jwt-cpp/jwt.h"
5
6#include "jsoncons/json.hpp"
7
8#include <sstream>
9
10namespace jwt {
14 namespace traits {
17 // Needs at least https://github.com/danielaparker/jsoncons/commit/28c56b90ec7337f98a5b8942574590111a5e5831
18 static_assert(jsoncons::version().minor >= 167, "A higher version of jsoncons is required!");
19
20 using json = jsoncons::json;
21 using value_type = json;
22 struct object_type : json::object {
23 // Add missing C++11 member types
24 // https://github.com/danielaparker/jsoncons/commit/1b1ceeb572f9a2db6d37cff47ac78a4f14e072e2#commitcomment-45391411
25 using value_type = key_value_type; // Enable optional jwt-cpp methods
26 using mapped_type = key_value_type::value_type;
27 using size_type = size_t; // for implementing count
28
29 object_type() = default;
30 object_type(const object_type&) = default;
31 explicit object_type(const json::object& o) : json::object(o) {}
32 object_type(object_type&&) = default;
33 explicit object_type(json::object&& o) : json::object(o) {}
34 ~object_type() = default;
35 object_type& operator=(const object_type& o) = default;
36 object_type& operator=(object_type&& o) noexcept = default;
37
38 // Add missing C++11 subscription operator
39 mapped_type& operator[](const key_type& key) {
40 // https://github.com/microsoft/STL/blob/2914b4301c59dc7ffc09d16ac6f7979fde2b7f2c/stl/inc/map#L325
41 return try_emplace(key).first->value();
42 }
43
44 // Add missing C++11 element access
45 const mapped_type& at(const key_type& key) const {
46 auto target = find(key);
47 if (target != end()) return target->value();
48
49 throw std::out_of_range("invalid key");
50 }
51
52 // Add missing C++11 lookup method
53 size_type count(const key_type& key) const {
54 struct compare {
55 bool operator()(const value_type& val, const key_type& key) const { return val.key() < key; }
56 bool operator()(const key_type& key, const value_type& val) const { return key < val.key(); }
57 };
58
59 // https://en.cppreference.com/w/cpp/algorithm/binary_search#Complexity
60 if (std::binary_search(this->begin(), this->end(), key, compare{})) return 1;
61 return 0;
62 }
63 };
64 using array_type = json::array;
65 using string_type = std::string; // current limitation of traits implementation
66 using number_type = double;
67 using integer_type = int64_t;
68 using boolean_type = bool;
69
70 static jwt::json::type get_type(const json& val) {
71 using jwt::json::type;
72
73 if (val.type() == jsoncons::json_type::bool_value) return type::boolean;
74 if (val.type() == jsoncons::json_type::int64_value) return type::integer;
75 if (val.type() == jsoncons::json_type::uint64_value) return type::integer;
76 if (val.type() == jsoncons::json_type::half_value) return type::number;
77 if (val.type() == jsoncons::json_type::double_value) return type::number;
78 if (val.type() == jsoncons::json_type::string_value) return type::string;
79 if (val.type() == jsoncons::json_type::array_value) return type::array;
80 if (val.type() == jsoncons::json_type::object_value) return type::object;
81
82 throw std::logic_error("invalid type");
83 }
84
85 static object_type as_object(const json& val) {
86 if (val.type() != jsoncons::json_type::object_value) throw std::bad_cast();
87 return object_type(val.object_value());
88 }
89
90 static array_type as_array(const json& val) {
91 if (val.type() != jsoncons::json_type::array_value) throw std::bad_cast();
92 return val.array_value();
93 }
94
95 static string_type as_string(const json& val) {
96 if (val.type() != jsoncons::json_type::string_value) throw std::bad_cast();
97 return val.as_string();
98 }
99
100 static number_type as_number(const json& val) {
101 if (get_type(val) != jwt::json::type::number) throw std::bad_cast();
102 return val.as_double();
103 }
104
105 static integer_type as_integer(const json& val) {
106 if (get_type(val) != jwt::json::type::integer) throw std::bad_cast();
107 return val.as<integer_type>();
108 }
109
110 static boolean_type as_boolean(const json& val) {
111 if (val.type() != jsoncons::json_type::bool_value) throw std::bad_cast();
112 return val.as_bool();
113 }
114
115 static bool parse(json& val, const std::string& str) {
116 val = json::parse(str);
117 return true;
118 }
119
120 static std::string serialize(const json& val) {
121 std::ostringstream os;
122 os << jsoncons::print(val);
123 return os.str();
124 }
125 };
126 } // namespace traits
127} // namespace jwt
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 jsoncons.
Definition traits.h:16