Conscience Core
traits.h
Go to the documentation of this file.
1 #ifndef JWT_CPP_BOOSTJSON_TRAITS_H
2 #define JWT_CPP_BOOSTJSON_TRAITS_H
3 
4 #define JWT_DISABLE_PICOJSON
5 #include "jwt-cpp/jwt.h"
6 
7 #include <boost/json.hpp>
8 // if not boost JSON standalone then error...
9 
10 namespace jwt {
11  namespace traits {
12  namespace json = boost::json;
13  struct boost_json {
14  using value_type = json::value;
15  using object_type = json::object;
16  using array_type = json::array;
17  using string_type = std::string;
18  using number_type = double;
19  using integer_type = std::int64_t;
20  using boolean_type = bool;
21 
22  static jwt::json::type get_type(const value_type& val) {
23  using jwt::json::type;
24 
25  if (val.kind() == json::kind::bool_) return type::boolean;
26  if (val.kind() == json::kind::int64) return type::integer;
27  if (val.kind() == json::kind::uint64) // boost internally tracks two types of integers
28  return type::integer;
29  if (val.kind() == json::kind::double_) return type::number;
30  if (val.kind() == json::kind::string) return type::string;
31  if (val.kind() == json::kind::array) return type::array;
32  if (val.kind() == json::kind::object) return type::object;
33 
34  throw std::logic_error("invalid type");
35  }
36 
37  static object_type as_object(const value_type& val) {
38  if (val.kind() != json::kind::object) throw std::bad_cast();
39  return val.get_object();
40  }
41 
42  static array_type as_array(const value_type& val) {
43  if (val.kind() != json::kind::array) throw std::bad_cast();
44  return val.get_array();
45  }
46 
47  static string_type as_string(const value_type& val) {
48  if (val.kind() != json::kind::string) throw std::bad_cast();
49  return string_type{val.get_string()};
50  }
51 
52  static integer_type as_int(const value_type& val) {
53  switch (val.kind()) {
54  case json::kind::int64: return val.get_int64();
55  case json::kind::uint64: return static_cast<int64_t>(val.get_uint64());
56  default: throw std::bad_cast();
57  }
58  }
59 
60  static boolean_type as_bool(const value_type& val) {
61  if (val.kind() != json::kind::bool_) throw std::bad_cast();
62  return val.get_bool();
63  }
64 
65  static number_type as_number(const value_type& val) {
66  if (val.kind() != json::kind::double_) throw std::bad_cast();
67  return val.get_double();
68  }
69 
70  static bool parse(value_type& val, string_type str) {
71  val = json::parse(str);
72  return true;
73  }
74 
75  static std::string serialize(const value_type& val) { return json::serialize(val); }
76  };
77  } // namespace traits
78 } // namespace jwt
79 
80 #endif // JWT_CPP_BOOSTJSON_TRAITS_H
jwt::traits::boost_json::number_type
double number_type
Definition: traits.h:18
nlohmann::json
basic_json<> json
default JSON class
Definition: json.hpp:3472
jwt::traits::boost_json::string_type
std::string string_type
Definition: traits.h:17
jwt::traits::boost_json::as_array
static array_type as_array(const value_type &val)
Definition: traits.h:42
jwt::traits::boost_json::parse
static bool parse(value_type &val, string_type str)
Definition: traits.h:70
jwt::traits::boost_json::as_number
static number_type as_number(const value_type &val)
Definition: traits.h:65
jwt::traits::boost_json::boolean_type
bool boolean_type
Definition: traits.h:20
jwt::traits::boost_json::get_type
static jwt::json::type get_type(const value_type &val)
Definition: traits.h:22
conscience_core::bridging::commands::environment_entities::bool
const string const string EntityVideoSourcesCommandDataType CscPoint3d CscPoint3d bool
Definition: environmentEntitiesCommands.h:545
jwt::traits::boost_json
Definition: traits.h:13
jwt
JSON Web Token.
Definition: base.h:20
jwt::traits::boost_json::as_int
static integer_type as_int(const value_type &val)
Definition: traits.h:52
jwt::traits::boost_json::integer_type
std::int64_t integer_type
Definition: traits.h:19
picojson::object
value::object object
Definition: picojson.h:210
picojson::parse
std::string parse(value &out, Iter &pos, const Iter &last)
Definition: picojson.h:1098
jwt::traits::boost_json::serialize
static std::string serialize(const value_type &val)
Definition: traits.h:75
jwt::json::type
type
Generic JSON types used in JWTs.
Definition: jwt.h:1794
jwt::traits::boost_json::as_string
static string_type as_string(const value_type &val)
Definition: traits.h:47
jwt::traits::boost_json::as_object
static object_type as_object(const value_type &val)
Definition: traits.h:37
jwt.h
picojson::array
value::array array
Definition: picojson.h:209
jwt::traits::boost_json::as_bool
static boolean_type as_bool(const value_type &val)
Definition: traits.h:60
jwt::traits::boost_json::array_type
json::array array_type
Definition: traits.h:16
jwt::traits::boost_json::value_type
json::value value_type
Definition: traits.h:14
jwt::traits::boost_json::object_type
json::object object_type
Definition: traits.h:15