xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
include
xrpl
json
json/Writer.h
1
#pragma once
2
3
#include <xrpl/basics/contract.h>
4
#include <xrpl/json/Output.h>
5
#include <xrpl/json/json_value.h>
6
7
#include <
cstddef
>
8
#include <
memory
>
9
#include <
stdexcept
>
10
#include <
string
>
11
12
namespace
json
{
13
108
109
class
Writer
110
{
111
public
:
112
enum class
CollectionType
{
Array
,
Object
};
113
114
explicit
Writer
(
Output
const
&
output
);
115
Writer
(
Writer
&&) noexcept;
116
Writer
&
117
operator=(
Writer
&&) noexcept;
118
119
~
Writer
();
120
124
void
startRoot
(
CollectionType
);
125
129
void
startAppend
(
CollectionType
);
130
134
void
135
startSet
(
CollectionType
,
std
::
string
const& key);
136
140
void
141
finish
();
142
147
void
148
finishAll
();
149
156
template <typename Scalar>
157
void
158
append
(Scalar t)
159
{
160
rawAppend
();
161
output
(t);
162
}
163
168
void
169
rawAppend
();
170
183
template
<
typename
Type>
184
void
185
set
(
std::string
const
& tag, Type t)
186
{
187
rawSet
(tag);
188
output
(t);
189
}
190
195
void
196
rawSet
(
std::string
const
& key);
197
198
// You won't need to call anything below here until you are writing single
199
// items (numbers, strings, bools, null) to a JSON stream.
200
201
/*** Output a string. */
202
void
203
output
(
std::string
const
&);
204
205
/*** Output a literal constant or C string. */
206
void
207
output
(
char
const
*);
208
209
/*** Output a json::Value. */
210
void
211
output
(
json::Value
const
&);
212
216
void
output
(
std::nullptr_t
);
217
221
void
222
output
(
float
);
223
227
void
228
output
(
double
);
229
233
void
234
output
(
bool
);
235
239
template
<
typename
Type>
240
void
241
output
(Type t)
242
{
243
implOutput
(
std::to_string
(t));
244
}
245
246
void
247
output
(
json::StaticString
const
& t)
248
{
249
output
(t.
cStr
());
250
}
251
252
private
:
253
class
Impl;
254
std::unique_ptr<Impl>
impl_
;
255
256
void
257
implOutput
(
std::string
const
&);
258
};
259
260
inline
void
261
check
(
bool
condition,
std::string
const
& message)
262
{
263
if
(!condition)
264
xrpl::Throw<std::logic_error>
(message);
265
}
266
267
}
// namespace json
std::string
json::StaticString
Lightweight wrapper to tag static string.
Definition
json_value.h:48
json::StaticString::cStr
constexpr char const * cStr() const
Definition
json_value.h:61
json::Value
Represents a JSON value.
Definition
json_value.h:117
json::Writer
Writer implements an O(1)-space, O(1)-granular output JSON writer.
Definition
json/Writer.h:110
json::Writer::finishAll
void finishAll()
Finish all objects and arrays.
Definition
libxrpl/json/Writer.cpp:303
json::Writer::set
void set(std::string const &tag, Type t)
Add a key, value assignment to an object.
Definition
json/Writer.h:185
json::Writer::rawAppend
void rawAppend()
Add a comma before this next item if not the first item in an array.
Definition
libxrpl/json/Writer.cpp:310
json::Writer::output
void output(json::StaticString const &t)
Definition
json/Writer.h:247
json::Writer::startRoot
void startRoot(CollectionType)
Start a new collection at the root level.
Definition
libxrpl/json/Writer.cpp:325
json::Writer::startAppend
void startAppend(CollectionType)
Start a new collection inside an array.
Definition
libxrpl/json/Writer.cpp:331
json::Writer::implOutput
void implOutput(std::string const &)
Definition
libxrpl/json/Writer.cpp:297
json::Writer::output
void output(std::string const &)
Definition
libxrpl/json/Writer.cpp:258
json::Writer::output
void output(Type t)
Output numbers or booleans.
Definition
json/Writer.h:241
json::Writer::Writer
Writer(Output const &output)
Definition
libxrpl/json/Writer.cpp:230
json::Writer::startSet
void startSet(CollectionType, std::string const &key)
Start a new collection inside an object.
Definition
libxrpl/json/Writer.cpp:338
json::Writer::impl_
std::unique_ptr< Impl > impl_
Definition
json/Writer.h:254
json::Writer::rawSet
void rawSet(std::string const &key)
Emit just "tag": as part of an object.
Definition
libxrpl/json/Writer.cpp:316
json::Writer::CollectionType
CollectionType
Definition
json/Writer.h:112
json::Writer::CollectionType::Array
@ Array
Definition
json/Writer.h:112
json::Writer::CollectionType::Object
@ Object
Definition
json/Writer.h:112
json::Writer::append
void append(Scalar t)
Append a value to an array.
Definition
json/Writer.h:158
json::Writer::finish
void finish()
Finish the collection most recently started.
Definition
libxrpl/json/Writer.cpp:346
cstddef
memory
json
JSON (JavaScript Object Notation).
Definition
json_errors.h:5
json::Output
std::function< void(std::string_view)> Output
Definition
include/xrpl/json/Output.h:11
json::check
void check(bool condition, std::string const &message)
Definition
json/Writer.h:261
std
STL namespace.
xrpl::Throw
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition
contract.h:52
std::nullptr_t
stdexcept
string
std::to_string
T to_string(T... args)
std::unique_ptr
Generated by
1.17.0