Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
JsonUtils.hpp
1#pragma once
2
3#include "rpc/JS.hpp"
4
5#include <boost/json.hpp>
6#include <boost/json/object.hpp>
7#include <xrpl/beast/core/LexicalCast.h>
8
9#include <algorithm>
10#include <cctype>
11#include <charconv>
12#include <concepts>
13#include <expected>
14#include <stdexcept>
15#include <string>
16
20namespace util {
21
28inline std::string
29toLower(std::string str)
30{
31 std::ranges::transform(str, std::begin(str), [](unsigned char c) { return std::tolower(c); });
32 return str;
33}
34
41inline std::string
42toUpper(std::string str)
43{
44 std::ranges::transform(str, std::begin(str), [](unsigned char c) { return std::toupper(c); });
45 return str;
46}
47
54inline boost::json::object
55removeSecret(boost::json::object const& object)
56{
57 auto newObject = object;
58 auto const secretFields = {"secret", "seed", "seed_hex", "passphrase"};
59
60 if (newObject.contains("params") and newObject.at("params").is_array() and
61 not newObject.at("params").as_array().empty() and
62 newObject.at("params").as_array()[0].is_object()) {
63 for (auto const& secretField : secretFields) {
64 if (newObject.at("params").as_array()[0].as_object().contains(secretField))
65 newObject.at("params").as_array()[0].as_object()[secretField] = "*";
66 }
67 }
68
69 // for websocket requests
70 for (auto const& secretField : secretFields) {
71 if (newObject.contains(secretField))
72 newObject[secretField] = "*";
73 }
74
75 return newObject;
76}
77
86template <std::integral Type>
87std::expected<Type, std::string>
88tryIntegralValueAs(boost::json::value const& value)
89{
90 if (value.is_uint64())
91 return static_cast<Type>(value.as_uint64());
92
93 if (value.is_int64())
94 return static_cast<Type>(value.as_int64());
95
96 return std::unexpected("Value neither uint64 nor int64");
97}
98
108template <std::integral Type>
109Type
110integralValueAs(boost::json::value const& value)
111{
112 auto expectedResult = tryIntegralValueAs<Type>(value);
113 if (expectedResult.has_value())
114 return *expectedResult;
115
116 throw std::logic_error(std::move(expectedResult).error());
117}
118
125[[nodiscard]] inline std::expected<uint32_t, std::string>
126getLedgerIndex(boost::json::value const& value)
127{
128 if (not value.is_string()) {
129 return tryIntegralValueAs<uint32_t>(value);
130 }
131 if (value.as_string() != "validated") {
132 uint32_t ledgerIndex{};
133 if (beast::lexicalCastChecked(ledgerIndex, value.as_string().c_str())) {
134 return ledgerIndex;
135 }
136 return std::unexpected("Invalid ledger index string");
137 }
138 return std::unexpected("'validated' ledger index is requested");
139}
140
141} // namespace util
This namespace contains various utilities.
Definition AccountUtils.hpp:11
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:110
std::string toUpper(std::string str)
Convert a string to uppercase.
Definition JsonUtils.hpp:42
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:88
boost::json::object removeSecret(boost::json::object const &object)
Removes any detected secret information from a response JSON object.
Definition JsonUtils.hpp:55
std::string toLower(std::string str)
Convert a string to lowercase.
Definition JsonUtils.hpp:29
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:126