rippled
Loading...
Searching...
No Matches
json/Writer.h
1#pragma once
2
3#include <xrpl/basics/ToString.h>
4#include <xrpl/basics/contract.h>
5#include <xrpl/json/Output.h>
6#include <xrpl/json/json_value.h>
7
8#include <memory>
9
10namespace Json {
11
108{
109public:
110 enum CollectionType { array, object };
111
112 explicit Writer(Output const& output);
113 Writer(Writer&&) noexcept;
114 Writer&
115 operator=(Writer&&) noexcept;
116
117 ~Writer();
118
121
124
126 void
127 startSet(CollectionType, std::string const& key);
128
130 void
131 finish();
132
135 void
136 finishAll();
137
143 template <typename Scalar>
144 void
145 append(Scalar t)
146 {
147 rawAppend();
148 output(t);
149 }
150
153 void
154 rawAppend();
155
167 template <typename Type>
168 void
169 set(std::string const& tag, Type t)
170 {
171 rawSet(tag);
172 output(t);
173 }
174
177 void
178 rawSet(std::string const& key);
179
180 // You won't need to call anything below here until you are writing single
181 // items (numbers, strings, bools, null) to a JSON stream.
182
183 /*** Output a string. */
184 void
185 output(std::string const&);
186
187 /*** Output a literal constant or C string. */
188 void
189 output(char const*);
190
191 /*** Output a Json::Value. */
192 void
193 output(Json::Value const&);
194
197
199 void
200 output(float);
201
203 void
204 output(double);
205
207 void
208 output(bool);
209
211 template <typename Type>
212 void
213 output(Type t)
214 {
216 }
217
218 void
220 {
221 output(t.c_str());
222 }
223
224private:
225 class Impl;
227
228 void
229 implOutput(std::string const&);
230};
231
232inline void
233check(bool condition, std::string const& message)
234{
235 if (!condition)
236 xrpl::Throw<std::logic_error>(message);
237}
238
239} // namespace Json
Lightweight wrapper to tag static string.
Definition json_value.h:44
constexpr char const * c_str() const
Definition json_value.h:57
Represents a JSON value.
Definition json_value.h:130
Writer implements an O(1)-space, O(1)-granular output JSON writer.
void finish()
Finish the collection most recently started.
void output(std::string const &)
void implOutput(std::string const &)
void startRoot(CollectionType)
Start a new collection at the root level.
void output(Type t)
Output numbers or booleans.
void append(Scalar t)
Append a value to an array.
void rawAppend()
Add a comma before this next item if not the first item in an array.
void rawSet(std::string const &key)
Emit just "tag": as part of an object.
void finishAll()
Finish all objects and arrays.
void startAppend(CollectionType)
Start a new collection inside an array.
void set(std::string const &tag, Type t)
Add a key, value assignment to an object.
void startSet(CollectionType, std::string const &key)
Start a new collection inside an object.
void output(Json::StaticString const &t)
std::unique_ptr< Impl > impl_
JSON (JavaScript Object Notation).
Definition json_errors.h:5
void check(bool condition, std::string const &message)
STL namespace.
T to_string(T... args)