forked from pocoproject/poco
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArray.cpp
212 lines (162 loc) · 3.34 KB
/
Array.cpp
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
//
// Array.cpp
//
// $Id$
//
// Library: JSON
// Package: JSON
// Module: Array
//
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "Poco/JSON/Array.h"
#include "Poco/JSON/Object.h"
#include "Poco/JSON/Stringifier.h"
using Poco::Dynamic::Var;
namespace Poco {
namespace JSON {
Array::Array()
{
}
Array::Array(const Array& copy) : _values(copy._values)
{
}
Array::~Array()
{
}
Var Array::get(unsigned int index) const
{
Var value;
try
{
value = _values.at(index);
}
catch(std::out_of_range&)
{
//Ignore, we return an empty value
}
return value;
}
Array::Ptr Array::getArray(unsigned int index) const
{
Array::Ptr result;
Var value = get(index);
if ( value.type() == typeid(Array::Ptr) )
{
result = value.extract<Array::Ptr>();
}
return result;
}
Object::Ptr Array::getObject(unsigned int index) const
{
Object::Ptr result;
Var value = get(index);
if ( value.type() == typeid(Object::Ptr) )
{
result = value.extract<Object::Ptr>();
}
return result;
}
bool Array::isNull(unsigned int index) const
{
if ( index < _values.size() )
{
Dynamic::Var value = _values[index];
return value.isEmpty();
}
return true;
}
bool Array::isObject(unsigned int index) const
{
Var value = get(index);
return isObject(value);
}
bool Array::isObject(const Dynamic::Var& value) const
{
return value.type() == typeid(Object::Ptr);
}
bool Array::isObject(ConstIterator& it) const
{
return it!= end() && isObject(*it);
}
void Array::stringify(std::ostream& out, unsigned int indent, int step) const
{
if (step == -1) step = indent;
out << "[";
if (indent > 0) out << std::endl;
for (ValueVec::const_iterator it = _values.begin(); it != _values.end();)
{
for(int i = 0; i < indent; i++) out << ' ';
Stringifier::stringify(*it, out, indent + step, step);
if ( ++it != _values.end() )
{
out << ",";
if (step > 0) out << '\n';
}
}
if (step > 0) out << '\n';
if (indent >= step) indent -= step;
for (int i = 0; i < indent; i++)
out << ' ';
out << "]";
}
Array::operator const Poco::Dynamic::Array& () const
{
if (!_pArray)
{
ValueVec::const_iterator it = _values.begin();
ValueVec::const_iterator end = _values.end();
_pArray = new Poco::Dynamic::Array;
int index = 0;
for (; it != end; ++it, ++index)
{
if (isObject(it))
{
_pArray->insert(_pArray->end(), Poco::JSON::Object::makeStruct(getObject(index)));
}
else if (isArray(it))
{
_pArray->insert(_pArray->end(), makeArray(getArray(index)));
}
else
{
_pArray->insert(_pArray->end(), *it);
}
}
}
return *_pArray;
}
Poco::Dynamic::Array Array::makeArray(const JSON::Array::Ptr& arr)
{
Poco::Dynamic::Array vec;
JSON::Array::ConstIterator it = arr->begin();
JSON::Array::ConstIterator end = arr->end();
int index = 0;
for (; it != end; ++it, ++index)
{
if (arr->isObject(it))
{
Object::Ptr pObj = arr->getObject(index);
DynamicStruct str = Poco::JSON::Object::makeStruct(pObj);
vec.insert(vec.end(), str);
}
else if (arr->isArray(it))
{
Array::Ptr pArr = arr->getArray(index);
std::vector<Poco::Dynamic::Var> v = makeArray(pArr);
vec.insert(vec.end(), v);
}
else
vec.insert(vec.end(), *it);
}
return vec;
}
void Array::clear()
{
_values.clear();
_pArray = 0;
}
} } // namespace Poco::JSON