Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
JsonUtils.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 "rpc/JS.hpp"
23
24#include <boost/json.hpp>
25#include <boost/json/object.hpp>
26#include <xrpl/beast/core/LexicalCast.h>
27
28#include <algorithm>
29#include <cctype>
30#include <charconv>
31#include <concepts>
32#include <expected>
33#include <stdexcept>
34#include <string>
35
39namespace util {
40
47inline std::string
48toLower(std::string str)
49{
50 std::ranges::transform(str, std::begin(str), [](unsigned char c) { return std::tolower(c); });
51 return str;
52}
53
60inline std::string
61toUpper(std::string str)
62{
63 std::ranges::transform(str, std::begin(str), [](unsigned char c) { return std::toupper(c); });
64 return str;
65}
66
73inline boost::json::object
74removeSecret(boost::json::object const& object)
75{
76 auto newObject = object;
77 auto const secretFields = {"secret", "seed", "seed_hex", "passphrase"};
78
79 if (newObject.contains("params") and newObject.at("params").is_array() and
80 not newObject.at("params").as_array().empty() and newObject.at("params").as_array()[0].is_object()) {
81 for (auto const& secretField : secretFields) {
82 if (newObject.at("params").as_array()[0].as_object().contains(secretField))
83 newObject.at("params").as_array()[0].as_object()[secretField] = "*";
84 }
85 }
86
87 // for websocket requests
88 for (auto const& secretField : secretFields) {
89 if (newObject.contains(secretField))
90 newObject[secretField] = "*";
91 }
92
93 return newObject;
94}
95
104template <std::integral Type>
105std::expected<Type, std::string>
106tryIntegralValueAs(boost::json::value const& value)
107{
108 if (value.is_uint64())
109 return static_cast<Type>(value.as_uint64());
110
111 if (value.is_int64())
112 return static_cast<Type>(value.as_int64());
113
114 return std::unexpected("Value neither uint64 nor int64");
115}
116
126template <std::integral Type>
127Type
128integralValueAs(boost::json::value const& value)
129{
130 auto expectedResult = tryIntegralValueAs<Type>(value);
131 if (expectedResult.has_value())
132 return *expectedResult;
133
134 throw std::logic_error(std::move(expectedResult).error());
135}
136
143[[nodiscard]] inline std::expected<uint32_t, std::string>
144getLedgerIndex(boost::json::value const& value)
145{
146 if (not value.is_string()) {
147 return tryIntegralValueAs<uint32_t>(value);
148 }
149 if (value.as_string() != "validated") {
150 uint32_t ledgerIndex{};
151 if (beast::lexicalCastChecked(ledgerIndex, value.as_string().c_str())) {
152 return ledgerIndex;
153 }
154 return std::unexpected("Invalid ledger index string");
155 }
156 return std::unexpected("'validated' ledger index is requested");
157}
158
159} // namespace util
This namespace contains various utilities.
Definition AccountUtils.hpp:30
Type integralValueAs(boost::json::value const &value)
Detects the type of number stored in value and casts it back to the requested Type.
Definition JsonUtils.hpp:128
std::string toUpper(std::string str)
Convert a string to uppercase.
Definition JsonUtils.hpp:61
std::expected< Type, std::string > tryIntegralValueAs(boost::json::value const &value)
Detects the type of number stored in value and casts it back to the requested Type.
Definition JsonUtils.hpp:106
boost::json::object removeSecret(boost::json::object const &object)
Removes any detected secret information from a response JSON object.
Definition JsonUtils.hpp:74
std::string toLower(std::string str)
Convert a string to lowercase.
Definition JsonUtils.hpp:48
std::expected< uint32_t, std::string > getLedgerIndex(boost::json::value const &value)
Extracts ledger index from a JSON value which can be either a number or a string.
Definition JsonUtils.hpp:144