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 
47 {
48  parent_ = that.parent_;
49  writer_ = that.writer_;
50  enabled_ = that.enabled_;
51 
52  that.parent_ = nullptr;
53  that.writer_ = nullptr;
54  that.enabled_ = false;
55 
56  return *this;
57 }
58 
60 {
61  *this = std::move(that);
62 }
63 
64 void
66 {
67  if (!enabled_)
68  ripple::Throw<std::logic_error>(label + ": not enabled");
69  if (!writer_)
70  ripple::Throw<std::logic_error>(label + ": not writable");
71 }
72 
73 //------------------------------------------------------------------------------
74 
75 Object::Root::Root(Writer& w) : Object(nullptr, &w)
76 {
78 }
79 
80 Object
82 {
83  checkWritable("Object::setObject");
84  if (writer_)
86  return Object(this, writer_);
87 }
88 
89 Array
91 {
92  checkWritable("Object::setArray");
93  if (writer_)
95  return Array(this, writer_);
96 }
97 
98 //------------------------------------------------------------------------------
99 
100 Object
102 {
103  checkWritable("Array::appendObject");
104  if (writer_)
106  return Object(this, writer_);
107 }
108 
109 Array
111 {
112  checkWritable("Array::makeArray");
113  if (writer_)
115  return Array(this, writer_);
116 }
117 
118 //------------------------------------------------------------------------------
119 
121  : object_(object), key_(key)
122 {
123 }
124 
127 {
128  return Proxy(*this, key);
129 }
130 
133 {
134  return Proxy(*this, std::string(key));
135 }
136 
137 //------------------------------------------------------------------------------
138 
139 void
141 {
142  auto t = v.type();
143  switch (t)
144  {
145  case Json::nullValue:
146  return append(nullptr);
147  case Json::intValue:
148  return append(v.asInt());
149  case Json::uintValue:
150  return append(v.asUInt());
151  case Json::realValue:
152  return append(v.asDouble());
153  case Json::stringValue:
154  return append(v.asString());
155  case Json::booleanValue:
156  return append(v.asBool());
157 
158  case Json::objectValue: {
159  auto object = appendObject();
160  copyFrom(object, v);
161  return;
162  }
163 
164  case Json::arrayValue: {
165  auto array = appendArray();
166  for (auto& item : v)
167  array.append(item);
168  return;
169  }
170  }
171  assert(false); // Can't get here.
172 }
173 
174 void
176 {
177  auto t = v.type();
178  switch (t)
179  {
180  case Json::nullValue:
181  return set(k, nullptr);
182  case Json::intValue:
183  return set(k, v.asInt());
184  case Json::uintValue:
185  return set(k, v.asUInt());
186  case Json::realValue:
187  return set(k, v.asDouble());
188  case Json::stringValue:
189  return set(k, v.asString());
190  case Json::booleanValue:
191  return set(k, v.asBool());
192 
193  case Json::objectValue: {
194  auto object = setObject(k);
195  copyFrom(object, v);
196  return;
197  }
198 
199  case Json::arrayValue: {
200  auto array = setArray(k);
201  for (auto& item : v)
202  array.append(item);
203  return;
204  }
205  }
206  assert(false); // Can't get here.
207 }
208 
209 //------------------------------------------------------------------------------
210 
211 namespace {
212 
213 template <class Object>
214 void
215 doCopyFrom(Object& to, Json::Value const& from)
216 {
217  assert(from.isObjectOrNull());
218  auto members = from.getMemberNames();
219  for (auto& m : members)
220  to[m] = from[m];
221 }
222 
223 } // namespace
224 
225 void
227 {
228  if (!to) // Short circuit this very common case.
229  to = from;
230  else
231  doCopyFrom(to, from);
232 }
233 
234 void
235 copyFrom(Object& to, Json::Value const& from)
236 {
237  doCopyFrom(to, from);
238 }
239 
240 WriterObject
242 {
243  return WriterObject(stringOutput(s));
244 }
245 
246 } // namespace Json
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::Object::Array
friend class Array
Definition: Object.h:229
Json::Collection::writer_
Writer * writer_
Definition: Object.h:169
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::booleanValue
@ booleanValue
bool value
Definition: json_value.h:41
Json::Array::appendArray
Array appendArray()
Append a new Array and return it.
Definition: Object.cpp:110
Json::Writer::finish
void finish()
Finish the collection most recently started.
Definition: Writer.cpp:351
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::realValue
@ realValue
double value
Definition: json_value.h:39
Json::check
void check(bool condition, std::string const &message)
Definition: json/Writer.h:252
Json::Writer::startAppend
void startAppend(CollectionType)
Start a new collection inside an array.
Definition: Writer.cpp:336
Json::Writer::startRoot
void startRoot(CollectionType)
Start a new collection at the root level.
Definition: Writer.cpp:330
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::Value::asBool
bool asBool() const
Definition: json_value.cpp:619
Json::uintValue
@ uintValue
unsigned integer value
Definition: json_value.h:38
Json
JSON (JavaScript Object Notation).
Definition: json_reader.cpp:27
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::Writer::array
@ array
Definition: json/Writer.h:129
Json::stringValue
@ stringValue
UTF-8 string value.
Definition: json_value.h:40
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
Json::Writer::startSet
void startSet(CollectionType, std::string const &key)
Start a new collection inside an object.
Definition: Writer.cpp:343
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::Value::getMemberNames
Members getMemberNames() const
Return a list of the member names.
Definition: json_value.cpp:948
Json::Writer::object
@ object
Definition: json/Writer.h:129
Json::StaticString
Lightweight wrapper to tag static string.
Definition: json_value.h:60
Json::intValue
@ intValue
signed integer value
Definition: json_value.h:37
cassert
Json::Value::asUInt
UInt asUInt() const
Definition: json_value.cpp:545
Json::nullValue
@ nullValue
'null' value
Definition: json_value.h:36
Json::Value::asInt
Int asInt() const
Definition: json_value.cpp:503
Json::stringOutput
Output stringOutput(std::string &s)
Definition: json/Output.h:33
Json::Value::asDouble
double asDouble() const
Definition: json_value.cpp:587
Json::Value::type
ValueType type() const
Definition: json_value.cpp:350
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:1033
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
Json::Value::asString
std::string asString() const
Returns the unquoted string value.
Definition: json_value.cpp:469