Clio develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
JsonBool.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2023, the clio developers.
5
6 Permission to use, copy, modify, and distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#pragma once
21
22#include <boost/json/conversion.hpp>
23#include <boost/json/kind.hpp>
24#include <boost/json/value.hpp>
25#include <boost/json/value_to.hpp>
26
27#include <stdexcept>
28
29namespace rpc {
30
34struct JsonBool {
35 bool value = false;
36
38 operator bool() const
39 {
40 return value;
41 }
43};
44
51inline JsonBool
52tag_invoke(boost::json::value_to_tag<JsonBool> const&, boost::json::value const& jsonValue)
53{
54 switch (jsonValue.kind()) {
55 case boost::json::kind::null:
56 return JsonBool{false};
57 case boost::json::kind::bool_:
58 return JsonBool{jsonValue.as_bool()};
59 case boost::json::kind::uint64:
60 [[fallthrough]];
61 case boost::json::kind::int64:
62 return JsonBool{jsonValue.as_int64() != 0};
63 case boost::json::kind::double_:
64 return JsonBool{jsonValue.as_double() != 0.0};
65 case boost::json::kind::string:
66 // Also should be `jsonValue.as_string() != "false"` but rippled doesn't do
67 // that. Anyway for v2 api we have bool validation
68 return JsonBool{!jsonValue.as_string().empty() && jsonValue.as_string()[0] != 0};
69 case boost::json::kind::array:
70 return JsonBool{!jsonValue.as_array().empty()};
71 case boost::json::kind::object:
72 return JsonBool{!jsonValue.as_object().empty()};
73 }
74 throw std::runtime_error("Invalid json value");
75}
76
77} // namespace rpc
This namespace contains all the RPC logic and handlers.
Definition AMMHelpers.cpp:36
void tag_invoke(boost::json::value_from_tag, boost::json::value &jv, BookChange const &change)
Implementation of value_from for BookChange type.
Definition BookChangesHelper.hpp:241
A wrapper around bool that allows to convert from any JSON value.
Definition JsonBool.hpp:34