rippled
Loading...
Searching...
No Matches
Object.cpp
1#include <xrpl/basics/contract.h>
2#include <xrpl/beast/utility/instrumentation.h>
3#include <xrpl/json/Object.h>
4#include <xrpl/json/Output.h>
5#include <xrpl/json/Writer.h>
6#include <xrpl/json/json_value.h>
7
8#include <stdexcept>
9#include <utility>
10
11namespace Json {
12
14 : parent_(parent), writer_(writer), enabled_(true)
15{
16 checkWritable("Collection::Collection()");
17 if (parent_)
18 {
19 check(parent_->enabled_, "Parent not enabled in constructor");
20 parent_->enabled_ = false;
21 }
22}
23
25{
26 if (writer_)
27 writer_->finish();
28 if (parent_)
29 parent_->enabled_ = true;
30}
31
34{
35 parent_ = that.parent_;
36 writer_ = that.writer_;
37 enabled_ = that.enabled_;
38
39 that.parent_ = nullptr;
40 that.writer_ = nullptr;
41 that.enabled_ = false;
42
43 return *this;
44}
45
47{
48 *this = std::move(that);
49}
50
51void
53{
54 if (!enabled_)
55 ripple::Throw<std::logic_error>(label + ": not enabled");
56 if (!writer_)
57 ripple::Throw<std::logic_error>(label + ": not writable");
58}
59
60//------------------------------------------------------------------------------
61
66
69{
70 checkWritable("Object::setObject");
71 if (writer_)
73 return Object(this, writer_);
74}
75
78{
79 checkWritable("Object::setArray");
80 if (writer_)
82 return Array(this, writer_);
83}
84
85//------------------------------------------------------------------------------
86
89{
90 checkWritable("Array::appendObject");
91 if (writer_)
93 return Object(this, writer_);
94}
95
98{
99 checkWritable("Array::makeArray");
100 if (writer_)
102 return Array(this, writer_);
103}
104
105//------------------------------------------------------------------------------
106
108 : object_(object), key_(key)
109{
110}
111
114{
115 return Proxy(*this, key);
116}
117
120{
121 return Proxy(*this, std::string(key));
122}
123
124//------------------------------------------------------------------------------
125
126void
128{
129 auto t = v.type();
130 switch (t)
131 {
132 case Json::nullValue:
133 return append(nullptr);
134 case Json::intValue:
135 return append(v.asInt());
136 case Json::uintValue:
137 return append(v.asUInt());
138 case Json::realValue:
139 return append(v.asDouble());
141 return append(v.asString());
143 return append(v.asBool());
144
145 case Json::objectValue: {
146 auto object = appendObject();
147 copyFrom(object, v);
148 return;
149 }
150
151 case Json::arrayValue: {
152 auto array = appendArray();
153 for (auto& item : v)
154 array.append(item);
155 return;
156 }
157 }
158 UNREACHABLE("Json::Array::append : invalid type"); // LCOV_EXCL_LINE
159}
160
161void
163{
164 auto t = v.type();
165 switch (t)
166 {
167 case Json::nullValue:
168 return set(k, nullptr);
169 case Json::intValue:
170 return set(k, v.asInt());
171 case Json::uintValue:
172 return set(k, v.asUInt());
173 case Json::realValue:
174 return set(k, v.asDouble());
176 return set(k, v.asString());
178 return set(k, v.asBool());
179
180 case Json::objectValue: {
181 auto object = setObject(k);
182 copyFrom(object, v);
183 return;
184 }
185
186 case Json::arrayValue: {
187 auto array = setArray(k);
188 for (auto& item : v)
189 array.append(item);
190 return;
191 }
192 }
193 UNREACHABLE("Json::Object::set : invalid type"); // LCOV_EXCL_LINE
194}
195
196//------------------------------------------------------------------------------
197
198namespace {
199
200template <class Object>
201void
202doCopyFrom(Object& to, Json::Value const& from)
203{
204 XRPL_ASSERT(from.isObjectOrNull(), "Json::doCopyFrom : valid input type");
205 auto members = from.getMemberNames();
206 for (auto& m : members)
207 to[m] = from[m];
208}
209
210} // namespace
211
212void
214{
215 if (!to) // Short circuit this very common case.
216 to = from;
217 else
218 doCopyFrom(to, from);
219}
220
221void
222copyFrom(Object& to, Json::Value const& from)
223{
224 doCopyFrom(to, from);
225}
226
227WriterObject
232
233} // namespace Json
Represents a JSON array being written to a Writer.
Definition Object.h:228
Object appendObject()
Append a new Object and return it.
Definition Object.cpp:88
Array appendArray()
Append a new Array and return it.
Definition Object.cpp:97
void append(Scalar const &)
Append a scalar to the Arrary.
Definition Object.h:379
void checkWritable(std::string const &label)
Definition Object.cpp:52
Collection & operator=(Collection &&c) noexcept
Definition Object.cpp:33
Collection()=delete
Collection * parent_
Definition Object.h:150
Writer * writer_
Definition Object.h:151
Proxy(Object &object, std::string const &key)
Definition Object.cpp:107
Represents a JSON object being written to a Writer.
Definition Object.h:161
Proxy operator[](std::string const &key)
Definition Object.cpp:113
void set(std::string const &key, Scalar const &)
Set a scalar value in the Object for a key.
Definition Object.h:388
friend class Array
Definition Object.h:211
Array setArray(std::string const &key)
Make a new Array at a key and return it.
Definition Object.cpp:77
Object setObject(std::string const &key)
Make a new Object at a key and return it.
Definition Object.cpp:68
Lightweight wrapper to tag static string.
Definition json_value.h:44
Represents a JSON value.
Definition json_value.h:130
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:316
Writer implements an O(1)-space, O(1)-granular output JSON writer.
void finish()
Finish the collection most recently started.
void startRoot(CollectionType)
Start a new collection at the root level.
void startAppend(CollectionType)
Start a new collection inside an array.
void startSet(CollectionType, std::string const &key)
Start a new collection inside an object.
JSON (JavaScript Object Notation).
Definition json_errors.h:6
Output stringOutput(std::string &s)
Json::Value & appendArray(Json::Value &)
Append a new subarray to a Json array.
Definition Object.h:420
@ booleanValue
bool value
Definition json_value.h:24
@ nullValue
'null' value
Definition json_value.h:19
@ stringValue
UTF-8 string value.
Definition json_value.h:23
@ realValue
double value
Definition json_value.h:22
@ arrayValue
array value (ordered list)
Definition json_value.h:25
@ intValue
signed integer value
Definition json_value.h:20
@ objectValue
object value (collection of name/value pairs).
Definition json_value.h:26
@ uintValue
unsigned integer value
Definition json_value.h:21
WriterObject stringWriterObject(std::string &)
Definition Object.cpp:228
Json::Value & appendObject(Json::Value &)
Append a new subobject to a Json object.
Definition Object.h:432
void copyFrom(Json::Value &to, Json::Value const &from)
Copy all the keys and values from one object into another.
Definition Object.cpp:213
void check(bool condition, std::string const &message)