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