rippled
Loading...
Searching...
No Matches
json/Writer.h
1#ifndef XRPL_JSON_WRITER_H_INCLUDED
2#define XRPL_JSON_WRITER_H_INCLUDED
3
4#include <xrpl/basics/ToString.h>
5#include <xrpl/basics/contract.h>
6#include <xrpl/json/Output.h>
7#include <xrpl/json/json_value.h>
8
9#include <memory>
10
11namespace Json {
12
109{
110public:
111 enum CollectionType { array, object };
112
113 explicit Writer(Output const& output);
114 Writer(Writer&&) noexcept;
115 Writer&
116 operator=(Writer&&) noexcept;
117
118 ~Writer();
119
122
125
127 void
128 startSet(CollectionType, std::string const& key);
129
131 void
132 finish();
133
136 void
137 finishAll();
138
144 template <typename Scalar>
145 void
146 append(Scalar t)
147 {
148 rawAppend();
149 output(t);
150 }
151
154 void
155 rawAppend();
156
168 template <typename Type>
169 void
170 set(std::string const& tag, Type t)
171 {
172 rawSet(tag);
173 output(t);
174 }
175
178 void
179 rawSet(std::string const& key);
180
181 // You won't need to call anything below here until you are writing single
182 // items (numbers, strings, bools, null) to a JSON stream.
183
184 /*** Output a string. */
185 void
186 output(std::string const&);
187
188 /*** Output a literal constant or C string. */
189 void
190 output(char const*);
191
192 /*** Output a Json::Value. */
193 void
194 output(Json::Value const&);
195
198
200 void
201 output(float);
202
204 void
205 output(double);
206
208 void
209 output(bool);
210
212 template <typename Type>
213 void
214 output(Type t)
215 {
217 }
218
219 void
221 {
222 output(t.c_str());
223 }
224
225private:
226 class Impl;
228
229 void
230 implOutput(std::string const&);
231};
232
233inline void
234check(bool condition, std::string const& message)
235{
236 if (!condition)
237 ripple::Throw<std::logic_error>(message);
238}
239
240} // namespace Json
241
242#endif
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:6
void check(bool condition, std::string const &message)
STL namespace.
T to_string(T... args)