xrpld
Loading...
Searching...
No Matches
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
12namespace json {
13
108
110{
111public:
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
125
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
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 {
244 }
245
246 void
248 {
249 output(t.cStr());
250 }
251
252private:
253 class Impl;
255
256 void
257 implOutput(std::string const&);
258};
259
260inline void
261check(bool condition, std::string const& message)
262{
263 if (!condition)
265}
266
267} // namespace json
Lightweight wrapper to tag static string.
Definition json_value.h:48
constexpr char const * cStr() const
Definition json_value.h:61
Represents a JSON value.
Definition json_value.h:117
Writer implements an O(1)-space, O(1)-granular output JSON writer.
void finishAll()
Finish all objects and arrays.
void set(std::string const &tag, Type t)
Add a key, value assignment to an object.
void rawAppend()
Add a comma before this next item if not the first item in an array.
void output(json::StaticString const &t)
void startRoot(CollectionType)
Start a new collection at the root level.
void startAppend(CollectionType)
Start a new collection inside an array.
void implOutput(std::string const &)
void output(std::string const &)
void output(Type t)
Output numbers or booleans.
Writer(Output const &output)
void startSet(CollectionType, std::string const &key)
Start a new collection inside an object.
std::unique_ptr< Impl > impl_
void rawSet(std::string const &key)
Emit just "tag": as part of an object.
void append(Scalar t)
Append a value to an array.
void finish()
Finish the collection most recently started.
JSON (JavaScript Object Notation).
Definition json_errors.h:5
std::function< void(boost::beast::string_view const &)> Output
void check(bool condition, std::string const &message)
STL namespace.
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition contract.h:52
T to_string(T... args)