/usr/include/Poco/JSON/Array.h is in libpoco-dev 1.8.0.1-1ubuntu4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 | //
// Array.h
//
// Library: JSON
// Package: JSON
// Module: Array
//
// Definition of the Array class.
//
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef JSON_Array_INCLUDED
#define JSON_Array_INCLUDED
#include "Poco/JSON/JSON.h"
#include "Poco/SharedPtr.h"
#include "Poco/Dynamic/Var.h"
#include <vector>
#include <sstream>
namespace Poco {
namespace JSON {
class Object;
class JSON_API Array
/// Represents a JSON array. Array provides a representation
/// based on shared pointers and optimized for performance. It is possible to
/// convert Array to Poco::Dynamic::Array. Conversion requires copying and therefore
/// has performance penalty; the benefit is in improved syntax, eg:
///
/// // use pointers to avoid copying
/// using namespace Poco::JSON;
/// std::string json = "[ {\"test\" : 0}, { \"test1\" : [1, 2, 3], \"test2\" : 4 } ]";
/// Parser parser;
/// Var result = parser.parse(json);
/// Array::Ptr arr = result.extract<Array::Ptr>();
/// Object::Ptr object = arr->getObject(0); // object == {\"test\" : 0}
/// int i = object->getElement<int>("test"); // i == 0;
/// Object::Ptr subObject = *arr->getObject(1); // subObject == {\"test\" : 0}
/// Array subArr::Ptr = subObject->getArray("test1"); // subArr == [1, 2, 3]
/// i = result = subArr->get(0); // i == 1;
///
/// // copy/convert to Poco::Dynamic::Array
/// Poco::Dynamic::Array da = *arr;
/// i = da[0]["test"]; // i == 0
/// i = da[1]["test1"][1]; // i == 2
/// i = da[1]["test2"]; // i == 4
/// ----
{
public:
typedef std::vector<Dynamic::Var> ValueVec;
typedef std::vector<Dynamic::Var>::iterator Iterator;
typedef std::vector<Dynamic::Var>::const_iterator ConstIterator;
typedef SharedPtr<Array> Ptr;
Array();
/// Creates an empty Array.
Array(const Array& copy);
/// Creates an Array by copying another one.
#ifdef POCO_ENABLE_CPP11
Array(Array&& other);
/// Move constructor
Array& operator=(Array&& other);
/// Move assignment operator.
#endif // POCO_ENABLE_CPP11
Array& operator=(const Array& other);
/// Assignment operator.
virtual ~Array();
/// Destroys the Array.
ValueVec::const_iterator begin() const;
/// Returns the begin iterator for values.
ValueVec::const_iterator end() const;
/// Returns the end iterator for values.
Dynamic::Var get(unsigned int index) const;
/// Retrieves the element at the given index.
/// Will return an empty value when the element doesn't exist.
Array::Ptr getArray(unsigned int index) const;
/// Retrieves an array. When the element is not
/// an Array or doesn't exist, an empty SharedPtr is returned.
template<typename T>
T getElement(unsigned int index) const
/// Retrieves an element and tries to convert it to the
/// template type. The convert<T> method of
/// Dynamic is called which can also throw
/// exceptions for invalid values.
/// Note: This will not work for an array or an object.
{
Dynamic::Var value = get(index);
return value.convert<T>();
}
SharedPtr<Object> getObject(unsigned int index) const;
/// Retrieves an object. When the element is not
/// an object or doesn't exist, an empty SharedPtr is returned.
std::size_t size() const;
/// Returns the size of the array.
bool isArray(unsigned int index) const;
/// Returns true when the element is an array.
bool isArray(const Dynamic::Var& value) const;
/// Returns true when the element is an array.
bool isArray(ConstIterator& value) const;
/// Returns true when the element is an array.
bool isNull(unsigned int index) const;
/// Returns true when the element is null or
/// when the element doesn't exist.
bool isObject(unsigned int index) const;
/// Returns true when the element is an object.
bool isObject(const Dynamic::Var& value) const;
/// Returns true when the element is an object.
bool isObject(ConstIterator& value) const;
/// Returns true when the element is an object.
template<typename T>
T optElement(unsigned int index, const T& def) const
/// Returns the element at the given index. When
/// the element is null, doesn't exist or can't
/// be converted to the given type, the default
/// value will be returned
{
T value = def;
if (index < _values.size())
{
try
{
value = _values[index].convert<T>();
}
catch (...)
{
// Default value is returned.
}
}
return value;
}
void add(const Dynamic::Var& value);
/// Add the given value to the array
void set(unsigned int index, const Dynamic::Var& value);
/// Update the element on the given index to specified value
void stringify(std::ostream& out, unsigned int indent = 0, int step = -1) const;
/// Prints the array to out. When indent has zero value,
/// the array will be printed without newline breaks and spaces between elements.
void remove(unsigned int index);
/// Removes the element on the given index.
operator const Poco::Dynamic::Array& () const;
/// Conversion operator to Dynamic::Array.
static Poco::Dynamic::Array makeArray(const JSON::Array::Ptr& arr);
/// Utility function for creation of array.
void clear();
/// Clears the contents of the array.
private:
void resetDynArray() const;
typedef SharedPtr<Poco::Dynamic::Array> ArrayPtr;
ValueVec _values;
mutable ArrayPtr _pArray;
mutable bool _modified;
};
//
// inlines
//
inline Array::ValueVec::const_iterator Array::begin() const
{
return _values.begin();
}
inline Array::ValueVec::const_iterator Array::end() const
{
return _values.end();
}
inline std::size_t Array::size() const
{
return static_cast<std::size_t>(_values.size());
}
inline bool Array::isArray(unsigned int index) const
{
Dynamic::Var value = get(index);
return isArray(value);
}
inline bool Array::isArray(const Dynamic::Var& value) const
{
return value.type() == typeid(Array::Ptr);
}
inline bool Array::isArray(ConstIterator& it) const
{
return it!= end() && isArray(*it);
}
inline void Array::add(const Dynamic::Var& value)
{
_values.push_back(value);
_modified = true;
}
inline void Array::set(unsigned int index, const Dynamic::Var& value)
{
if (index >= _values.size()) _values.resize(index + 1);
_values[index] = value;
_modified = true;
}
inline void Array::remove(unsigned int index)
{
_values.erase(_values.begin() + index);
}
} } // namespace Poco::JSON
namespace Poco {
namespace Dynamic {
template <>
class VarHolderImpl<JSON::Array::Ptr>: public VarHolder
{
public:
VarHolderImpl(const JSON::Array::Ptr& val): _val(val)
{
}
~VarHolderImpl()
{
}
const std::type_info& type() const
{
return typeid(JSON::Array::Ptr);
}
void convert(Int8&) const
{
throw BadCastException();
}
void convert(Int16&) const
{
throw BadCastException();
}
void convert(Int32&) const
{
throw BadCastException();
}
void convert(Int64&) const
{
throw BadCastException();
}
void convert(UInt8&) const
{
throw BadCastException();
}
void convert(UInt16&) const
{
throw BadCastException();
}
void convert(UInt32&) const
{
throw BadCastException();
}
void convert(UInt64&) const
{
throw BadCastException();
}
void convert(bool& value) const
{
value = !_val.isNull() && _val->size() > 0;
}
void convert(float&) const
{
throw BadCastException();
}
void convert(double&) const
{
throw BadCastException();
}
void convert(char&) const
{
throw BadCastException();
}
void convert(std::string& s) const
{
std::ostringstream oss;
_val->stringify(oss, 2);
s = oss.str();
}
void convert(DateTime& /*val*/) const
{
throw BadCastException("Cannot convert Array to DateTime");
}
void convert(LocalDateTime& /*ldt*/) const
{
throw BadCastException("Cannot convert Array to LocalDateTime");
}
void convert(Timestamp& /*ts*/) const
{
throw BadCastException("Cannot convert Array to Timestamp");
}
VarHolder* clone(Placeholder<VarHolder>* pVarHolder = 0) const
{
return cloneHolder(pVarHolder, _val);
}
const JSON::Array::Ptr& value() const
{
return _val;
}
bool isInteger() const
{
return false;
}
bool isSigned() const
{
return false;
}
bool isNumeric() const
{
return false;
}
bool isString() const
{
return false;
}
private:
JSON::Array::Ptr _val;
};
template <>
class VarHolderImpl<JSON::Array>: public VarHolder
{
public:
VarHolderImpl(const JSON::Array& val): _val(val)
{
}
~VarHolderImpl()
{
}
const std::type_info& type() const
{
return typeid(JSON::Array);
}
void convert(Int8&) const
{
throw BadCastException();
}
void convert(Int16&) const
{
throw BadCastException();
}
void convert(Int32&) const
{
throw BadCastException();
}
void convert(Int64&) const
{
throw BadCastException();
}
void convert(UInt8&) const
{
throw BadCastException();
}
void convert(UInt16&) const
{
throw BadCastException();
}
void convert(UInt32&) const
{
throw BadCastException();
}
void convert(UInt64&) const
{
throw BadCastException();
}
void convert(bool& value) const
{
value = _val.size() > 0;
}
void convert(float&) const
{
throw BadCastException();
}
void convert(double&) const
{
throw BadCastException();
}
void convert(char&) const
{
throw BadCastException();
}
void convert(std::string& s) const
{
std::ostringstream oss;
_val.stringify(oss, 2);
s = oss.str();
}
void convert(DateTime& /*val*/) const
{
throw BadCastException("Cannot convert Array to DateTime");
}
void convert(LocalDateTime& /*ldt*/) const
{
throw BadCastException("Cannot convert Array to LocalDateTime");
}
void convert(Timestamp& /*ts*/) const
{
throw BadCastException("Cannot convert Array to Timestamp");
}
VarHolder* clone(Placeholder<VarHolder>* pVarHolder = 0) const
{
return cloneHolder(pVarHolder, _val);
}
const JSON::Array& value() const
{
return _val;
}
bool isInteger() const
{
return false;
}
bool isSigned() const
{
return false;
}
bool isNumeric() const
{
return false;
}
bool isString() const
{
return false;
}
private:
JSON::Array _val;
};
} } // namespace Poco::Dynamic
#endif // JSON_Array_INCLUDED
|