aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/poco/Util/src/JSONConfiguration.cpp
blob: 282ac8ce5e6895c3a42f8070186a59a553834169 (plain) (blame)
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
//
// JSONConfiguration.cpp
//
// Library: Util
// Package: JSON
// Module:  JSONConfiguration
//
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier:	BSL-1.0
//



#include "Poco/Util/JSONConfiguration.h"


#ifndef POCO_UTIL_NO_JSONCONFIGURATION


#include "Poco/FileStream.h"
#include "Poco/StringTokenizer.h"
#include "Poco/JSON/Parser.h"
#include "Poco/JSON/Query.h"
#include "Poco/RegularExpression.h"
#include "Poco/NumberParser.h"


namespace Poco {
namespace Util {


JSONConfiguration::JSONConfiguration() : _object(new JSON::Object())
{
}


JSONConfiguration::JSONConfiguration(const std::string& path)
{
	load(path);
}


JSONConfiguration::JSONConfiguration(std::istream& istr)
{
	load(istr);
}


JSONConfiguration::JSONConfiguration(const JSON::Object::Ptr& object) : _object(object)
{
}


JSONConfiguration::~JSONConfiguration()
{
}


void JSONConfiguration::load(const std::string& path)
{
	Poco::FileInputStream fis(path);
	load(fis);
}


void JSONConfiguration::load(std::istream& istr)
{
	JSON::Parser parser;
	parser.parse(istr);
	DynamicAny result = parser.result();
	if ( result.type() == typeid(JSON::Object::Ptr) )
	{
		_object = result.extract<JSON::Object::Ptr>();
	}
}


void JSONConfiguration::loadEmpty(const std::string& root)
{
	_object = new JSON::Object();
	JSON::Object::Ptr rootObject = new JSON::Object();
	_object->set(root, rootObject);
}


bool JSONConfiguration::getRaw(const std::string & key, std::string & value) const
{
	JSON::Query query(_object);
	Poco::DynamicAny result = query.find(key);
	if ( ! result.isEmpty() )
	{
		value = result.convert<std::string>();
		return true;
	}
	return false;
}


void JSONConfiguration::getIndexes(std::string& name, std::vector<int>& indexes)
{
	indexes.clear();

	RegularExpression::MatchVec matches;
	int firstOffset = -1;
	int offset = 0;
	RegularExpression regex("\\[([0-9]+)\\]");
	while(regex.match(name, offset, matches) > 0 )
	{
		if ( firstOffset == -1 )
		{
			firstOffset = static_cast<int>(matches[0].offset);
		}
		std::string num = name.substr(matches[1].offset, matches[1].length);
		indexes.push_back(NumberParser::parse(num));
		offset = static_cast<int>(matches[0].offset + matches[0].length);
	}

	if ( firstOffset != -1 )
	{
		name = name.substr(0, firstOffset);
	}
}


JSON::Object::Ptr JSONConfiguration::findStart(const std::string& key, std::string& lastPart)
{
	JSON::Object::Ptr currentObject = _object;

	StringTokenizer tokenizer(key, ".");
	lastPart = tokenizer[tokenizer.count() - 1];

	for(int i = 0; i < tokenizer.count() - 1; ++i)
	{
		std::vector<int> indexes;
		std::string name = tokenizer[i];
		getIndexes(name, indexes);

		DynamicAny result = currentObject->get(name);

		if ( result.isEmpty() ) // Not found
		{
			if ( indexes.empty() ) // We want an object, create it
			{
				JSON::Object::Ptr newObject = new JSON::Object();
				currentObject->set(name, newObject);
				currentObject = newObject;
			}
			else // We need an array
			{
				JSON::Array::Ptr newArray;
				JSON::Array::Ptr parentArray;
				JSON::Array::Ptr topArray;
				for(std::vector<int>::iterator it = indexes.begin(); it != indexes.end(); ++it)
				{
					newArray = new JSON::Array();
					if ( topArray.isNull() )
					{
						topArray = newArray;
					}

					if ( ! parentArray.isNull() )
					{
						parentArray->add(newArray);
					}

					for(int i = 0; i <= *it - 1; ++i)
					{
						Poco::DynamicAny nullValue;
						newArray->add(nullValue);
					}

					parentArray = newArray;
				}

				currentObject->set(name, topArray);
				currentObject = new JSON::Object();
				newArray->add(currentObject);
			}
		}
		else // We have a value
		{
			if ( indexes.empty() ) // We want an object
			{
				if ( result.type() == typeid(JSON::Object::Ptr) )
				{
					currentObject = result.extract<JSON::Object::Ptr>();
				}
				else
				{
					throw SyntaxException("Expected a JSON object");
				}
			}
			else
			{
				if ( result.type() == typeid(JSON::Array::Ptr) )
				{
					JSON::Array::Ptr arr = result.extract<JSON::Array::Ptr>();

					for(std::vector<int>::iterator it = indexes.begin(); it != indexes.end() - 1; ++it)
					{
						JSON::Array::Ptr currentArray = arr;
						arr = arr->getArray(*it);
						if ( arr.isNull() )
						{
							arr = new JSON::Array();
							currentArray->add(arr);
						}
					}

					result = arr->get(*indexes.rbegin());
					if ( result.isEmpty() ) // Index doesn't exist
					{
						JSON::Object::Ptr newObject = new JSON::Object();
						arr->add(newObject);
						currentObject = newObject;
					}
					else // Index is available
					{
						if ( result.type() == typeid(JSON::Object::Ptr) )
						{
							currentObject = result.extract<JSON::Object::Ptr>();
						}
						else
						{
							throw SyntaxException("Expected a JSON object");
						}
					}
				}
				else
				{
					throw SyntaxException("Expected a JSON array");
				}
			}
		}
	}
	return currentObject;
}


void JSONConfiguration::setValue(const std::string& key, const Poco::DynamicAny& value)
{

	std::string sValue;
	
	value.convert<std::string>(sValue);
	KeyValue kv(key, sValue);
	
	if (eventsEnabled())
	{
		propertyChanging(this, kv);
	}
	
	std::string lastPart;
	JSON::Object::Ptr parentObject = findStart(key, lastPart);

	std::vector<int> indexes;
	getIndexes(lastPart, indexes);

	if ( indexes.empty() ) // No Array
	{
		parentObject->set(lastPart, value);
	}
	else
	{
		DynamicAny result = parentObject->get(lastPart);
		if ( result.isEmpty() )
		{
			result = JSON::Array::Ptr(new JSON::Array());
			parentObject->set(lastPart, result);
		}
		else if ( result.type() != typeid(JSON::Array::Ptr) )
		{
			throw SyntaxException("Expected a JSON array");
		}

		JSON::Array::Ptr arr = result.extract<JSON::Array::Ptr>();
		for(std::vector<int>::iterator it = indexes.begin(); it != indexes.end() - 1; ++it)
		{
			JSON::Array::Ptr nextArray = arr->getArray(*it);
			if ( nextArray.isNull()  )
			{
				for(int i = static_cast<int>(arr->size()); i <= *it; ++i)
				{
					Poco::DynamicAny nullValue;
					arr->add(nullValue);
				}
				nextArray = new JSON::Array();
				arr->add(nextArray);
			}
			arr = nextArray;
		}
		arr->set(indexes.back(), value);
	}

	if (eventsEnabled())
	{
		propertyChanged(this, kv);
	}
}


void JSONConfiguration::setString(const std::string& key, const std::string& value)
{
	setValue(key, value);
}


void JSONConfiguration::setRaw(const std::string& key, const std::string& value)
{
	setValue(key, value);
}


void JSONConfiguration::setInt(const std::string& key, int value)
{
	setValue(key, value);
}


void JSONConfiguration::setBool(const std::string& key, bool value)
{
	setValue(key, value);
}


void JSONConfiguration::setDouble(const std::string& key, double value)
{
	setValue(key, value);
}


void JSONConfiguration::enumerate(const std::string& key, Keys& range) const
{
	JSON::Query query(_object);
	Poco::DynamicAny result = query.find(key);
	if ( result.type() == typeid(JSON::Object::Ptr) )
	{
		JSON::Object::Ptr object = result.extract<JSON::Object::Ptr>();
		object->getNames(range);
	}
}


void JSONConfiguration::save(std::ostream& ostr, unsigned int indent) const
{
	_object->stringify(ostr, indent);
}


void JSONConfiguration::removeRaw(const std::string& key)

{
	
	std::string lastPart;
	JSON::Object::Ptr parentObject = findStart(key, lastPart);
	std::vector<int> indexes;
	getIndexes(lastPart, indexes);

	if ( indexes.empty() ) // No Array
	{
		parentObject->remove(lastPart);
	}
	else
	{
		DynamicAny result = parentObject->get(lastPart);
		if (!result.isEmpty() && result.type() == typeid(JSON::Array::Ptr))
		{

			JSON::Array::Ptr arr = result.extract<JSON::Array::Ptr>();
			for(std::vector<int>::iterator it = indexes.begin(); it != indexes.end() - 1; ++it)
			{
				arr = arr->getArray(*it);
			}
			arr->remove(indexes.back());
		}
	}

}


} } // namespace Poco::Util


#endif // POCO_UTIL_NO_JSONCONFIGURATION