/usr/include/Wt/Json/Value is in libwt-dev 3.3.4+dfsg-6ubuntu1.
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 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 | // This may look like C code, but it's really -*- C++ -*-
/*
* Copyright (C) 2011 Emweb bvba, Kessel-Lo, Belgium.
*
* See the LICENSE file for terms of use.
*/
#ifndef WT_JSON_VALUE_H_
#define WT_JSON_VALUE_H_
#include <Wt/WException>
#include <Wt/WString>
#include <boost/any.hpp>
namespace Wt {
/*! \brief Namespace for the \ref json
*/
namespace Json {
class Object;
class Array;
/*! \defgroup json JSON Library (Wt::Json)
* \brief A JSON representation and parsing library.
*
* The JSON library contains data types to represent a JSON data
* structure (Value, Object and Array), and a JSON parser.
*
* Usage example:
* \code
* Json::Object result;
* Json::parse("{ "
* " \"a\": \"That's great\", "
* " \"b\": true "
* "}",
* result);
*
* std::cerr << "Size: " << result.size(); << std::endl; // Size: 2
* WString s = result.get("a");
* bool b = result.get("b");
* std::cerr << "a: " << s << ", b: " << b << std::endl; // a: That's great, b: true
* \endcode
*/
/*! \brief Enumeration for the type of a JSON value.
*
* \sa Value::type()
*
* \ingroup json
*/
enum Type {
NullType, //!< "null" or missing value
StringType, //!< a (unicode) string
BoolType, //!< "true" or "false"
NumberType, //!< a number (integer or floating point)
ObjectType, //!< an Object
ArrayType, //!< an Array
};
/*! \class TypeException Wt/Json/Value Wt/Json/Value
* \brief Exception that indicates a type error.
*
* This exception is thrown when a Value is being casted to an
* incompatible C++ type.
*
* \note To avoid exceptions, coerce the type first, and handle Null values.
*
* \ingroup json
*/
class TypeException : public WException
{
public:
TypeException(const std::string& name, Type actualType, Type expectedType);
TypeException(Type actualType, Type expectedType);
virtual ~TypeException() throw();
/*! \brief Returns the object field name (if known) */
const std::string& name() const { return name_; }
/*! \brief Returns the value type */
Type actualType() const { return actualType_; }
/*! \brief Returns the expected value type */
Type expectedType() const { return expectedType_; }
private:
std::string name_;
Type actualType_, expectedType_;
};
/*! \brief A JSON value
*
* This class represents a JSON value, which may be:
* - a simple type (boolean, number, string)
* - a composed type (Array or Object)
* - #Null: this represents values which are represented as "null" in
* JSON notations, but also the values returned for missing members in
* an Object, or values which are the result of a failed type coercion.
*
* \ingroup json
*/
class WT_API Value
{
public:
/*! \brief Default construtor.
*
* This creates a #Null value.
*/
Value();
/*! \brief Creates a value from a string.
*
* This creates a \link Wt::Json::StringType
* Json::StringType\endlink value.
*/
Value(const WT_USTRING& value);
/*! \brief Creates a value from a boolean.
*
* This creates a \link Wt::Json::BoolType
* Json::BoolType\endlink value.
*/
Value(bool value);
/*! \brief Creates a value from an integer.
*
* This creates a \link Wt::Json::NumberType
* Json::NumberType\endlink value.
*/
Value(int value);
/*! \brief Creates a value from a long.
*
* This creates a \link Wt::Json::NumberType
* Json::NumberType\endlink value.
*/
Value(long long value);
/*! \brief Creates a value from a double.
*
* This creates a \link Wt::Json::NumberType
* Json::NumberType\endlink value.
*/
Value(double value);
/*! \brief Creates a value with a given type.
*
* This creates a value of the given type, using a default constructed value
* of that type:
* - \link Wt::Json::BoolType Json::BoolType\endlink: false
* - \link Wt::Json::NumberType Json::NumberType\endlink: 0
* - \link Wt::Json::StringType Json::StringType\endlink: ""
* - \link Wt::Json::ArrayType Json::ArrayType\endlink: an empty array
* - \link Wt::Json::ObjectType Json::ObjectType\endlink: an empty object
*/
Value(Type type);
/*! \brief Copy constructor.
*/
Value(const Value& other);
/*! \brief Assignment operator.
*
* As a result of an assignment, both value and type are set to the value and
* type of the \p other value.
*/
Value& operator= (const Value& other);
/*! \brief Comparison operator.
*
* Returns whether two values have the same type and value.
*/
bool operator== (const Value& other) const;
/*! \brief Comparison operator.
*
* Returns whether two values have a different type or value.
*/
bool operator!= (const Value& other) const;
/*! \brief Returns the type.
*
* Returns the type of this value.
*/
Type type() const;
/*! \brief Returns whether the value is #Null.
*
* This returns \c true when the type is \link Wt::Json::NullType
* Json::NullType\endlink.
*/
bool isNull() const { return type() == NullType; }
/*! \brief Returns whether the value is compatible with a given C++ type.
*
* This returns whether the value type can be contained in the given
* C++ type, i.e. when a casting operation will not fail throwing a
* TypeException.
*
* \sa typeOf()
*/
bool hasType(const std::type_info& type) const;
/*! \brief Returns the JSON type that corresponds to a C++ type.
*
* This is a utility method for converting between C++ types and
* JSON types.
*/
static Type typeOf(const std::type_info& type);
/*! \brief Extracts the string value.
*
* This returns the value of a \link Wt::Json::StringType
* string\endlink JSON value.
*
* For example:
* \code
* const Json::Object& person = ...;
* try {
* const WString& occupation = person.get("occupation");
* ...
* } catch (const std::exception& e) {
* ...
* }
* \endcode
*
* To coerce a value of another type to a string use toString()
* first. To provide a fallback in case the value is null or could
* not be coerced to a string, use orIfNull().
*
* For example, the following code does not throw exceptions:
* \code
* const Json::Object& person = ...;
* const WString& occupation = person.get("occupation").toString().orIfNull(WString("manager"));
* \endcode
*
* \throws TypeException if the value type is not \link Wt::Json::StringType
* Json::StringType\endlink
*/
operator const WT_USTRING&() const;
/*! \brief Extracts the string value (UTF-8 encoded).
*
* This returns the value of a \link Wt::Json::StringType
* string\endlink JSON value.
*
* For example:
* \code
* const Json::Object& person = ...;
* try {
* std::string occupation = person.get("occupation");
* ...
* } catch (const std::exception& e) {
* ...
* }
* \endcode
*
* To coerce a value of another type to a string use toString()
* first. To provide a fallback in case the value is null or could
* not be coerced to a string, use orIfNull().
*
* For example, the following code does not throw exceptions:
* \code
* const Json::Object& person = ...;
* const std::string occupation = person.get("occupation").toString().orIfNull("manager");
* \endcode
*
* \throws TypeException if the value type is not \link Wt::Json::StringType
* Json::StringType\endlink
*/
operator std::string() const;
/*! \brief Extracts the boolean value.
*
* This returns the value of a \link Wt::Json::BoolType
* boolean \endlink JSON value.
*
* For example:
* \code
* const Json::Object& person = ...;
* try {
* bool happy = person.get("happy");
* ...
* } catch (const std::exception& e) {
* ...
* }
* \endcode
*
* To coerce a value of another type to a boolean use toBool()
* first. To provide a fallback in case the value is null or could
* not be coerced to a boolean, use orIfNull().
*
* For example, the following code does not throw exceptions:
* \code
* const Json::Object& person = ...;
* bool happy = person.get("happy").toBool().orIfNull(false);
* \endcode
*
* \throws TypeException if the value type is not \link Wt::Json::BoolType
* Json::BoolType\endlink
*/
operator bool() const;
/*! \brief Extracts the integer number value.
*
* This returns the value of a \link Wt::Json::NumberType
* number \endlink JSON value.
*
* For example:
* \code
* const Json::Object& person = ...;
* try {
* int cost = person.get("cost");
* ...
* } catch (const std::exception& e) {
* ...
* }
* \endcode
*
* To coerce a value of another type to a number use toNumber()
* first. To provide a fallback in case the value is null or could
* not be coerced to a number, use orIfNull().
*
* For example, the following code does not throw exceptions:
* \code
* const Json::Object& person = ...;
* int cost = person.get("cost").toNumber().orIfNull(0);
* \endcode
*
* \throws TypeException if the value type is not \link Wt::Json::NumberType
* Json::NumberType\endlink
*/
operator int() const;
/*! \brief Extracts the integer number value.
*
* This returns the value of a \link Wt::Json::NumberType
* number \endlink JSON value.
*
* For example:
* \code
* const Json::Object& person = ...;
* try {
* long long cost = person.get("cost");
* ...
* } catch (const std::exception& e) {
* ...
* }
* \endcode
*
* To coerce a value of another type to a number use toNumber()
* first. To provide a fallback in case the value is null or could
* not be coerced to a number, use orIfNull().
*
* For example, the following code does not throw exceptions:
* \code
* const Json::Object& person = ...;
* long long cost = person.get("cost").toNumber().orIfNull(0LL);
* \endcode
*
* \throws TypeException if the value type is not \link Wt::Json::NumberType
* Json::NumberType\endlink
*/
operator long long() const;
/*! \brief Extracts the floating point number value.
*
* This returns the value of a \link Wt::Json::NumberType
* number \endlink JSON value.
*
* For example:
* \code
* const Json::Object& person = ...;
* try {
* double cost = person.get("cost");
* ...
* } catch (const std::exception& e) {
* ...
* }
* \endcode
*
* To coerce a value of another type to a number use toNumber()
* first. To provide a fallback in case the value is null or could
* not be coerced to a number, use orIfNull().
*
* For example, the following code does not throw exceptions:
* \code
* const Json::Object& person = ...;
* double cost = person.get("cost").toNumber().orIfNull(0.0);
* \endcode
*
* \throws TypeException if the value type is not \link Wt::Json::NumberType
* Json::NumberType\endlink
*/
operator double() const;
/*! \brief Extracts the array value.
*
* This returns the value of a \link Wt::Json::ArrayType
* array \endlink JSON value.
*
* For example:
* \code
* const Json::Object& person = ...;
* try {
* const Array& children = person.get("children");
* ...
* } catch (const std::exception& e) {
* ...
* }
* \endcode
*
* To provide a fallback in case the value is null, use orIfNull().
*
* \throws TypeException if the value type is not \link Wt::Json::ArrayType
* Json::ArrayType\endlink
*/
operator const Array&() const;
/*! \brief Extracts the object value.
*
* This returns the value of a \link Wt::Json::ObjectType
* object \endlink JSON value.
*
* For example:
* \code
* const Json::Object& person = ...;
* try {
* const Object& employer = person.get("employer");
* ...
* } catch (const std::exception& e) {
* ...
* }
* \endcode
*
* To provide a fallback in case the value is null, use orIfNull().
*
* \throws TypeException if the value type is not \link Wt::Json::ObjectType
* Json::ObjectType\endlink
*/
operator const Object&() const;
/*! \brief Accesses the array value.
*
* This returns the value of a \link Wt::Json::ArrayType
* array \endlink JSON value.
*
* Use this method to modify the contained array in-place.
*
* For example:
* \code
* Json::Object person;
* person["children"] = Json::Value(Json::ArrayType);
* Json::Array& children = person.get("children");
* // add children ...
* \endcode
*
* \throws TypeException if the value type is not \link Wt::Json::ArrayType
* Json::ArrayType\endlink
*/
operator Array&();
/*! \brief Accesses the object value.
*
* This returns the value of a \link Wt::Json::ObjectType
* object \endlink JSON value.
*
* Use this method to modify the contained object in-place.
*
* For example:
* \code
* Json::Array& children = ...;
* for (unsigned i = 0; i < 3; ++i) {
* children.push_back(Json::Value(Json::ObjectType));
* Json::Object& child = children.back();
* ...
* }
* \endcode
*
* \throws TypeException if the value type is not \link Wt::Json::ObjectType
* Json::ObjectType\endlink
*/
operator Object&();
/*! \brief Extracts the string value, using a fallback when null.
*
* This is similar to the string cast operator, but this method
* returns a fallback when the value is null instead of throwing an
* exception.
*
* \throws TypeException if the value is not null and has a type
* other than \link Wt::Json::StringType
* Json::StringType\endlink
*/
const WT_USTRING& orIfNull(const WT_USTRING& v) const;
/*! \brief Extracts the UTF-8 encoded string value, using a fallback when null.
*
* This is similar to the string cast operator, but this method
* returns a fallback when the value is null instead of throwing an
* exception.
*
* \throws TypeException if the value is not null and has a type
* other than \link Wt::Json::StringType
* Json::StringType\endlink
*/
std::string orIfNull(const char *v) const;
#ifndef WT_TARGET_JAVA
/*! \brief Extracts the UTF-8 encoded string value, using a fallback when null.
*
* This is similar to the string cast operator, but this method
* returns a fallback when the value is null instead of throwing an
* exception.
*
* \throws TypeException if the value is not null and has a type
* other than \link Wt::Json::StringType
* Json::StringType\endlink
*/
std::string orIfNull(const std::string& v) const;
#endif
/*! \brief Extracts the boolean value, using a fallback when null.
*
* This is similar to the boolean cast operator, but this method
* returns a fallback when the value is null instead of throwing an
* exception.
*
* \throws TypeException if the value is not null and has a type
* other than \link Wt::Json::BoolType
* Json::BoolType\endlink
*/
bool orIfNull(bool v) const;
/*! \brief Extracts the number value, using a fallback when null.
*
* This is similar to the int cast operator, but this method returns
* a fallback when the value is null instead of throwing an
* exception.
*
* \throws TypeException if the value is not null and has a type
* other than \link Wt::Json::NumberType
* Json::NumberType\endlink
*/
int orIfNull(int v) const;
/*! \brief Extracts the number value, using a fallback when null.
*
* This is similar to the long long cast operator, but this method
* returns a fallback when the value is null instead of throwing an
* exception.
*
* \throws TypeException if the value is not null and has a type
* other than \link Wt::Json::NumberType
* Json::NumberType\endlink
*/
long long orIfNull(long long v) const;
/*! \brief Extracts the number value, using a fallback when null.
*
* This is similar to the double cast operator, but this method
* returns a fallback when the value is null instead of throwing an
* exception.
*
* \throws TypeException if the value is not null and has a type
* other than \link Wt::Json::NumberType
* Json::NumberType\endlink
*/
double orIfNull(double v) const;
/*! \brief Extracts the array value, using a fallback when null.
*
* This is similar to the Array cast operator, but this method
* returns a fallback when the value is null instead of throwing an
* exception.
*
* \throws TypeException if the value is not null and has a type
* other than \link Wt::Json::ArrayType
* Json::ArrayType\endlink
*/
const Array& orIfNull(const Array& v) const;
/*! \brief Extracts the object value, using a fallback when null.
*
* This is similar to the Object cast operator, but this method
* returns a fallback when the value is null instead of throwing an
* exception.
*
* \throws TypeException if the value is not null and has a type
* other than \link Wt::Json::ObjectType
* Json::ObjectType\endlink
*/
const Object& orIfNull(const Object& v) const;
/*! \brief Converts the value to a string.
*
* The value is lexically casted to a string. For an object or array
* value, this coercion is not defined and #Null is returned.
*
* \throws WException if the Value is a number initialized to NaN
*
*/
Value toString() const;
/*! \brief Converts the value to a boolean.
*
* A string value of "true" or "false" is interpreted as a boolean.
* Otherwise, #Null is returned.
*/
Value toBool() const;
/*! \brief Converts the value to a number.
*
* A string value is lexically casted to a number. If this fails, or
* for a boolean, array or object type, #Null is returned.
*/
Value toNumber() const;
/*! \brief Null constant.
*
* A constant value with type \link Wt::Json::NullType
* Json::NullType\endlink, i.e. as constructed by
* <tt>Json::Value()</tt>.
*/
static const Value Null;
/*! \brief True constant.
*
* A constant value of type \link Wt::Json::BoolType
* Json::BoolType\endlink with value \c true, i.e. as
* constructed by <tt>Json::Value(true)</tt>.
*/
static const Value True;
/*! \brief False constant.
*
* A constant value of type \link Wt::Json::BoolType
* Json::BoolType\endlink with value \c false, i.e. as
* constructed by <tt>Json::Value(false)</tt>
*/
static const Value False;
private:
boost::any v_;
template <typename T> T get(Type type) const;
template <typename T> const T& getCR(Type type) const;
template <typename T> T& getR(Type type);
};
}
}
#endif // WT_JSON_OBJECT_H_
|