rippled
Object.cpp
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 #include <ripple/basics/contract.h>
21 #include <ripple/json/Object.h>
22 #include <cassert>
23 
24 namespace Json {
25 
27  : parent_ (parent), writer_ (writer), enabled_ (true)
28 {
29  checkWritable ("Collection::Collection()");
30  if (parent_)
31  {
32  check (parent_->enabled_, "Parent not enabled in constructor");
33  parent_->enabled_ = false;
34  }
35 }
36 
38 {
39  if (writer_)
40  writer_->finish ();
41  if (parent_)
42  parent_->enabled_ = true;
43 }
44 
46 {
47  parent_ = that.parent_;
48  writer_ = that.writer_;
49  enabled_ = that.enabled_;
50 
51  that.parent_ = nullptr;
52  that.writer_ = nullptr;
53  that.enabled_ = false;
54 
55  return *this;
56 }
57 
59 {
60  *this = std::move (that);
61 }
62 
64 {
65  if (! enabled_)
66  ripple::Throw<std::logic_error> (label + ": not enabled");
67  if (! writer_)
68  ripple::Throw<std::logic_error> (label + ": not writable");
69 }
70 
71 //------------------------------------------------------------------------------
72 
73 Object::Root::Root (Writer& w) : Object (nullptr, &w)
74 {
76 }
77 
79 {
80  checkWritable ("Object::setObject");
81  if (writer_)
83  return Object (this, writer_);
84 }
85 
87  checkWritable ("Object::setArray");
88  if (writer_)
90  return Array (this, writer_);
91 }
92 
93 //------------------------------------------------------------------------------
94 
96 {
97  checkWritable ("Array::appendObject");
98  if (writer_)
100  return Object (this, writer_);
101 }
102 
104 {
105  checkWritable ("Array::makeArray");
106  if (writer_)
108  return Array (this, writer_);
109 }
110 
111 //------------------------------------------------------------------------------
112 
114  : object_ (object)
115  , key_ (key)
116 {
117 }
118 
120 {
121  return Proxy (*this, key);
122 }
123 
125 {
126  return Proxy (*this, std::string (key));
127 }
128 
129 //------------------------------------------------------------------------------
130 
131 void Array::append (Json::Value const& v)
132 {
133  auto t = v.type();
134  switch (t)
135  {
136  case Json::nullValue: return append (nullptr);
137  case Json::intValue: return append (v.asInt());
138  case Json::uintValue: return append (v.asUInt());
139  case Json::realValue: return append (v.asDouble());
140  case Json::stringValue: return append (v.asString());
141  case Json::booleanValue: return append (v.asBool());
142 
143  case Json::objectValue:
144  {
145  auto object = appendObject ();
146  copyFrom (object, v);
147  return;
148  }
149 
150  case Json::arrayValue:
151  {
152  auto array = appendArray ();
153  for (auto& item: v)
154  array.append (item);
155  return;
156  }
157  }
158  assert (false); // Can't get here.
159 }
160 
161 void Object::set (std::string const& k, Json::Value const& v)
162 {
163  auto t = v.type();
164  switch (t)
165  {
166  case Json::nullValue: return set (k, nullptr);
167  case Json::intValue: return set (k, v.asInt());
168  case Json::uintValue: return set (k, v.asUInt());
169  case Json::realValue: return set (k, v.asDouble());
170  case Json::stringValue: return set (k, v.asString());
171  case Json::booleanValue: return set (k, v.asBool());
172 
173  case Json::objectValue:
174  {
175  auto object = setObject (k);
176  copyFrom (object, v);
177  return;
178  }
179 
180  case Json::arrayValue:
181  {
182  auto array = setArray (k);
183  for (auto& item: v)
184  array.append (item);
185  return;
186  }
187  }
188  assert (false); // Can't get here.
189 }
190 
191 //------------------------------------------------------------------------------
192 
193 namespace {
194 
195 template <class Object>
196 void doCopyFrom (Object& to, Json::Value const& from)
197 {
198  assert (from.isObjectOrNull());
199  auto members = from.getMemberNames();
200  for (auto& m: members)
201  to[m] = from[m];
202 }
203 
204 }
205 
206 void copyFrom (Json::Value& to, Json::Value const& from)
207 {
208  if (!to) // Short circuit this very common case.
209  to = from;
210  else
211  doCopyFrom (to, from);
212 }
213 
214 void copyFrom (Object& to, Json::Value const& from)
215 {
216  doCopyFrom (to, from);
217 }
218 
220 {
221  return WriterObject (stringOutput (s));
222 }
223 
224 } // Json
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::Object::Array
friend class Array
Definition: Object.h:221
Json::Collection::writer_
Writer * writer_
Definition: Object.h:167
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::booleanValue
@ booleanValue
bool value
Definition: json_value.h:43
Json::Array::appendArray
Array appendArray()
Append a new Array and return it.
Definition: Object.cpp:103
Json::Writer::finish
void finish()
Finish the collection most recently started.
Definition: Writer.cpp:316
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::realValue
@ realValue
double value
Definition: json_value.h:41
Json::check
void check(bool condition, std::string const &message)
Definition: json/Writer.h:234
Json::Writer::startAppend
void startAppend(CollectionType)
Start a new collection inside an array.
Definition: Writer.cpp:303
Json::Writer::startRoot
void startRoot(CollectionType)
Start a new collection at the root level.
Definition: Writer.cpp:298
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::Value::asBool
bool asBool() const
Definition: json_value.cpp:626
Json::uintValue
@ uintValue
unsigned integer value
Definition: json_value.h:40
Json
JSON (JavaScript Object Notation).
Definition: json_reader.cpp:26
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::Writer::array
@ array
Definition: json/Writer.h:129
Json::stringValue
@ stringValue
UTF-8 string value.
Definition: json_value.h:42
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
Json::Writer::startSet
void startSet(CollectionType, std::string const &key)
Start a new collection inside an object.
Definition: Writer.cpp:309
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::Value::getMemberNames
Members getMemberNames() const
Return a list of the member names.
Definition: json_value.cpp:979
Json::Writer::object
@ object
Definition: json/Writer.h:129
Json::StaticString
Lightweight wrapper to tag static string.
Definition: json_value.h:62
Json::intValue
@ intValue
signed integer value
Definition: json_value.h:39
cassert
Json::Value::asUInt
UInt asUInt() const
Definition: json_value.cpp:555
Json::nullValue
@ nullValue
'null' value
Definition: json_value.h:38
Json::Value::asInt
Int asInt() const
Definition: json_value.cpp:516
Json::stringOutput
Output stringOutput(std::string &s)
Definition: json/Output.h:33
Json::Value::asDouble
double asDouble() const
Definition: json_value.cpp:594
Json::Value::type
ValueType type() const
Definition: json_value.cpp:363
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::Value::isObjectOrNull
bool isObjectOrNull() const
Definition: json_value.cpp:1075
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
Json::Value::asString
std::string asString() const
Returns the unquoted string value.
Definition: json_value.cpp:482