Goby3 3.2.3
2025.05.13
Loading...
Searching...
No Matches
traits.h
Go to the documentation of this file.
1#ifndef JWT_CPP_NLOHMANN_JSON_TRAITS_H
2#define JWT_CPP_NLOHMANN_JSON_TRAITS_H
3
6
7namespace jwt
8{
9namespace traits
10{
12{
13 using json = nlohmann::json;
15 using object_type = json::object_t;
16 using array_type = json::array_t;
17 using string_type = std::string; // current limitation of traits implementation
18 using number_type = json::number_float_t;
19 using integer_type = json::number_integer_t;
20 using boolean_type = json::boolean_t;
21
22 static jwt::json::type get_type(const json& val)
23 {
24 using jwt::json::type;
25
26 if (val.type() == json::value_t::boolean)
27 return type::boolean;
28 // nlohmann internally tracks two types of integers
29 if (val.type() == json::value_t::number_integer)
30 return type::integer;
31 if (val.type() == json::value_t::number_unsigned)
32 return type::integer;
33 if (val.type() == json::value_t::number_float)
34 return type::number;
35 if (val.type() == json::value_t::string)
36 return type::string;
37 if (val.type() == json::value_t::array)
38 return type::array;
39 if (val.type() == json::value_t::object)
40 return type::object;
41
42 throw std::logic_error("invalid type");
43 }
44
45 static json::object_t as_object(const json& val)
46 {
47 if (val.type() != json::value_t::object)
48 throw std::bad_cast();
49 return val.get<json::object_t>();
50 }
51
52 static std::string as_string(const json& val)
53 {
54 if (val.type() != json::value_t::string)
55 throw std::bad_cast();
56 return val.get<std::string>();
57 }
58
59 static json::array_t as_array(const json& val)
60 {
61 if (val.type() != json::value_t::array)
62 throw std::bad_cast();
63 return val.get<json::array_t>();
64 }
65
66 static int64_t as_int(const json& val)
67 {
68 switch (val.type())
69 {
70 case json::value_t::number_integer:
71 case json::value_t::number_unsigned: return val.get<int64_t>();
72 default: throw std::bad_cast();
73 }
74 }
75
76 static bool as_bool(const json& val)
77 {
78 if (val.type() != json::value_t::boolean)
79 throw std::bad_cast();
80 return val.get<bool>();
81 }
82
83 static double as_number(const json& val)
84 {
85 if (val.type() != json::value_t::number_float)
86 throw std::bad_cast();
87 return val.get<double>();
88 }
89
90 static bool parse(json& val, std::string str)
91 {
92 val = json::parse(str.begin(), str.end());
93 return true;
94 }
95
96 static std::string serialize(const json& val) { return val.dump(); }
97};
98} // namespace traits
99} // namespace jwt
100
101#endif
JSON Web Token.
Definition base.h:19
json::boolean_t boolean_type
Definition traits.h:20
static int64_t as_int(const json &val)
Definition traits.h:66
static bool parse(json &val, std::string str)
Definition traits.h:90
json::number_float_t number_type
Definition traits.h:18
static std::string as_string(const json &val)
Definition traits.h:52
static bool as_bool(const json &val)
Definition traits.h:76
static json::object_t as_object(const json &val)
Definition traits.h:45
static std::string serialize(const json &val)
Definition traits.h:96
std::string string_type
Definition traits.h:17
static jwt::json::type get_type(const json &val)
Definition traits.h:22
static double as_number(const json &val)
Definition traits.h:83
static json::array_t as_array(const json &val)
Definition traits.h:59
nlohmann::json json
Definition traits.h:13
json::number_integer_t integer_type
Definition traits.h:19
json::object_t object_type
Definition traits.h:15
json::array_t array_type
Definition traits.h:16