JWT-CPP v0.7.2
A header only library for creating and validating JSON Web Tokens (JWT) in C++
Loading...
Searching...
No Matches
traits.h
1#ifndef JWT_CPP_REFLECT_CPP_TRAITS_H
2#define JWT_CPP_REFLECT_CPP_TRAITS_H
3
4#ifndef JWT_DISABLE_PICOJSON
5#define JWT_DISABLE_PICOJSON
6#endif
7
8#include "jwt-cpp/jwt.h"
9
10#include <rfl/Generic.hpp>
11#include <rfl/json.hpp>
12#include <variant>
13
14namespace jwt::traits {
15
17
18 using value_type = rfl::Generic;
19 using object_type = rfl::Generic::Object;
20 using array_type = rfl::Generic::Array;
21 using string_type = std::string;
22 using number_type = double;
23 using integer_type = int64_t;
24 using boolean_type = bool;
25
26 template<class... Ts>
27 struct variant_overloaded : Ts... {
28 using Ts::operator()...;
29 };
30
31 static jwt::json::type get_type(const value_type& val) {
32 return std::visit(
34 [](boolean_type const&) -> jwt::json::type { return jwt::json::type::boolean; },
35 [](integer_type const&) -> jwt::json::type { return jwt::json::type::integer; },
36 [](number_type const&) -> jwt::json::type { return jwt::json::type::number; },
37 [](string_type const&) -> jwt::json::type { return jwt::json::type::string; },
38 [](array_type const&) -> jwt::json::type { return jwt::json::type::array; },
39 [](object_type const&) -> jwt::json::type { return jwt::json::type::object; },
40 [](std::nullopt_t const&) -> jwt::json::type { throw std::logic_error("invalid type"); },
41 },
42 val.get());
43 }
44
45 static object_type as_object(const value_type& val) {
46 const auto& variant = val.get();
47 if (!std::holds_alternative<object_type>(variant)) throw std::bad_cast();
48 return std::get<object_type>(variant);
49 }
50
51 static array_type as_array(const value_type& val) {
52 const auto& variant = val.get();
53 if (!std::holds_alternative<array_type>(variant)) throw std::bad_cast();
54 return std::get<array_type>(variant);
55 }
56
57 static string_type as_string(const value_type& val) {
58 const auto& variant = val.get();
59 if (!std::holds_alternative<string_type>(variant)) throw std::bad_cast();
60 return std::get<string_type>(variant);
61 }
62
63 static integer_type as_integer(const value_type& val) {
64 const auto& variant = val.get();
65 if (!std::holds_alternative<integer_type>(variant)) throw std::bad_cast();
66 return std::get<integer_type>(variant);
67 }
68
69 static boolean_type as_boolean(const value_type& val) {
70 const auto& variant = val.get();
71 if (!std::holds_alternative<boolean_type>(variant)) throw std::bad_cast();
72 return std::get<boolean_type>(variant);
73 }
74
75 static number_type as_number(const value_type& val) {
76 const auto& variant = val.get();
77 if (!std::holds_alternative<number_type>(variant)) throw std::bad_cast();
78 return std::get<number_type>(variant);
79 }
80
81 static bool parse(value_type& out, string_type const& json) {
82 auto res = rfl::json::read<rfl::Generic>(json);
83 if (res) {
84 out = *std::move(res);
85 return true;
86 }
87 return false;
88 }
89
90 static std::string serialize(const value_type& val) { return rfl::json::write(val); }
91 };
92
93} // namespace jwt::traits
94
95#endif
type
Categories for the various JSON types used in JWTs.
Definition jwt.h:2314
Namespace containing all the json_trait implementations for a jwt::basic_claim.
Definition traits.h:14
Definition traits.h:16