rippled
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 <ripple/json/Writer.h>
24 #include <memory>
25 
26 namespace Json {
27 
152 {
153 public:
154  Collection(Collection&& c) noexcept;
155  Collection&
156  operator=(Collection&& c) noexcept;
157  Collection() = delete;
158 
159  ~Collection();
160 
161 protected:
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 
170  bool enabled_;
171 };
172 
173 class Array;
174 
175 //------------------------------------------------------------------------------
176 
178 class Object : protected Collection
179 {
180 public:
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 
228 protected:
229  friend class Array;
230  Object(Collection* parent, Writer* w) : Collection(parent, w)
231  {
232  }
233 };
234 
235 class Object::Root : public Object
236 {
237 public:
239  Root(Writer&);
240 };
241 
242 //------------------------------------------------------------------------------
243 
245 class Array : private Collection
246 {
247 public:
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 
280 protected:
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 
297 Array
298 setArray(Object&, Json::StaticString const& key);
299 
303 
305 Object
306 addObject(Object&, Json::StaticString const& key);
307 
311 
313 Array
314 appendArray(Array&);
315 
319 
321 Object
322 appendObject(Array&);
323 
325 void
326 copyFrom(Json::Value& to, Json::Value const& from);
327 
329 void
330 copyFrom(Object& to, Json::Value const& from);
331 
334 {
335 public:
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 
356 private:
359 };
360 
363 
364 //------------------------------------------------------------------------------
365 // Implementation details.
366 
367 // Detail class for Object::operator[].
369 {
370 private:
373 
374 public:
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 
395 template <typename Scalar>
396 void
397 Array::append(Scalar const& value)
398 {
399  checkWritable("append");
400  if (writer_)
401  writer_->append(value);
402 }
403 
404 template <typename Scalar>
405 void
406 Object::set(std::string const& key, Scalar const& value)
407 {
408  checkWritable("set");
409  if (writer_)
410  writer_->set(key, value);
411 }
412 
413 inline Json::Value&
415 {
416  return (json[key] = Json::arrayValue);
417 }
418 
419 inline Array
421 {
422  return json.setArray(std::string(key));
423 }
424 
425 inline Json::Value&
427 {
428  return (json[key] = Json::objectValue);
429 }
430 
431 inline Object
432 addObject(Object& object, Json::StaticString const& key)
433 {
434  return object.setObject(std::string(key));
435 }
436 
437 inline Json::Value&
439 {
440  return json.append(Json::arrayValue);
441 }
442 
443 inline Array
445 {
446  return json.appendArray();
447 }
448 
449 inline Json::Value&
451 {
452  return json.append(Json::objectValue);
453 }
454 
455 inline Object
457 {
458  return json.appendObject();
459 }
460 
461 } // namespace Json
462 
463 #endif
Json::appendObject
Json::Value & appendObject(Json::Value &)
Append a new subobject to a Json object.
Definition: Object.h:450
Json::Object::setArray
Array setArray(std::string const &key)
Make a new Array at a key and return it.
Definition: Object.cpp:90
Json::Collection::writer_
Writer * writer_
Definition: Object.h:169
Json::Writer::append
void append(Scalar t)
Append a value to an array.
Definition: json/Writer.h:164
std::string
STL class.
Json::Collection::parent_
Collection * parent_
Definition: Object.h:168
Json::WriterObject
An Object that contains its own Writer.
Definition: Object.h:333
Json::arrayValue
@ arrayValue
array value (ordered list)
Definition: json_value.h:42
Json::Array::appendArray
Array appendArray()
Append a new Array and return it.
Definition: Object.cpp:110
Json::Object::Root::Root
Root(Writer &)
Each Object::Root must be constructed with its own unique Writer.
Definition: Object.cpp:75
Json::Object::Object
Object(Collection *parent, Writer *w)
Definition: Object.h:230
Json::Collection::~Collection
~Collection()
Definition: Object.cpp:37
Json::Writer::set
void set(std::string const &tag, Type t)
Add a key, value assignment to an object.
Definition: json/Writer.h:188
Json::WriterObject::object_
std::unique_ptr< Object::Root > object_
Definition: Object.h:358
std::function
Json::copyFrom
void copyFrom(Json::Value &to, Json::Value const &from)
Copy all the keys and values from one object into another.
Definition: Object.cpp:226
Json::stringWriterObject
WriterObject stringWriterObject(std::string &s)
Definition: Object.cpp:241
Json::Collection::enabled_
bool enabled_
Definition: Object.h:170
Json::Collection
Definition: Object.h:151
Json::Object::Proxy::object_
Object & object_
Definition: Object.h:371
Json::Object::Root
Definition: Object.h:235
Json
JSON (JavaScript Object Notation).
Definition: json_reader.cpp:27
Json::Value::append
Value & append(const Value &value)
Append value to array at the end.
Definition: json_value.cpp:882
Json::Collection::checkWritable
void checkWritable(std::string const &label)
Definition: Object.cpp:65
Json::Object::set
void set(std::string const &key, Scalar const &)
Set a scalar value in the Object for a key.
Definition: Object.h:406
Json::objectValue
@ objectValue
object value (collection of name/value pairs).
Definition: json_value.h:43
Json::Object::operator[]
Proxy operator[](std::string const &key)
Definition: Object.cpp:126
Json::Array
Represents a JSON array being written to a Writer.
Definition: Object.h:245
Json::Object::setObject
Object setObject(std::string const &key)
Make a new Object at a key and return it.
Definition: Object.cpp:81
Json::Object::Proxy::Proxy
Proxy(Object &object, std::string const &key)
Definition: Object.cpp:120
Json::Array::Array
Array(Collection *parent, Writer *w)
Definition: Object.h:282
Json::WriterObject::operator*
Object & operator*()
Definition: Object.h:351
Json::Collection::operator=
Collection & operator=(Collection &&c) noexcept
Definition: Object.cpp:46
Json::Array::appendObject
Object appendObject()
Append a new Object and return it.
Definition: Object.cpp:101
memory
Json::setArray
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::Array::append
void append(Scalar const &)
Append a scalar to the Arrary.
Definition: Object.h:397
Json::Object
Represents a JSON object being written to a Writer.
Definition: Object.h:178
Json::StaticString
Lightweight wrapper to tag static string.
Definition: json_value.h:60
std
STL namespace.
Json::addObject
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::WriterObject::writer_
std::unique_ptr< Writer > writer_
Definition: Object.h:357
Json::WriterObject::operator->
Object * operator->()
Definition: Object.h:345
Json::WriterObject::WriterObject
WriterObject(Output const &output)
Definition: Object.h:336
std::unique_ptr
STL class.
Json::Writer
Writer implements an O(1)-space, O(1)-granular output JSON writer.
Definition: json/Writer.h:126
Json::Collection::Collection
Collection()=delete
Json::Object::Proxy::operator=
void operator=(T const &t)
Definition: Object.h:379
Json::Object::Proxy::key_
const std::string key_
Definition: Object.h:372
Json::Object::Proxy
Definition: Object.h:368
Json::Value
Represents a JSON value.
Definition: json_value.h:145
Json::appendArray
Json::Value & appendArray(Json::Value &)
Append a new subarray to a Json array.
Definition: Object.h:438