annotate json/json.h @ 49:178642d134a7 tip

xtra files
author Robert Tubb <rt300@eecs.qmul.ac.uk>
date Wed, 01 May 2013 17:34:33 +0100
parents 58585d6ada62
children
rev   line source
rt300@9 1 /// Json-cpp amalgated header (http://jsoncpp.sourceforge.net/).
rt300@9 2 /// It is intented to be used with #include <json/json.h>
rt300@9 3
rt300@9 4 // //////////////////////////////////////////////////////////////////////
rt300@9 5 // Beginning of content of file: LICENSE
rt300@9 6 // //////////////////////////////////////////////////////////////////////
rt300@9 7
rt300@9 8 /*
rt300@9 9 The JsonCpp library's source code, including accompanying documentation,
rt300@9 10 tests and demonstration applications, are licensed under the following
rt300@9 11 conditions...
rt300@9 12
rt300@9 13 The author (Baptiste Lepilleur) explicitly disclaims copyright in all
rt300@9 14 jurisdictions which recognize such a disclaimer. In such jurisdictions,
rt300@9 15 this software is released into the Public Domain.
rt300@9 16
rt300@9 17 In jurisdictions which do not recognize Public Domain property (e.g. Germany as of
rt300@9 18 2010), this software is Copyright (c) 2007-2010 by Baptiste Lepilleur, and is
rt300@9 19 released under the terms of the MIT License (see below).
rt300@9 20
rt300@9 21 In jurisdictions which recognize Public Domain property, the user of this
rt300@9 22 software may choose to accept it either as 1) Public Domain, 2) under the
rt300@9 23 conditions of the MIT License (see below), or 3) under the terms of dual
rt300@9 24 Public Domain/MIT License conditions described here, as they choose.
rt300@9 25
rt300@9 26 The MIT License is about as close to Public Domain as a license can get, and is
rt300@9 27 described in clear, concise terms at:
rt300@9 28
rt300@9 29 http://en.wikipedia.org/wiki/MIT_License
rt300@9 30
rt300@9 31 The full text of the MIT License follows:
rt300@9 32
rt300@9 33 ========================================================================
rt300@9 34 Copyright (c) 2007-2010 Baptiste Lepilleur
rt300@9 35
rt300@9 36 Permission is hereby granted, free of charge, to any person
rt300@9 37 obtaining a copy of this software and associated documentation
rt300@9 38 files (the "Software"), to deal in the Software without
rt300@9 39 restriction, including without limitation the rights to use, copy,
rt300@9 40 modify, merge, publish, distribute, sublicense, and/or sell copies
rt300@9 41 of the Software, and to permit persons to whom the Software is
rt300@9 42 furnished to do so, subject to the following conditions:
rt300@9 43
rt300@9 44 The above copyright notice and this permission notice shall be
rt300@9 45 included in all copies or substantial portions of the Software.
rt300@9 46
rt300@9 47 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
rt300@9 48 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
rt300@9 49 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
rt300@9 50 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
rt300@9 51 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
rt300@9 52 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
rt300@9 53 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
rt300@9 54 SOFTWARE.
rt300@9 55 ========================================================================
rt300@9 56 (END LICENSE TEXT)
rt300@9 57
rt300@9 58 The MIT license is compatible with both the GPL and commercial
rt300@9 59 software, affording one all of the rights of Public Domain with the
rt300@9 60 minor nuisance of being required to keep the above copyright notice
rt300@9 61 and license text in the source code. Note also that by accepting the
rt300@9 62 Public Domain "license" you can re-license your copy using whatever
rt300@9 63 license you like.
rt300@9 64
rt300@9 65 */
rt300@9 66
rt300@9 67 // //////////////////////////////////////////////////////////////////////
rt300@9 68 // End of content of file: LICENSE
rt300@9 69 // //////////////////////////////////////////////////////////////////////
rt300@9 70
rt300@9 71
rt300@9 72
rt300@9 73
rt300@9 74
rt300@9 75 #ifndef JSON_AMALGATED_H_INCLUDED
rt300@9 76 # define JSON_AMALGATED_H_INCLUDED
rt300@9 77 /// If defined, indicates that the source file is amalgated
rt300@9 78 /// to prevent private header inclusion.
rt300@11 79 #define JSON_IS_AMALGAMATION
rt300@9 80
rt300@9 81 // //////////////////////////////////////////////////////////////////////
rt300@9 82 // Beginning of content of file: include/json/config.h
rt300@9 83 // //////////////////////////////////////////////////////////////////////
rt300@9 84
rt300@9 85 // Copyright 2007-2010 Baptiste Lepilleur
rt300@9 86 // Distributed under MIT license, or public domain if desired and
rt300@9 87 // recognized in your jurisdiction.
rt300@9 88 // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
rt300@9 89
rt300@9 90 #ifndef JSON_CONFIG_H_INCLUDED
rt300@9 91 # define JSON_CONFIG_H_INCLUDED
rt300@9 92
rt300@9 93 /// If defined, indicates that json library is embedded in CppTL library.
rt300@9 94 //# define JSON_IN_CPPTL 1
rt300@9 95
rt300@9 96 /// If defined, indicates that json may leverage CppTL library
rt300@9 97 //# define JSON_USE_CPPTL 1
rt300@9 98 /// If defined, indicates that cpptl vector based map should be used instead of std::map
rt300@9 99 /// as Value container.
rt300@9 100 //# define JSON_USE_CPPTL_SMALLMAP 1
rt300@9 101 /// If defined, indicates that Json specific container should be used
rt300@9 102 /// (hash table & simple deque container with customizable allocator).
rt300@9 103 /// THIS FEATURE IS STILL EXPERIMENTAL! There is know bugs: See #3177332
rt300@9 104 //# define JSON_VALUE_USE_INTERNAL_MAP 1
rt300@9 105 /// Force usage of standard new/malloc based allocator instead of memory pool based allocator.
rt300@9 106 /// The memory pools allocator used optimization (initializing Value and ValueInternalLink
rt300@9 107 /// as if it was a POD) that may cause some validation tool to report errors.
rt300@9 108 /// Only has effects if JSON_VALUE_USE_INTERNAL_MAP is defined.
rt300@9 109 //# define JSON_USE_SIMPLE_INTERNAL_ALLOCATOR 1
rt300@9 110
rt300@9 111 /// If defined, indicates that Json use exception to report invalid type manipulation
rt300@9 112 /// instead of C assert macro.
rt300@9 113 # define JSON_USE_EXCEPTION 1
rt300@9 114
rt300@9 115 /// If defined, indicates that the source file is amalgated
rt300@9 116 /// to prevent private header inclusion.
rt300@9 117 /// Remarks: it is automatically defined in the generated amalgated header.
rt300@9 118 // #define JSON_IS_AMALGAMATION
rt300@9 119
rt300@9 120
rt300@9 121 # ifdef JSON_IN_CPPTL
rt300@9 122 # include <cpptl/config.h>
rt300@9 123 # ifndef JSON_USE_CPPTL
rt300@9 124 # define JSON_USE_CPPTL 1
rt300@9 125 # endif
rt300@9 126 # endif
rt300@9 127
rt300@9 128 # ifdef JSON_IN_CPPTL
rt300@9 129 # define JSON_API CPPTL_API
rt300@9 130 # elif defined(JSON_DLL_BUILD)
rt300@9 131 # define JSON_API __declspec(dllexport)
rt300@9 132 # elif defined(JSON_DLL)
rt300@9 133 # define JSON_API __declspec(dllimport)
rt300@9 134 # else
rt300@9 135 # define JSON_API
rt300@9 136 # endif
rt300@9 137
rt300@9 138 // If JSON_NO_INT64 is defined, then Json only support C++ "int" type for integer
rt300@9 139 // Storages, and 64 bits integer support is disabled.
rt300@9 140 // #define JSON_NO_INT64 1
rt300@9 141
rt300@9 142 #if defined(_MSC_VER) && _MSC_VER <= 1200 // MSVC 6
rt300@9 143 // Microsoft Visual Studio 6 only support conversion from __int64 to double
rt300@9 144 // (no conversion from unsigned __int64).
rt300@9 145 #define JSON_USE_INT64_DOUBLE_CONVERSION 1
rt300@9 146 #endif // if defined(_MSC_VER) && _MSC_VER < 1200 // MSVC 6
rt300@9 147
rt300@9 148 #if defined(_MSC_VER) && _MSC_VER >= 1500 // MSVC 2008
rt300@9 149 /// Indicates that the following function is deprecated.
rt300@9 150 # define JSONCPP_DEPRECATED(message) __declspec(deprecated(message))
rt300@9 151 #endif
rt300@9 152
rt300@9 153 #if !defined(JSONCPP_DEPRECATED)
rt300@9 154 # define JSONCPP_DEPRECATED(message)
rt300@9 155 #endif // if !defined(JSONCPP_DEPRECATED)
rt300@9 156
rt300@9 157 namespace Json {
rt300@9 158 typedef int Int;
rt300@9 159 typedef unsigned int UInt;
rt300@9 160 # if defined(JSON_NO_INT64)
rt300@9 161 typedef int LargestInt;
rt300@9 162 typedef unsigned int LargestUInt;
rt300@9 163 # undef JSON_HAS_INT64
rt300@9 164 # else // if defined(JSON_NO_INT64)
rt300@9 165 // For Microsoft Visual use specific types as long long is not supported
rt300@9 166 # if defined(_MSC_VER) // Microsoft Visual Studio
rt300@9 167 typedef __int64 Int64;
rt300@9 168 typedef unsigned __int64 UInt64;
rt300@9 169 # else // if defined(_MSC_VER) // Other platforms, use long long
rt300@9 170 typedef long long int Int64;
rt300@9 171 typedef unsigned long long int UInt64;
rt300@9 172 # endif // if defined(_MSC_VER)
rt300@9 173 typedef Int64 LargestInt;
rt300@9 174 typedef UInt64 LargestUInt;
rt300@9 175 # define JSON_HAS_INT64
rt300@9 176 # endif // if defined(JSON_NO_INT64)
rt300@9 177 } // end namespace Json
rt300@9 178
rt300@9 179
rt300@9 180 #endif // JSON_CONFIG_H_INCLUDED
rt300@9 181
rt300@9 182 // //////////////////////////////////////////////////////////////////////
rt300@9 183 // End of content of file: include/json/config.h
rt300@9 184 // //////////////////////////////////////////////////////////////////////
rt300@9 185
rt300@9 186
rt300@9 187
rt300@9 188
rt300@9 189
rt300@9 190
rt300@9 191 // //////////////////////////////////////////////////////////////////////
rt300@9 192 // Beginning of content of file: include/json/forwards.h
rt300@9 193 // //////////////////////////////////////////////////////////////////////
rt300@9 194
rt300@9 195 // Copyright 2007-2010 Baptiste Lepilleur
rt300@9 196 // Distributed under MIT license, or public domain if desired and
rt300@9 197 // recognized in your jurisdiction.
rt300@9 198 // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
rt300@9 199
rt300@9 200 #ifndef JSON_FORWARDS_H_INCLUDED
rt300@9 201 # define JSON_FORWARDS_H_INCLUDED
rt300@9 202
rt300@9 203 #if !defined(JSON_IS_AMALGAMATION)
rt300@9 204 # include "config.h"
rt300@9 205 #endif // if !defined(JSON_IS_AMALGAMATION)
rt300@9 206
rt300@9 207 namespace Json {
rt300@9 208
rt300@9 209 // writer.h
rt300@9 210 class FastWriter;
rt300@9 211 class StyledWriter;
rt300@9 212
rt300@9 213 // reader.h
rt300@9 214 class Reader;
rt300@9 215
rt300@9 216 // features.h
rt300@9 217 class Features;
rt300@9 218
rt300@9 219 // value.h
rt300@9 220 typedef unsigned int ArrayIndex;
rt300@9 221 class StaticString;
rt300@9 222 class Path;
rt300@9 223 class PathArgument;
rt300@9 224 class Value;
rt300@9 225 class ValueIteratorBase;
rt300@9 226 class ValueIterator;
rt300@9 227 class ValueConstIterator;
rt300@9 228 #ifdef JSON_VALUE_USE_INTERNAL_MAP
rt300@9 229 class ValueMapAllocator;
rt300@9 230 class ValueInternalLink;
rt300@9 231 class ValueInternalArray;
rt300@9 232 class ValueInternalMap;
rt300@9 233 #endif // #ifdef JSON_VALUE_USE_INTERNAL_MAP
rt300@9 234
rt300@9 235 } // namespace Json
rt300@9 236
rt300@9 237
rt300@9 238 #endif // JSON_FORWARDS_H_INCLUDED
rt300@9 239
rt300@9 240 // //////////////////////////////////////////////////////////////////////
rt300@9 241 // End of content of file: include/json/forwards.h
rt300@9 242 // //////////////////////////////////////////////////////////////////////
rt300@9 243
rt300@9 244
rt300@9 245
rt300@9 246
rt300@9 247
rt300@9 248
rt300@9 249 // //////////////////////////////////////////////////////////////////////
rt300@9 250 // Beginning of content of file: include/json/features.h
rt300@9 251 // //////////////////////////////////////////////////////////////////////
rt300@9 252
rt300@9 253 // Copyright 2007-2010 Baptiste Lepilleur
rt300@9 254 // Distributed under MIT license, or public domain if desired and
rt300@9 255 // recognized in your jurisdiction.
rt300@9 256 // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
rt300@9 257
rt300@9 258 #ifndef CPPTL_JSON_FEATURES_H_INCLUDED
rt300@9 259 # define CPPTL_JSON_FEATURES_H_INCLUDED
rt300@9 260
rt300@9 261 #if !defined(JSON_IS_AMALGAMATION)
rt300@9 262 # include "forwards.h"
rt300@9 263 #endif // if !defined(JSON_IS_AMALGAMATION)
rt300@9 264
rt300@9 265 namespace Json {
rt300@9 266
rt300@9 267 /** \brief Configuration passed to reader and writer.
rt300@9 268 * This configuration object can be used to force the Reader or Writer
rt300@9 269 * to behave in a standard conforming way.
rt300@9 270 */
rt300@9 271 class JSON_API Features
rt300@9 272 {
rt300@9 273 public:
rt300@9 274 /** \brief A configuration that allows all features and assumes all strings are UTF-8.
rt300@9 275 * - C & C++ comments are allowed
rt300@9 276 * - Root object can be any JSON value
rt300@9 277 * - Assumes Value strings are encoded in UTF-8
rt300@9 278 */
rt300@9 279 static Features all();
rt300@9 280
rt300@9 281 /** \brief A configuration that is strictly compatible with the JSON specification.
rt300@9 282 * - Comments are forbidden.
rt300@9 283 * - Root object must be either an array or an object value.
rt300@9 284 * - Assumes Value strings are encoded in UTF-8
rt300@9 285 */
rt300@9 286 static Features strictMode();
rt300@9 287
rt300@9 288 /** \brief Initialize the configuration like JsonConfig::allFeatures;
rt300@9 289 */
rt300@9 290 Features();
rt300@9 291
rt300@9 292 /// \c true if comments are allowed. Default: \c true.
rt300@9 293 bool allowComments_;
rt300@9 294
rt300@9 295 /// \c true if root must be either an array or an object value. Default: \c false.
rt300@9 296 bool strictRoot_;
rt300@9 297 };
rt300@9 298
rt300@9 299 } // namespace Json
rt300@9 300
rt300@9 301 #endif // CPPTL_JSON_FEATURES_H_INCLUDED
rt300@9 302
rt300@9 303 // //////////////////////////////////////////////////////////////////////
rt300@9 304 // End of content of file: include/json/features.h
rt300@9 305 // //////////////////////////////////////////////////////////////////////
rt300@9 306
rt300@9 307
rt300@9 308
rt300@9 309
rt300@9 310
rt300@9 311
rt300@9 312 // //////////////////////////////////////////////////////////////////////
rt300@9 313 // Beginning of content of file: include/json/value.h
rt300@9 314 // //////////////////////////////////////////////////////////////////////
rt300@9 315
rt300@9 316 // Copyright 2007-2010 Baptiste Lepilleur
rt300@9 317 // Distributed under MIT license, or public domain if desired and
rt300@9 318 // recognized in your jurisdiction.
rt300@9 319 // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
rt300@9 320
rt300@9 321 #ifndef CPPTL_JSON_H_INCLUDED
rt300@9 322 # define CPPTL_JSON_H_INCLUDED
rt300@9 323
rt300@9 324 #if !defined(JSON_IS_AMALGAMATION)
rt300@9 325 # include "forwards.h"
rt300@9 326 #endif // if !defined(JSON_IS_AMALGAMATION)
rt300@9 327 # include <string>
rt300@9 328 # include <vector>
rt300@9 329
rt300@9 330 # ifndef JSON_USE_CPPTL_SMALLMAP
rt300@9 331 # include <map>
rt300@9 332 # else
rt300@9 333 # include <cpptl/smallmap.h>
rt300@9 334 # endif
rt300@9 335 # ifdef JSON_USE_CPPTL
rt300@9 336 # include <cpptl/forwards.h>
rt300@9 337 # endif
rt300@9 338
rt300@9 339 /** \brief JSON (JavaScript Object Notation).
rt300@9 340 */
rt300@9 341 namespace Json {
rt300@9 342
rt300@9 343 /** \brief Type of the value held by a Value object.
rt300@9 344 */
rt300@9 345 enum ValueType
rt300@9 346 {
rt300@9 347 nullValue = 0, ///< 'null' value
rt300@9 348 intValue, ///< signed integer value
rt300@9 349 uintValue, ///< unsigned integer value
rt300@9 350 realValue, ///< double value
rt300@9 351 stringValue, ///< UTF-8 string value
rt300@9 352 booleanValue, ///< bool value
rt300@9 353 arrayValue, ///< array value (ordered list)
rt300@9 354 objectValue ///< object value (collection of name/value pairs).
rt300@9 355 };
rt300@9 356
rt300@9 357 enum CommentPlacement
rt300@9 358 {
rt300@9 359 commentBefore = 0, ///< a comment placed on the line before a value
rt300@9 360 commentAfterOnSameLine, ///< a comment just after a value on the same line
rt300@9 361 commentAfter, ///< a comment on the line after a value (only make sense for root value)
rt300@9 362 numberOfCommentPlacement
rt300@9 363 };
rt300@9 364
rt300@9 365 //# ifdef JSON_USE_CPPTL
rt300@9 366 // typedef CppTL::AnyEnumerator<const char *> EnumMemberNames;
rt300@9 367 // typedef CppTL::AnyEnumerator<const Value &> EnumValues;
rt300@9 368 //# endif
rt300@9 369
rt300@9 370 /** \brief Lightweight wrapper to tag static string.
rt300@9 371 *
rt300@9 372 * Value constructor and objectValue member assignement takes advantage of the
rt300@9 373 * StaticString and avoid the cost of string duplication when storing the
rt300@9 374 * string or the member name.
rt300@9 375 *
rt300@9 376 * Example of usage:
rt300@9 377 * \code
rt300@9 378 * Json::Value aValue( StaticString("some text") );
rt300@9 379 * Json::Value object;
rt300@9 380 * static const StaticString code("code");
rt300@9 381 * object[code] = 1234;
rt300@9 382 * \endcode
rt300@9 383 */
rt300@9 384 class JSON_API StaticString
rt300@9 385 {
rt300@9 386 public:
rt300@9 387 explicit StaticString( const char *czstring )
rt300@9 388 : str_( czstring )
rt300@9 389 {
rt300@9 390 }
rt300@9 391
rt300@9 392 operator const char *() const
rt300@9 393 {
rt300@9 394 return str_;
rt300@9 395 }
rt300@9 396
rt300@9 397 const char *c_str() const
rt300@9 398 {
rt300@9 399 return str_;
rt300@9 400 }
rt300@9 401
rt300@9 402 private:
rt300@9 403 const char *str_;
rt300@9 404 };
rt300@9 405
rt300@9 406 /** \brief Represents a <a HREF="http://www.json.org">JSON</a> value.
rt300@9 407 *
rt300@9 408 * This class is a discriminated union wrapper that can represents a:
rt300@9 409 * - signed integer [range: Value::minInt - Value::maxInt]
rt300@9 410 * - unsigned integer (range: 0 - Value::maxUInt)
rt300@9 411 * - double
rt300@9 412 * - UTF-8 string
rt300@9 413 * - boolean
rt300@9 414 * - 'null'
rt300@9 415 * - an ordered list of Value
rt300@9 416 * - collection of name/value pairs (javascript object)
rt300@9 417 *
rt300@9 418 * The type of the held value is represented by a #ValueType and
rt300@9 419 * can be obtained using type().
rt300@9 420 *
rt300@9 421 * values of an #objectValue or #arrayValue can be accessed using operator[]() methods.
rt300@9 422 * Non const methods will automatically create the a #nullValue element
rt300@9 423 * if it does not exist.
rt300@9 424 * The sequence of an #arrayValue will be automatically resize and initialized
rt300@9 425 * with #nullValue. resize() can be used to enlarge or truncate an #arrayValue.
rt300@9 426 *
rt300@9 427 * The get() methods can be used to obtanis default value in the case the required element
rt300@9 428 * does not exist.
rt300@9 429 *
rt300@9 430 * It is possible to iterate over the list of a #objectValue values using
rt300@9 431 * the getMemberNames() method.
rt300@9 432 */
rt300@9 433 class JSON_API Value
rt300@9 434 {
rt300@9 435 friend class ValueIteratorBase;
rt300@9 436 # ifdef JSON_VALUE_USE_INTERNAL_MAP
rt300@9 437 friend class ValueInternalLink;
rt300@9 438 friend class ValueInternalMap;
rt300@9 439 # endif
rt300@9 440 public:
rt300@9 441 typedef std::vector<std::string> Members;
rt300@9 442 typedef ValueIterator iterator;
rt300@9 443 typedef ValueConstIterator const_iterator;
rt300@9 444 typedef Json::UInt UInt;
rt300@9 445 typedef Json::Int Int;
rt300@9 446 # if defined(JSON_HAS_INT64)
rt300@9 447 typedef Json::UInt64 UInt64;
rt300@9 448 typedef Json::Int64 Int64;
rt300@9 449 #endif // defined(JSON_HAS_INT64)
rt300@9 450 typedef Json::LargestInt LargestInt;
rt300@9 451 typedef Json::LargestUInt LargestUInt;
rt300@9 452 typedef Json::ArrayIndex ArrayIndex;
rt300@9 453
rt300@9 454 static const Value null;
rt300@9 455 /// Minimum signed integer value that can be stored in a Json::Value.
rt300@9 456 static const LargestInt minLargestInt;
rt300@9 457 /// Maximum signed integer value that can be stored in a Json::Value.
rt300@9 458 static const LargestInt maxLargestInt;
rt300@9 459 /// Maximum unsigned integer value that can be stored in a Json::Value.
rt300@9 460 static const LargestUInt maxLargestUInt;
rt300@9 461
rt300@9 462 /// Minimum signed int value that can be stored in a Json::Value.
rt300@9 463 static const Int minInt;
rt300@9 464 /// Maximum signed int value that can be stored in a Json::Value.
rt300@9 465 static const Int maxInt;
rt300@9 466 /// Maximum unsigned int value that can be stored in a Json::Value.
rt300@9 467 static const UInt maxUInt;
rt300@9 468
rt300@9 469 /// Minimum signed 64 bits int value that can be stored in a Json::Value.
rt300@9 470 static const Int64 minInt64;
rt300@9 471 /// Maximum signed 64 bits int value that can be stored in a Json::Value.
rt300@9 472 static const Int64 maxInt64;
rt300@9 473 /// Maximum unsigned 64 bits int value that can be stored in a Json::Value.
rt300@9 474 static const UInt64 maxUInt64;
rt300@9 475
rt300@9 476 private:
rt300@9 477 #ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
rt300@9 478 # ifndef JSON_VALUE_USE_INTERNAL_MAP
rt300@9 479 class CZString
rt300@9 480 {
rt300@9 481 public:
rt300@9 482 enum DuplicationPolicy
rt300@9 483 {
rt300@9 484 noDuplication = 0,
rt300@9 485 duplicate,
rt300@9 486 duplicateOnCopy
rt300@9 487 };
rt300@9 488 CZString( ArrayIndex index );
rt300@9 489 CZString( const char *cstr, DuplicationPolicy allocate );
rt300@9 490 CZString( const CZString &other );
rt300@9 491 ~CZString();
rt300@9 492 CZString &operator =( const CZString &other );
rt300@9 493 bool operator<( const CZString &other ) const;
rt300@9 494 bool operator==( const CZString &other ) const;
rt300@9 495 ArrayIndex index() const;
rt300@9 496 const char *c_str() const;
rt300@9 497 bool isStaticString() const;
rt300@9 498 private:
rt300@9 499 void swap( CZString &other );
rt300@9 500 const char *cstr_;
rt300@9 501 ArrayIndex index_;
rt300@9 502 };
rt300@9 503
rt300@9 504 public:
rt300@9 505 # ifndef JSON_USE_CPPTL_SMALLMAP
rt300@9 506 typedef std::map<CZString, Value> ObjectValues;
rt300@9 507 # else
rt300@9 508 typedef CppTL::SmallMap<CZString, Value> ObjectValues;
rt300@9 509 # endif // ifndef JSON_USE_CPPTL_SMALLMAP
rt300@9 510 # endif // ifndef JSON_VALUE_USE_INTERNAL_MAP
rt300@9 511 #endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
rt300@9 512
rt300@9 513 public:
rt300@9 514 /** \brief Create a default Value of the given type.
rt300@9 515
rt300@9 516 This is a very useful constructor.
rt300@9 517 To create an empty array, pass arrayValue.
rt300@9 518 To create an empty object, pass objectValue.
rt300@9 519 Another Value can then be set to this one by assignment.
rt300@9 520 This is useful since clear() and resize() will not alter types.
rt300@9 521
rt300@9 522 Examples:
rt300@9 523 \code
rt300@9 524 Json::Value null_value; // null
rt300@9 525 Json::Value arr_value(Json::arrayValue); // []
rt300@9 526 Json::Value obj_value(Json::objectValue); // {}
rt300@9 527 \endcode
rt300@9 528 */
rt300@9 529 Value( ValueType type = nullValue );
rt300@9 530 Value( Int value );
rt300@9 531 Value( UInt value );
rt300@9 532 #if defined(JSON_HAS_INT64)
rt300@9 533 Value( Int64 value );
rt300@9 534 Value( UInt64 value );
rt300@9 535 #endif // if defined(JSON_HAS_INT64)
rt300@9 536 Value( double value );
rt300@9 537 Value( const char *value );
rt300@9 538 Value( const char *beginValue, const char *endValue );
rt300@9 539 /** \brief Constructs a value from a static string.
rt300@9 540
rt300@9 541 * Like other value string constructor but do not duplicate the string for
rt300@9 542 * internal storage. The given string must remain alive after the call to this
rt300@9 543 * constructor.
rt300@9 544 * Example of usage:
rt300@9 545 * \code
rt300@9 546 * Json::Value aValue( StaticString("some text") );
rt300@9 547 * \endcode
rt300@9 548 */
rt300@9 549 Value( const StaticString &value );
rt300@9 550 Value( const std::string &value );
rt300@9 551 # ifdef JSON_USE_CPPTL
rt300@9 552 Value( const CppTL::ConstString &value );
rt300@9 553 # endif
rt300@9 554 Value( bool value );
rt300@9 555 Value( const Value &other );
rt300@9 556 ~Value();
rt300@9 557
rt300@9 558 Value &operator=( const Value &other );
rt300@9 559 /// Swap values.
rt300@9 560 /// \note Currently, comments are intentionally not swapped, for
rt300@9 561 /// both logic and efficiency.
rt300@9 562 void swap( Value &other );
rt300@9 563
rt300@9 564 ValueType type() const;
rt300@9 565
rt300@9 566 bool operator <( const Value &other ) const;
rt300@9 567 bool operator <=( const Value &other ) const;
rt300@9 568 bool operator >=( const Value &other ) const;
rt300@9 569 bool operator >( const Value &other ) const;
rt300@9 570
rt300@9 571 bool operator ==( const Value &other ) const;
rt300@9 572 bool operator !=( const Value &other ) const;
rt300@9 573
rt300@9 574 int compare( const Value &other ) const;
rt300@9 575
rt300@9 576 const char *asCString() const;
rt300@9 577 std::string asString() const;
rt300@9 578 # ifdef JSON_USE_CPPTL
rt300@9 579 CppTL::ConstString asConstString() const;
rt300@9 580 # endif
rt300@9 581 Int asInt() const;
rt300@9 582 UInt asUInt() const;
rt300@9 583 Int64 asInt64() const;
rt300@9 584 UInt64 asUInt64() const;
rt300@9 585 LargestInt asLargestInt() const;
rt300@9 586 LargestUInt asLargestUInt() const;
rt300@9 587 float asFloat() const;
rt300@9 588 double asDouble() const;
rt300@9 589 bool asBool() const;
rt300@9 590
rt300@9 591 bool isNull() const;
rt300@9 592 bool isBool() const;
rt300@9 593 bool isInt() const;
rt300@9 594 bool isUInt() const;
rt300@9 595 bool isIntegral() const;
rt300@9 596 bool isDouble() const;
rt300@9 597 bool isNumeric() const;
rt300@9 598 bool isString() const;
rt300@9 599 bool isArray() const;
rt300@9 600 bool isObject() const;
rt300@9 601
rt300@9 602 bool isConvertibleTo( ValueType other ) const;
rt300@9 603
rt300@9 604 /// Number of values in array or object
rt300@9 605 ArrayIndex size() const;
rt300@9 606
rt300@9 607 /// \brief Return true if empty array, empty object, or null;
rt300@9 608 /// otherwise, false.
rt300@9 609 bool empty() const;
rt300@9 610
rt300@9 611 /// Return isNull()
rt300@9 612 bool operator!() const;
rt300@9 613
rt300@9 614 /// Remove all object members and array elements.
rt300@9 615 /// \pre type() is arrayValue, objectValue, or nullValue
rt300@9 616 /// \post type() is unchanged
rt300@9 617 void clear();
rt300@9 618
rt300@9 619 /// Resize the array to size elements.
rt300@9 620 /// New elements are initialized to null.
rt300@9 621 /// May only be called on nullValue or arrayValue.
rt300@9 622 /// \pre type() is arrayValue or nullValue
rt300@9 623 /// \post type() is arrayValue
rt300@9 624 void resize( ArrayIndex size );
rt300@9 625
rt300@9 626 /// Access an array element (zero based index ).
rt300@9 627 /// If the array contains less than index element, then null value are inserted
rt300@9 628 /// in the array so that its size is index+1.
rt300@9 629 /// (You may need to say 'value[0u]' to get your compiler to distinguish
rt300@9 630 /// this from the operator[] which takes a string.)
rt300@9 631 Value &operator[]( ArrayIndex index );
rt300@9 632
rt300@9 633 /// Access an array element (zero based index ).
rt300@9 634 /// If the array contains less than index element, then null value are inserted
rt300@9 635 /// in the array so that its size is index+1.
rt300@9 636 /// (You may need to say 'value[0u]' to get your compiler to distinguish
rt300@9 637 /// this from the operator[] which takes a string.)
rt300@9 638 Value &operator[]( int index );
rt300@9 639
rt300@9 640 /// Access an array element (zero based index )
rt300@9 641 /// (You may need to say 'value[0u]' to get your compiler to distinguish
rt300@9 642 /// this from the operator[] which takes a string.)
rt300@9 643 const Value &operator[]( ArrayIndex index ) const;
rt300@9 644
rt300@9 645 /// Access an array element (zero based index )
rt300@9 646 /// (You may need to say 'value[0u]' to get your compiler to distinguish
rt300@9 647 /// this from the operator[] which takes a string.)
rt300@9 648 const Value &operator[]( int index ) const;
rt300@9 649
rt300@9 650 /// If the array contains at least index+1 elements, returns the element value,
rt300@9 651 /// otherwise returns defaultValue.
rt300@9 652 Value get( ArrayIndex index,
rt300@9 653 const Value &defaultValue ) const;
rt300@9 654 /// Return true if index < size().
rt300@9 655 bool isValidIndex( ArrayIndex index ) const;
rt300@9 656 /// \brief Append value to array at the end.
rt300@9 657 ///
rt300@9 658 /// Equivalent to jsonvalue[jsonvalue.size()] = value;
rt300@9 659 Value &append( const Value &value );
rt300@9 660
rt300@9 661 /// Access an object value by name, create a null member if it does not exist.
rt300@9 662 Value &operator[]( const char *key );
rt300@9 663 /// Access an object value by name, returns null if there is no member with that name.
rt300@9 664 const Value &operator[]( const char *key ) const;
rt300@9 665 /// Access an object value by name, create a null member if it does not exist.
rt300@9 666 Value &operator[]( const std::string &key );
rt300@9 667 /// Access an object value by name, returns null if there is no member with that name.
rt300@9 668 const Value &operator[]( const std::string &key ) const;
rt300@9 669 /** \brief Access an object value by name, create a null member if it does not exist.
rt300@9 670
rt300@9 671 * If the object as no entry for that name, then the member name used to store
rt300@9 672 * the new entry is not duplicated.
rt300@9 673 * Example of use:
rt300@9 674 * \code
rt300@9 675 * Json::Value object;
rt300@9 676 * static const StaticString code("code");
rt300@9 677 * object[code] = 1234;
rt300@9 678 * \endcode
rt300@9 679 */
rt300@9 680 Value &operator[]( const StaticString &key );
rt300@9 681 # ifdef JSON_USE_CPPTL
rt300@9 682 /// Access an object value by name, create a null member if it does not exist.
rt300@9 683 Value &operator[]( const CppTL::ConstString &key );
rt300@9 684 /// Access an object value by name, returns null if there is no member with that name.
rt300@9 685 const Value &operator[]( const CppTL::ConstString &key ) const;
rt300@9 686 # endif
rt300@9 687 /// Return the member named key if it exist, defaultValue otherwise.
rt300@9 688 Value get( const char *key,
rt300@9 689 const Value &defaultValue ) const;
rt300@9 690 /// Return the member named key if it exist, defaultValue otherwise.
rt300@9 691 Value get( const std::string &key,
rt300@9 692 const Value &defaultValue ) const;
rt300@9 693 # ifdef JSON_USE_CPPTL
rt300@9 694 /// Return the member named key if it exist, defaultValue otherwise.
rt300@9 695 Value get( const CppTL::ConstString &key,
rt300@9 696 const Value &defaultValue ) const;
rt300@9 697 # endif
rt300@9 698 /// \brief Remove and return the named member.
rt300@9 699 ///
rt300@9 700 /// Do nothing if it did not exist.
rt300@9 701 /// \return the removed Value, or null.
rt300@9 702 /// \pre type() is objectValue or nullValue
rt300@9 703 /// \post type() is unchanged
rt300@9 704 Value removeMember( const char* key );
rt300@9 705 /// Same as removeMember(const char*)
rt300@9 706 Value removeMember( const std::string &key );
rt300@9 707
rt300@9 708 /// Return true if the object has a member named key.
rt300@9 709 bool isMember( const char *key ) const;
rt300@9 710 /// Return true if the object has a member named key.
rt300@9 711 bool isMember( const std::string &key ) const;
rt300@9 712 # ifdef JSON_USE_CPPTL
rt300@9 713 /// Return true if the object has a member named key.
rt300@9 714 bool isMember( const CppTL::ConstString &key ) const;
rt300@9 715 # endif
rt300@9 716
rt300@9 717 /// \brief Return a list of the member names.
rt300@9 718 ///
rt300@9 719 /// If null, return an empty list.
rt300@9 720 /// \pre type() is objectValue or nullValue
rt300@9 721 /// \post if type() was nullValue, it remains nullValue
rt300@9 722 Members getMemberNames() const;
rt300@9 723
rt300@9 724 //# ifdef JSON_USE_CPPTL
rt300@9 725 // EnumMemberNames enumMemberNames() const;
rt300@9 726 // EnumValues enumValues() const;
rt300@9 727 //# endif
rt300@9 728
rt300@9 729 /// Comments must be //... or /* ... */
rt300@9 730 void setComment( const char *comment,
rt300@9 731 CommentPlacement placement );
rt300@9 732 /// Comments must be //... or /* ... */
rt300@9 733 void setComment( const std::string &comment,
rt300@9 734 CommentPlacement placement );
rt300@9 735 bool hasComment( CommentPlacement placement ) const;
rt300@9 736 /// Include delimiters and embedded newlines.
rt300@9 737 std::string getComment( CommentPlacement placement ) const;
rt300@9 738
rt300@9 739 std::string toStyledString() const;
rt300@9 740
rt300@9 741 const_iterator begin() const;
rt300@9 742 const_iterator end() const;
rt300@9 743
rt300@9 744 iterator begin();
rt300@9 745 iterator end();
rt300@9 746
rt300@9 747 private:
rt300@9 748 Value &resolveReference( const char *key,
rt300@9 749 bool isStatic );
rt300@9 750
rt300@9 751 # ifdef JSON_VALUE_USE_INTERNAL_MAP
rt300@9 752 inline bool isItemAvailable() const
rt300@9 753 {
rt300@9 754 return itemIsUsed_ == 0;
rt300@9 755 }
rt300@9 756
rt300@9 757 inline void setItemUsed( bool isUsed = true )
rt300@9 758 {
rt300@9 759 itemIsUsed_ = isUsed ? 1 : 0;
rt300@9 760 }
rt300@9 761
rt300@9 762 inline bool isMemberNameStatic() const
rt300@9 763 {
rt300@9 764 return memberNameIsStatic_ == 0;
rt300@9 765 }
rt300@9 766
rt300@9 767 inline void setMemberNameIsStatic( bool isStatic )
rt300@9 768 {
rt300@9 769 memberNameIsStatic_ = isStatic ? 1 : 0;
rt300@9 770 }
rt300@9 771 # endif // # ifdef JSON_VALUE_USE_INTERNAL_MAP
rt300@9 772
rt300@9 773 private:
rt300@9 774 struct CommentInfo
rt300@9 775 {
rt300@9 776 CommentInfo();
rt300@9 777 ~CommentInfo();
rt300@9 778
rt300@9 779 void setComment( const char *text );
rt300@9 780
rt300@9 781 char *comment_;
rt300@9 782 };
rt300@9 783
rt300@9 784 //struct MemberNamesTransform
rt300@9 785 //{
rt300@9 786 // typedef const char *result_type;
rt300@9 787 // const char *operator()( const CZString &name ) const
rt300@9 788 // {
rt300@9 789 // return name.c_str();
rt300@9 790 // }
rt300@9 791 //};
rt300@9 792
rt300@9 793 union ValueHolder
rt300@9 794 {
rt300@9 795 LargestInt int_;
rt300@9 796 LargestUInt uint_;
rt300@9 797 double real_;
rt300@9 798 bool bool_;
rt300@9 799 char *string_;
rt300@9 800 # ifdef JSON_VALUE_USE_INTERNAL_MAP
rt300@9 801 ValueInternalArray *array_;
rt300@9 802 ValueInternalMap *map_;
rt300@9 803 #else
rt300@9 804 ObjectValues *map_;
rt300@9 805 # endif
rt300@9 806 } value_;
rt300@9 807 ValueType type_ : 8;
rt300@9 808 int allocated_ : 1; // Notes: if declared as bool, bitfield is useless.
rt300@9 809 # ifdef JSON_VALUE_USE_INTERNAL_MAP
rt300@9 810 unsigned int itemIsUsed_ : 1; // used by the ValueInternalMap container.
rt300@9 811 int memberNameIsStatic_ : 1; // used by the ValueInternalMap container.
rt300@9 812 # endif
rt300@9 813 CommentInfo *comments_;
rt300@9 814 };
rt300@9 815
rt300@9 816
rt300@9 817 /** \brief Experimental and untested: represents an element of the "path" to access a node.
rt300@9 818 */
rt300@9 819 class PathArgument
rt300@9 820 {
rt300@9 821 public:
rt300@9 822 friend class Path;
rt300@9 823
rt300@9 824 PathArgument();
rt300@9 825 PathArgument( ArrayIndex index );
rt300@9 826 PathArgument( const char *key );
rt300@9 827 PathArgument( const std::string &key );
rt300@9 828
rt300@9 829 private:
rt300@9 830 enum Kind
rt300@9 831 {
rt300@9 832 kindNone = 0,
rt300@9 833 kindIndex,
rt300@9 834 kindKey
rt300@9 835 };
rt300@9 836 std::string key_;
rt300@9 837 ArrayIndex index_;
rt300@9 838 Kind kind_;
rt300@9 839 };
rt300@9 840
rt300@9 841 /** \brief Experimental and untested: represents a "path" to access a node.
rt300@9 842 *
rt300@9 843 * Syntax:
rt300@9 844 * - "." => root node
rt300@9 845 * - ".[n]" => elements at index 'n' of root node (an array value)
rt300@9 846 * - ".name" => member named 'name' of root node (an object value)
rt300@9 847 * - ".name1.name2.name3"
rt300@9 848 * - ".[0][1][2].name1[3]"
rt300@9 849 * - ".%" => member name is provided as parameter
rt300@9 850 * - ".[%]" => index is provied as parameter
rt300@9 851 */
rt300@9 852 class Path
rt300@9 853 {
rt300@9 854 public:
rt300@9 855 Path( const std::string &path,
rt300@9 856 const PathArgument &a1 = PathArgument(),
rt300@9 857 const PathArgument &a2 = PathArgument(),
rt300@9 858 const PathArgument &a3 = PathArgument(),
rt300@9 859 const PathArgument &a4 = PathArgument(),
rt300@9 860 const PathArgument &a5 = PathArgument() );
rt300@9 861
rt300@9 862 const Value &resolve( const Value &root ) const;
rt300@9 863 Value resolve( const Value &root,
rt300@9 864 const Value &defaultValue ) const;
rt300@9 865 /// Creates the "path" to access the specified node and returns a reference on the node.
rt300@9 866 Value &make( Value &root ) const;
rt300@9 867
rt300@9 868 private:
rt300@9 869 typedef std::vector<const PathArgument *> InArgs;
rt300@9 870 typedef std::vector<PathArgument> Args;
rt300@9 871
rt300@9 872 void makePath( const std::string &path,
rt300@9 873 const InArgs &in );
rt300@9 874 void addPathInArg( const std::string &path,
rt300@9 875 const InArgs &in,
rt300@9 876 InArgs::const_iterator &itInArg,
rt300@9 877 PathArgument::Kind kind );
rt300@9 878 void invalidPath( const std::string &path,
rt300@9 879 int location );
rt300@9 880
rt300@9 881 Args args_;
rt300@9 882 };
rt300@9 883
rt300@9 884
rt300@9 885
rt300@9 886 #ifdef JSON_VALUE_USE_INTERNAL_MAP
rt300@9 887 /** \brief Allocator to customize Value internal map.
rt300@9 888 * Below is an example of a simple implementation (default implementation actually
rt300@9 889 * use memory pool for speed).
rt300@9 890 * \code
rt300@9 891 class DefaultValueMapAllocator : public ValueMapAllocator
rt300@9 892 {
rt300@9 893 public: // overridden from ValueMapAllocator
rt300@9 894 virtual ValueInternalMap *newMap()
rt300@9 895 {
rt300@9 896 return new ValueInternalMap();
rt300@9 897 }
rt300@9 898
rt300@9 899 virtual ValueInternalMap *newMapCopy( const ValueInternalMap &other )
rt300@9 900 {
rt300@9 901 return new ValueInternalMap( other );
rt300@9 902 }
rt300@9 903
rt300@9 904 virtual void destructMap( ValueInternalMap *map )
rt300@9 905 {
rt300@9 906 delete map;
rt300@9 907 }
rt300@9 908
rt300@9 909 virtual ValueInternalLink *allocateMapBuckets( unsigned int size )
rt300@9 910 {
rt300@9 911 return new ValueInternalLink[size];
rt300@9 912 }
rt300@9 913
rt300@9 914 virtual void releaseMapBuckets( ValueInternalLink *links )
rt300@9 915 {
rt300@9 916 delete [] links;
rt300@9 917 }
rt300@9 918
rt300@9 919 virtual ValueInternalLink *allocateMapLink()
rt300@9 920 {
rt300@9 921 return new ValueInternalLink();
rt300@9 922 }
rt300@9 923
rt300@9 924 virtual void releaseMapLink( ValueInternalLink *link )
rt300@9 925 {
rt300@9 926 delete link;
rt300@9 927 }
rt300@9 928 };
rt300@9 929 * \endcode
rt300@9 930 */
rt300@9 931 class JSON_API ValueMapAllocator
rt300@9 932 {
rt300@9 933 public:
rt300@9 934 virtual ~ValueMapAllocator();
rt300@9 935 virtual ValueInternalMap *newMap() = 0;
rt300@9 936 virtual ValueInternalMap *newMapCopy( const ValueInternalMap &other ) = 0;
rt300@9 937 virtual void destructMap( ValueInternalMap *map ) = 0;
rt300@9 938 virtual ValueInternalLink *allocateMapBuckets( unsigned int size ) = 0;
rt300@9 939 virtual void releaseMapBuckets( ValueInternalLink *links ) = 0;
rt300@9 940 virtual ValueInternalLink *allocateMapLink() = 0;
rt300@9 941 virtual void releaseMapLink( ValueInternalLink *link ) = 0;
rt300@9 942 };
rt300@9 943
rt300@9 944 /** \brief ValueInternalMap hash-map bucket chain link (for internal use only).
rt300@9 945 * \internal previous_ & next_ allows for bidirectional traversal.
rt300@9 946 */
rt300@9 947 class JSON_API ValueInternalLink
rt300@9 948 {
rt300@9 949 public:
rt300@9 950 enum { itemPerLink = 6 }; // sizeof(ValueInternalLink) = 128 on 32 bits architecture.
rt300@9 951 enum InternalFlags {
rt300@9 952 flagAvailable = 0,
rt300@9 953 flagUsed = 1
rt300@9 954 };
rt300@9 955
rt300@9 956 ValueInternalLink();
rt300@9 957
rt300@9 958 ~ValueInternalLink();
rt300@9 959
rt300@9 960 Value items_[itemPerLink];
rt300@9 961 char *keys_[itemPerLink];
rt300@9 962 ValueInternalLink *previous_;
rt300@9 963 ValueInternalLink *next_;
rt300@9 964 };
rt300@9 965
rt300@9 966
rt300@9 967 /** \brief A linked page based hash-table implementation used internally by Value.
rt300@9 968 * \internal ValueInternalMap is a tradional bucket based hash-table, with a linked
rt300@9 969 * list in each bucket to handle collision. There is an addional twist in that
rt300@9 970 * each node of the collision linked list is a page containing a fixed amount of
rt300@9 971 * value. This provides a better compromise between memory usage and speed.
rt300@9 972 *
rt300@9 973 * Each bucket is made up of a chained list of ValueInternalLink. The last
rt300@9 974 * link of a given bucket can be found in the 'previous_' field of the following bucket.
rt300@9 975 * The last link of the last bucket is stored in tailLink_ as it has no following bucket.
rt300@9 976 * Only the last link of a bucket may contains 'available' item. The last link always
rt300@9 977 * contains at least one element unless is it the bucket one very first link.
rt300@9 978 */
rt300@9 979 class JSON_API ValueInternalMap
rt300@9 980 {
rt300@9 981 friend class ValueIteratorBase;
rt300@9 982 friend class Value;
rt300@9 983 public:
rt300@9 984 typedef unsigned int HashKey;
rt300@9 985 typedef unsigned int BucketIndex;
rt300@9 986
rt300@9 987 # ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
rt300@9 988 struct IteratorState
rt300@9 989 {
rt300@9 990 IteratorState()
rt300@9 991 : map_(0)
rt300@9 992 , link_(0)
rt300@9 993 , itemIndex_(0)
rt300@9 994 , bucketIndex_(0)
rt300@9 995 {
rt300@9 996 }
rt300@9 997 ValueInternalMap *map_;
rt300@9 998 ValueInternalLink *link_;
rt300@9 999 BucketIndex itemIndex_;
rt300@9 1000 BucketIndex bucketIndex_;
rt300@9 1001 };
rt300@9 1002 # endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
rt300@9 1003
rt300@9 1004 ValueInternalMap();
rt300@9 1005 ValueInternalMap( const ValueInternalMap &other );
rt300@9 1006 ValueInternalMap &operator =( const ValueInternalMap &other );
rt300@9 1007 ~ValueInternalMap();
rt300@9 1008
rt300@9 1009 void swap( ValueInternalMap &other );
rt300@9 1010
rt300@9 1011 BucketIndex size() const;
rt300@9 1012
rt300@9 1013 void clear();
rt300@9 1014
rt300@9 1015 bool reserveDelta( BucketIndex growth );
rt300@9 1016
rt300@9 1017 bool reserve( BucketIndex newItemCount );
rt300@9 1018
rt300@9 1019 const Value *find( const char *key ) const;
rt300@9 1020
rt300@9 1021 Value *find( const char *key );
rt300@9 1022
rt300@9 1023 Value &resolveReference( const char *key,
rt300@9 1024 bool isStatic );
rt300@9 1025
rt300@9 1026 void remove( const char *key );
rt300@9 1027
rt300@9 1028 void doActualRemove( ValueInternalLink *link,
rt300@9 1029 BucketIndex index,
rt300@9 1030 BucketIndex bucketIndex );
rt300@9 1031
rt300@9 1032 ValueInternalLink *&getLastLinkInBucket( BucketIndex bucketIndex );
rt300@9 1033
rt300@9 1034 Value &setNewItem( const char *key,
rt300@9 1035 bool isStatic,
rt300@9 1036 ValueInternalLink *link,
rt300@9 1037 BucketIndex index );
rt300@9 1038
rt300@9 1039 Value &unsafeAdd( const char *key,
rt300@9 1040 bool isStatic,
rt300@9 1041 HashKey hashedKey );
rt300@9 1042
rt300@9 1043 HashKey hash( const char *key ) const;
rt300@9 1044
rt300@9 1045 int compare( const ValueInternalMap &other ) const;
rt300@9 1046
rt300@9 1047 private:
rt300@9 1048 void makeBeginIterator( IteratorState &it ) const;
rt300@9 1049 void makeEndIterator( IteratorState &it ) const;
rt300@9 1050 static bool equals( const IteratorState &x, const IteratorState &other );
rt300@9 1051 static void increment( IteratorState &iterator );
rt300@9 1052 static void incrementBucket( IteratorState &iterator );
rt300@9 1053 static void decrement( IteratorState &iterator );
rt300@9 1054 static const char *key( const IteratorState &iterator );
rt300@9 1055 static const char *key( const IteratorState &iterator, bool &isStatic );
rt300@9 1056 static Value &value( const IteratorState &iterator );
rt300@9 1057 static int distance( const IteratorState &x, const IteratorState &y );
rt300@9 1058
rt300@9 1059 private:
rt300@9 1060 ValueInternalLink *buckets_;
rt300@9 1061 ValueInternalLink *tailLink_;
rt300@9 1062 BucketIndex bucketsSize_;
rt300@9 1063 BucketIndex itemCount_;
rt300@9 1064 };
rt300@9 1065
rt300@9 1066 /** \brief A simplified deque implementation used internally by Value.
rt300@9 1067 * \internal
rt300@9 1068 * It is based on a list of fixed "page", each page contains a fixed number of items.
rt300@9 1069 * Instead of using a linked-list, a array of pointer is used for fast item look-up.
rt300@9 1070 * Look-up for an element is as follow:
rt300@9 1071 * - compute page index: pageIndex = itemIndex / itemsPerPage
rt300@9 1072 * - look-up item in page: pages_[pageIndex][itemIndex % itemsPerPage]
rt300@9 1073 *
rt300@9 1074 * Insertion is amortized constant time (only the array containing the index of pointers
rt300@9 1075 * need to be reallocated when items are appended).
rt300@9 1076 */
rt300@9 1077 class JSON_API ValueInternalArray
rt300@9 1078 {
rt300@9 1079 friend class Value;
rt300@9 1080 friend class ValueIteratorBase;
rt300@9 1081 public:
rt300@9 1082 enum { itemsPerPage = 8 }; // should be a power of 2 for fast divide and modulo.
rt300@9 1083 typedef Value::ArrayIndex ArrayIndex;
rt300@9 1084 typedef unsigned int PageIndex;
rt300@9 1085
rt300@9 1086 # ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
rt300@9 1087 struct IteratorState // Must be a POD
rt300@9 1088 {
rt300@9 1089 IteratorState()
rt300@9 1090 : array_(0)
rt300@9 1091 , currentPageIndex_(0)
rt300@9 1092 , currentItemIndex_(0)
rt300@9 1093 {
rt300@9 1094 }
rt300@9 1095 ValueInternalArray *array_;
rt300@9 1096 Value **currentPageIndex_;
rt300@9 1097 unsigned int currentItemIndex_;
rt300@9 1098 };
rt300@9 1099 # endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
rt300@9 1100
rt300@9 1101 ValueInternalArray();
rt300@9 1102 ValueInternalArray( const ValueInternalArray &other );
rt300@9 1103 ValueInternalArray &operator =( const ValueInternalArray &other );
rt300@9 1104 ~ValueInternalArray();
rt300@9 1105 void swap( ValueInternalArray &other );
rt300@9 1106
rt300@9 1107 void clear();
rt300@9 1108 void resize( ArrayIndex newSize );
rt300@9 1109
rt300@9 1110 Value &resolveReference( ArrayIndex index );
rt300@9 1111
rt300@9 1112 Value *find( ArrayIndex index ) const;
rt300@9 1113
rt300@9 1114 ArrayIndex size() const;
rt300@9 1115
rt300@9 1116 int compare( const ValueInternalArray &other ) const;
rt300@9 1117
rt300@9 1118 private:
rt300@9 1119 static bool equals( const IteratorState &x, const IteratorState &other );
rt300@9 1120 static void increment( IteratorState &iterator );
rt300@9 1121 static void decrement( IteratorState &iterator );
rt300@9 1122 static Value &dereference( const IteratorState &iterator );
rt300@9 1123 static Value &unsafeDereference( const IteratorState &iterator );
rt300@9 1124 static int distance( const IteratorState &x, const IteratorState &y );
rt300@9 1125 static ArrayIndex indexOf( const IteratorState &iterator );
rt300@9 1126 void makeBeginIterator( IteratorState &it ) const;
rt300@9 1127 void makeEndIterator( IteratorState &it ) const;
rt300@9 1128 void makeIterator( IteratorState &it, ArrayIndex index ) const;
rt300@9 1129
rt300@9 1130 void makeIndexValid( ArrayIndex index );
rt300@9 1131
rt300@9 1132 Value **pages_;
rt300@9 1133 ArrayIndex size_;
rt300@9 1134 PageIndex pageCount_;
rt300@9 1135 };
rt300@9 1136
rt300@9 1137 /** \brief Experimental: do not use. Allocator to customize Value internal array.
rt300@9 1138 * Below is an example of a simple implementation (actual implementation use
rt300@9 1139 * memory pool).
rt300@9 1140 \code
rt300@9 1141 class DefaultValueArrayAllocator : public ValueArrayAllocator
rt300@9 1142 {
rt300@9 1143 public: // overridden from ValueArrayAllocator
rt300@9 1144 virtual ~DefaultValueArrayAllocator()
rt300@9 1145 {
rt300@9 1146 }
rt300@9 1147
rt300@9 1148 virtual ValueInternalArray *newArray()
rt300@9 1149 {
rt300@9 1150 return new ValueInternalArray();
rt300@9 1151 }
rt300@9 1152
rt300@9 1153 virtual ValueInternalArray *newArrayCopy( const ValueInternalArray &other )
rt300@9 1154 {
rt300@9 1155 return new ValueInternalArray( other );
rt300@9 1156 }
rt300@9 1157
rt300@9 1158 virtual void destruct( ValueInternalArray *array )
rt300@9 1159 {
rt300@9 1160 delete array;
rt300@9 1161 }
rt300@9 1162
rt300@9 1163 virtual void reallocateArrayPageIndex( Value **&indexes,
rt300@9 1164 ValueInternalArray::PageIndex &indexCount,
rt300@9 1165 ValueInternalArray::PageIndex minNewIndexCount )
rt300@9 1166 {
rt300@9 1167 ValueInternalArray::PageIndex newIndexCount = (indexCount*3)/2 + 1;
rt300@9 1168 if ( minNewIndexCount > newIndexCount )
rt300@9 1169 newIndexCount = minNewIndexCount;
rt300@9 1170 void *newIndexes = realloc( indexes, sizeof(Value*) * newIndexCount );
rt300@9 1171 if ( !newIndexes )
rt300@9 1172 throw std::bad_alloc();
rt300@9 1173 indexCount = newIndexCount;
rt300@9 1174 indexes = static_cast<Value **>( newIndexes );
rt300@9 1175 }
rt300@9 1176 virtual void releaseArrayPageIndex( Value **indexes,
rt300@9 1177 ValueInternalArray::PageIndex indexCount )
rt300@9 1178 {
rt300@9 1179 if ( indexes )
rt300@9 1180 free( indexes );
rt300@9 1181 }
rt300@9 1182
rt300@9 1183 virtual Value *allocateArrayPage()
rt300@9 1184 {
rt300@9 1185 return static_cast<Value *>( malloc( sizeof(Value) * ValueInternalArray::itemsPerPage ) );
rt300@9 1186 }
rt300@9 1187
rt300@9 1188 virtual void releaseArrayPage( Value *value )
rt300@9 1189 {
rt300@9 1190 if ( value )
rt300@9 1191 free( value );
rt300@9 1192 }
rt300@9 1193 };
rt300@9 1194 \endcode
rt300@9 1195 */
rt300@9 1196 class JSON_API ValueArrayAllocator
rt300@9 1197 {
rt300@9 1198 public:
rt300@9 1199 virtual ~ValueArrayAllocator();
rt300@9 1200 virtual ValueInternalArray *newArray() = 0;
rt300@9 1201 virtual ValueInternalArray *newArrayCopy( const ValueInternalArray &other ) = 0;
rt300@9 1202 virtual void destructArray( ValueInternalArray *array ) = 0;
rt300@9 1203 /** \brief Reallocate array page index.
rt300@9 1204 * Reallocates an array of pointer on each page.
rt300@9 1205 * \param indexes [input] pointer on the current index. May be \c NULL.
rt300@9 1206 * [output] pointer on the new index of at least
rt300@9 1207 * \a minNewIndexCount pages.
rt300@9 1208 * \param indexCount [input] current number of pages in the index.
rt300@9 1209 * [output] number of page the reallocated index can handle.
rt300@9 1210 * \b MUST be >= \a minNewIndexCount.
rt300@9 1211 * \param minNewIndexCount Minimum number of page the new index must be able to
rt300@9 1212 * handle.
rt300@9 1213 */
rt300@9 1214 virtual void reallocateArrayPageIndex( Value **&indexes,
rt300@9 1215 ValueInternalArray::PageIndex &indexCount,
rt300@9 1216 ValueInternalArray::PageIndex minNewIndexCount ) = 0;
rt300@9 1217 virtual void releaseArrayPageIndex( Value **indexes,
rt300@9 1218 ValueInternalArray::PageIndex indexCount ) = 0;
rt300@9 1219 virtual Value *allocateArrayPage() = 0;
rt300@9 1220 virtual void releaseArrayPage( Value *value ) = 0;
rt300@9 1221 };
rt300@9 1222 #endif // #ifdef JSON_VALUE_USE_INTERNAL_MAP
rt300@9 1223
rt300@9 1224
rt300@9 1225 /** \brief base class for Value iterators.
rt300@9 1226 *
rt300@9 1227 */
rt300@9 1228 class ValueIteratorBase
rt300@9 1229 {
rt300@9 1230 public:
rt300@9 1231 typedef unsigned int size_t;
rt300@9 1232 typedef int difference_type;
rt300@9 1233 typedef ValueIteratorBase SelfType;
rt300@9 1234
rt300@9 1235 ValueIteratorBase();
rt300@9 1236 #ifndef JSON_VALUE_USE_INTERNAL_MAP
rt300@9 1237 explicit ValueIteratorBase( const Value::ObjectValues::iterator &current );
rt300@9 1238 #else
rt300@9 1239 ValueIteratorBase( const ValueInternalArray::IteratorState &state );
rt300@9 1240 ValueIteratorBase( const ValueInternalMap::IteratorState &state );
rt300@9 1241 #endif
rt300@9 1242
rt300@9 1243 bool operator ==( const SelfType &other ) const
rt300@9 1244 {
rt300@9 1245 return isEqual( other );
rt300@9 1246 }
rt300@9 1247
rt300@9 1248 bool operator !=( const SelfType &other ) const
rt300@9 1249 {
rt300@9 1250 return !isEqual( other );
rt300@9 1251 }
rt300@9 1252
rt300@9 1253 difference_type operator -( const SelfType &other ) const
rt300@9 1254 {
rt300@9 1255 return computeDistance( other );
rt300@9 1256 }
rt300@9 1257
rt300@9 1258 /// Return either the index or the member name of the referenced value as a Value.
rt300@9 1259 Value key() const;
rt300@9 1260
rt300@9 1261 /// Return the index of the referenced Value. -1 if it is not an arrayValue.
rt300@9 1262 UInt index() const;
rt300@9 1263
rt300@9 1264 /// Return the member name of the referenced Value. "" if it is not an objectValue.
rt300@9 1265 const char *memberName() const;
rt300@9 1266
rt300@9 1267 protected:
rt300@9 1268 Value &deref() const;
rt300@9 1269
rt300@9 1270 void increment();
rt300@9 1271
rt300@9 1272 void decrement();
rt300@9 1273
rt300@9 1274 difference_type computeDistance( const SelfType &other ) const;
rt300@9 1275
rt300@9 1276 bool isEqual( const SelfType &other ) const;
rt300@9 1277
rt300@9 1278 void copy( const SelfType &other );
rt300@9 1279
rt300@9 1280 private:
rt300@9 1281 #ifndef JSON_VALUE_USE_INTERNAL_MAP
rt300@9 1282 Value::ObjectValues::iterator current_;
rt300@9 1283 // Indicates that iterator is for a null value.
rt300@9 1284 bool isNull_;
rt300@9 1285 #else
rt300@9 1286 union
rt300@9 1287 {
rt300@9 1288 ValueInternalArray::IteratorState array_;
rt300@9 1289 ValueInternalMap::IteratorState map_;
rt300@9 1290 } iterator_;
rt300@9 1291 bool isArray_;
rt300@9 1292 #endif
rt300@9 1293 };
rt300@9 1294
rt300@9 1295 /** \brief const iterator for object and array value.
rt300@9 1296 *
rt300@9 1297 */
rt300@9 1298 class ValueConstIterator : public ValueIteratorBase
rt300@9 1299 {
rt300@9 1300 friend class Value;
rt300@9 1301 public:
rt300@9 1302 typedef unsigned int size_t;
rt300@9 1303 typedef int difference_type;
rt300@9 1304 typedef const Value &reference;
rt300@9 1305 typedef const Value *pointer;
rt300@9 1306 typedef ValueConstIterator SelfType;
rt300@9 1307
rt300@9 1308 ValueConstIterator();
rt300@9 1309 private:
rt300@9 1310 /*! \internal Use by Value to create an iterator.
rt300@9 1311 */
rt300@9 1312 #ifndef JSON_VALUE_USE_INTERNAL_MAP
rt300@9 1313 explicit ValueConstIterator( const Value::ObjectValues::iterator &current );
rt300@9 1314 #else
rt300@9 1315 ValueConstIterator( const ValueInternalArray::IteratorState &state );
rt300@9 1316 ValueConstIterator( const ValueInternalMap::IteratorState &state );
rt300@9 1317 #endif
rt300@9 1318 public:
rt300@9 1319 SelfType &operator =( const ValueIteratorBase &other );
rt300@9 1320
rt300@9 1321 SelfType operator++( int )
rt300@9 1322 {
rt300@9 1323 SelfType temp( *this );
rt300@9 1324 ++*this;
rt300@9 1325 return temp;
rt300@9 1326 }
rt300@9 1327
rt300@9 1328 SelfType operator--( int )
rt300@9 1329 {
rt300@9 1330 SelfType temp( *this );
rt300@9 1331 --*this;
rt300@9 1332 return temp;
rt300@9 1333 }
rt300@9 1334
rt300@9 1335 SelfType &operator--()
rt300@9 1336 {
rt300@9 1337 decrement();
rt300@9 1338 return *this;
rt300@9 1339 }
rt300@9 1340
rt300@9 1341 SelfType &operator++()
rt300@9 1342 {
rt300@9 1343 increment();
rt300@9 1344 return *this;
rt300@9 1345 }
rt300@9 1346
rt300@9 1347 reference operator *() const
rt300@9 1348 {
rt300@9 1349 return deref();
rt300@9 1350 }
rt300@9 1351 };
rt300@9 1352
rt300@9 1353
rt300@9 1354 /** \brief Iterator for object and array value.
rt300@9 1355 */
rt300@9 1356 class ValueIterator : public ValueIteratorBase
rt300@9 1357 {
rt300@9 1358 friend class Value;
rt300@9 1359 public:
rt300@9 1360 typedef unsigned int size_t;
rt300@9 1361 typedef int difference_type;
rt300@9 1362 typedef Value &reference;
rt300@9 1363 typedef Value *pointer;
rt300@9 1364 typedef ValueIterator SelfType;
rt300@9 1365
rt300@9 1366 ValueIterator();
rt300@9 1367 ValueIterator( const ValueConstIterator &other );
rt300@9 1368 ValueIterator( const ValueIterator &other );
rt300@9 1369 private:
rt300@9 1370 /*! \internal Use by Value to create an iterator.
rt300@9 1371 */
rt300@9 1372 #ifndef JSON_VALUE_USE_INTERNAL_MAP
rt300@9 1373 explicit ValueIterator( const Value::ObjectValues::iterator &current );
rt300@9 1374 #else
rt300@9 1375 ValueIterator( const ValueInternalArray::IteratorState &state );
rt300@9 1376 ValueIterator( const ValueInternalMap::IteratorState &state );
rt300@9 1377 #endif
rt300@9 1378 public:
rt300@9 1379
rt300@9 1380 SelfType &operator =( const SelfType &other );
rt300@9 1381
rt300@9 1382 SelfType operator++( int )
rt300@9 1383 {
rt300@9 1384 SelfType temp( *this );
rt300@9 1385 ++*this;
rt300@9 1386 return temp;
rt300@9 1387 }
rt300@9 1388
rt300@9 1389 SelfType operator--( int )
rt300@9 1390 {
rt300@9 1391 SelfType temp( *this );
rt300@9 1392 --*this;
rt300@9 1393 return temp;
rt300@9 1394 }
rt300@9 1395
rt300@9 1396 SelfType &operator--()
rt300@9 1397 {
rt300@9 1398 decrement();
rt300@9 1399 return *this;
rt300@9 1400 }
rt300@9 1401
rt300@9 1402 SelfType &operator++()
rt300@9 1403 {
rt300@9 1404 increment();
rt300@9 1405 return *this;
rt300@9 1406 }
rt300@9 1407
rt300@9 1408 reference operator *() const
rt300@9 1409 {
rt300@9 1410 return deref();
rt300@9 1411 }
rt300@9 1412 };
rt300@9 1413
rt300@9 1414
rt300@9 1415 } // namespace Json
rt300@9 1416
rt300@9 1417
rt300@9 1418 #endif // CPPTL_JSON_H_INCLUDED
rt300@9 1419
rt300@9 1420 // //////////////////////////////////////////////////////////////////////
rt300@9 1421 // End of content of file: include/json/value.h
rt300@9 1422 // //////////////////////////////////////////////////////////////////////
rt300@9 1423
rt300@9 1424
rt300@9 1425
rt300@9 1426
rt300@9 1427
rt300@9 1428
rt300@9 1429 // //////////////////////////////////////////////////////////////////////
rt300@9 1430 // Beginning of content of file: include/json/reader.h
rt300@9 1431 // //////////////////////////////////////////////////////////////////////
rt300@9 1432
rt300@9 1433 // Copyright 2007-2010 Baptiste Lepilleur
rt300@9 1434 // Distributed under MIT license, or public domain if desired and
rt300@9 1435 // recognized in your jurisdiction.
rt300@9 1436 // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
rt300@9 1437
rt300@9 1438 #ifndef CPPTL_JSON_READER_H_INCLUDED
rt300@9 1439 # define CPPTL_JSON_READER_H_INCLUDED
rt300@9 1440
rt300@9 1441 #if !defined(JSON_IS_AMALGAMATION)
rt300@9 1442 # include "features.h"
rt300@9 1443 # include "value.h"
rt300@9 1444 #endif // if !defined(JSON_IS_AMALGAMATION)
rt300@9 1445 # include <deque>
rt300@9 1446 # include <stack>
rt300@9 1447 # include <string>
rt300@9 1448 # include <iostream>
rt300@9 1449
rt300@9 1450 namespace Json {
rt300@9 1451
rt300@9 1452 /** \brief Unserialize a <a HREF="http://www.json.org">JSON</a> document into a Value.
rt300@9 1453 *
rt300@9 1454 */
rt300@9 1455 class JSON_API Reader
rt300@9 1456 {
rt300@9 1457 public:
rt300@9 1458 typedef char Char;
rt300@9 1459 typedef const Char *Location;
rt300@9 1460
rt300@9 1461 /** \brief Constructs a Reader allowing all features
rt300@9 1462 * for parsing.
rt300@9 1463 */
rt300@9 1464 Reader();
rt300@9 1465
rt300@9 1466 /** \brief Constructs a Reader allowing the specified feature set
rt300@9 1467 * for parsing.
rt300@9 1468 */
rt300@9 1469 Reader( const Features &features );
rt300@9 1470
rt300@9 1471 /** \brief Read a Value from a <a HREF="http://www.json.org">JSON</a> document.
rt300@9 1472 * \param document UTF-8 encoded string containing the document to read.
rt300@9 1473 * \param root [out] Contains the root value of the document if it was
rt300@9 1474 * successfully parsed.
rt300@9 1475 * \param collectComments \c true to collect comment and allow writing them back during
rt300@9 1476 * serialization, \c false to discard comments.
rt300@9 1477 * This parameter is ignored if Features::allowComments_
rt300@9 1478 * is \c false.
rt300@9 1479 * \return \c true if the document was successfully parsed, \c false if an error occurred.
rt300@9 1480 */
rt300@9 1481 bool parse( const std::string &document,
rt300@9 1482 Value &root,
rt300@9 1483 bool collectComments = true );
rt300@9 1484
rt300@9 1485 /** \brief Read a Value from a <a HREF="http://www.json.org">JSON</a> document.
rt300@9 1486 * \param beginDoc Pointer on the beginning of the UTF-8 encoded string of the document to read.
rt300@9 1487 * \param endDoc Pointer on the end of the UTF-8 encoded string of the document to read.
rt300@9 1488 \ Must be >= beginDoc.
rt300@9 1489 * \param root [out] Contains the root value of the document if it was
rt300@9 1490 * successfully parsed.
rt300@9 1491 * \param collectComments \c true to collect comment and allow writing them back during
rt300@9 1492 * serialization, \c false to discard comments.
rt300@9 1493 * This parameter is ignored if Features::allowComments_
rt300@9 1494 * is \c false.
rt300@9 1495 * \return \c true if the document was successfully parsed, \c false if an error occurred.
rt300@9 1496 */
rt300@9 1497 bool parse( const char *beginDoc, const char *endDoc,
rt300@9 1498 Value &root,
rt300@9 1499 bool collectComments = true );
rt300@9 1500
rt300@9 1501 /// \brief Parse from input stream.
rt300@9 1502 /// \see Json::operator>>(std::istream&, Json::Value&).
rt300@9 1503 bool parse( std::istream &is,
rt300@9 1504 Value &root,
rt300@9 1505 bool collectComments = true );
rt300@9 1506
rt300@9 1507 /** \brief Returns a user friendly string that list errors in the parsed document.
rt300@9 1508 * \return Formatted error message with the list of errors with their location in
rt300@9 1509 * the parsed document. An empty string is returned if no error occurred
rt300@9 1510 * during parsing.
rt300@9 1511 * \deprecated Use getFormattedErrorMessages() instead (typo fix).
rt300@9 1512 */
rt300@9 1513 JSONCPP_DEPRECATED("Use getFormattedErrorMessages instead")
rt300@9 1514 std::string getFormatedErrorMessages() const;
rt300@9 1515
rt300@9 1516 /** \brief Returns a user friendly string that list errors in the parsed document.
rt300@9 1517 * \return Formatted error message with the list of errors with their location in
rt300@9 1518 * the parsed document. An empty string is returned if no error occurred
rt300@9 1519 * during parsing.
rt300@9 1520 */
rt300@9 1521 std::string getFormattedErrorMessages() const;
rt300@9 1522
rt300@9 1523 private:
rt300@9 1524 enum TokenType
rt300@9 1525 {
rt300@9 1526 tokenEndOfStream = 0,
rt300@9 1527 tokenObjectBegin,
rt300@9 1528 tokenObjectEnd,
rt300@9 1529 tokenArrayBegin,
rt300@9 1530 tokenArrayEnd,
rt300@9 1531 tokenString,
rt300@9 1532 tokenNumber,
rt300@9 1533 tokenTrue,
rt300@9 1534 tokenFalse,
rt300@9 1535 tokenNull,
rt300@9 1536 tokenArraySeparator,
rt300@9 1537 tokenMemberSeparator,
rt300@9 1538 tokenComment,
rt300@9 1539 tokenError
rt300@9 1540 };
rt300@9 1541
rt300@9 1542 class Token
rt300@9 1543 {
rt300@9 1544 public:
rt300@9 1545 TokenType type_;
rt300@9 1546 Location start_;
rt300@9 1547 Location end_;
rt300@9 1548 };
rt300@9 1549
rt300@9 1550 class ErrorInfo
rt300@9 1551 {
rt300@9 1552 public:
rt300@9 1553 Token token_;
rt300@9 1554 std::string message_;
rt300@9 1555 Location extra_;
rt300@9 1556 };
rt300@9 1557
rt300@9 1558 typedef std::deque<ErrorInfo> Errors;
rt300@9 1559
rt300@9 1560 bool expectToken( TokenType type, Token &token, const char *message );
rt300@9 1561 bool readToken( Token &token );
rt300@9 1562 void skipSpaces();
rt300@9 1563 bool match( Location pattern,
rt300@9 1564 int patternLength );
rt300@9 1565 bool readComment();
rt300@9 1566 bool readCStyleComment();
rt300@9 1567 bool readCppStyleComment();
rt300@9 1568 bool readString();
rt300@9 1569 void readNumber();
rt300@9 1570 bool readValue();
rt300@9 1571 bool readObject( Token &token );
rt300@9 1572 bool readArray( Token &token );
rt300@9 1573 bool decodeNumber( Token &token );
rt300@9 1574 bool decodeString( Token &token );
rt300@9 1575 bool decodeString( Token &token, std::string &decoded );
rt300@9 1576 bool decodeDouble( Token &token );
rt300@9 1577 bool decodeUnicodeCodePoint( Token &token,
rt300@9 1578 Location &current,
rt300@9 1579 Location end,
rt300@9 1580 unsigned int &unicode );
rt300@9 1581 bool decodeUnicodeEscapeSequence( Token &token,
rt300@9 1582 Location &current,
rt300@9 1583 Location end,
rt300@9 1584 unsigned int &unicode );
rt300@9 1585 bool addError( const std::string &message,
rt300@9 1586 Token &token,
rt300@9 1587 Location extra = 0 );
rt300@9 1588 bool recoverFromError( TokenType skipUntilToken );
rt300@9 1589 bool addErrorAndRecover( const std::string &message,
rt300@9 1590 Token &token,
rt300@9 1591 TokenType skipUntilToken );
rt300@9 1592 void skipUntilSpace();
rt300@9 1593 Value &currentValue();
rt300@9 1594 Char getNextChar();
rt300@9 1595 void getLocationLineAndColumn( Location location,
rt300@9 1596 int &line,
rt300@9 1597 int &column ) const;
rt300@9 1598 std::string getLocationLineAndColumn( Location location ) const;
rt300@9 1599 void addComment( Location begin,
rt300@9 1600 Location end,
rt300@9 1601 CommentPlacement placement );
rt300@9 1602 void skipCommentTokens( Token &token );
rt300@9 1603
rt300@9 1604 typedef std::stack<Value *> Nodes;
rt300@9 1605 Nodes nodes_;
rt300@9 1606 Errors errors_;
rt300@9 1607 std::string document_;
rt300@9 1608 Location begin_;
rt300@9 1609 Location end_;
rt300@9 1610 Location current_;
rt300@9 1611 Location lastValueEnd_;
rt300@9 1612 Value *lastValue_;
rt300@9 1613 std::string commentsBefore_;
rt300@9 1614 Features features_;
rt300@9 1615 bool collectComments_;
rt300@9 1616 };
rt300@9 1617
rt300@9 1618 /** \brief Read from 'sin' into 'root'.
rt300@9 1619
rt300@9 1620 Always keep comments from the input JSON.
rt300@9 1621
rt300@9 1622 This can be used to read a file into a particular sub-object.
rt300@9 1623 For example:
rt300@9 1624 \code
rt300@9 1625 Json::Value root;
rt300@9 1626 cin >> root["dir"]["file"];
rt300@9 1627 cout << root;
rt300@9 1628 \endcode
rt300@9 1629 Result:
rt300@9 1630 \verbatim
rt300@9 1631 {
rt300@9 1632 "dir": {
rt300@9 1633 "file": {
rt300@9 1634 // The input stream JSON would be nested here.
rt300@9 1635 }
rt300@9 1636 }
rt300@9 1637 }
rt300@9 1638 \endverbatim
rt300@9 1639 \throw std::exception on parse error.
rt300@9 1640 \see Json::operator<<()
rt300@9 1641 */
rt300@9 1642 std::istream& operator>>( std::istream&, Value& );
rt300@9 1643
rt300@9 1644 } // namespace Json
rt300@9 1645
rt300@9 1646 #endif // CPPTL_JSON_READER_H_INCLUDED
rt300@9 1647
rt300@9 1648 // //////////////////////////////////////////////////////////////////////
rt300@9 1649 // End of content of file: include/json/reader.h
rt300@9 1650 // //////////////////////////////////////////////////////////////////////
rt300@9 1651
rt300@9 1652
rt300@9 1653
rt300@9 1654
rt300@9 1655
rt300@9 1656
rt300@9 1657 // //////////////////////////////////////////////////////////////////////
rt300@9 1658 // Beginning of content of file: include/json/writer.h
rt300@9 1659 // //////////////////////////////////////////////////////////////////////
rt300@9 1660
rt300@9 1661 // Copyright 2007-2010 Baptiste Lepilleur
rt300@9 1662 // Distributed under MIT license, or public domain if desired and
rt300@9 1663 // recognized in your jurisdiction.
rt300@9 1664 // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
rt300@9 1665
rt300@9 1666 #ifndef JSON_WRITER_H_INCLUDED
rt300@9 1667 # define JSON_WRITER_H_INCLUDED
rt300@9 1668
rt300@9 1669 #if !defined(JSON_IS_AMALGAMATION)
rt300@9 1670 # include "value.h"
rt300@9 1671 #endif // if !defined(JSON_IS_AMALGAMATION)
rt300@9 1672 # include <vector>
rt300@9 1673 # include <string>
rt300@9 1674 # include <iostream>
rt300@9 1675
rt300@9 1676 namespace Json {
rt300@9 1677
rt300@9 1678 class Value;
rt300@9 1679
rt300@9 1680 /** \brief Abstract class for writers.
rt300@9 1681 */
rt300@9 1682 class JSON_API Writer
rt300@9 1683 {
rt300@9 1684 public:
rt300@9 1685 virtual ~Writer();
rt300@9 1686
rt300@9 1687 virtual std::string write( const Value &root ) = 0;
rt300@9 1688 };
rt300@9 1689
rt300@9 1690 /** \brief Outputs a Value in <a HREF="http://www.json.org">JSON</a> format without formatting (not human friendly).
rt300@9 1691 *
rt300@9 1692 * The JSON document is written in a single line. It is not intended for 'human' consumption,
rt300@9 1693 * but may be usefull to support feature such as RPC where bandwith is limited.
rt300@9 1694 * \sa Reader, Value
rt300@9 1695 */
rt300@9 1696 class JSON_API FastWriter : public Writer
rt300@9 1697 {
rt300@9 1698 public:
rt300@9 1699 FastWriter();
rt300@9 1700 virtual ~FastWriter(){}
rt300@9 1701
rt300@9 1702 void enableYAMLCompatibility();
rt300@9 1703
rt300@9 1704 public: // overridden from Writer
rt300@9 1705 virtual std::string write( const Value &root );
rt300@9 1706
rt300@9 1707 private:
rt300@9 1708 void writeValue( const Value &value );
rt300@9 1709
rt300@9 1710 std::string document_;
rt300@9 1711 bool yamlCompatiblityEnabled_;
rt300@9 1712 };
rt300@9 1713
rt300@9 1714 /** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a human friendly way.
rt300@9 1715 *
rt300@9 1716 * The rules for line break and indent are as follow:
rt300@9 1717 * - Object value:
rt300@9 1718 * - if empty then print {} without indent and line break
rt300@9 1719 * - if not empty the print '{', line break & indent, print one value per line
rt300@9 1720 * and then unindent and line break and print '}'.
rt300@9 1721 * - Array value:
rt300@9 1722 * - if empty then print [] without indent and line break
rt300@9 1723 * - if the array contains no object value, empty array or some other value types,
rt300@9 1724 * and all the values fit on one lines, then print the array on a single line.
rt300@9 1725 * - otherwise, it the values do not fit on one line, or the array contains
rt300@9 1726 * object or non empty array, then print one value per line.
rt300@9 1727 *
rt300@9 1728 * If the Value have comments then they are outputed according to their #CommentPlacement.
rt300@9 1729 *
rt300@9 1730 * \sa Reader, Value, Value::setComment()
rt300@9 1731 */
rt300@9 1732 class JSON_API StyledWriter: public Writer
rt300@9 1733 {
rt300@9 1734 public:
rt300@9 1735 StyledWriter();
rt300@9 1736 virtual ~StyledWriter(){}
rt300@9 1737
rt300@9 1738 public: // overridden from Writer
rt300@9 1739 /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
rt300@9 1740 * \param root Value to serialize.
rt300@9 1741 * \return String containing the JSON document that represents the root value.
rt300@9 1742 */
rt300@9 1743 virtual std::string write( const Value &root );
rt300@9 1744
rt300@9 1745 private:
rt300@9 1746 void writeValue( const Value &value );
rt300@9 1747 void writeArrayValue( const Value &value );
rt300@9 1748 bool isMultineArray( const Value &value );
rt300@9 1749 void pushValue( const std::string &value );
rt300@9 1750 void writeIndent();
rt300@9 1751 void writeWithIndent( const std::string &value );
rt300@9 1752 void indent();
rt300@9 1753 void unindent();
rt300@9 1754 void writeCommentBeforeValue( const Value &root );
rt300@9 1755 void writeCommentAfterValueOnSameLine( const Value &root );
rt300@9 1756 bool hasCommentForValue( const Value &value );
rt300@9 1757 static std::string normalizeEOL( const std::string &text );
rt300@9 1758
rt300@9 1759 typedef std::vector<std::string> ChildValues;
rt300@9 1760
rt300@9 1761 ChildValues childValues_;
rt300@9 1762 std::string document_;
rt300@9 1763 std::string indentString_;
rt300@9 1764 int rightMargin_;
rt300@9 1765 int indentSize_;
rt300@9 1766 bool addChildValues_;
rt300@9 1767 };
rt300@9 1768
rt300@9 1769 /** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a human friendly way,
rt300@9 1770 to a stream rather than to a string.
rt300@9 1771 *
rt300@9 1772 * The rules for line break and indent are as follow:
rt300@9 1773 * - Object value:
rt300@9 1774 * - if empty then print {} without indent and line break
rt300@9 1775 * - if not empty the print '{', line break & indent, print one value per line
rt300@9 1776 * and then unindent and line break and print '}'.
rt300@9 1777 * - Array value:
rt300@9 1778 * - if empty then print [] without indent and line break
rt300@9 1779 * - if the array contains no object value, empty array or some other value types,
rt300@9 1780 * and all the values fit on one lines, then print the array on a single line.
rt300@9 1781 * - otherwise, it the values do not fit on one line, or the array contains
rt300@9 1782 * object or non empty array, then print one value per line.
rt300@9 1783 *
rt300@9 1784 * If the Value have comments then they are outputed according to their #CommentPlacement.
rt300@9 1785 *
rt300@9 1786 * \param indentation Each level will be indented by this amount extra.
rt300@9 1787 * \sa Reader, Value, Value::setComment()
rt300@9 1788 */
rt300@9 1789 class JSON_API StyledStreamWriter
rt300@9 1790 {
rt300@9 1791 public:
rt300@9 1792 StyledStreamWriter( std::string indentation="\t" );
rt300@9 1793 ~StyledStreamWriter(){}
rt300@9 1794
rt300@9 1795 public:
rt300@9 1796 /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
rt300@9 1797 * \param out Stream to write to. (Can be ostringstream, e.g.)
rt300@9 1798 * \param root Value to serialize.
rt300@9 1799 * \note There is no point in deriving from Writer, since write() should not return a value.
rt300@9 1800 */
rt300@9 1801 void write( std::ostream &out, const Value &root );
rt300@9 1802
rt300@9 1803 private:
rt300@9 1804 void writeValue( const Value &value );
rt300@9 1805 void writeArrayValue( const Value &value );
rt300@9 1806 bool isMultineArray( const Value &value );
rt300@9 1807 void pushValue( const std::string &value );
rt300@9 1808 void writeIndent();
rt300@9 1809 void writeWithIndent( const std::string &value );
rt300@9 1810 void indent();
rt300@9 1811 void unindent();
rt300@9 1812 void writeCommentBeforeValue( const Value &root );
rt300@9 1813 void writeCommentAfterValueOnSameLine( const Value &root );
rt300@9 1814 bool hasCommentForValue( const Value &value );
rt300@9 1815 static std::string normalizeEOL( const std::string &text );
rt300@9 1816
rt300@9 1817 typedef std::vector<std::string> ChildValues;
rt300@9 1818
rt300@9 1819 ChildValues childValues_;
rt300@9 1820 std::ostream* document_;
rt300@9 1821 std::string indentString_;
rt300@9 1822 int rightMargin_;
rt300@9 1823 std::string indentation_;
rt300@9 1824 bool addChildValues_;
rt300@9 1825 };
rt300@9 1826
rt300@9 1827 # if defined(JSON_HAS_INT64)
rt300@9 1828 std::string JSON_API valueToString( Int value );
rt300@9 1829 std::string JSON_API valueToString( UInt value );
rt300@9 1830 # endif // if defined(JSON_HAS_INT64)
rt300@9 1831 std::string JSON_API valueToString( LargestInt value );
rt300@9 1832 std::string JSON_API valueToString( LargestUInt value );
rt300@9 1833 std::string JSON_API valueToString( double value );
rt300@9 1834 std::string JSON_API valueToString( bool value );
rt300@9 1835 std::string JSON_API valueToQuotedString( const char *value );
rt300@9 1836
rt300@9 1837 /// \brief Output using the StyledStreamWriter.
rt300@9 1838 /// \see Json::operator>>()
rt300@9 1839 std::ostream& operator<<( std::ostream&, const Value &root );
rt300@9 1840
rt300@9 1841 } // namespace Json
rt300@9 1842
rt300@9 1843
rt300@9 1844
rt300@9 1845 #endif // JSON_WRITER_H_INCLUDED
rt300@9 1846
rt300@9 1847 // //////////////////////////////////////////////////////////////////////
rt300@9 1848 // End of content of file: include/json/writer.h
rt300@9 1849 // //////////////////////////////////////////////////////////////////////
rt300@9 1850
rt300@9 1851
rt300@9 1852
rt300@9 1853
rt300@9 1854
rt300@9 1855 #endif //ifndef JSON_AMALGATED_H_INCLUDED