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 <boost/json.hpp>
23#include <boost/json/object.hpp>
24
25#include <algorithm>
26#include <cctype>
27#include <concepts>
28#include <stdexcept>
29#include <string>
30
34namespace util {
35
42inline std::string
43toLower(std::string str)
44{
45 std::ranges::transform(str, std::begin(str), [](unsigned char c) { return std::tolower(c); });
46 return str;
47}
48
55inline std::string
56toUpper(std::string str)
57{
58 std::ranges::transform(str, std::begin(str), [](unsigned char c) { return std::toupper(c); });
59 return str;
60}
61
68inline boost::json::object
69removeSecret(boost::json::object const& object)
70{
71 auto newObject = object;
72 auto const secretFields = {"secret", "seed", "seed_hex", "passphrase"};
73
74 if (newObject.contains("params") and newObject.at("params").is_array() and
75 not newObject.at("params").as_array().empty() and newObject.at("params").as_array()[0].is_object()) {
76 for (auto const& secretField : secretFields) {
77 if (newObject.at("params").as_array()[0].as_object().contains(secretField))
78 newObject.at("params").as_array()[0].as_object()[secretField] = "*";
79 }
80 }
81
82 // for websocket requests
83 for (auto const& secretField : secretFields) {
84 if (newObject.contains(secretField))
85 newObject[secretField] = "*";
86 }
87
88 return newObject;
89}
90
100template <std::integral Type>
101Type
102integralValueAs(boost::json::value const& value)
103{
104 if (value.is_uint64())
105 return static_cast<Type>(value.as_uint64());
106
107 if (value.is_int64())
108 return static_cast<Type>(value.as_int64());
109
110 throw std::logic_error("Value neither uint64 nor int64");
111}
112
113} // 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:102
std::string toUpper(std::string str)
Convert a string to uppercase.
Definition JsonUtils.hpp:56
boost::json::object removeSecret(boost::json::object const &object)
Removes any detected secret information from a response JSON object.
Definition JsonUtils.hpp:69
std::string toLower(std::string str)
Convert a string to lowercase.
Definition JsonUtils.hpp:43