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#include <memory>
25
26namespace Json {
27
152{
153public:
154 Collection(Collection&& c) noexcept;
156 operator=(Collection&& c) noexcept;
157 Collection() = delete;
158
159 ~Collection();
160
161protected:
162 // A null parent means "no parent at all".
163 // Writers cannot be null.
164 Collection(Collection* parent, Writer*);
165 void
166 checkWritable(std::string const& label);
167
171};
172
173class Array;
174
175//------------------------------------------------------------------------------
176
178class Object : protected Collection
179{
180public:
182 class Root;
183
197 template <typename Scalar>
198 void
199 set(std::string const& key, Scalar const&);
200
201 void
202 set(std::string const& key, Json::Value const&);
203
204 // Detail class and method used to implement operator[].
205 class Proxy;
206
207 Proxy
208 operator[](std::string const& key);
209 Proxy
210 operator[](Json::StaticString const& key);
211
217 Object
218 setObject(std::string const& key);
219
225 Array
226 setArray(std::string const& key);
227
228protected:
229 friend class Array;
230 Object(Collection* parent, Writer* w) : Collection(parent, w)
231 {
232 }
233};
234
235class Object::Root : public Object
236{
237public:
239 Root(Writer&);
240};
241
242//------------------------------------------------------------------------------
243
245class Array : private Collection
246{
247public:
253 template <typename Scalar>
254 void
255 append(Scalar const&);
256
261 void
262 append(Json::Value const&);
263
269 Object
270 appendObject();
271
277 Array
278 appendArray();
279
280protected:
281 friend class Object;
282 Array(Collection* parent, Writer* w) : Collection(parent, w)
283 {
284 }
285};
286
287//------------------------------------------------------------------------------
288
289// Generic accessor functions to allow Json::Value and Collection to
290// interoperate.
291
295
297Array
298setArray(Object&, Json::StaticString const& key);
299
303
305Object
306addObject(Object&, Json::StaticString const& key);
307
311
313Array
314appendArray(Array&);
315
319
321Object
322appendObject(Array&);
323
325void
326copyFrom(Json::Value& to, Json::Value const& from);
327
329void
330copyFrom(Object& to, Json::Value const& from);
331
334{
335public:
336 WriterObject(Output const& output)
337 : writer_(std::make_unique<Writer>(output))
338 , object_(std::make_unique<Object::Root>(*writer_))
339 {
340 }
341
342 WriterObject(WriterObject&& other) = default;
343
344 Object*
346 {
347 return object_.get();
348 }
349
350 Object&
352 {
353 return *object_;
354 }
355
356private:
359};
360
363
364//------------------------------------------------------------------------------
365// Implementation details.
366
367// Detail class for Object::operator[].
369{
370private:
373
374public:
375 Proxy(Object& object, std::string const& key);
376
377 template <class T>
378 void
379 operator=(T const& t)
380 {
381 object_.set(key_, t);
382 // Note: This function shouldn't return *this, because it's a trap.
383 //
384 // In Json::Value, foo[jss::key] returns a reference to a
385 // mutable Json::Value contained _inside_ foo. But in the case of
386 // Json::Object, where we write once only, there isn't any such
387 // reference that can be returned. Returning *this would return an
388 // object "a level higher" than in Json::Value, leading to obscure bugs,
389 // particularly in generic code.
390 }
391};
392
393//------------------------------------------------------------------------------
394
395template <typename Scalar>
396void
397Array::append(Scalar const& value)
398{
399 checkWritable("append");
400 if (writer_)
401 writer_->append(value);
402}
403
404template <typename Scalar>
405void
406Object::set(std::string const& key, Scalar const& value)
407{
408 checkWritable("set");
409 if (writer_)
410 writer_->set(key, value);
411}
412
413inline Json::Value&
415{
416 return (json[key] = Json::arrayValue);
417}
418
419inline Array
421{
422 return json.setArray(std::string(key));
423}
424
425inline Json::Value&
427{
428 return (json[key] = Json::objectValue);
429}
430
431inline Object
433{
434 return object.setObject(std::string(key));
435}
436
437inline Json::Value&
439{
440 return json.append(Json::arrayValue);
441}
442
443inline Array
445{
446 return json.appendArray();
447}
448
449inline Json::Value&
451{
452 return json.append(Json::objectValue);
453}
454
455inline Object
457{
458 return json.appendObject();
459}
460
461} // namespace Json
462
463#endif
Represents a JSON array being written to a Writer.
Definition: Object.h:246
Object appendObject()
Append a new Object and return it.
Definition: Object.cpp:101
Array(Collection *parent, Writer *w)
Definition: Object.h:282
Array appendArray()
Append a new Array and return it.
Definition: Object.cpp:110
void append(Scalar const &)
Append a scalar to the Arrary.
Definition: Object.h:397
void checkWritable(std::string const &label)
Definition: Object.cpp:65
Collection & operator=(Collection &&c) noexcept
Definition: Object.cpp:46
Collection()=delete
Collection * parent_
Definition: Object.h:168
Writer * writer_
Definition: Object.h:169
Object & object_
Definition: Object.h:371
void operator=(T const &t)
Definition: Object.h:379
std::string const key_
Definition: Object.h:372
Represents a JSON object being written to a Writer.
Definition: Object.h:179
Proxy operator[](std::string const &key)
Definition: Object.cpp:126
Object(Collection *parent, Writer *w)
Definition: Object.h:230
void set(std::string const &key, Scalar const &)
Set a scalar value in the Object for a key.
Definition: Object.h:406
Array setArray(std::string const &key)
Make a new Array at a key and return it.
Definition: Object.cpp:90
Object setObject(std::string const &key)
Make a new Object at a key and return it.
Definition: Object.cpp:81
Lightweight wrapper to tag static string.
Definition: json_value.h:61
Represents a JSON value.
Definition: json_value.h:147
Value & append(const Value &value)
Append value to array at the end.
Definition: json_value.cpp:891
An Object that contains its own Writer.
Definition: Object.h:334
std::unique_ptr< Writer > writer_
Definition: Object.h:357
WriterObject(WriterObject &&other)=default
WriterObject(Output const &output)
Definition: Object.h:336
Object * operator->()
Definition: Object.h:345
std::unique_ptr< Object::Root > object_
Definition: Object.h:358
Object & operator*()
Definition: Object.h:351
Writer implements an O(1)-space, O(1)-granular output JSON writer.
Definition: json/Writer.h:127
void append(Scalar t)
Append a value to an array.
Definition: json/Writer.h:164
void set(std::string const &tag, Type t)
Add a key, value assignment to an object.
Definition: json/Writer.h:188
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:438
@ arrayValue
array value (ordered list)
Definition: json_value.h:42
@ objectValue
object value (collection of name/value pairs).
Definition: json_value.h:43
WriterObject stringWriterObject(std::string &)
Definition: Object.cpp:241
Json::Value & setArray(Json::Value &, Json::StaticString const &key)
Add a new subarray at a named key in a Json object.
Definition: Object.h:414
Json::Value & addObject(Json::Value &, Json::StaticString const &key)
Add a new subobject at a named key in a Json object.
Definition: Object.h:426
Json::Value & appendObject(Json::Value &)
Append a new subobject to a Json object.
Definition: Object.h:450
void copyFrom(Json::Value &to, Json::Value const &from)
Copy all the keys and values from one object into another.
Definition: Object.cpp:226
STL namespace.