annotate json/json.h @ 52:89944ab3e129 tip

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