forked from pocoproject/poco
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObject.cpp
More file actions
261 lines (211 loc) · 4.91 KB
/
Object.cpp
File metadata and controls
261 lines (211 loc) · 4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
//
// Object.cpp
//
// Library: JSON
// Package: JSON
// Module: Object
//
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "Poco/JSON/Object.h"
#include <iostream>
#include <sstream>
using Poco::Dynamic::Var;
namespace Poco {
namespace JSON {
Object::Object(bool preserveInsOrder):
_preserveInsOrder(preserveInsOrder),
_modified(false)
{
}
Object::Object(const Object& copy) : _values(copy._values),
_preserveInsOrder(copy._preserveInsOrder),
_pStruct(!copy._modified ? copy._pStruct : 0),
_modified(copy._modified)
{
if (_preserveInsOrder)
{
// need to update pointers in _keys to point to copied _values
for (KeyPtrList::const_iterator it = copy._keys.begin(); it != copy._keys.end(); ++it)
{
ValueMap::const_iterator itv = _values.find(**it);
poco_assert (itv != _values.end());
_keys.push_back(&itv->first);
}
}
}
Object::Object(Object&& other) :
_values(std::move(other._values)),
_keys(std::move(other._keys)),
_preserveInsOrder(other._preserveInsOrder),
_pStruct(!other._modified ? other._pStruct : 0),
_modified(other._modified)
{
}
Object::~Object()
{
}
Object &Object::operator= (const Object &other)
{
if (&other != this)
{
_values = other._values;
_keys = other._keys;
_preserveInsOrder = other._preserveInsOrder;
_pStruct = !other._modified ? other._pStruct : 0;
_modified = other._modified;
}
return *this;
}
Object &Object::operator= (Object &&other)
{
if (&other != this)
{
_values = std::move(other._values);
_keys = std::move(other._keys);
_preserveInsOrder = other._preserveInsOrder;
_pStruct = !other._modified ? other._pStruct : 0;
_modified = other._modified;
}
return *this;
}
Var Object::get(const std::string& key) const
{
ValueMap::const_iterator it = _values.find(key);
if (it != _values.end())
{
return it->second;
}
return Var();
}
Array::Ptr Object::getArray(const std::string& key) const
{
ValueMap::const_iterator it = _values.find(key);
if ((it != _values.end()) && (it->second.type() == typeid(Array::Ptr)))
{
return it->second.extract<Array::Ptr>();
}
return 0;
}
Object::Ptr Object::getObject(const std::string& key) const
{
ValueMap::const_iterator it = _values.find(key);
if ((it != _values.end()) && (it->second.type() == typeid(Object::Ptr)))
{
return it->second.extract<Object::Ptr>();
}
return 0;
}
void Object::getNames(std::vector<std::string>& names) const
{
names.clear();
for (ValueMap::const_iterator it = _values.begin(); it != _values.end(); ++it)
{
names.push_back(it->first);
}
}
void Object::stringify(std::ostream& out, unsigned int indent, int step) const
{
if (step < 0) step = indent;
if (!_preserveInsOrder)
doStringify(_values, out, indent, step);
else
doStringify(_keys, out, indent, step);
}
const std::string& Object::getKey(KeyPtrList::const_iterator& iter) const
{
ValueMap::const_iterator it = _values.begin();
ValueMap::const_iterator end = _values.end();
for (; it != end; ++it)
{
if (it->first == **iter) return it->first;
}
throw NotFoundException(**iter);
}
void Object::set(const std::string& key, const Dynamic::Var& value)
{
std::pair<ValueMap::iterator, bool> ret = _values.insert(ValueMap::value_type(key, value));
if (!ret.second) ret.first->second = value;
if (_preserveInsOrder)
{
KeyPtrList::iterator it = _keys.begin();
KeyPtrList::iterator end = _keys.end();
for (; it != end; ++it)
{
if (key == **it) return;
}
_keys.push_back(&ret.first->first);
}
_modified = true;
}
Poco::DynamicStruct Object::makeStruct(const Object::Ptr& obj)
{
Poco::DynamicStruct ds;
ConstIterator it = obj->begin();
ConstIterator end = obj->end();
for (; it != end; ++it)
{
if (obj->isObject(it))
{
Object::Ptr pObj = obj->getObject(it->first);
DynamicStruct str = makeStruct(pObj);
ds.insert(it->first, str);
}
else if (obj->isArray(it))
{
Array::Ptr pArr = obj->getArray(it->first);
std::vector<Poco::Dynamic::Var> v = Poco::JSON::Array::makeArray(pArr);
ds.insert(it->first, v);
}
else
ds.insert(it->first, it->second);
}
return ds;
}
void Object::resetDynStruct() const
{
if (!_pStruct)
_pStruct = new Poco::DynamicStruct;
else
_pStruct->clear();
}
Object::operator const Poco::DynamicStruct& () const
{
if (!_values.size())
{
resetDynStruct();
}
else if (_modified)
{
ValueMap::const_iterator it = _values.begin();
ValueMap::const_iterator end = _values.end();
resetDynStruct();
for (; it != end; ++it)
{
if (isObject(it))
{
_pStruct->insert(it->first, makeStruct(getObject(it->first)));
}
else if (isArray(it))
{
_pStruct->insert(it->first, Poco::JSON::Array::makeArray(getArray(it->first)));
}
else
{
_pStruct->insert(it->first, it->second);
}
}
}
return *_pStruct;
}
void Object::clear()
{
_values.clear();
_keys.clear();
_pStruct = 0;
_modified = true;
}
} } // namespace Poco::JSON