Skip to content

Commit 95c62c0

Browse files
committed
Copy JSON from SandBox to trunk
1 parent 5a63907 commit 95c62c0

File tree

121 files changed

+5085
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

121 files changed

+5085
-0
lines changed

JSON/Makefile

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#
2+
# Makefile
3+
#
4+
# $Id$
5+
#
6+
# Makefile for Poco JSON
7+
#
8+
9+
include $(POCO_BASE)/build/rules/global
10+
11+
INCLUDE += -I $(POCO_BASE)/JSON/include/Poco/JSON
12+
13+
objects = Array Object Parser Handler \
14+
Stringifier DefaultHandler Query JSONException \
15+
Template TemplateCache
16+
17+
target = PocoJSON
18+
target_version = $(LIBVERSION)
19+
target_libs = PocoFoundation
20+
21+
include $(POCO_BASE)/build/rules/lib

JSON/include/Poco/JSON/Array.h

Lines changed: 358 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,358 @@
1+
//
2+
// Array.h
3+
//
4+
// $Id$
5+
//
6+
// Library: JSON
7+
// Package: JSON
8+
// Module: Array
9+
//
10+
// Definition of the Array class.
11+
//
12+
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
13+
// and Contributors.
14+
//
15+
// Permission is hereby granted, free of charge, to any person or organization
16+
// obtaining a copy of the software and accompanying documentation covered by
17+
// this license (the "Software") to use, reproduce, display, distribute,
18+
// execute, and transmit the Software, and to prepare derivative works of the
19+
// Software, and to permit third-parties to whom the Software is furnished to
20+
// do so, all subject to the following:
21+
//
22+
// The copyright notices in the Software and this entire statement, including
23+
// the above license grant, this restriction and the following disclaimer,
24+
// must be included in all copies of the Software, in whole or in part, and
25+
// all derivative works of the Software, unless such copies or derivative
26+
// works are solely in the form of machine-executable object code generated by
27+
// a source language processor.
28+
//
29+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31+
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
32+
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
33+
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
34+
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
35+
// DEALINGS IN THE SOFTWARE.
36+
//
37+
38+
#ifndef JSON_Array_INCLUDED
39+
#define JSON_Array_INCLUDED
40+
41+
42+
#include <vector>
43+
#include <sstream>
44+
45+
#include "Poco/JSON/JSON.h"
46+
#include "Poco/SharedPtr.h"
47+
#include "Poco/DynamicAny.h"
48+
49+
namespace Poco
50+
{
51+
namespace JSON
52+
{
53+
54+
class Object;
55+
56+
57+
class JSON_API Array
58+
{
59+
public:
60+
61+
typedef std::vector<DynamicAny> ValueVector;
62+
63+
64+
typedef SharedPtr<Array> Ptr;
65+
66+
67+
Array();
68+
/// Default constructor
69+
70+
71+
Array(const Array& copy);
72+
/// Copy Constructor
73+
74+
75+
virtual ~Array();
76+
/// Destructor
77+
78+
79+
ValueVector::const_iterator begin() const;
80+
/// Returns iterator
81+
82+
83+
ValueVector::const_iterator end() const;
84+
/// Returns iterator
85+
86+
87+
DynamicAny get(unsigned int index) const;
88+
/// Retrieves an element. Will return an empty value
89+
/// when the element doesn't exist.
90+
91+
92+
Array::Ptr getArray(unsigned int index) const;
93+
/// Retrieves an array. When the element is not
94+
/// an array or doesn't exist, an empty SharedPtr is returned.
95+
96+
template<typename T>
97+
T getElement(unsigned int index) const
98+
/// Retrieves an element and tries to convert it to the
99+
/// template type. The convert<T> method of
100+
/// Dynamic is called which can also throw
101+
/// exceptions for invalid values.
102+
/// Note: This will not work for an array or an object.
103+
{
104+
DynamicAny value = get(index);
105+
return value.convert<T>();
106+
}
107+
108+
109+
SharedPtr<Object> getObject(unsigned int index) const;
110+
/// Retrieves an object. When the element is not
111+
/// an object or doesn't exist, an empty SharedPtr is returned.
112+
113+
unsigned int size() const;
114+
/// Returns the size of the array
115+
116+
117+
bool isArray(unsigned int index) const;
118+
/// Returns true when the element is an array
119+
120+
121+
bool isNull(unsigned int index) const;
122+
/// Returns true when the element is null or
123+
/// when the element doesn't exist.
124+
125+
126+
bool isObject(unsigned int index) const;
127+
/// Returns true when the element is an object
128+
129+
130+
template<typename T>
131+
T optElement(unsigned int index, const T& def) const
132+
/// Returns the element at the given index. When
133+
/// the element is null, doesn't exist or can't
134+
/// be converted to the given type, the default
135+
/// value will be returned
136+
{
137+
T value = def;
138+
if ( index < _values.size() )
139+
{
140+
try
141+
{
142+
value = _values[index].convert<T>();
143+
}
144+
catch(...)
145+
{
146+
// Default value is returned.
147+
}
148+
}
149+
return value;
150+
}
151+
152+
153+
void add(const DynamicAny& value)
154+
/// Add the given value to the array
155+
{
156+
_values.push_back(value);
157+
}
158+
159+
160+
void stringify(std::ostream& out, unsigned int indent) const;
161+
/// Prints the array to out. When indent is 0, the array
162+
/// will be printed on one line without indentation.
163+
164+
165+
void remove(unsigned int index);
166+
/// Removes the element on the given index.
167+
168+
private:
169+
170+
ValueVector _values;
171+
};
172+
173+
174+
inline Array::ValueVector::const_iterator Array::begin() const
175+
{
176+
return _values.begin();
177+
}
178+
179+
inline Array::ValueVector::const_iterator Array::end() const
180+
181+
{
182+
return _values.end();
183+
}
184+
185+
inline unsigned int Array::size() const
186+
{
187+
return _values.size();
188+
}
189+
190+
inline bool Array::isArray(unsigned int index) const
191+
{
192+
DynamicAny value = get(index);
193+
return value.type() == typeid(Array::Ptr);
194+
}
195+
196+
inline bool Array::isNull(unsigned int index) const
197+
{
198+
if ( index < _values.size() )
199+
{
200+
DynamicAny value = _values[index];
201+
return value.isEmpty();
202+
}
203+
return true;
204+
}
205+
206+
207+
inline void Array::remove(unsigned int index)
208+
{
209+
_values.erase(_values.begin() + index);
210+
}
211+
212+
}} // Namespace Poco::JSON
213+
214+
namespace Poco
215+
{
216+
217+
template <>
218+
class DynamicAnyHolderImpl<JSON::Array::Ptr>: public DynamicAnyHolder
219+
{
220+
public:
221+
DynamicAnyHolderImpl(const JSON::Array::Ptr& val): _val(val)
222+
{
223+
}
224+
225+
~DynamicAnyHolderImpl()
226+
{
227+
}
228+
229+
const std::type_info& type() const
230+
{
231+
return typeid(JSON::Array::Ptr);
232+
}
233+
234+
void convert(Int8&) const
235+
{
236+
throw BadCastException();
237+
}
238+
239+
void convert(Int16&) const
240+
{
241+
throw BadCastException();
242+
}
243+
244+
void convert(Int32&) const
245+
{
246+
throw BadCastException();
247+
}
248+
249+
void convert(Int64&) const
250+
{
251+
throw BadCastException();
252+
}
253+
254+
void convert(UInt8&) const
255+
{
256+
throw BadCastException();
257+
}
258+
259+
void convert(UInt16&) const
260+
{
261+
throw BadCastException();
262+
}
263+
264+
void convert(UInt32&) const
265+
{
266+
throw BadCastException();
267+
}
268+
269+
void convert(UInt64&) const
270+
{
271+
throw BadCastException();
272+
}
273+
274+
void convert(bool& value) const
275+
{
276+
value = !_val.isNull() && _val->size() > 0;
277+
}
278+
279+
void convert(float&) const
280+
{
281+
throw BadCastException();
282+
}
283+
284+
void convert(double&) const
285+
{
286+
throw BadCastException();
287+
}
288+
289+
void convert(char&) const
290+
{
291+
throw BadCastException();
292+
}
293+
294+
void convert(std::string& s) const
295+
{
296+
std::ostringstream oss;
297+
_val->stringify(oss, 2);
298+
s = oss.str();
299+
}
300+
301+
void convert(DateTime& val) const
302+
{
303+
throw BadCastException();
304+
}
305+
306+
void convert(LocalDateTime& ldt) const
307+
{
308+
throw BadCastException();
309+
}
310+
311+
void convert(Timestamp& ts) const
312+
{
313+
throw BadCastException();
314+
}
315+
316+
DynamicAnyHolder* clone() const
317+
{
318+
return new DynamicAnyHolderImpl(_val);
319+
}
320+
321+
const JSON::Array::Ptr& value() const
322+
{
323+
return _val;
324+
}
325+
326+
bool isArray() const
327+
{
328+
return false;
329+
}
330+
331+
bool isInteger() const
332+
{
333+
return false;
334+
}
335+
336+
bool isSigned() const
337+
{
338+
return false;
339+
}
340+
341+
bool isNumeric() const
342+
{
343+
return false;
344+
}
345+
346+
bool isString() const
347+
{
348+
return false;
349+
}
350+
351+
private:
352+
JSON::Array::Ptr _val;
353+
};
354+
355+
} // Namespace Poco
356+
357+
358+
#endif // JSON_Array_INCLUDED

0 commit comments

Comments
 (0)