rippled
Loading...
Searching...
No Matches
Object.h
1#ifndef XRPL_JSON_OBJECT_H_INCLUDED
2#define XRPL_JSON_OBJECT_H_INCLUDED
3
4#include <xrpl/json/Writer.h>
5
6#include <memory>
7
8namespace Json {
9
134{
135public:
136 Collection(Collection&& c) noexcept;
138 operator=(Collection&& c) noexcept;
139 Collection() = delete;
140
141 ~Collection();
142
143protected:
144 // A null parent means "no parent at all".
145 // Writers cannot be null.
146 Collection(Collection* parent, Writer*);
147 void
148 checkWritable(std::string const& label);
149
153};
154
155class Array;
156
157//------------------------------------------------------------------------------
158
160class Object : protected Collection
161{
162public:
164 class Root;
165
179 template <typename Scalar>
180 void
181 set(std::string const& key, Scalar const&);
182
183 void
184 set(std::string const& key, Json::Value const&);
185
186 // Detail class and method used to implement operator[].
187 class Proxy;
188
189 Proxy
190 operator[](std::string const& key);
191 Proxy
192 operator[](Json::StaticString const& key);
193
199 Object
200 setObject(std::string const& key);
201
207 Array
208 setArray(std::string const& key);
209
210protected:
211 friend class Array;
212 Object(Collection* parent, Writer* w) : Collection(parent, w)
213 {
214 }
215};
216
217class Object::Root : public Object
218{
219public:
221 Root(Writer&);
222};
223
224//------------------------------------------------------------------------------
225
227class Array : private Collection
228{
229public:
235 template <typename Scalar>
236 void
237 append(Scalar const&);
238
243 void
244 append(Json::Value const&);
245
251 Object
252 appendObject();
253
259 Array
260 appendArray();
261
262protected:
263 friend class Object;
264 Array(Collection* parent, Writer* w) : Collection(parent, w)
265 {
266 }
267};
268
269//------------------------------------------------------------------------------
270
271// Generic accessor functions to allow Json::Value and Collection to
272// interoperate.
273
277
279Array
280setArray(Object&, Json::StaticString const& key);
281
285
287Object
288addObject(Object&, Json::StaticString const& key);
289
293
295Array
296appendArray(Array&);
297
301
303Object
304appendObject(Array&);
305
307void
308copyFrom(Json::Value& to, Json::Value const& from);
309
311void
312copyFrom(Object& to, Json::Value const& from);
313
316{
317public:
318 WriterObject(Output const& output)
319 : writer_(std::make_unique<Writer>(output))
320 , object_(std::make_unique<Object::Root>(*writer_))
321 {
322 }
323
324 WriterObject(WriterObject&& other) = default;
325
326 Object*
328 {
329 return object_.get();
330 }
331
332 Object&
334 {
335 return *object_;
336 }
337
338private:
341};
342
345
346//------------------------------------------------------------------------------
347// Implementation details.
348
349// Detail class for Object::operator[].
351{
352private:
355
356public:
357 Proxy(Object& object, std::string const& key);
358
359 template <class T>
360 void
361 operator=(T const& t)
362 {
363 object_.set(key_, t);
364 // Note: This function shouldn't return *this, because it's a trap.
365 //
366 // In Json::Value, foo[jss::key] returns a reference to a
367 // mutable Json::Value contained _inside_ foo. But in the case of
368 // Json::Object, where we write once only, there isn't any such
369 // reference that can be returned. Returning *this would return an
370 // object "a level higher" than in Json::Value, leading to obscure bugs,
371 // particularly in generic code.
372 }
373};
374
375//------------------------------------------------------------------------------
376
377template <typename Scalar>
378void
379Array::append(Scalar const& value)
380{
381 checkWritable("append");
382 if (writer_)
383 writer_->append(value);
384}
385
386template <typename Scalar>
387void
388Object::set(std::string const& key, Scalar const& value)
389{
390 checkWritable("set");
391 if (writer_)
392 writer_->set(key, value);
393}
394
395inline Json::Value&
397{
398 return (json[key] = Json::arrayValue);
399}
400
401inline Array
403{
404 return json.setArray(std::string(key));
405}
406
407inline Json::Value&
409{
410 return (json[key] = Json::objectValue);
411}
412
413inline Object
415{
416 return object.setObject(std::string(key));
417}
418
419inline Json::Value&
421{
422 return json.append(Json::arrayValue);
423}
424
425inline Array
427{
428 return json.appendArray();
429}
430
431inline Json::Value&
433{
434 return json.append(Json::objectValue);
435}
436
437inline Object
439{
440 return json.appendObject();
441}
442
443} // namespace Json
444
445#endif
Represents a JSON array being written to a Writer.
Definition Object.h:228
Object appendObject()
Append a new Object and return it.
Definition Object.cpp:88
Array(Collection *parent, Writer *w)
Definition Object.h:264
Array appendArray()
Append a new Array and return it.
Definition Object.cpp:97
void append(Scalar const &)
Append a scalar to the Arrary.
Definition Object.h:379
void checkWritable(std::string const &label)
Definition Object.cpp:52
Collection & operator=(Collection &&c) noexcept
Definition Object.cpp:33
Collection()=delete
Collection * parent_
Definition Object.h:150
Writer * writer_
Definition Object.h:151
Object & object_
Definition Object.h:353
void operator=(T const &t)
Definition Object.h:361
std::string const key_
Definition Object.h:354
Represents a JSON object being written to a Writer.
Definition Object.h:161
Proxy operator[](std::string const &key)
Definition Object.cpp:113
Object(Collection *parent, Writer *w)
Definition Object.h:212
void set(std::string const &key, Scalar const &)
Set a scalar value in the Object for a key.
Definition Object.h:388
Array setArray(std::string const &key)
Make a new Array at a key and return it.
Definition Object.cpp:77
Object setObject(std::string const &key)
Make a new Object at a key and return it.
Definition Object.cpp:68
Lightweight wrapper to tag static string.
Definition json_value.h:44
Represents a JSON value.
Definition json_value.h:130
Value & append(Value const &value)
Append value to array at the end.
An Object that contains its own Writer.
Definition Object.h:316
std::unique_ptr< Writer > writer_
Definition Object.h:339
WriterObject(WriterObject &&other)=default
WriterObject(Output const &output)
Definition Object.h:318
Object * operator->()
Definition Object.h:327
std::unique_ptr< Object::Root > object_
Definition Object.h:340
Object & operator*()
Definition Object.h:333
Writer implements an O(1)-space, O(1)-granular output JSON writer.
void append(Scalar t)
Append a value to an array.
void set(std::string const &tag, Type t)
Add a key, value assignment to an object.
JSON (JavaScript Object Notation).
Definition json_errors.h:6
Json::Value & appendArray(Json::Value &)
Append a new subarray to a Json array.
Definition Object.h:420
@ arrayValue
array value (ordered list)
Definition json_value.h:25
@ objectValue
object value (collection of name/value pairs).
Definition json_value.h:26
WriterObject stringWriterObject(std::string &)
Definition Object.cpp:228
Json::Value & setArray(Json::Value &, Json::StaticString const &key)
Add a new subarray at a named key in a Json object.
Definition Object.h:396
Json::Value & addObject(Json::Value &, Json::StaticString const &key)
Add a new subobject at a named key in a Json object.
Definition Object.h:408
Json::Value & appendObject(Json::Value &)
Append a new subobject to a Json object.
Definition Object.h:432
void copyFrom(Json::Value &to, Json::Value const &from)
Copy all the keys and values from one object into another.
Definition Object.cpp:213
STL namespace.