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_GLAZE_JSON_TRAITS_H
2#define JWT_CPP_GLAZE_JSON_TRAITS_H
3
4#define JWT_DISABLE_PICOJSON
5#include "jwt-cpp/jwt.h"
6
7#include <glaze/json.hpp>
8
9namespace jwt {
13 namespace traits {
15 struct glaze_json {
16 using value_type = glz::generic;
17 using object_type = value_type::object_t;
18 using array_type = value_type::array_t;
19 using string_type = std::string;
20 using number_type = double;
21 using integer_type = std::int64_t;
22 using boolean_type = bool;
23
24 static bool is_integer(double value) { return std::trunc(value) == value; }
25
26 static jwt::json::type get_type(const value_type& val) {
27 using jwt::json::type;
28
29 if (val.is_object()) { return type::object; }
30 if (val.is_array()) { return type::array; }
31 if (val.is_string()) { return type::string; }
32 if (val.is_number() && is_integer(val.get_number())) { return type::integer; }
33 if (val.is_number()) { return type::number; }
34 if (val.is_boolean()) { return type::boolean; }
35
36 throw std::logic_error("invalid type");
37 }
38
39 static object_type as_object(const value_type& val) {
40 if (get_type(val) != jwt::json::type::object) throw std::bad_cast();
41 return val.get_object();
42 }
43
44 static array_type as_array(const value_type& val) {
45 if (get_type(val) != jwt::json::type::array) throw std::bad_cast();
46 return val.get_array();
47 }
48
49 static string_type as_string(const value_type& val) {
50 if (get_type(val) != jwt::json::type::string) throw std::bad_cast();
51 return val.get_string();
52 }
53
54 static integer_type as_integer(const value_type& val) {
55 if (get_type(val) != jwt::json::type::integer) throw std::bad_cast();
56 return val.get_number();
57 }
58
59 static boolean_type as_boolean(const value_type& val) {
60 if (get_type(val) != jwt::json::type::boolean) throw std::bad_cast();
61 return val.get_boolean();
62 }
63
64 static number_type as_number(const value_type& val) {
65 if (get_type(val) != jwt::json::type::number) throw std::bad_cast();
66 return val.get_number();
67 }
68
69 static bool parse(value_type& val, string_type str) {
70 if (auto parsed = glz::read_json<glz::generic>(str); parsed) {
71 val = parsed.value();
72 return true;
73 }
74
75 return false;
76 }
77
78 static string_type serialize(const value_type& val) { return val.dump().value(); }
79 };
80 } // namespace traits
81} // namespace jwt
82
83#endif // JWT_CPP_GLAZE_JSON_TRAITS_H
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
JSON Web Token.
Definition base.h:21
basic_claim's JSON trait implementation for Glaze
Definition traits.h:15