rippled
Loading...
Searching...
No Matches
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 <xrpl/basics/contract.h>
21#include <xrpl/beast/utility/instrumentation.h>
22#include <xrpl/json/Object.h>
23#include <xrpl/json/Output.h>
24#include <xrpl/json/Writer.h>
25#include <xrpl/json/json_value.h>
26
27#include <stdexcept>
28#include <utility>
29
30namespace Json {
31
33 : parent_(parent), writer_(writer), enabled_(true)
34{
35 checkWritable("Collection::Collection()");
36 if (parent_)
37 {
38 check(parent_->enabled_, "Parent not enabled in constructor");
39 parent_->enabled_ = false;
40 }
41}
42
44{
45 if (writer_)
46 writer_->finish();
47 if (parent_)
48 parent_->enabled_ = true;
49}
50
53{
54 parent_ = that.parent_;
55 writer_ = that.writer_;
56 enabled_ = that.enabled_;
57
58 that.parent_ = nullptr;
59 that.writer_ = nullptr;
60 that.enabled_ = false;
61
62 return *this;
63}
64
66{
67 *this = std::move(that);
68}
69
70void
72{
73 if (!enabled_)
74 ripple::Throw<std::logic_error>(label + ": not enabled");
75 if (!writer_)
76 ripple::Throw<std::logic_error>(label + ": not writable");
77}
78
79//------------------------------------------------------------------------------
80
85
88{
89 checkWritable("Object::setObject");
90 if (writer_)
92 return Object(this, writer_);
93}
94
97{
98 checkWritable("Object::setArray");
99 if (writer_)
101 return Array(this, writer_);
102}
103
104//------------------------------------------------------------------------------
105
106Object
108{
109 checkWritable("Array::appendObject");
110 if (writer_)
112 return Object(this, writer_);
113}
114
115Array
117{
118 checkWritable("Array::makeArray");
119 if (writer_)
121 return Array(this, writer_);
122}
123
124//------------------------------------------------------------------------------
125
127 : object_(object), key_(key)
128{
129}
130
133{
134 return Proxy(*this, key);
135}
136
139{
140 return Proxy(*this, std::string(key));
141}
142
143//------------------------------------------------------------------------------
144
145void
147{
148 auto t = v.type();
149 switch (t)
150 {
151 case Json::nullValue:
152 return append(nullptr);
153 case Json::intValue:
154 return append(v.asInt());
155 case Json::uintValue:
156 return append(v.asUInt());
157 case Json::realValue:
158 return append(v.asDouble());
160 return append(v.asString());
162 return append(v.asBool());
163
164 case Json::objectValue: {
165 auto object = appendObject();
166 copyFrom(object, v);
167 return;
168 }
169
170 case Json::arrayValue: {
171 auto array = appendArray();
172 for (auto& item : v)
173 array.append(item);
174 return;
175 }
176 }
177 UNREACHABLE("Json::Array::append : invalid type");
178}
179
180void
182{
183 auto t = v.type();
184 switch (t)
185 {
186 case Json::nullValue:
187 return set(k, nullptr);
188 case Json::intValue:
189 return set(k, v.asInt());
190 case Json::uintValue:
191 return set(k, v.asUInt());
192 case Json::realValue:
193 return set(k, v.asDouble());
195 return set(k, v.asString());
197 return set(k, v.asBool());
198
199 case Json::objectValue: {
200 auto object = setObject(k);
201 copyFrom(object, v);
202 return;
203 }
204
205 case Json::arrayValue: {
206 auto array = setArray(k);
207 for (auto& item : v)
208 array.append(item);
209 return;
210 }
211 }
212 UNREACHABLE("Json::Object::set : invalid type");
213}
214
215//------------------------------------------------------------------------------
216
217namespace {
218
219template <class Object>
220void
221doCopyFrom(Object& to, Json::Value const& from)
222{
223 XRPL_ASSERT(from.isObjectOrNull(), "Json::doCopyFrom : valid input type");
224 auto members = from.getMemberNames();
225 for (auto& m : members)
226 to[m] = from[m];
227}
228
229} // namespace
230
231void
233{
234 if (!to) // Short circuit this very common case.
235 to = from;
236 else
237 doCopyFrom(to, from);
238}
239
240void
241copyFrom(Object& to, Json::Value const& from)
242{
243 doCopyFrom(to, from);
244}
245
246WriterObject
251
252} // namespace Json
Represents a JSON array being written to a Writer.
Definition Object.h:247
Object appendObject()
Append a new Object and return it.
Definition Object.cpp:107
Array appendArray()
Append a new Array and return it.
Definition Object.cpp:116
void append(Scalar const &)
Append a scalar to the Arrary.
Definition Object.h:398
void checkWritable(std::string const &label)
Definition Object.cpp:71
Collection & operator=(Collection &&c) noexcept
Definition Object.cpp:52
Collection()=delete
Collection * parent_
Definition Object.h:169
Writer * writer_
Definition Object.h:170
Proxy(Object &object, std::string const &key)
Definition Object.cpp:126
Represents a JSON object being written to a Writer.
Definition Object.h:180
Proxy operator[](std::string const &key)
Definition Object.cpp:132
void set(std::string const &key, Scalar const &)
Set a scalar value in the Object for a key.
Definition Object.h:407
friend class Array
Definition Object.h:230
Array setArray(std::string const &key)
Make a new Array at a key and return it.
Definition Object.cpp:96
Object setObject(std::string const &key)
Make a new Object at a key and return it.
Definition Object.cpp:87
Lightweight wrapper to tag static string.
Definition json_value.h:63
Represents a JSON value.
Definition json_value.h:149
bool isObjectOrNull() const
Int asInt() const
UInt asUInt() const
Members getMemberNames() const
Return a list of the member names.
ValueType type() const
std::string asString() const
Returns the unquoted string value.
bool asBool() const
double asDouble() const
An Object that contains its own Writer.
Definition Object.h:335
Writer implements an O(1)-space, O(1)-granular output JSON writer.
void finish()
Finish the collection most recently started.
Definition Writer.cpp:358
void startRoot(CollectionType)
Start a new collection at the root level.
Definition Writer.cpp:337
void startAppend(CollectionType)
Start a new collection inside an array.
Definition Writer.cpp:343
void startSet(CollectionType, std::string const &key)
Start a new collection inside an object.
Definition Writer.cpp:350
JSON (JavaScript Object Notation).
Definition json_errors.h:25
Output stringOutput(std::string &s)
Json::Value & appendArray(Json::Value &)
Append a new subarray to a Json array.
Definition Object.h:439
@ booleanValue
bool value
Definition json_value.h:43
@ nullValue
'null' value
Definition json_value.h:38
@ stringValue
UTF-8 string value.
Definition json_value.h:42
@ realValue
double value
Definition json_value.h:41
@ arrayValue
array value (ordered list)
Definition json_value.h:44
@ intValue
signed integer value
Definition json_value.h:39
@ objectValue
object value (collection of name/value pairs).
Definition json_value.h:45
@ uintValue
unsigned integer value
Definition json_value.h:40
WriterObject stringWriterObject(std::string &)
Definition Object.cpp:247
Json::Value & appendObject(Json::Value &)
Append a new subobject to a Json object.
Definition Object.h:451
void copyFrom(Json::Value &to, Json::Value const &from)
Copy all the keys and values from one object into another.
Definition Object.cpp:232
void check(bool condition, std::string const &message)