xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
include
xrpl
protocol
json_get_or_throw.h
1
#pragma once
2
3
#include <xrpl/basics/Buffer.h>
4
#include <xrpl/basics/StringUtilities.h>
5
#include <xrpl/basics/contract.h>
6
#include <xrpl/json/json_value.h>
7
#include <xrpl/protocol/SField.h>
8
9
#include <
charconv
>
10
#include <
cstdint
>
11
#include <
exception
>
12
#include <
optional
>
13
#include <
string
>
14
#include <
system_error
>
15
#include <
utility
>
16
17
namespace
json
{
18
struct
JsonMissingKeyError
:
std::exception
19
{
20
char
const
*
const
key
;
21
mutable
std::string
msg
;
22
JsonMissingKeyError
(
json::StaticString
const
& k) :
key
{k.cStr()}
23
{
24
}
25
char
const
*
26
what
() const noexcept
override
27
{
28
if
(
msg
.empty())
29
{
30
msg
=
std::string
(
"Missing json key: "
) +
key
;
31
}
32
return
msg
.c_str();
33
}
34
};
35
36
struct
JsonTypeMismatchError
:
std::exception
37
{
38
char
const
*
const
key
;
39
std::string
const
expectedType
;
40
mutable
std::string
msg
;
41
JsonTypeMismatchError
(
json::StaticString
const
& k,
std::string
et)
42
:
key
{k.cStr()},
expectedType
{
std
::move(et)}
43
{
44
}
45
char
const
*
46
what
() const noexcept
override
47
{
48
if
(
msg
.empty())
49
{
50
msg
=
std::string
(
"Type mismatch on json key: "
) +
key
+
51
"; expected type: "
+
expectedType
;
52
}
53
return
msg
.c_str();
54
}
55
};
56
57
template
<
class
T>
58
T
59
getOrThrow
(
json::Value
const
& v,
xrpl::SField
const
& field)
60
{
61
static_assert
(
sizeof
(T) == -1,
"This function must be specialized"
);
62
}
63
64
template
<>
65
inline
std::string
66
getOrThrow
(
json::Value
const
& v,
xrpl::SField
const
& field)
67
{
68
using namespace
xrpl
;
69
json::StaticString
const
& key = field.
getJsonName
();
70
if
(!v.
isMember
(key))
71
Throw<JsonMissingKeyError>
(key);
72
73
json::Value
const
& inner = v[key];
74
if
(!inner.
isString
())
75
Throw<JsonTypeMismatchError>
(key,
"string"
);
76
return
inner.
asString
();
77
}
78
79
// Note, this allows integer numeric fields to act as bools
80
template
<>
81
inline
bool
82
getOrThrow
(
json::Value
const
& v,
xrpl::SField
const
& field)
83
{
84
using namespace
xrpl
;
85
json::StaticString
const
& key = field.
getJsonName
();
86
if
(!v.
isMember
(key))
87
Throw<JsonMissingKeyError>
(key);
88
json::Value
const
& inner = v[key];
89
if
(inner.
isBool
())
90
return
inner.
asBool
();
91
if
(!inner.
isIntegral
())
92
Throw<JsonTypeMismatchError>
(key,
"bool"
);
93
94
return
inner.
asInt
() != 0;
95
}
96
97
template
<>
98
inline
std::uint64_t
99
getOrThrow
(
json::Value
const
& v,
xrpl::SField
const
& field)
100
{
101
using namespace
xrpl
;
102
json::StaticString
const
& key = field.
getJsonName
();
103
if
(!v.
isMember
(key))
104
Throw<JsonMissingKeyError>
(key);
105
json::Value
const
& inner = v[key];
106
if
(inner.
isUInt
())
107
return
inner.
asUInt
();
108
if
(inner.
isInt
())
109
{
110
auto
const
r = inner.
asInt
();
111
if
(r < 0)
112
Throw<JsonTypeMismatchError>
(key,
"uint64"
);
113
return
r;
114
}
115
if
(inner.
isString
())
116
{
117
auto
const
s = inner.
asString
();
118
// parse as hex
119
std::uint64_t
val = 0;
120
121
auto
[p, ec] =
std::from_chars
(s.data(), s.data() + s.size(), val, 16);
122
123
if
(ec !=
std::errc
() || (p != s.data() + s.size()))
124
Throw<JsonTypeMismatchError>
(key,
"uint64"
);
125
return
val;
126
}
127
Throw<JsonTypeMismatchError>
(key,
"uint64"
);
128
}
129
130
template
<>
131
inline
xrpl::Buffer
132
getOrThrow
(
json::Value
const
& v,
xrpl::SField
const
& field)
133
{
134
using namespace
xrpl
;
135
std::string
const
hex =
getOrThrow<std::string>
(v, field);
136
if
(
auto
const
r =
strUnHex
(hex))
137
{
138
// TODO: mismatch between a buffer and a blob
139
return
Buffer
{r->data(), r->size()};
140
}
141
Throw<JsonTypeMismatchError>
(field.
getJsonName
(),
"Buffer"
);
142
}
143
144
// This function may be used by external projects (like the witness server).
145
template
<
class
T>
146
std::optional<T>
147
getOptional
(
json::Value
const
& v,
xrpl::SField
const
& field)
148
{
149
try
150
{
151
return
getOrThrow<T>
(v, field);
152
}
153
catch
(...)
// NOLINT(bugprone-empty-catch)
154
{
155
}
156
return
{};
157
}
158
159
}
// namespace json
std::string
charconv
json::StaticString
Lightweight wrapper to tag static string.
Definition
json_value.h:48
json::Value
Represents a JSON value.
Definition
json_value.h:117
json::Value::isIntegral
bool isIntegral() const
Definition
json_value.cpp:1116
json::Value::asBool
bool asBool() const
Definition
json_value.cpp:708
json::Value::isBool
bool isBool() const
Definition
json_value.cpp:1098
json::Value::isString
bool isString() const
Definition
json_value.cpp:1134
json::Value::isUInt
bool isUInt() const
Definition
json_value.cpp:1110
json::Value::isInt
bool isInt() const
Definition
json_value.cpp:1104
json::Value::asUInt
UInt asUInt() const
Definition
json_value.cpp:631
json::Value::asString
std::string asString() const
Returns the unquoted string value.
Definition
json_value.cpp:494
json::Value::isMember
bool isMember(char const *key) const
Return true if the object has a member named key.
Definition
json_value.cpp:1049
json::Value::asInt
Int asInt() const
Definition
json_value.cpp:530
xrpl::Buffer
Like std::vector<char> but better.
Definition
Buffer.h:19
xrpl::SField
Identifies fields.
Definition
SField.h:132
xrpl::SField::getJsonName
json::StaticString const & getJsonName() const
Definition
SField.h:210
cstdint
std::errc
exception
std::from_chars
T from_chars(T... args)
std::uint64_t
json
JSON (JavaScript Object Notation).
Definition
json_errors.h:5
json::getOptional
std::optional< T > getOptional(json::Value const &v, xrpl::SField const &field)
Definition
json_get_or_throw.h:147
json::getOrThrow
xrpl::AccountID getOrThrow(json::Value const &v, xrpl::SField const &field)
Definition
AccountID.h:126
std
STL namespace.
xrpl
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition
algorithm.h:5
xrpl::strUnHex
std::optional< Blob > strUnHex(std::size_t strSize, Iterator begin, Iterator end)
Definition
StringUtilities.h:64
xrpl::Throw
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition
contract.h:52
optional
string
json::JsonMissingKeyError::key
char const *const key
Definition
json_get_or_throw.h:20
json::JsonMissingKeyError::msg
std::string msg
Definition
json_get_or_throw.h:21
json::JsonMissingKeyError::JsonMissingKeyError
JsonMissingKeyError(json::StaticString const &k)
Definition
json_get_or_throw.h:22
json::JsonMissingKeyError::what
char const * what() const noexcept override
Definition
json_get_or_throw.h:26
json::JsonTypeMismatchError::JsonTypeMismatchError
JsonTypeMismatchError(json::StaticString const &k, std::string et)
Definition
json_get_or_throw.h:41
json::JsonTypeMismatchError::what
char const * what() const noexcept override
Definition
json_get_or_throw.h:46
json::JsonTypeMismatchError::expectedType
std::string const expectedType
Definition
json_get_or_throw.h:39
json::JsonTypeMismatchError::msg
std::string msg
Definition
json_get_or_throw.h:40
json::JsonTypeMismatchError::key
char const *const key
Definition
json_get_or_throw.h:38
system_error
utility
Generated by
1.17.0