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#ifndef JWT_CPP_PICOJSON_TRAITS_H
2#define JWT_CPP_PICOJSON_TRAITS_H
3
4#ifndef PICOJSON_USE_INT64
5#define PICOJSON_USE_INT64
6#endif
7#include "picojson/picojson.h"
8
9#ifndef JWT_DISABLE_PICOJSON
10#define JWT_DISABLE_PICOJSON
11#endif
12#include "jwt-cpp/jwt.h"
13
14namespace jwt {
18 namespace traits {
21 using value_type = picojson::value;
22 using object_type = picojson::object;
23 using array_type = picojson::array;
24 using string_type = std::string;
25 using number_type = double;
26 using integer_type = int64_t;
27 using boolean_type = bool;
28
29 static json::type get_type(const picojson::value& val) {
30 using json::type;
31 if (val.is<bool>()) return type::boolean;
32 if (val.is<int64_t>()) return type::integer;
33 if (val.is<double>()) return type::number;
34 if (val.is<std::string>()) return type::string;
35 if (val.is<picojson::array>()) return type::array;
36 if (val.is<picojson::object>()) return type::object;
37
38 throw std::logic_error("invalid type");
39 }
40
41 static picojson::object as_object(const picojson::value& val) {
42 if (!val.is<picojson::object>()) throw std::bad_cast();
43 return val.get<picojson::object>();
44 }
45
46 static std::string as_string(const picojson::value& val) {
47 if (!val.is<std::string>()) throw std::bad_cast();
48 return val.get<std::string>();
49 }
50
51 static picojson::array as_array(const picojson::value& val) {
52 if (!val.is<picojson::array>()) throw std::bad_cast();
53 return val.get<picojson::array>();
54 }
55
56 static int64_t as_integer(const picojson::value& val) {
57 if (!val.is<int64_t>()) throw std::bad_cast();
58 return val.get<int64_t>();
59 }
60
61 static bool as_boolean(const picojson::value& val) {
62 if (!val.is<bool>()) throw std::bad_cast();
63 return val.get<bool>();
64 }
65
66 static double as_number(const picojson::value& val) {
67 if (!val.is<double>()) throw std::bad_cast();
68 return val.get<double>();
69 }
70
71 static bool parse(picojson::value& val, const std::string& str) {
72 return picojson::parse(val, str).empty();
73 }
74
75 static std::string serialize(const picojson::value& val) { return val.serialize(); }
76 };
77 } // namespace traits
78} // namespace jwt
79
80#endif
JSON Web Token.
Definition base.h:21
basic_claim's JSON trait implementation for picojson
Definition traits.h:20