ORE Studio 0.0.4
Loading...
Searching...
No Matches
boost_json_traits.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * Copyright (C) 2025 Marco Craveiro <marco.craveiro@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation; either version 3 of the License, or (at your option) any later
8 * version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 *
19 */
20#ifndef ORES_SECURITY_JWT_BOOST_JSON_TRAITS_HPP
21#define ORES_SECURITY_JWT_BOOST_JSON_TRAITS_HPP
22
23#include <boost/json.hpp>
24#include <jwt-cpp/jwt.h>
25
26namespace jwt::traits {
27
34struct boost_json {
35 using value_type = boost::json::value;
36 using object_type = boost::json::object;
37 using array_type = boost::json::array;
38 using string_type = std::string;
39 using number_type = double;
40 using integer_type = std::int64_t;
41 using boolean_type = bool;
42
43 static jwt::json::type get_type(const value_type& val) {
44 using jwt::json::type;
45 if (val.is_bool()) return type::boolean;
46 if (val.is_int64() || val.is_uint64()) return type::integer;
47 if (val.is_double()) return type::number;
48 if (val.is_string()) return type::string;
49 if (val.is_array()) return type::array;
50 return type::object;
51 }
52
53 static bool parse(value_type& val, const string_type& str) {
54 try {
55 val = boost::json::parse(str);
56 return true;
57 } catch (...) {
58 return false;
59 }
60 }
61
62 static std::string serialize(const value_type& val) {
63 return boost::json::serialize(val);
64 }
65
66 static object_type as_object(const value_type& val) {
67 return val.as_object();
68 }
69
70 static array_type as_array(const value_type& val) {
71 return val.as_array();
72 }
73
74 static string_type as_string(const value_type& val) {
75 return std::string(val.as_string());
76 }
77
78 static number_type as_number(const value_type& val) {
79 if (val.is_double()) {
80 return val.as_double();
81 } else if (val.is_int64()) {
82 return static_cast<double>(val.as_int64());
83 } else {
84 return static_cast<double>(val.as_uint64());
85 }
86 }
87
88 static integer_type as_integer(const value_type& val) {
89 if (val.is_int64()) {
90 return val.as_int64();
91 } else {
92 return static_cast<integer_type>(val.as_uint64());
93 }
94 }
95
96 static boolean_type as_boolean(const value_type& val) {
97 return val.as_bool();
98 }
99};
100
101}
102
103#endif
jwt-cpp traits for boost::json.
Definition boost_json_traits.hpp:34