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
24namespace 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
64void
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
76{
78}
79
82{
83 checkWritable("Object::setObject");
84 if (writer_)
86 return Object(this, writer_);
87}
88
91{
92 checkWritable("Object::setArray");
93 if (writer_)
95 return Array(this, writer_);
96}
97
98//------------------------------------------------------------------------------
99
100Object
102{
103 checkWritable("Array::appendObject");
104 if (writer_)
106 return Object(this, writer_);
107}
108
109Array
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
139void
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());
154 return append(v.asString());
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 UNREACHABLE("Json::Array::append : invalid type");
172}
173
174void
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());
189 return set(k, v.asString());
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 UNREACHABLE("Json::Object::set : invalid type");
207}
208
209//------------------------------------------------------------------------------
210
211namespace {
212
213template <class Object>
214void
215doCopyFrom(Object& to, Json::Value const& from)
216{
217 XRPL_ASSERT(from.isObjectOrNull(), "Json::doCopyFrom : valid input type");
218 auto members = from.getMemberNames();
219 for (auto& m : members)
220 to[m] = from[m];
221}
222
223} // namespace
224
225void
227{
228 if (!to) // Short circuit this very common case.
229 to = from;
230 else
231 doCopyFrom(to, from);
232}
233
234void
235copyFrom(Object& to, Json::Value const& from)
236{
237 doCopyFrom(to, from);
238}
239
240WriterObject
242{
243 return WriterObject(stringOutput(s));
244}
245
246} // namespace Json
Represents a JSON array being written to a Writer.
Definition: Object.h:246
Object appendObject()
Append a new Object and return it.
Definition: Object.cpp:101
Array appendArray()
Append a new Array and return it.
Definition: Object.cpp:110
void append(Scalar const &)
Append a scalar to the Arrary.
Definition: Object.h:397
void checkWritable(std::string const &label)
Definition: Object.cpp:65
Collection & operator=(Collection &&c) noexcept
Definition: Object.cpp:46
Collection()=delete
Collection * parent_
Definition: Object.h:168
Writer * writer_
Definition: Object.h:169
Proxy(Object &object, std::string const &key)
Definition: Object.cpp:120
Represents a JSON object being written to a Writer.
Definition: Object.h:179
Proxy operator[](std::string const &key)
Definition: Object.cpp:126
void set(std::string const &key, Scalar const &)
Set a scalar value in the Object for a key.
Definition: Object.h:406
friend class Array
Definition: Object.h:229
Array setArray(std::string const &key)
Make a new Array at a key and return it.
Definition: Object.cpp:90
Object setObject(std::string const &key)
Make a new Object at a key and return it.
Definition: Object.cpp:81
Lightweight wrapper to tag static string.
Definition: json_value.h:61
Represents a JSON value.
Definition: json_value.h:147
bool isObjectOrNull() const
Int asInt() const
Definition: json_value.cpp:503
UInt asUInt() const
Definition: json_value.cpp:545
Members getMemberNames() const
Return a list of the member names.
Definition: json_value.cpp:959
ValueType type() const
Definition: json_value.cpp:350
std::string asString() const
Returns the unquoted string value.
Definition: json_value.cpp:469
bool asBool() const
Definition: json_value.cpp:619
double asDouble() const
Definition: json_value.cpp:587
An Object that contains its own Writer.
Definition: Object.h:334
Writer implements an O(1)-space, O(1)-granular output JSON writer.
Definition: json/Writer.h:127
void finish()
Finish the collection most recently started.
Definition: Writer.cpp:350
void startRoot(CollectionType)
Start a new collection at the root level.
Definition: Writer.cpp:329
void startAppend(CollectionType)
Start a new collection inside an array.
Definition: Writer.cpp:335
void startSet(CollectionType, std::string const &key)
Start a new collection inside an object.
Definition: Writer.cpp:342
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:438
@ booleanValue
bool value
Definition: json_value.h:41
@ nullValue
'null' value
Definition: json_value.h:36
@ stringValue
UTF-8 string value.
Definition: json_value.h:40
@ realValue
double value
Definition: json_value.h:39
@ arrayValue
array value (ordered list)
Definition: json_value.h:42
@ intValue
signed integer value
Definition: json_value.h:37
@ objectValue
object value (collection of name/value pairs).
Definition: json_value.h:43
@ uintValue
unsigned integer value
Definition: json_value.h:38
WriterObject stringWriterObject(std::string &)
Definition: Object.cpp:241
Json::Value & appendObject(Json::Value &)
Append a new subobject to a Json object.
Definition: Object.h:450
void copyFrom(Json::Value &to, Json::Value const &from)
Copy all the keys and values from one object into another.
Definition: Object.cpp:226
void check(bool condition, std::string const &message)
Definition: json/Writer.h:252