rippled
Loading...
Searching...
No Matches
Object.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_JSON_OBJECT_H_INCLUDED
21#define RIPPLE_JSON_OBJECT_H_INCLUDED
22
23#include <xrpl/json/Writer.h>
24
25#include <memory>
26
27namespace Json {
28
153{
154public:
155 Collection(Collection&& c) noexcept;
157 operator=(Collection&& c) noexcept;
158 Collection() = delete;
159
160 ~Collection();
161
162protected:
163 // A null parent means "no parent at all".
164 // Writers cannot be null.
165 Collection(Collection* parent, Writer*);
166 void
167 checkWritable(std::string const& label);
168
172};
173
174class Array;
175
176//------------------------------------------------------------------------------
177
179class Object : protected Collection
180{
181public:
183 class Root;
184
198 template <typename Scalar>
199 void
200 set(std::string const& key, Scalar const&);
201
202 void
203 set(std::string const& key, Json::Value const&);
204
205 // Detail class and method used to implement operator[].
206 class Proxy;
207
208 Proxy
209 operator[](std::string const& key);
210 Proxy
211 operator[](Json::StaticString const& key);
212
218 Object
219 setObject(std::string const& key);
220
226 Array
227 setArray(std::string const& key);
228
229protected:
230 friend class Array;
231 Object(Collection* parent, Writer* w) : Collection(parent, w)
232 {
233 }
234};
235
236class Object::Root : public Object
237{
238public:
240 Root(Writer&);
241};
242
243//------------------------------------------------------------------------------
244
246class Array : private Collection
247{
248public:
254 template <typename Scalar>
255 void
256 append(Scalar const&);
257
262 void
263 append(Json::Value const&);
264
270 Object
271 appendObject();
272
278 Array
279 appendArray();
280
281protected:
282 friend class Object;
283 Array(Collection* parent, Writer* w) : Collection(parent, w)
284 {
285 }
286};
287
288//------------------------------------------------------------------------------
289
290// Generic accessor functions to allow Json::Value and Collection to
291// interoperate.
292
296
298Array
299setArray(Object&, Json::StaticString const& key);
300
304
306Object
307addObject(Object&, Json::StaticString const& key);
308
312
314Array
315appendArray(Array&);
316
320
322Object
323appendObject(Array&);
324
326void
327copyFrom(Json::Value& to, Json::Value const& from);
328
330void
331copyFrom(Object& to, Json::Value const& from);
332
335{
336public:
337 WriterObject(Output const& output)
338 : writer_(std::make_unique<Writer>(output))
339 , object_(std::make_unique<Object::Root>(*writer_))
340 {
341 }
342
343 WriterObject(WriterObject&& other) = default;
344
345 Object*
347 {
348 return object_.get();
349 }
350
351 Object&
353 {
354 return *object_;
355 }
356
357private:
360};
361
364
365//------------------------------------------------------------------------------
366// Implementation details.
367
368// Detail class for Object::operator[].
370{
371private:
374
375public:
376 Proxy(Object& object, std::string const& key);
377
378 template <class T>
379 void
380 operator=(T const& t)
381 {
382 object_.set(key_, t);
383 // Note: This function shouldn't return *this, because it's a trap.
384 //
385 // In Json::Value, foo[jss::key] returns a reference to a
386 // mutable Json::Value contained _inside_ foo. But in the case of
387 // Json::Object, where we write once only, there isn't any such
388 // reference that can be returned. Returning *this would return an
389 // object "a level higher" than in Json::Value, leading to obscure bugs,
390 // particularly in generic code.
391 }
392};
393
394//------------------------------------------------------------------------------
395
396template <typename Scalar>
397void
398Array::append(Scalar const& value)
399{
400 checkWritable("append");
401 if (writer_)
402 writer_->append(value);
403}
404
405template <typename Scalar>
406void
407Object::set(std::string const& key, Scalar const& value)
408{
409 checkWritable("set");
410 if (writer_)
411 writer_->set(key, value);
412}
413
414inline Json::Value&
416{
417 return (json[key] = Json::arrayValue);
418}
419
420inline Array
422{
423 return json.setArray(std::string(key));
424}
425
426inline Json::Value&
428{
429 return (json[key] = Json::objectValue);
430}
431
432inline Object
434{
435 return object.setObject(std::string(key));
436}
437
438inline Json::Value&
440{
441 return json.append(Json::arrayValue);
442}
443
444inline Array
446{
447 return json.appendArray();
448}
449
450inline Json::Value&
452{
453 return json.append(Json::objectValue);
454}
455
456inline Object
458{
459 return json.appendObject();
460}
461
462} // namespace Json
463
464#endif
Represents a JSON array being written to a Writer.
Definition: Object.h:247
Object appendObject()
Append a new Object and return it.
Definition: Object.cpp:107
Array(Collection *parent, Writer *w)
Definition: Object.h:283
Array appendArray()
Append a new Array and return it.
Definition: Object.cpp:116
void append(Scalar const &)
Append a scalar to the Arrary.
Definition: Object.h:398
void checkWritable(std::string const &label)
Definition: Object.cpp:71
Collection & operator=(Collection &&c) noexcept
Definition: Object.cpp:52
Collection()=delete
Collection * parent_
Definition: Object.h:169
Writer * writer_
Definition: Object.h:170
Object & object_
Definition: Object.h:372
void operator=(T const &t)
Definition: Object.h:380
std::string const key_
Definition: Object.h:373
Represents a JSON object being written to a Writer.
Definition: Object.h:180
Proxy operator[](std::string const &key)
Definition: Object.cpp:132
Object(Collection *parent, Writer *w)
Definition: Object.h:231
void set(std::string const &key, Scalar const &)
Set a scalar value in the Object for a key.
Definition: Object.h:407
Array setArray(std::string const &key)
Make a new Array at a key and return it.
Definition: Object.cpp:96
Object setObject(std::string const &key)
Make a new Object at a key and return it.
Definition: Object.cpp:87
Lightweight wrapper to tag static string.
Definition: json_value.h:64
Represents a JSON value.
Definition: json_value.h:150
Value & append(Value const &value)
Append value to array at the end.
Definition: json_value.cpp:910
An Object that contains its own Writer.
Definition: Object.h:335
std::unique_ptr< Writer > writer_
Definition: Object.h:358
WriterObject(WriterObject &&other)=default
WriterObject(Output const &output)
Definition: Object.h:337
Object * operator->()
Definition: Object.h:346
std::unique_ptr< Object::Root > object_
Definition: Object.h:359
Object & operator*()
Definition: Object.h:352
Writer implements an O(1)-space, O(1)-granular output JSON writer.
Definition: json/Writer.h:128
void append(Scalar t)
Append a value to an array.
Definition: json/Writer.h:165
void set(std::string const &tag, Type t)
Add a key, value assignment to an object.
Definition: json/Writer.h:189
JSON (JavaScript Object Notation).
Definition: json_errors.h:25
Json::Value & appendArray(Json::Value &)
Append a new subarray to a Json array.
Definition: Object.h:439
@ arrayValue
array value (ordered list)
Definition: json_value.h:45
@ objectValue
object value (collection of name/value pairs).
Definition: json_value.h:46
WriterObject stringWriterObject(std::string &)
Definition: Object.cpp:247
Json::Value & setArray(Json::Value &, Json::StaticString const &key)
Add a new subarray at a named key in a Json object.
Definition: Object.h:415
Json::Value & addObject(Json::Value &, Json::StaticString const &key)
Add a new subobject at a named key in a Json object.
Definition: Object.h:427
Json::Value & appendObject(Json::Value &)
Append a new subobject to a Json object.
Definition: Object.h:451
void copyFrom(Json::Value &to, Json::Value const &from)
Copy all the keys and values from one object into another.
Definition: Object.cpp:232
STL namespace.