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& operator= (Collection&& c) noexcept;
156  Collection() = delete;
157 
158  ~Collection();
159 
160 protected:
161  // A null parent means "no parent at all".
162  // Writers cannot be null.
163  Collection (Collection* parent, Writer*);
164  void checkWritable (std::string const& label);
165 
168  bool enabled_;
169 };
170 
171 class Array;
172 
173 //------------------------------------------------------------------------------
174 
176 class Object : protected Collection
177 {
178 public:
180  class Root;
181 
195  template <typename Scalar>
196  void set (std::string const& key, Scalar const&);
197 
198  void set (std::string const& key, Json::Value const&);
199 
200  // Detail class and method used to implement operator[].
201  class Proxy;
202 
203  Proxy operator[] (std::string const& key);
205 
211  Object setObject (std::string const& key);
212 
218  Array setArray (std::string const& key);
219 
220 protected:
221  friend class Array;
222  Object (Collection* parent, Writer* w) : Collection (parent, w) {}
223 };
224 
225 class Object::Root : public Object
226 {
227  public:
229  Root (Writer&);
230 };
231 
232 //------------------------------------------------------------------------------
233 
235 class Array : private Collection
236 {
237 public:
243  template <typename Scalar>
244  void append (Scalar const&);
245 
250  void append (Json::Value const&);
251 
257  Object appendObject ();
258 
264  Array appendArray ();
265 
266  protected:
267  friend class Object;
268  Array (Collection* parent, Writer* w) : Collection (parent, w) {}
269 };
270 
271 //------------------------------------------------------------------------------
272 
273 // Generic accessor functions to allow Json::Value and Collection to
274 // interoperate.
275 
278 
280 Array setArray (Object&, Json::StaticString const& key);
281 
282 
285 
287 Object addObject (Object&, Json::StaticString const& key);
288 
289 
292 
294 Array appendArray (Array&);
295 
296 
299 
301 Object appendObject (Array&);
302 
303 
305 void copyFrom (Json::Value& to, Json::Value const& from);
306 
308 void copyFrom (Object& to, Json::Value const& from);
309 
310 
313 {
314 public:
315  WriterObject (Output const& output)
316  : writer_ (std::make_unique<Writer> (output)),
317  object_ (std::make_unique<Object::Root> (*writer_))
318  {
319  }
320 
321  WriterObject (WriterObject&& other) = default;
322 
324  {
325  return object_.get();
326  }
327 
329  {
330  return *object_;
331  }
332 
333 private:
336 };
337 
339 
340 //------------------------------------------------------------------------------
341 // Implementation details.
342 
343 // Detail class for Object::operator[].
345 {
346 private:
349 
350 public:
351  Proxy (Object& object, std::string const& key);
352 
353  template <class T>
354  void operator= (T const& t)
355  {
356  object_.set (key_, t);
357  // Note: This function shouldn't return *this, because it's a trap.
358  //
359  // In Json::Value, foo[jss::key] returns a reference to a
360  // mutable Json::Value contained _inside_ foo. But in the case of
361  // Json::Object, where we write once only, there isn't any such
362  // reference that can be returned. Returning *this would return an
363  // object "a level higher" than in Json::Value, leading to obscure bugs,
364  // particularly in generic code.
365  }
366 };
367 
368 //------------------------------------------------------------------------------
369 
370 template <typename Scalar>
371 void Array::append (Scalar const& value)
372 {
373  checkWritable ("append");
374  if (writer_)
375  writer_->append (value);
376 }
377 
378 template <typename Scalar>
379 void Object::set (std::string const& key, Scalar const& value)
380 {
381  checkWritable ("set");
382  if (writer_)
383  writer_->set (key, value);
384 }
385 
386 inline
388 {
389  return (json[key] = Json::arrayValue);
390 }
391 
392 inline
394 {
395  return json.setArray (std::string (key));
396 }
397 
398 inline
400 {
401  return (json[key] = Json::objectValue);
402 }
403 
404 inline
406 {
407  return object.setObject (std::string (key));
408 }
409 
410 inline
412 {
413  return json.append (Json::arrayValue);
414 }
415 
416 inline
418 {
419  return json.appendArray ();
420 }
421 
422 inline
424 {
425  return json.append (Json::objectValue);
426 }
427 
428 inline
430 {
431  return json.appendObject ();
432 }
433 
434 } // Json
435 
436 #endif
Json::appendObject
Json::Value & appendObject(Json::Value &)
Append a new subobject to a Json object.
Definition: Object.h:423
Json::Object::setArray
Array setArray(std::string const &key)
Make a new Array at a key and return it.
Definition: Object.cpp:86
Json::Collection::writer_
Writer * writer_
Definition: Object.h:167
Json::Writer::append
void append(Scalar t)
Append a value to an array.
Definition: json/Writer.h:159
std::string
STL class.
Json::Collection::parent_
Collection * parent_
Definition: Object.h:166
Json::WriterObject
An Object that contains its own Writer.
Definition: Object.h:312
Json::arrayValue
@ arrayValue
array value (ordered list)
Definition: json_value.h:44
Json::Array::appendArray
Array appendArray()
Append a new Array and return it.
Definition: Object.cpp:103
Json::Object::Root::Root
Root(Writer &)
Each Object::Root must be constructed with its own unique Writer.
Definition: Object.cpp:73
Json::Object::Object
Object(Collection *parent, Writer *w)
Definition: Object.h:222
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:181
Json::WriterObject::object_
std::unique_ptr< Object::Root > object_
Definition: Object.h:335
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:206
Json::stringWriterObject
WriterObject stringWriterObject(std::string &s)
Definition: Object.cpp:219
Json::Collection::enabled_
bool enabled_
Definition: Object.h:168
Json::Collection
Definition: Object.h:151
Json::Object::Proxy::object_
Object & object_
Definition: Object.h:347
Json::Object::Root
Definition: Object.h:225
Json
JSON (JavaScript Object Notation).
Definition: json_reader.cpp:26
Json::Value::append
Value & append(const Value &value)
Append value to array at the end.
Definition: json_value.cpp:907
Json::Collection::checkWritable
void checkWritable(std::string const &label)
Definition: Object.cpp:63
Json::Object::set
void set(std::string const &key, Scalar const &)
Set a scalar value in the Object for a key.
Definition: Object.h:379
Json::objectValue
@ objectValue
object value (collection of name/value pairs).
Definition: json_value.h:45
Json::Object::operator[]
Proxy operator[](std::string const &key)
Definition: Object.cpp:119
Json::Array
Represents a JSON array being written to a Writer.
Definition: Object.h:235
Json::Object::setObject
Object setObject(std::string const &key)
Make a new Object at a key and return it.
Definition: Object.cpp:78
Json::Object::Proxy::Proxy
Proxy(Object &object, std::string const &key)
Definition: Object.cpp:113
Json::Array::Array
Array(Collection *parent, Writer *w)
Definition: Object.h:268
Json::WriterObject::operator*
Object & operator*()
Definition: Object.h:328
Json::Collection::operator=
Collection & operator=(Collection &&c) noexcept
Definition: Object.cpp:45
Json::Array::appendObject
Object appendObject()
Append a new Object and return it.
Definition: Object.cpp:95
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:387
Json::Array::append
void append(Scalar const &)
Append a scalar to the Arrary.
Definition: Object.h:371
Json::Object
Represents a JSON object being written to a Writer.
Definition: Object.h:176
Json::StaticString
Lightweight wrapper to tag static string.
Definition: json_value.h:62
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:399
Json::WriterObject::writer_
std::unique_ptr< Writer > writer_
Definition: Object.h:334
Json::WriterObject::operator->
Object * operator->()
Definition: Object.h:323
Json::WriterObject::WriterObject
WriterObject(Output const &output)
Definition: Object.h:315
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:354
Json::Object::Proxy::key_
const std::string key_
Definition: Object.h:348
Json::Object::Proxy
Definition: Object.h:344
Json::Value
Represents a JSON value.
Definition: json_value.h:141
Json::appendArray
Json::Value & appendArray(Json::Value &)
Append a new subarray to a Json array.
Definition: Object.h:411