Conscience Core
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 
4 #include "jwt-cpp/jwt.h"
5 #include "nlohmann/json.hpp"
6 
7 namespace jwt {
8  namespace traits {
9  struct nlohmann_json {
11  using value_type = json;
14  using string_type = std::string; // current limitation of traits implementation
18 
19  static jwt::json::type get_type(const json& val) {
20  using jwt::json::type;
21 
22  if (val.type() == json::value_t::boolean) return type::boolean;
23  // nlohmann internally tracks two types of integers
24  if (val.type() == json::value_t::number_integer) return type::integer;
25  if (val.type() == json::value_t::number_unsigned) return type::integer;
26  if (val.type() == json::value_t::number_float) return type::number;
27  if (val.type() == json::value_t::string) return type::string;
28  if (val.type() == json::value_t::array) return type::array;
29  if (val.type() == json::value_t::object) return type::object;
30 
31  throw std::logic_error("invalid type");
32  }
33 
34  static json::object_t as_object(const json& val) {
35  if (val.type() != json::value_t::object) throw std::bad_cast();
36  return val.get<json::object_t>();
37  }
38 
39  static std::string as_string(const json& val) {
40  if (val.type() != json::value_t::string) throw std::bad_cast();
41  return val.get<std::string>();
42  }
43 
44  static json::array_t as_array(const json& val) {
45  if (val.type() != json::value_t::array) throw std::bad_cast();
46  return val.get<json::array_t>();
47  }
48 
49  static int64_t as_int(const json& val) {
50  switch (val.type()) {
52  case json::value_t::number_unsigned: return val.get<int64_t>();
53  default: throw std::bad_cast();
54  }
55  }
56 
57  static bool as_bool(const json& val) {
58  if (val.type() != json::value_t::boolean) throw std::bad_cast();
59  return val.get<bool>();
60  }
61 
62  static double as_number(const json& val) {
63  if (val.type() != json::value_t::number_float) throw std::bad_cast();
64  return val.get<double>();
65  }
66 
67  static bool parse(json& val, std::string str) {
68  val = json::parse(str.begin(), str.end());
69  return true;
70  }
71 
72  static std::string serialize(const json& val) { return val.dump(); }
73  };
74  } // namespace traits
75 } // namespace jwt
76 
77 #endif
jwt::traits::nlohmann_json::as_object
static json::object_t as_object(const json &val)
Definition: traits.h:34
jwt::traits::nlohmann_json::as_bool
static bool as_bool(const json &val)
Definition: traits.h:57
jwt::traits::nlohmann_json::object_type
json::object_t object_type
Definition: traits.h:12
jwt::traits::nlohmann_json::array_type
json::array_t array_type
Definition: traits.h:13
jwt::traits::nlohmann_json::as_int
static int64_t as_int(const json &val)
Definition: traits.h:49
nlohmann::json
basic_json<> json
default JSON class
Definition: json.hpp:3472
nlohmann::detail::value_t::object
@ object
object (unordered set of name/value pairs)
jwt::traits::nlohmann_json::as_number
static double as_number(const json &val)
Definition: traits.h:62
nlohmann::basic_json::type
constexpr value_t type() const noexcept
return the type of the JSON value (explicit)
Definition: json.hpp:19972
nlohmann::detail::value_t::number_float
@ number_float
number value (floating-point)
nlohmann::detail::value_t::number_integer
@ number_integer
number value (signed integer)
nlohmann::basic_json::array_t
ArrayType< basic_json, AllocatorType< basic_json > > array_t
a type for an array
Definition: json.hpp:18061
jwt::traits::nlohmann_json::as_string
static std::string as_string(const json &val)
Definition: traits.h:39
nlohmann::basic_json::number_integer_t
NumberIntegerType number_integer_t
a type for a number (integer)
Definition: json.hpp:18212
nlohmann::basic_json::parse
static JSON_HEDLEY_WARN_UNUSED_RESULT basic_json parse(InputType &&i, const parser_callback_t cb=nullptr, const bool allow_exceptions=true, const bool ignore_comments=false)
deserialize from a compatible input
Definition: json.hpp:24398
jwt::traits::nlohmann_json::get_type
static jwt::json::type get_type(const json &val)
Definition: traits.h:19
jwt::traits::nlohmann_json::parse
static bool parse(json &val, std::string str)
Definition: traits.h:67
nlohmann::detail::value_t::string
@ string
string value
jwt::traits::nlohmann_json
Definition: traits.h:9
jwt::traits::nlohmann_json::json
nlohmann::json json
Definition: traits.h:10
jwt::traits::nlohmann_json::string_type
std::string string_type
Definition: traits.h:14
jwt
JSON Web Token.
Definition: base.h:20
picojson::object
value::object object
Definition: picojson.h:210
nlohmann::basic_json::object_t
ObjectType< StringType, basic_json, object_comparator_t, AllocatorType< std::pair< const StringType, basic_json > >> object_t
a type for an object
Definition: json.hpp:18015
nlohmann::detail::value_t::number_unsigned
@ number_unsigned
number value (unsigned integer)
jwt::json::type
type
Generic JSON types used in JWTs.
Definition: jwt.h:1794
nlohmann::detail::value_t::array
@ array
array (ordered collection of values)
nlohmann::basic_json::boolean_t
BooleanType boolean_t
a type for a boolean
Definition: json.hpp:18140
jwt::traits::nlohmann_json::integer_type
json::number_integer_t integer_type
Definition: traits.h:16
nlohmann::basic_json::dump
string_t dump(const int indent=-1, const char indent_char=' ', const bool ensure_ascii=false, const error_handler_t error_handler=error_handler_t::strict) const
serialization
Definition: json.hpp:19919
nlohmann::detail::value_t::boolean
@ boolean
boolean value
nlohmann::basic_json
a class to store JSON values
Definition: json.hpp:3448
jwt.h
jwt::traits::nlohmann_json::number_type
json::number_float_t number_type
Definition: traits.h:15
picojson::array
value::array array
Definition: picojson.h:209
nlohmann::basic_json::number_float_t
NumberFloatType number_float_t
a type for a number (floating-point)
Definition: json.hpp:18351
jwt::traits::nlohmann_json::serialize
static std::string serialize(const json &val)
Definition: traits.h:72
jwt::traits::nlohmann_json::as_array
static json::array_t as_array(const json &val)
Definition: traits.h:44
jwt::traits::nlohmann_json::boolean_type
json::boolean_t boolean_type
Definition: traits.h:17
nlohmann::basic_json::get
auto get() const noexcept(noexcept(std::declval< const basic_json_t & >().template get_impl< ValueType >(detail::priority_tag< 4 > {}))) -> decltype(std::declval< const basic_json_t & >().template get_impl< ValueType >(detail::priority_tag< 4 >
get a (pointer) value (explicit)
Definition: json.hpp:20708