Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/miloyip/rapidjson.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMilo Yip <miloyip@gmail.com>2018-01-13 07:44:22 +0300
committerMilo Yip <miloyip@gmail.com>2018-01-13 07:44:22 +0300
commit62e785babf57cee1522c69cb1a61e9549cc1da1f (patch)
tree9a708fd3404ec51948f246f73889da4fa13f3dc8
parentfacb432f91b588b282c42d2a1a643894ae550e8f (diff)
parent0d95d58f8b6259e06e391ce962ec2062260bcdb8 (diff)
Merge branch 'master' into travis
-rw-r--r--.gitignore4
-rw-r--r--.gitmodules2
-rw-r--r--.travis.yml100
-rw-r--r--CHANGELOG.md91
-rw-r--r--CMakeLists.txt104
-rw-r--r--CMakeModules/FindGTestSrc.cmake2
-rw-r--r--RapidJSON.pc.in2
-rw-r--r--RapidJSONConfig.cmake.in18
-rw-r--r--appveyor.yml4
-rw-r--r--bin/jsonschema/remotes/.DS_Storebin6148 -> 0 bytes
-rw-r--r--bin/jsonschema/tests/.DS_Storebin6148 -> 0 bytes
-rw-r--r--bin/jsonschema/tests/draft4/.DS_Storebin6148 -> 0 bytes
-rw-r--r--[-rwxr-xr-x]bin/types/booleans.json0
-rw-r--r--[-rwxr-xr-x]bin/types/floats.json0
-rw-r--r--[-rwxr-xr-x]bin/types/guids.json0
-rw-r--r--[-rwxr-xr-x]bin/types/integers.json0
-rw-r--r--[-rwxr-xr-x]bin/types/mixed.json0
-rw-r--r--[-rwxr-xr-x]bin/types/nulls.json0
-rw-r--r--[-rwxr-xr-x]bin/types/paragraphs.json0
-rw-r--r--contrib/natvis/LICENSE45
-rw-r--r--contrib/natvis/README.md7
-rw-r--r--contrib/natvis/rapidjson.natvis38
-rw-r--r--doc/CMakeLists.txt4
-rw-r--r--doc/Doxyfile.in1
-rw-r--r--doc/Doxyfile.zh-cn.in3
-rw-r--r--doc/diagram/move2.dot8
-rw-r--r--doc/diagram/move3.dot8
-rw-r--r--doc/dom.md9
-rw-r--r--doc/dom.zh-cn.md139
-rw-r--r--doc/encoding.md6
-rw-r--r--doc/encoding.zh-cn.md70
-rw-r--r--doc/faq.md24
-rw-r--r--doc/faq.zh-cn.md202
-rw-r--r--doc/features.md10
-rw-r--r--doc/features.zh-cn.md82
-rw-r--r--doc/internals.md15
-rw-r--r--doc/internals.zh-cn.md363
-rw-r--r--doc/misc/footer.html16
-rw-r--r--doc/misc/header.html11
-rw-r--r--doc/npm.md31
-rw-r--r--doc/performance.md6
-rw-r--r--doc/performance.zh-cn.md8
-rw-r--r--doc/pointer.md4
-rw-r--r--doc/pointer.zh-cn.md8
-rw-r--r--doc/sax.md74
-rw-r--r--doc/sax.zh-cn.md133
-rw-r--r--doc/schema.md82
-rw-r--r--doc/schema.zh-cn.md118
-rw-r--r--doc/stream.zh-cn.md116
-rw-r--r--doc/tutorial.md77
-rw-r--r--doc/tutorial.zh-cn.md265
-rw-r--r--example/CMakeLists.txt17
-rw-r--r--example/archiver/archiver.cpp292
-rw-r--r--example/archiver/archiver.h139
-rw-r--r--example/archiver/archivertest.cpp281
-rw-r--r--example/filterkey/filterkey.cpp135
-rw-r--r--example/filterkeydom/filterkeydom.cpp170
-rw-r--r--example/lookaheadparser/lookaheadparser.cpp350
-rw-r--r--example/parsebyparts/parsebyparts.cpp10
-rw-r--r--example/simplepullreader/simplepullreader.cpp53
-rw-r--r--example/simplewriter/simplewriter.cpp23
-rw-r--r--include/rapidjson/allocators.h34
-rw-r--r--include/rapidjson/cursorstreamwrapper.h78
-rw-r--r--include/rapidjson/document.h231
-rw-r--r--include/rapidjson/encodedstream.h6
-rw-r--r--include/rapidjson/encodings.h32
-rw-r--r--include/rapidjson/error/en.h2
-rw-r--r--include/rapidjson/error/error.h10
-rw-r--r--include/rapidjson/filereadstream.h2
-rw-r--r--include/rapidjson/filewritestream.h4
-rw-r--r--[-rwxr-xr-x]include/rapidjson/internal/biginteger.h0
-rw-r--r--include/rapidjson/internal/diyfp.h2
-rw-r--r--include/rapidjson/internal/dtoa.h9
-rw-r--r--include/rapidjson/internal/ieee754.h4
-rw-r--r--include/rapidjson/internal/regex.h246
-rw-r--r--include/rapidjson/internal/stack.h3
-rw-r--r--include/rapidjson/internal/strfunc.h14
-rw-r--r--include/rapidjson/internal/strtod.h16
-rw-r--r--include/rapidjson/istreamwrapper.h7
-rw-r--r--include/rapidjson/ostreamwrapper.h5
-rw-r--r--include/rapidjson/pointer.h37
-rw-r--r--include/rapidjson/prettywriter.h72
-rw-r--r--include/rapidjson/rapidjson.h59
-rw-r--r--include/rapidjson/reader.h734
-rw-r--r--include/rapidjson/schema.h186
-rw-r--r--include/rapidjson/stream.h54
-rw-r--r--include/rapidjson/stringbuffer.h4
-rw-r--r--include/rapidjson/writer.h213
-rw-r--r--include_dirs.js2
-rw-r--r--library.json7
-rw-r--r--package.json24
-rw-r--r--rapidjson.autopkg10
-rw-r--r--readme.md67
-rw-r--r--readme.zh-cn.md105
-rw-r--r--test/perftest/CMakeLists.txt2
-rw-r--r--test/perftest/perftest.h3
-rw-r--r--test/perftest/rapidjsontest.cpp31
-rw-r--r--test/unittest/CMakeLists.txt34
-rw-r--r--test/unittest/cursorstreamwrappertest.cpp115
-rw-r--r--test/unittest/documenttest.cpp64
-rw-r--r--test/unittest/dtoatest.cpp7
-rw-r--r--test/unittest/encodingstest.cpp30
-rw-r--r--test/unittest/fwdtest.cpp25
-rw-r--r--test/unittest/istreamwrappertest.cpp14
-rw-r--r--test/unittest/itoatest.cpp14
-rw-r--r--test/unittest/jsoncheckertest.cpp54
-rw-r--r--test/unittest/namespacetest.cpp4
-rw-r--r--test/unittest/ostreamwrappertest.cpp5
-rw-r--r--test/unittest/pointertest.cpp6
-rw-r--r--test/unittest/prettywritertest.cpp164
-rw-r--r--test/unittest/readertest.cpp492
-rw-r--r--test/unittest/regextest.cpp632
-rw-r--r--test/unittest/schematest.cpp184
-rw-r--r--test/unittest/simdtest.cpp154
-rw-r--r--test/unittest/stringbuffertest.cpp31
-rw-r--r--test/unittest/strtodtest.cpp4
-rw-r--r--test/unittest/unittest.cpp1
-rw-r--r--test/unittest/unittest.h6
-rw-r--r--test/unittest/valuetest.cpp91
-rw-r--r--test/unittest/writertest.cpp166
-rw-r--r--test/valgrind.supp17
m---------thirdparty/gtest0
-rwxr-xr-xtravis-doxygen.sh11
123 files changed, 6400 insertions, 2020 deletions
diff --git a/.gitignore b/.gitignore
index 2c412c2b..1d3073f7 100644
--- a/.gitignore
+++ b/.gitignore
@@ -20,5 +20,9 @@ Testing
/googletest
install_manifest.txt
Doxyfile
+Doxyfile.zh-cn
DartConfiguration.tcl
*.nupkg
+
+# Files created by OS
+*.DS_Store
diff --git a/.gitmodules b/.gitmodules
index 8e9d1f37..5e41f7c9 100644
--- a/.gitmodules
+++ b/.gitmodules
@@ -1,3 +1,3 @@
[submodule "thirdparty/gtest"]
path = thirdparty/gtest
- url = https://chromium.googlesource.com/external/googletest.git
+ url = https://github.com/google/googletest.git
diff --git a/.travis.yml b/.travis.yml
index 9266277b..df821a70 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,127 +1,57 @@
+sudo: required
+dist: trusty
+group: edge
+
language: cpp
-sudo: false
cache:
- ccache
-addons:
- apt:
- packages: &default_packages
- - cmake
- - valgrind
-
env:
-global:
+ global:
- USE_CCACHE=1
- CCACHE_SLOPPINESS=pch_defines,time_macros
- CCACHE_COMPRESS=1
- CCACHE_MAXSIZE=100M
- ARCH_FLAGS_x86='-m32' # #266: don't use SSE on 32-bit
- ARCH_FLAGS_x86_64='-msse4.2' # use SSE4.2 on 64-bit
- - GITHUB_REPO='miloyip/rapidjson'
+ - GITHUB_REPO='Tencent/rapidjson'
- secure: "HrsaCb+N66EG1HR+LWH1u51SjaJyRwJEDzqJGYMB7LJ/bfqb9mWKF1fLvZGk46W5t7TVaXRDD5KHFx9DPWvKn4gRUVkwTHEy262ah5ORh8M6n/6VVVajeV/AYt2C0sswdkDBDO4Xq+xy5gdw3G8s1A4Inbm73pUh+6vx+7ltBbk="
+before_install:
+ - sudo apt-add-repository -y ppa:ubuntu-toolchain-r/test
+ - sudo apt-get update -qq
+ - sudo apt-get install -y cmake valgrind g++-multilib libc6-dbg:i386
+
matrix:
include:
# gcc
- env: CONF=release ARCH=x86 CXX11=ON
compiler: gcc
- addons:
- apt:
- packages:
- - *default_packages
- - g++-multilib
- - libc6-dbg:i386
- env: CONF=release ARCH=x86_64 CXX11=ON
compiler: gcc
- env: CONF=debug ARCH=x86 CXX11=OFF
compiler: gcc
- addons:
- apt:
- packages:
- - *default_packages
- - g++-multilib
- - libc6-dbg:i386
- env: CONF=debug ARCH=x86_64 CXX11=OFF
compiler: gcc
# clang
- env: CONF=debug ARCH=x86 CXX11=ON CCACHE_CPP2=yes
compiler: clang
- addons:
- apt:
- sources:
- - llvm-toolchain-precise-3.7
- - ubuntu-toolchain-r-test
- packages:
- - *default_packages
- - g++-multilib
- - libc6-dbg:i386
- - clang-3.7
- env: CONF=debug ARCH=x86_64 CXX11=ON CCACHE_CPP2=yes
compiler: clang
- addons:
- apt:
- sources:
- - llvm-toolchain-precise-3.7
- - ubuntu-toolchain-r-test
- packages:
- - *default_packages
- - clang-3.7
- env: CONF=debug ARCH=x86 CXX11=OFF CCACHE_CPP2=yes
compiler: clang
- addons:
- apt:
- sources:
- - llvm-toolchain-precise-3.7
- - ubuntu-toolchain-r-test
- packages:
- - *default_packages
- - g++-multilib
- - libc6-dbg:i386
- - clang-3.7
- env: CONF=debug ARCH=x86_64 CXX11=OFF CCACHE_CPP2=yes
compiler: clang
- addons:
- apt:
- sources:
- - llvm-toolchain-precise-3.7
- - ubuntu-toolchain-r-test
- packages:
- - *default_packages
- - clang-3.7
- env: CONF=release ARCH=x86 CXX11=ON CCACHE_CPP2=yes
compiler: clang
- addons:
- apt:
- sources:
- - llvm-toolchain-precise-3.7
- - ubuntu-toolchain-r-test
- packages:
- - *default_packages
- - g++-multilib
- - libc6-dbg:i386
- - clang-3.7
- env: CONF=release ARCH=x86_64 CXX11=ON CCACHE_CPP2=yes
compiler: clang
- addons:
- apt:
- sources:
- - llvm-toolchain-precise-3.7
- - ubuntu-toolchain-r-test
- packages:
- - *default_packages
- - clang-3.7
# coverage report
- env: CONF=debug ARCH=x86 CXX11=ON GCOV_FLAGS='--coverage'
compiler: gcc
cache:
- ccache
- pip
- addons:
- apt:
- packages:
- - *default_packages
- - g++-multilib
- - libc6-dbg:i386
after_success:
- pip install --user cpp-coveralls
- coveralls -r .. --gcov-options '\-lp' -e thirdparty -e example -e test -e build/CMakeFiles -e include/rapidjson/msinttypes -e include/rapidjson/internal/meta.h -e include/rapidjson/error/en.h
@@ -130,12 +60,6 @@ matrix:
cache:
- ccache
- pip
- addons:
- apt:
- packages:
- - *default_packages
- - g++-multilib
- - libc6-dbg:i386
after_success:
- pip install --user cpp-coveralls
- coveralls -r .. --gcov-options '\-lp' -e thirdparty -e example -e test -e build/CMakeFiles -e include/rapidjson/msinttypes -e include/rapidjson/internal/meta.h -e include/rapidjson/error/en.h
@@ -158,7 +82,7 @@ before_script:
- mkdir build
script:
- - if [ "$CXX" = "clang++" ]; then export CXX="clang++-3.7" CC="clang-3.7"; fi
+ - if [ "$CXX" = "clang++" ]; then export CXXFLAGS="-stdlib=libc++ ${CXXFLAGS}"; fi
- >
eval "ARCH_FLAGS=\${ARCH_FLAGS_${ARCH}}" ;
(cd build && cmake
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8ad9b3c3..1c580bd1 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,6 +4,83 @@ This project adheres to [Semantic Versioning](http://semver.org/).
## [Unreleased]
+## 1.1.0 - 2016-08-25
+
+### Added
+* Add GenericDocument ctor overload to specify JSON type (#369)
+* Add FAQ (#372, #373, #374, #376)
+* Add forward declaration header `fwd.h`
+* Add @PlatformIO Library Registry manifest file (#400)
+* Implement assignment operator for BigInteger (#404)
+* Add comments support (#443)
+* Adding coapp definition (#460)
+* documenttest.cpp: EXPECT_THROW when checking empty allocator (470)
+* GenericDocument: add implicit conversion to ParseResult (#480)
+* Use <wchar.h> with C++ linkage on Windows ARM (#485)
+* Detect little endian for Microsoft ARM targets
+* Check Nan/Inf when writing a double (#510)
+* Add JSON Schema Implementation (#522)
+* Add iostream wrapper (#530)
+* Add Jsonx example for converting JSON into JSONx (a XML format) (#531)
+* Add optional unresolvedTokenIndex parameter to Pointer::Get() (#532)
+* Add encoding validation option for Writer/PrettyWriter (#534)
+* Add Writer::SetMaxDecimalPlaces() (#536)
+* Support {0, } and {0, m} in Regex (#539)
+* Add Value::Get/SetFloat(), Value::IsLossLessFloat/Double() (#540)
+* Add stream position check to reader unit tests (#541)
+* Add Templated accessors and range-based for (#542)
+* Add (Pretty)Writer::RawValue() (#543)
+* Add Document::Parse(std::string), Document::Parse(const char*, size_t length) and related APIs. (#553)
+* Add move constructor for GenericSchemaDocument (#554)
+* Add VS2010 and VS2015 to AppVeyor CI (#555)
+* Add parse-by-parts example (#556, #562)
+* Support parse number as string (#564, #589)
+* Add kFormatSingleLineArray for PrettyWriter (#577)
+* Added optional support for trailing commas (#584)
+* Added filterkey and filterkeydom examples (#615)
+* Added npm docs (#639)
+* Allow options for writing and parsing NaN/Infinity (#641)
+* Add std::string overload to PrettyWriter::Key() when RAPIDJSON_HAS_STDSTRING is defined (#698)
+
+### Fixed
+* Fix gcc/clang/vc warnings (#350, #394, #397, #444, #447, #473, #515, #582, #589, #595, #667)
+* Fix documentation (#482, #511, #550, #557, #614, #635, #660)
+* Fix emscripten alignment issue (#535)
+* Fix missing allocator to uses of AddMember in document (#365)
+* CMake will no longer complain that the minimum CMake version is not specified (#501)
+* Make it usable with old VC8 (VS2005) (#383)
+* Prohibit C++11 move from Document to Value (#391)
+* Try to fix incorrect 64-bit alignment (#419)
+* Check return of fwrite to avoid warn_unused_result build failures (#421)
+* Fix UB in GenericDocument::ParseStream (#426)
+* Keep Document value unchanged on parse error (#439)
+* Add missing return statement (#450)
+* Fix Document::Parse(const Ch*) for transcoding (#478)
+* encodings.h: fix typo in preprocessor condition (#495)
+* Custom Microsoft headers are necessary only for Visual Studio 2012 and lower (#559)
+* Fix memory leak for invalid regex (26e69ffde95ba4773ab06db6457b78f308716f4b)
+* Fix a bug in schema minimum/maximum keywords for 64-bit integer (e7149d665941068ccf8c565e77495521331cf390)
+* Fix a crash bug in regex (#605)
+* Fix schema "required" keyword cannot handle duplicated keys (#609)
+* Fix cmake CMP0054 warning (#612)
+* Added missing include guards in istreamwrapper.h and ostreamwrapper.h (#634)
+* Fix undefined behaviour (#646)
+* Fix buffer overrun using PutN (#673)
+* Fix rapidjson::value::Get<std::string>() may returns wrong data (#681)
+* Add Flush() for all value types (#689)
+* Handle malloc() fail in PoolAllocator (#691)
+* Fix builds on x32 platform. #703
+
+### Changed
+* Clarify problematic JSON license (#392)
+* Move Travis to container based infrastructure (#504, #558)
+* Make whitespace array more compact (#513)
+* Optimize Writer::WriteString() with SIMD (#544)
+* x86-64 48-bit pointer optimization for GenericValue (#546)
+* Define RAPIDJSON_HAS_CXX11_RVALUE_REFS directly in clang (#617)
+* Make GenericSchemaDocument constructor explicit (#674)
+* Optimize FindMember when use std::string (#690)
+
## [1.0.2] - 2015-05-14
### Added
@@ -12,6 +89,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
### Fixed
* Include rapidjson.h for all internal/error headers.
* Parsing some numbers incorrectly in full-precision mode (`kFullPrecisionParseFlag`) (#342)
+* Fix some numbers parsed incorrectly (#336)
* Fix alignment of 64bit platforms (#328)
* Fix MemoryPoolAllocator::Clear() to clear user-buffer (0691502573f1afd3341073dd24b12c3db20fbde4)
@@ -31,7 +109,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
## [1.0.0] - 2015-04-22
### Added
-* 100% [Coverall](https://coveralls.io/r/miloyip/rapidjson?branch=master) coverage.
+* 100% [Coverall](https://coveralls.io/r/Tencent/rapidjson?branch=master) coverage.
* Version macros (#311)
### Fixed
@@ -62,7 +140,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
* Redo all documentation (English, Simplified Chinese)
### Changed
-* Copyright ownership transfered to THL A29 Limited (a Tencent company).
+* Copyright ownership transferred to THL A29 Limited (a Tencent company).
* Migrating from Premake to CMAKE (#192)
* Resolve all warning reports
@@ -73,7 +151,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
## 0.1 - 2011-11-18
-[Unreleased]: https://github.com/miloyip/rapidjson/compare/v1.0.2...HEAD
-[1.0.2]: https://github.com/miloyip/rapidjson/compare/v1.0.1...v1.0.2
-[1.0.1]: https://github.com/miloyip/rapidjson/compare/v1.0.0...v1.0.1
-[1.0.0]: https://github.com/miloyip/rapidjson/compare/v1.0-beta...v1.0.0
+[Unreleased]: https://github.com/Tencent/rapidjson/compare/v1.1.0...HEAD
+[1.1.0]: https://github.com/Tencent/rapidjson/compare/v1.0.2...v1.1.0
+[1.0.2]: https://github.com/Tencent/rapidjson/compare/v1.0.1...v1.0.2
+[1.0.1]: https://github.com/Tencent/rapidjson/compare/v1.0.0...v1.0.1
+[1.0.0]: https://github.com/Tencent/rapidjson/compare/v1.0-beta...v1.0.0
diff --git a/CMakeLists.txt b/CMakeLists.txt
index fa2bdcfe..8d69855f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,11 +1,19 @@
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
+if(POLICY CMP0025)
+ # detect Apple's Clang
+ cmake_policy(SET CMP0025 NEW)
+endif()
+if(POLICY CMP0054)
+ cmake_policy(SET CMP0054 NEW)
+endif()
+
SET(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/CMakeModules)
PROJECT(RapidJSON CXX)
set(LIB_MAJOR_VERSION "1")
-set(LIB_MINOR_VERSION "0")
-set(LIB_PATCH_VERSION "2")
+set(LIB_MINOR_VERSION "1")
+set(LIB_PATCH_VERSION "0")
set(LIB_VERSION_STRING "${LIB_MAJOR_VERSION}.${LIB_MINOR_VERSION}.${LIB_PATCH_VERSION}")
# compile in release with debug info mode by default
@@ -24,6 +32,11 @@ option(RAPIDJSON_BUILD_THIRDPARTY_GTEST
option(RAPIDJSON_BUILD_CXX11 "Build rapidjson with C++11 (gcc/clang)" ON)
+option(RAPIDJSON_BUILD_ASAN "Build rapidjson with address sanitizer (gcc/clang)" OFF)
+option(RAPIDJSON_BUILD_UBSAN "Build rapidjson with undefined behavior sanitizer (gcc/clang)" OFF)
+
+option(RAPIDJSON_ENABLE_INSTRUMENTATION_OPT "Build rapidjson with -march or -mcpu options" ON)
+
option(RAPIDJSON_HAS_STDSTRING "" OFF)
if(RAPIDJSON_HAS_STDSTRING)
add_definitions(-DRAPIDJSON_HAS_STDSTRING)
@@ -39,7 +52,16 @@ if(CCACHE_FOUND)
endif(CCACHE_FOUND)
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
- set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native -Wall -Wextra -Werror")
+ if(${RAPIDJSON_ENABLE_INSTRUMENTATION_OPT})
+ if("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "powerpc" OR "${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "ppc64" OR "${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "ppc64le")
+ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mcpu=native")
+ else()
+ #FIXME: x86 is -march=native, but doesn't mean every arch is this option. To keep original project's compatibility, I leave this except POWER.
+ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native")
+ endif()
+ endif()
+ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Werror")
+ set(EXTRA_CXX_FLAGS -Weffc++ -Wswitch-default -Wfloat-equal -Wconversion -Wsign-conversion)
if (RAPIDJSON_BUILD_CXX11)
if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "4.7.0")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
@@ -47,13 +69,47 @@ if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
endif()
endif()
+ if (RAPIDJSON_BUILD_ASAN)
+ if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "4.8.0")
+ message(FATAL_ERROR "GCC < 4.8 doesn't support the address sanitizer")
+ else()
+ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address")
+ endif()
+ endif()
+ if (RAPIDJSON_BUILD_UBSAN)
+ if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "4.9.0")
+ message(FATAL_ERROR "GCC < 4.9 doesn't support the undefined behavior sanitizer")
+ else()
+ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined")
+ endif()
+ endif()
elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
- set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native -Wall -Wextra -Werror -Wno-missing-field-initializers")
+ if("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "powerpc" OR "${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "ppc64" OR "${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "ppc64le")
+ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mcpu=native")
+ else()
+ #FIXME: x86 is -march=native, but doesn't mean every arch is this option. To keep original project's compatibility, I leave this except POWER.
+ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native")
+ endif()
+ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Werror -Wno-missing-field-initializers")
+ set(EXTRA_CXX_FLAGS -Weffc++ -Wswitch-default -Wfloat-equal -Wconversion -Wimplicit-fallthrough)
if (RAPIDJSON_BUILD_CXX11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
endif()
+ if (RAPIDJSON_BUILD_ASAN)
+ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address")
+ endif()
+ if (RAPIDJSON_BUILD_UBSAN)
+ if (CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
+ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined-trap -fsanitize-undefined-trap-on-error")
+ else()
+ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined")
+ endif()
+ endif()
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
add_definitions(-D_CRT_SECURE_NO_WARNINGS=1)
+ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc")
+elseif (CMAKE_CXX_COMPILER_ID MATCHES "XL")
+ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -qarch=auto")
endif()
#add extra search paths for libraries and includes
@@ -121,15 +177,37 @@ install(DIRECTORY example/
# Provide config and version files to be used by other applications
# ===============================
-export(PACKAGE ${PROJECT_NAME})
-
-# cmake-modules
-CONFIGURE_FILE(${PROJECT_NAME}Config.cmake.in
- ${PROJECT_NAME}Config.cmake
- @ONLY)
-CONFIGURE_FILE(${PROJECT_NAME}ConfigVersion.cmake.in
- ${PROJECT_NAME}ConfigVersion.cmake
- @ONLY)
+################################################################################
+# Export package for use from the build tree
+EXPORT( PACKAGE ${PROJECT_NAME} )
+
+# Create the RapidJSONConfig.cmake file for other cmake projects.
+# ... for the build tree
+SET( CONFIG_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
+SET( CONFIG_DIR ${CMAKE_CURRENT_BINARY_DIR})
+SET( ${PROJECT_NAME}_INCLUDE_DIR "\${${PROJECT_NAME}_SOURCE_DIR}/include" )
+
+CONFIGURE_FILE( ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}Config.cmake.in
+ ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake @ONLY )
+CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}ConfigVersion.cmake.in
+ ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake @ONLY)
+
+# ... for the install tree
+SET( CMAKECONFIG_INSTALL_DIR lib/cmake/${PROJECT_NAME} )
+FILE( RELATIVE_PATH REL_INCLUDE_DIR
+ "${CMAKE_INSTALL_PREFIX}/${CMAKECONFIG_INSTALL_DIR}"
+ "${CMAKE_INSTALL_PREFIX}/include" )
+
+SET( ${PROJECT_NAME}_INCLUDE_DIR "\${${PROJECT_NAME}_CMAKE_DIR}/${REL_INCLUDE_DIR}" )
+SET( CONFIG_SOURCE_DIR )
+SET( CONFIG_DIR )
+CONFIGURE_FILE( ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}Config.cmake.in
+ ${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/${PROJECT_NAME}Config.cmake @ONLY )
+
+INSTALL(FILES "${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/${PROJECT_NAME}Config.cmake"
+ DESTINATION ${CMAKECONFIG_INSTALL_DIR} )
+
+# Install files
INSTALL(FILES
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
diff --git a/CMakeModules/FindGTestSrc.cmake b/CMakeModules/FindGTestSrc.cmake
index f942a8da..f3cb8c99 100644
--- a/CMakeModules/FindGTestSrc.cmake
+++ b/CMakeModules/FindGTestSrc.cmake
@@ -1,7 +1,7 @@
SET(GTEST_SEARCH_PATH
"${GTEST_SOURCE_DIR}"
- "${CMAKE_CURRENT_LIST_DIR}/../thirdparty/gtest")
+ "${CMAKE_CURRENT_LIST_DIR}/../thirdparty/gtest/googletest")
IF(UNIX)
IF(RAPIDJSON_BUILD_THIRDPARTY_GTEST)
diff --git a/RapidJSON.pc.in b/RapidJSON.pc.in
index 7467f977..6afb079f 100644
--- a/RapidJSON.pc.in
+++ b/RapidJSON.pc.in
@@ -3,5 +3,5 @@ includedir=@INCLUDE_INSTALL_DIR@
Name: @PROJECT_NAME@
Description: A fast JSON parser/generator for C++ with both SAX/DOM style API
Version: @LIB_VERSION_STRING@
-URL: https://github.com/miloyip/rapidjson
+URL: https://github.com/Tencent/rapidjson
Cflags: -I${includedir}
diff --git a/RapidJSONConfig.cmake.in b/RapidJSONConfig.cmake.in
index 9fa12186..e3c65a54 100644
--- a/RapidJSONConfig.cmake.in
+++ b/RapidJSONConfig.cmake.in
@@ -1,3 +1,15 @@
-get_filename_component(RAPIDJSON_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
-set(RAPIDJSON_INCLUDE_DIRS "@INCLUDE_INSTALL_DIR@")
-message(STATUS "RapidJSON found. Headers: ${RAPIDJSON_INCLUDE_DIRS}")
+################################################################################
+# RapidJSON source dir
+set( RapidJSON_SOURCE_DIR "@CONFIG_SOURCE_DIR@")
+
+################################################################################
+# RapidJSON build dir
+set( RapidJSON_DIR "@CONFIG_DIR@")
+
+################################################################################
+# Compute paths
+get_filename_component(RapidJSON_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
+
+set( RapidJSON_INCLUDE_DIR "@RapidJSON_INCLUDE_DIR@" )
+set( RapidJSON_INCLUDE_DIRS "@RapidJSON_INCLUDE_DIR@" )
+message(STATUS "RapidJSON found. Headers: ${RapidJSON_INCLUDE_DIRS}")
diff --git a/appveyor.yml b/appveyor.yml
index 13d8b94d..dfedf9c2 100644
--- a/appveyor.yml
+++ b/appveyor.yml
@@ -1,5 +1,5 @@
os: Visual Studio 2015 CTP
-version: 1.0.2.{build}
+version: 1.1.0.{build}
configuration:
- Debug
@@ -30,7 +30,7 @@ environment:
before_build:
- git submodule update --init --recursive
-- cmake -H. -BBuild/VS -G "Visual Studio %VS_VERSION%" -DCMAKE_GENERATOR_PLATFORM=%VS_PLATFORM% -DBUILD_SHARED_LIBS=true -Wno-dev
+- cmake -H. -BBuild/VS -G "Visual Studio %VS_VERSION%" -DCMAKE_GENERATOR_PLATFORM=%VS_PLATFORM% -DCMAKE_VERBOSE_MAKEFILE=ON -DBUILD_SHARED_LIBS=true -Wno-dev
build:
project: Build\VS\RapidJSON.sln
diff --git a/bin/jsonschema/remotes/.DS_Store b/bin/jsonschema/remotes/.DS_Store
deleted file mode 100644
index 1d098a41..00000000
--- a/bin/jsonschema/remotes/.DS_Store
+++ /dev/null
Binary files differ
diff --git a/bin/jsonschema/tests/.DS_Store b/bin/jsonschema/tests/.DS_Store
deleted file mode 100644
index dae9b18e..00000000
--- a/bin/jsonschema/tests/.DS_Store
+++ /dev/null
Binary files differ
diff --git a/bin/jsonschema/tests/draft4/.DS_Store b/bin/jsonschema/tests/draft4/.DS_Store
deleted file mode 100644
index ef142295..00000000
--- a/bin/jsonschema/tests/draft4/.DS_Store
+++ /dev/null
Binary files differ
diff --git a/bin/types/booleans.json b/bin/types/booleans.json
index 2dcbb5fe..2dcbb5fe 100755..100644
--- a/bin/types/booleans.json
+++ b/bin/types/booleans.json
diff --git a/bin/types/floats.json b/bin/types/floats.json
index 12b94a11..12b94a11 100755..100644
--- a/bin/types/floats.json
+++ b/bin/types/floats.json
diff --git a/bin/types/guids.json b/bin/types/guids.json
index 9d7f5dbc..9d7f5dbc 100755..100644
--- a/bin/types/guids.json
+++ b/bin/types/guids.json
diff --git a/bin/types/integers.json b/bin/types/integers.json
index 5dd05e09..5dd05e09 100755..100644
--- a/bin/types/integers.json
+++ b/bin/types/integers.json
diff --git a/bin/types/mixed.json b/bin/types/mixed.json
index 43e9a1d7..43e9a1d7 100755..100644
--- a/bin/types/mixed.json
+++ b/bin/types/mixed.json
diff --git a/bin/types/nulls.json b/bin/types/nulls.json
index 7a636ec8..7a636ec8 100755..100644
--- a/bin/types/nulls.json
+++ b/bin/types/nulls.json
diff --git a/bin/types/paragraphs.json b/bin/types/paragraphs.json
index 8ab3e1c5..8ab3e1c5 100755..100644
--- a/bin/types/paragraphs.json
+++ b/bin/types/paragraphs.json
diff --git a/contrib/natvis/LICENSE b/contrib/natvis/LICENSE
new file mode 100644
index 00000000..f57da96c
--- /dev/null
+++ b/contrib/natvis/LICENSE
@@ -0,0 +1,45 @@
+The MIT License (MIT)
+
+Copyright (c) 2017 Bart Muzzin
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+
+Derived from:
+
+The MIT License (MIT)
+
+Copyright (c) 2015 mojmir svoboda
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
diff --git a/contrib/natvis/README.md b/contrib/natvis/README.md
new file mode 100644
index 00000000..9685c7f7
--- /dev/null
+++ b/contrib/natvis/README.md
@@ -0,0 +1,7 @@
+# rapidjson.natvis
+
+This file can be used as a [Visual Studio Visualizer](https://docs.microsoft.com/en-gb/visualstudio/debugger/create-custom-views-of-native-objects) to aid in visualizing rapidjson structures within the Visual Studio debugger. Natvis visualizers are supported in Visual Studio 2012 and later. To install, copy the file into this directory:
+
+`%USERPROFILE%\Documents\Visual Studio 2012\Visualizers`
+
+Each version of Visual Studio has a similar directory, it must be copied into each directory to be used with that particular version. In Visual Studio 2015 and later, this can be done without restarting Visual Studio (a new debugging session must be started).
diff --git a/contrib/natvis/rapidjson.natvis b/contrib/natvis/rapidjson.natvis
new file mode 100644
index 00000000..a804b7bf
--- /dev/null
+++ b/contrib/natvis/rapidjson.natvis
@@ -0,0 +1,38 @@
+<?xml version="1.0" encoding="utf-8"?>
+<AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">
+ <!-- rapidjson::GenericValue - basic support -->
+ <Type Name="rapidjson::GenericValue&lt;*,*&gt;">
+ <DisplayString Condition="(data_.f.flags &amp; kTypeMask) == kNullType">null</DisplayString>
+ <DisplayString Condition="data_.f.flags == kTrueFlag">true</DisplayString>
+ <DisplayString Condition="data_.f.flags == kFalseFlag">false</DisplayString>
+ <DisplayString Condition="data_.f.flags == kShortStringFlag">{data_.ss.str}</DisplayString>
+ <DisplayString Condition="(data_.f.flags &amp; kTypeMask) == kStringType">{(const char*)((size_t)data_.s.str &amp; 0x0000FFFFFFFFFFFF)}</DisplayString>
+ <DisplayString Condition="(data_.f.flags &amp; kNumberIntFlag) == kNumberIntFlag">{data_.n.i.i}</DisplayString>
+ <DisplayString Condition="(data_.f.flags &amp; kNumberUintFlag) == kNumberUintFlag">{data_.n.u.u}</DisplayString>
+ <DisplayString Condition="(data_.f.flags &amp; kNumberInt64Flag) == kNumberInt64Flag">{data_.n.i64}</DisplayString>
+ <DisplayString Condition="(data_.f.flags &amp; kNumberUint64Flag) == kNumberUint64Flag">{data_.n.u64}</DisplayString>
+ <DisplayString Condition="(data_.f.flags &amp; kNumberDoubleFlag) == kNumberDoubleFlag">{data_.n.d}</DisplayString>
+ <DisplayString Condition="data_.f.flags == kObjectType">Object members={data_.o.size}</DisplayString>
+ <DisplayString Condition="data_.f.flags == kArrayType">Array members={data_.a.size}</DisplayString>
+ <Expand>
+ <Item Condition="data_.f.flags == kObjectType" Name="[size]">data_.o.size</Item>
+ <Item Condition="data_.f.flags == kObjectType" Name="[capacity]">data_.o.capacity</Item>
+ <ArrayItems Condition="data_.f.flags == kObjectType">
+ <Size>data_.o.size</Size>
+ <!-- NOTE: Rapidjson stores some extra data in the high bits of pointers, which is why the mask -->
+ <ValuePointer>(rapidjson::GenericMember&lt;$T1,$T2&gt;*)(((size_t)data_.o.members) &amp; 0x0000FFFFFFFFFFFF)</ValuePointer>
+ </ArrayItems>
+
+ <Item Condition="data_.f.flags == kArrayType" Name="[size]">data_.a.size</Item>
+ <Item Condition="data_.f.flags == kArrayType" Name="[capacity]">data_.a.capacity</Item>
+ <ArrayItems Condition="data_.f.flags == kArrayType">
+ <Size>data_.a.size</Size>
+ <!-- NOTE: Rapidjson stores some extra data in the high bits of pointers, which is why the mask -->
+ <ValuePointer>(rapidjson::GenericValue&lt;$T1,$T2&gt;*)(((size_t)data_.a.elements) &amp; 0x0000FFFFFFFFFFFF)</ValuePointer>
+ </ArrayItems>
+
+ </Expand>
+ </Type>
+
+</AutoVisualizer>
+
diff --git a/doc/CMakeLists.txt b/doc/CMakeLists.txt
index c1f165a3..c5345ba6 100644
--- a/doc/CMakeLists.txt
+++ b/doc/CMakeLists.txt
@@ -10,11 +10,13 @@ ELSE()
CONFIGURE_FILE(Doxyfile.in Doxyfile @ONLY)
CONFIGURE_FILE(Doxyfile.zh-cn.in Doxyfile.zh-cn @ONLY)
+ file(GLOB DOXYFILES ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile*)
+
add_custom_command(OUTPUT html
COMMAND ${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile
COMMAND ${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile.zh-cn
COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/html
- DEPENDS ${MARKDOWN_DOC} ${SOURCES} ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile*
+ DEPENDS ${MARKDOWN_DOC} ${SOURCES} ${DOXYFILES}
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/../
)
diff --git a/doc/Doxyfile.in b/doc/Doxyfile.in
index fcb09266..ca142339 100644
--- a/doc/Doxyfile.in
+++ b/doc/Doxyfile.in
@@ -765,6 +765,7 @@ WARN_LOGFILE =
# Note: If this tag is empty the current directory is searched.
INPUT = readme.md \
+ CHANGELOG.md \
include/rapidjson/rapidjson.h \
include/ \
doc/features.md \
diff --git a/doc/Doxyfile.zh-cn.in b/doc/Doxyfile.zh-cn.in
index 76d828b5..e7fffa67 100644
--- a/doc/Doxyfile.zh-cn.in
+++ b/doc/Doxyfile.zh-cn.in
@@ -765,6 +765,7 @@ WARN_LOGFILE =
# Note: If this tag is empty the current directory is searched.
INPUT = readme.zh-cn.md \
+ CHANGELOG.md \
include/rapidjson/rapidjson.h \
include/ \
doc/features.zh-cn.md \
@@ -776,7 +777,7 @@ INPUT = readme.zh-cn.md \
doc/sax.zh-cn.md \
doc/schema.zh-cn.md \
doc/performance.zh-cn.md \
- doc/internals.md \
+ doc/internals.zh-cn.md \
doc/faq.zh-cn.md
# This tag can be used to specify the character encoding of the source files
diff --git a/doc/diagram/move2.dot b/doc/diagram/move2.dot
index 7037ea6c..2319871b 100644
--- a/doc/diagram/move2.dot
+++ b/doc/diagram/move2.dot
@@ -18,7 +18,7 @@ digraph {
node [shape=Mrecord, style=filled, colorscheme=spectral7]
- c1 [label="{contact:array|}", fillcolor=4]
+ c1 [label="{contacts:array|}", fillcolor=4]
c11 [label="{|}"]
c12 [label="{|}"]
c13 [shape="none", label="...", style="solid"]
@@ -41,13 +41,13 @@ digraph {
node [shape=Mrecord, style=filled, colorscheme=spectral7]
- c2 [label="{contact:array|}", fillcolor=4]
+ c2 [label="{contacts:array|}", fillcolor=4]
c3 [label="{array|}", fillcolor=4]
c21 [label="{|}"]
c22 [label="{|}"]
c23 [shape=none, label="...", style="solid"]
o2 [label="{o:object|}", fillcolor=3]
- cs [label="{string|\"contact\"}", fillcolor=5]
+ cs [label="{string|\"contacts\"}", fillcolor=5]
c31 [label="{|}"]
c32 [label="{|}"]
c33 [shape="none", label="...", style="solid"]
@@ -59,4 +59,4 @@ digraph {
c3 -> { c31; c32; c33 }
}
ghost -> o2 [style=invis]
-} \ No newline at end of file
+}
diff --git a/doc/diagram/move3.dot b/doc/diagram/move3.dot
index c197b99d..57adb4f9 100644
--- a/doc/diagram/move3.dot
+++ b/doc/diagram/move3.dot
@@ -19,7 +19,7 @@ digraph {
node [shape=Mrecord, style=filled, colorscheme=spectral7]
- c1 [label="{contact:array|}", fillcolor=4]
+ c1 [label="{contacts:array|}", fillcolor=4]
c11 [label="{|}"]
c12 [label="{|}"]
c13 [shape=none, label="...", style="solid"]
@@ -42,13 +42,13 @@ digraph {
node [shape=Mrecord, style=filled, colorscheme=spectral7]
- c2 [label="{contact:null|}", fillcolor=1]
+ c2 [label="{contacts:null|}", fillcolor=1]
c3 [label="{array|}", fillcolor=4]
c21 [label="{|}"]
c22 [label="{|}"]
c23 [shape="none", label="...", style="solid"]
o2 [label="{o:object|}", fillcolor=3]
- cs [label="{string|\"contact\"}", fillcolor=5]
+ cs [label="{string|\"contacts\"}", fillcolor=5]
c2 -> o2 [style="dashed", constraint=false, label="AddMember", style=invis]
edge [arrowhead=vee]
@@ -57,4 +57,4 @@ digraph {
cs -> c3 [arrowhead=none]
}
ghost -> o2 [style=invis]
-} \ No newline at end of file
+}
diff --git a/doc/dom.md b/doc/dom.md
index cb25fc4f..0079b64b 100644
--- a/doc/dom.md
+++ b/doc/dom.md
@@ -116,6 +116,9 @@ Parse flags | Meaning
`kParseStopWhenDoneFlag` | After parsing a complete JSON root from stream, stop further processing the rest of stream. When this flag is used, parser will not generate `kParseErrorDocumentRootNotSingular` error. Using this flag for parsing multiple JSONs in the same stream.
`kParseFullPrecisionFlag` | Parse number in full precision (slower). If this flag is not set, the normal precision (faster) is used. Normal precision has maximum 3 [ULP](http://en.wikipedia.org/wiki/Unit_in_the_last_place) error.
`kParseCommentsFlag` | Allow one-line `// ...` and multi-line `/* ... */` comments (relaxed JSON syntax).
+`kParseNumbersAsStringsFlag` | Parse numerical type values as strings.
+`kParseTrailingCommasFlag` | Allow trailing commas at the end of objects and arrays (relaxed JSON syntax).
+`kParseNanAndInfFlag` | Allow parsing `NaN`, `Inf`, `Infinity`, `-Inf` and `-Infinity` as `double` values (relaxed JSON syntax).
By using a non-type template parameter, instead of a function parameter, C++ compiler can generate code which is optimized for specified combinations, improving speed, and reducing code size (if only using a single specialization). The downside is the flags needed to be determined in compile-time.
@@ -125,7 +128,7 @@ And the `InputStream` is type of input stream.
## Parse Error {#ParseError}
-When the parse processing succeeded, the `Document` contains the parse results. When there is an error, the original DOM is *unchanged*. And the error state of parsing can be obtained by `bool HasParseError()`, `ParseErrorCode GetParseError()` and `size_t GetParseOffset()`.
+When the parse processing succeeded, the `Document` contains the parse results. When there is an error, the original DOM is *unchanged*. And the error state of parsing can be obtained by `bool HasParseError()`, `ParseErrorCode GetParseError()` and `size_t GetErrorOffset()`.
Parse Error Code | Description
--------------------------------------------|---------------------------------------------------
@@ -161,7 +164,7 @@ Document d;
if (d.Parse(json).HasParseError()) {
fprintf(stderr, "\nError(offset %u): %s\n",
(unsigned)d.GetErrorOffset(),
- GetParseError_En(d.GetParseErrorCode()));
+ GetParseError_En(d.GetParseError()));
// ...
}
~~~~~~~~~~
@@ -238,7 +241,7 @@ Some techniques about using DOM API is discussed here.
## DOM as SAX Event Publisher
-In RapidJSON, stringifying a DOM with `Writer` may be look a little bit weired.
+In RapidJSON, stringifying a DOM with `Writer` may be look a little bit weird.
~~~~~~~~~~cpp
// ...
diff --git a/doc/dom.zh-cn.md b/doc/dom.zh-cn.md
index 2adf3435..9743b7ac 100644
--- a/doc/dom.zh-cn.md
+++ b/doc/dom.zh-cn.md
@@ -1,12 +1,12 @@
# DOM
-文档对象模型(Document Object Model, DOM)是一ç§ç½äºŽå†…存中的JSON表示方å¼ï¼Œä»¥ä¾›æŸ¥è¯¢åŠæ“作。我们己于[教程](doc/tutorial.md)中介ç»äº†DOM的基本用法,本节将讲述一些细节åŠé«˜çº§ç”¨æ³•ã€‚
+文档对象模型(Document Object Model, DOM)是一ç§ç½äºŽå†…存中的 JSON 表示方å¼ï¼Œä»¥ä¾›æŸ¥è¯¢åŠæ“作。我们己于 [教程](doc/tutorial.zh-cn.md) 中介ç»äº† DOM 的基本用法,本节将讲述一些细节åŠé«˜çº§ç”¨æ³•ã€‚
[TOC]
# æ¨¡æ¿ {#Template}
-教程中使用了`Value`å’Œ`Document`类型。与`std::string`相似,这些类型其实是两个模æ¿ç±»çš„`typedef`:
+教程中使用了 `Value` å’Œ `Document` 类型。与 `std::string` 相似,这些类型其实是两个模æ¿ç±»çš„ `typedef`:
~~~~~~~~~~cpp
namespace rapidjson {
@@ -31,9 +31,9 @@ typedef GenericDocument<UTF8<> > Document;
## ç¼–ç  {#Encoding}
-`Encoding`å‚数指明在内存中的JSON String使用哪ç§ç¼–ç ã€‚å¯è¡Œçš„选项有`UTF8`ã€`UTF16`ã€`UTF32`。è¦æ³¨æ„è¿™3个类型其实也是模æ¿ç±»ã€‚`UTF8<>`ç­‰åŒ`UTF8<char>`,这代表它使用`char`æ¥å­˜å‚¨å­—符串。更多细节å¯ä»¥å‚考[ç¼–ç ](encoding.md)。
+`Encoding` å‚数指明在内存中的 JSON String 使用哪ç§ç¼–ç ã€‚å¯è¡Œçš„选项有 `UTF8`ã€`UTF16`ã€`UTF32`。è¦æ³¨æ„è¿™ 3 个类型其实也是模æ¿ç±»ã€‚`UTF8<>` ç­‰åŒ `UTF8<char>`,这代表它使用 `char` æ¥å­˜å‚¨å­—符串。更多细节å¯ä»¥å‚考 [ç¼–ç ](doc/encoding.zh-cn.md)。
-这里是一个例å­ã€‚å‡è®¾ä¸€ä¸ªWindows应用软件希望查询存储于JSON中的本地化字符串。Windows中å«Unicode的函数使用UTF-16(宽字符)编ç ã€‚无论JSON文件使用哪ç§ç¼–ç ï¼Œæˆ‘们都å¯ä»¥æŠŠå­—符串以UTF-16å½¢å¼å­˜å‚¨åœ¨å†…存。
+这里是一个例å­ã€‚å‡è®¾ä¸€ä¸ª Windows 应用软件希望查询存储于 JSON 中的本地化字符串。Windows ä¸­å« Unicode 的函数使用 UTF-16(宽字符)编ç ã€‚无论 JSON 文件使用哪ç§ç¼–ç ï¼Œæˆ‘们都å¯ä»¥æŠŠå­—符串以 UTF-16 å½¢å¼å­˜å‚¨åœ¨å†…存。
~~~~~~~~~~cpp
using namespace rapidjson;
@@ -41,12 +41,12 @@ using namespace rapidjson;
typedef GenericDocument<UTF16<> > WDocument;
typedef GenericValue<UTF16<> > WValue;
-FILE* fp = fopen("localization.json", "rb"); // éžWindowså¹³å°ä½¿ç”¨"r"
+FILE* fp = fopen("localization.json", "rb"); // éž Windows å¹³å°ä½¿ç”¨ "r"
char readBuffer[256];
FileReadStream bis(fp, readBuffer, sizeof(readBuffer));
-AutoUTFInputStream<unsigned, FileReadStream> eis(bis); // 包装bisæˆeis
+AutoUTFInputStream<unsigned, FileReadStream> eis(bis); // 包装 bis æˆ eis
WDocument d;
d.ParseStream<0, AutoUTF<unsigned> >(eis);
@@ -58,15 +58,15 @@ MessageBoxW(hWnd, d[locale].GetString(), L"Test", MB_OK);
## 分é…器 {#Allocator}
-`Allocator`定义当`Document`/`Value`分é…或释放内存时使用那个分é…类。`Document`拥有或引用到一个`Allocator`实例。而为了节çœå†…存,`Value`没有这么åšã€‚
+`Allocator` 定义当 `Document`/`Value` 分é…或释放内存时使用那个分é…类。`Document` 拥有或引用到一个 `Allocator` 实例。而为了节çœå†…存,`Value` 没有这么åšã€‚
-`GenericDocument`的缺çœåˆ†é…器是`MemoryPoolAllocator`。此分é…器实际上会顺åºåœ°åˆ†é…内存,并且ä¸èƒ½é€ä¸€é‡Šæ”¾ã€‚当è¦è§£æžä¸€ä¸ªJSON并生æˆDOM,这ç§åˆ†é…器是éžå¸¸åˆé€‚的。
+`GenericDocument` 的缺çœåˆ†é…器是 `MemoryPoolAllocator`。此分é…器实际上会顺åºåœ°åˆ†é…内存,并且ä¸èƒ½é€ä¸€é‡Šæ”¾ã€‚当è¦è§£æžä¸€ä¸ª JSON å¹¶ç”Ÿæˆ DOM,这ç§åˆ†é…器是éžå¸¸åˆé€‚的。
-RapidJSON还æä¾›å¦ä¸€ä¸ªåˆ†é…器`CrtAllocator`,当中CRT是Cè¿è¡Œåº“(C RunTime library)的缩写。此分é…器简å•åœ°è¯»ç”¨æ ‡å‡†çš„`malloc()`/`realloc()`/`free()`。当我们需è¦è®¸å¤šå¢žå‡æ“作,这ç§åˆ†é…器会更为适åˆã€‚然而这ç§åˆ†é…器远远比`MemoryPoolAllocator`低效。
+RapidJSON 还æä¾›å¦ä¸€ä¸ªåˆ†é…器 `CrtAllocator`,当中 CRT 是 C è¿è¡Œåº“(C RunTime library)的缩写。此分é…器简å•åœ°è¯»ç”¨æ ‡å‡†çš„ `malloc()`/`realloc()`/`free()`。当我们需è¦è®¸å¤šå¢žå‡æ“作,这ç§åˆ†é…器会更为适åˆã€‚然而这ç§åˆ†é…器远远比 `MemoryPoolAllocator` 低效。
# è§£æž {#Parsing}
-`Document`æ供几个解æžå‡½æ•°ã€‚以下的(1)是根本的函数,其他都是调用(1)çš„å助函数。
+`Document` æ供几个解æžå‡½æ•°ã€‚以下的 (1) 是根本的函数,其他都是调用 (1) çš„å助函数。
~~~~~~~~~~cpp
using namespace rapidjson;
@@ -94,7 +94,7 @@ GenericDocument& GenericDocument::ParseInsitu(Ch* str);
template <unsigned parseFlags, typename SourceEncoding>
GenericDocument& GenericDocument::Parse(const Ch* str);
-// (7) 正常解æžä¸€ä¸ªå­—符串,使用Documentçš„ç¼–ç 
+// (7) 正常解æžä¸€ä¸ªå­—符串,使用 Document çš„ç¼–ç 
template <unsigned parseFlags>
GenericDocument& GenericDocument::Parse(const Ch* str);
@@ -102,30 +102,33 @@ GenericDocument& GenericDocument::Parse(const Ch* str);
GenericDocument& GenericDocument::Parse(const Ch* str);
~~~~~~~~~~
-[教程](tutorial.md)中的例使用(8)去正常解æžå­—符串。而[æµ](stream.md)的例å­ä½¿ç”¨å‰3个函数。我们将ç¨åŽä»‹ç»åŽŸä½ï¼ˆ*In situ*) 解æžã€‚
+[教程](doc/tutorial.zh-cn.md) 中的例使用 (8) 去正常解æžå­—符串。而 [æµ](doc/stream.zh-cn.md) 的例å­ä½¿ç”¨å‰ 3 个函数。我们将ç¨åŽä»‹ç»åŽŸä½ï¼ˆ*In situ*) 解æžã€‚
-`parseFlags`是以下ä½æ ‡ç½®çš„组åˆï¼š
+`parseFlags` 是以下ä½æ ‡ç½®çš„组åˆï¼š
解æžä½æ ‡å¿— | æ„义
------------------------------|-----------------------------------
`kParseNoFlags` | 没有任何标志。
-`kParseDefaultFlags` | 缺çœçš„解æžé€‰é¡¹ã€‚它等于`RAPIDJSON_PARSE_DEFAULT_FLAGS`å®ï¼Œæ­¤å®å®šä¹‰ä¸º`kParseNoFlags`。
+`kParseDefaultFlags` | 缺çœçš„解æžé€‰é¡¹ã€‚它等于 `RAPIDJSON_PARSE_DEFAULT_FLAGS` å®ï¼Œæ­¤å®å®šä¹‰ä¸º `kParseNoFlags`。
`kParseInsituFlag` | 原ä½ï¼ˆç ´å性)解æžã€‚
-`kParseValidateEncodingFlag` | 校验JSON字符串的编ç ã€‚
+`kParseValidateEncodingFlag` | 校验 JSON 字符串的编ç ã€‚
`kParseIterativeFlag` | 迭代å¼ï¼ˆè°ƒç”¨å †æ ˆå¤§å°ä¸ºå¸¸æ•°å¤æ‚度)解æžã€‚
-`kParseStopWhenDoneFlag` | 当从æµè§£æžäº†ä¸€ä¸ªå®Œæ•´çš„JSON根节点之åŽï¼Œåœæ­¢ç»§ç»­å¤„ç†ä½™ä¸‹çš„æµã€‚当使用了此标志,解æžå™¨ä¾¿ä¸ä¼šäº§ç”Ÿ`kParseErrorDocumentRootNotSingular`错误。å¯ä½¿ç”¨æœ¬æ ‡å¿—去解æžåŒä¸€ä¸ªæµé‡Œçš„多个JSON。
-`kParseFullPrecisionFlag` | 使用完整的精确度去解æžæ•°å­—(较慢)。如ä¸è®¾ç½®æ­¤æ ‡èŠ‚,则会使用正常的精确度(较快)。正常精确度会有最多3个[ULP](http://en.wikipedia.org/wiki/Unit_in_the_last_place)的误差。
-`kParseCommentsFlag` | 容许å•è¡Œ `// ...` åŠå¤šè¡Œ `/* ... */` 注释(放宽的JSON语法)。
+`kParseStopWhenDoneFlag` | 当从æµè§£æžäº†ä¸€ä¸ªå®Œæ•´çš„ JSON 根节点之åŽï¼Œåœæ­¢ç»§ç»­å¤„ç†ä½™ä¸‹çš„æµã€‚当使用了此标志,解æžå™¨ä¾¿ä¸ä¼šäº§ç”Ÿ `kParseErrorDocumentRootNotSingular` 错误。å¯ä½¿ç”¨æœ¬æ ‡å¿—去解æžåŒä¸€ä¸ªæµé‡Œçš„多个 JSON。
+`kParseFullPrecisionFlag` | 使用完整的精确度去解æžæ•°å­—(较慢)。如ä¸è®¾ç½®æ­¤æ ‡èŠ‚,则会使用正常的精确度(较快)。正常精确度会有最多 3 个 [ULP](http://en.wikipedia.org/wiki/Unit_in_the_last_place) 的误差。
+`kParseCommentsFlag` | 容许å•è¡Œ `// ...` åŠå¤šè¡Œ `/* ... */` 注释(放宽的 JSON 语法)。
+`kParseNumbersAsStringsFlag` | 把数字类型解æžæˆå­—符串。
+`kParseTrailingCommasFlag` | 容许在对象和数组结æŸå‰å«æœ‰é€—å·ï¼ˆæ”¾å®½çš„ JSON 语法)。
+`kParseNanAndInfFlag` | 容许 `NaN`ã€`Inf`ã€`Infinity`ã€`-Inf` åŠ `-Infinity` 作为 `double` 值(放宽的 JSON 语法)。
-由于使用了éžç±»åž‹æ¨¡æ¿å‚数,而ä¸æ˜¯å‡½æ•°å‚数,C++编译器能为个别组åˆç”Ÿæˆä»£ç ï¼Œä»¥æ”¹å–„性能åŠå‡å°‘代ç å°ºå¯¸ï¼ˆå½“åªç”¨å•ç§ç‰¹åŒ–)。缺点是需è¦åœ¨ç¼–译期决定标志。
+由于使用了éžç±»åž‹æ¨¡æ¿å‚数,而ä¸æ˜¯å‡½æ•°å‚数,C++ 编译器能为个别组åˆç”Ÿæˆä»£ç ï¼Œä»¥æ”¹å–„性能åŠå‡å°‘代ç å°ºå¯¸ï¼ˆå½“åªç”¨å•ç§ç‰¹åŒ–)。缺点是需è¦åœ¨ç¼–译期决定标志。
-`SourceEncoding`å‚数定义æµä½¿ç”¨äº†ä»€ä¹ˆç¼–ç ã€‚这与`Document`çš„`Encoding`ä¸ç›¸åŒã€‚细节å¯å‚考[转ç å’Œæ ¡éªŒ](#TranscodingAndValidation)一节。
+`SourceEncoding` å‚数定义æµä½¿ç”¨äº†ä»€ä¹ˆç¼–ç ã€‚这与 `Document` çš„ `Encoding` ä¸ç›¸åŒã€‚细节å¯å‚考 [转ç å’Œæ ¡éªŒ](#TranscodingAndValidation) 一节。
-此外`InputStream`是输入æµçš„类型。
+此外 `InputStream` 是输入æµçš„类型。
## 解æžé”™è¯¯ {#ParseError}
-当解æžè¿‡ç¨‹é¡ºåˆ©å®Œæˆï¼Œ`Document`便会å«æœ‰è§£æžç»“果。当过程出现错误,原æ¥çš„DOM会*ç»´æŒä¸ä¾¿*。å¯ä½¿ç”¨`bool HasParseError()`ã€`ParseErrorCode GetParseError()`åŠ`size_t GetParseOffset()`获å–解æžçš„错误状æ€ã€‚
+当解æžè¿‡ç¨‹é¡ºåˆ©å®Œæˆï¼Œ`Document` 便会å«æœ‰è§£æžç»“果。当过程出现错误,原æ¥çš„ DOM 会*ç»´æŒä¸å˜*。å¯ä½¿ç”¨ `bool HasParseError()`ã€`ParseErrorCode GetParseError()` åŠ `size_t GetErrorOffset()` 获å–解æžçš„错误状æ€ã€‚
解æžé”™è¯¯ä»£å· | æè¿°
--------------------------------------------|---------------------------------------------------
@@ -133,22 +136,22 @@ GenericDocument& GenericDocument::Parse(const Ch* str);
`kParseErrorDocumentEmpty` | 文档是空的。
`kParseErrorDocumentRootNotSingular` | 文档的根åŽé¢ä¸èƒ½æœ‰å…¶å®ƒå€¼ã€‚
`kParseErrorValueInvalid` | ä¸åˆæ³•çš„值。
-`kParseErrorObjectMissName` | Objectæˆå‘˜ç¼ºå°‘å字。
-`kParseErrorObjectMissColon` | Objectæˆå‘˜åå­—åŽç¼ºå°‘冒å·ã€‚
-`kParseErrorObjectMissCommaOrCurlyBracket` | Objectæˆå‘˜åŽç¼ºå°‘逗å·æˆ–`}`。
-`kParseErrorArrayMissCommaOrSquareBracket` | Array元素åŽç¼ºå°‘逗å·æˆ–`]` 。
-`kParseErrorStringUnicodeEscapeInvalidHex` | String中的`\\u`转义符åŽå«éžåå…­è¿›ä½æ•°å­—。
-`kParseErrorStringUnicodeSurrogateInvalid` | String中的代ç†å¯¹ï¼ˆsurrogate pair)ä¸åˆæ³•ã€‚
-`kParseErrorStringEscapeInvalid` | Stringå«éžæ³•è½¬ä¹‰å­—符。
-`kParseErrorStringMissQuotationMark` | String缺少关闭引å·ã€‚
-`kParseErrorStringInvalidEncoding` | Stringå«éžæ³•ç¼–ç ã€‚
-`kParseErrorNumberTooBig` | Number的值太大,ä¸èƒ½å­˜å‚¨äºŽ`double`。
-`kParseErrorNumberMissFraction` | Number缺少了å°æ•°éƒ¨åˆ†ã€‚
-`kParseErrorNumberMissExponent` | Number缺少了指数。
-
-错误的å移é‡å®šä¹‰ä¸ºä»Žæµå¼€å§‹è‡³é”™è¯¯å¤„的字符数é‡ã€‚ç›®å‰RapidJSONä¸è®°å½•é”™è¯¯è¡Œå·ã€‚
-
-è¦å–得错误讯æ¯ï¼ŒRapidJSON在`rapidjson/error/en.h`中æ供了英文错误讯æ¯ã€‚使用者å¯ä»¥ä¿®æ”¹å®ƒç”¨äºŽå…¶ä»–语言环境,或使用一个自定义的本地化系统。
+`kParseErrorObjectMissName` | Object æˆå‘˜ç¼ºå°‘å字。
+`kParseErrorObjectMissColon` | Object æˆå‘˜åå­—åŽç¼ºå°‘冒å·ã€‚
+`kParseErrorObjectMissCommaOrCurlyBracket` | Object æˆå‘˜åŽç¼ºå°‘逗å·æˆ– `}`。
+`kParseErrorArrayMissCommaOrSquareBracket` | Array 元素åŽç¼ºå°‘逗å·æˆ– `]` 。
+`kParseErrorStringUnicodeEscapeInvalidHex` | String 中的 `\\u` 转义符åŽå«éžåå…­è¿›ä½æ•°å­—。
+`kParseErrorStringUnicodeSurrogateInvalid` | String 中的代ç†å¯¹ï¼ˆsurrogate pair)ä¸åˆæ³•ã€‚
+`kParseErrorStringEscapeInvalid` | String å«éžæ³•è½¬ä¹‰å­—符。
+`kParseErrorStringMissQuotationMark` | String 缺少关闭引å·ã€‚
+`kParseErrorStringInvalidEncoding` | String å«éžæ³•ç¼–ç ã€‚
+`kParseErrorNumberTooBig` | Number 的值太大,ä¸èƒ½å­˜å‚¨äºŽ `double`。
+`kParseErrorNumberMissFraction` | Number 缺少了å°æ•°éƒ¨åˆ†ã€‚
+`kParseErrorNumberMissExponent` | Number 缺少了指数。
+
+错误的å移é‡å®šä¹‰ä¸ºä»Žæµå¼€å§‹è‡³é”™è¯¯å¤„的字符数é‡ã€‚ç›®å‰ RapidJSON ä¸è®°å½•é”™è¯¯è¡Œå·ã€‚
+
+è¦å–得错误讯æ¯ï¼ŒRapidJSON 在 `rapidjson/error/en.h` 中æ供了英文错误讯æ¯ã€‚使用者å¯ä»¥ä¿®æ”¹å®ƒç”¨äºŽå…¶ä»–语言环境,或使用一个自定义的本地化系统。
以下是一个处ç†é”™è¯¯çš„例å­ã€‚
@@ -168,7 +171,7 @@ if (d.Parse(json).HasParseError()) {
## 原ä½è§£æž {#InSituParsing}
-æ ¹æ®[维基百科](http://en.wikipedia.org/wiki/In_situ):
+æ ¹æ® [维基百科](http://en.wikipedia.org/wiki/In_situ):
> *In situ* ... is a Latin phrase that translates literally to "on site" or "in position". It means "locally", "on site", "on the premises" or "in place" to describe an event where it takes place, and is used in many different contexts.
> ...
@@ -176,24 +179,24 @@ if (d.Parse(json).HasParseError()) {
> 翻译:*In situ*……是一个拉ä¸æ–‡ç‰‡è¯­ï¼Œå­—é¢ä¸Šçš„æ„æ€æ˜¯æŒ‡ã€ŒçŽ°åœºã€ã€ã€Œåœ¨ä½ç½®ã€ã€‚在许多ä¸åŒè¯­å¢ƒä¸­ï¼Œå®ƒæ述一个事件å‘生的ä½ç½®ï¼Œæ„指「本地ã€ã€ã€ŒçŽ°åœºã€ã€ã€Œåœ¨å¤„所ã€ã€ã€Œå°±ä½ã€ã€‚
> ……
-> (在计算机科学中)一个算法若称为原ä½ç®—法,或在ä½ç®—法,是指执行该算法所需的é¢å¤–内存空间是O(1)的,æ¢å¥è¯è¯´ï¼Œæ— è®ºè¾“入大å°éƒ½åªéœ€è¦å¸¸æ•°ç©ºé—´ã€‚例如,堆排åºæ˜¯ä¸€ä¸ªåŽŸä½æŽ’åºç®—法。
+> (在计算机科学中)一个算法若称为原ä½ç®—法,或在ä½ç®—法,是指执行该算法所需的é¢å¤–内存空间是 O(1) 的,æ¢å¥è¯è¯´ï¼Œæ— è®ºè¾“入大å°éƒ½åªéœ€è¦å¸¸æ•°ç©ºé—´ã€‚例如,堆排åºæ˜¯ä¸€ä¸ªåŽŸä½æŽ’åºç®—法。
-在正常的解æžè¿‡ç¨‹ä¸­ï¼Œå¯¹JSON string解ç å¹¶å¤åˆ¶è‡³å…¶ä»–缓冲区是一个很大的开销。原ä½è§£æžï¼ˆ*in situ* parsing)把这些JSON string直接解ç äºŽå®ƒåŽŸæ¥å­˜å‚¨çš„地方。由于解ç åŽçš„string长度总是短于或等于原æ¥å‚¨å­˜äºŽJSONçš„string,所以这是å¯è¡Œçš„。在这个语境下,对JSON string进行解ç æ˜¯æŒ‡å¤„ç†è½¬ä¹‰ç¬¦ï¼Œå¦‚`"\n"`ã€`"\u1234"`等,以åŠåœ¨string末端加入空终止符å·(`'\0'`)。
+在正常的解æžè¿‡ç¨‹ä¸­ï¼Œå¯¹ JSON string 解ç å¹¶å¤åˆ¶è‡³å…¶ä»–缓冲区是一个很大的开销。原ä½è§£æžï¼ˆ*in situ* parsing)把这些 JSON string 直接解ç äºŽå®ƒåŽŸæ¥å­˜å‚¨çš„地方。由于解ç åŽçš„ string 长度总是短于或等于原æ¥å‚¨å­˜äºŽ JSON çš„ string,所以这是å¯è¡Œçš„。在这个语境下,对 JSON string 进行解ç æ˜¯æŒ‡å¤„ç†è½¬ä¹‰ç¬¦ï¼Œå¦‚ `"\n"`ã€`"\u1234"` 等,以åŠåœ¨ string æœ«ç«¯åŠ å…¥ç©ºç»ˆæ­¢ç¬¦å· (`'\0'`)。
-以下的图比较正常åŠåŽŸä½è§£æžã€‚JSON string值包å«æŒ‡å‘解ç åŽçš„字符串。
+以下的图比较正常åŠåŽŸä½è§£æžã€‚JSON string 值包å«æŒ‡å‘解ç åŽçš„字符串。
![正常解æž](diagram/normalparsing.png)
-在正常解æžä¸­ï¼Œè§£ç åŽçš„字符串被å¤åˆ¶è‡³å…¨æ–°åˆ†é…的缓冲区中。`"\\n"`(2个字符)被解ç æˆ`"\n"`(1个字符)。`"\\u0073"`(6个字符)被解ç æˆ`"s"`(1个字符)。
+在正常解æžä¸­ï¼Œè§£ç åŽçš„字符串被å¤åˆ¶è‡³å…¨æ–°åˆ†é…的缓冲区中。`"\\n"`(2 个字符)被解ç æˆ `"\n"`(1 个字符)。`"\\u0073"`(6 个字符)被解ç æˆ `"s"`(1 个字符)。
![原ä½è§£æž](diagram/insituparsing.png)
-原ä½è§£æžç›´æŽ¥ä¿®æ”¹äº†åŽŸæ¥çš„JSON。图中高亮了被更新的字符。若JSON stringä¸å«è½¬ä¹‰ç¬¦ï¼Œä¾‹å¦‚`"msg"`,那么解æžè¿‡ç¨‹ä»…仅是以空字符代替结æŸåŒå¼•å·ã€‚
+原ä½è§£æžç›´æŽ¥ä¿®æ”¹äº†åŽŸæ¥çš„ JSON。图中高亮了被更新的字符。若 JSON string ä¸å«è½¬ä¹‰ç¬¦ï¼Œä¾‹å¦‚ `"msg"`,那么解æžè¿‡ç¨‹ä»…仅是以空字符代替结æŸåŒå¼•å·ã€‚
-由于原ä½è§£æžä¿®æ”¹äº†è¾“入,其解æžAPI需è¦`char*`而éž`const char*`。
+由于原ä½è§£æžä¿®æ”¹äº†è¾“å…¥ï¼Œå…¶è§£æž API éœ€è¦ `char*` è€Œéž `const char*`。
~~~~~~~~~~cpp
-// 把整个文件读入buffer
+// 把整个文件读入 buffer
FILE* fp = fopen("test.json", "r");
fseek(fp, 0, SEEK_END);
size_t filesize = (size_t)ftell(fp);
@@ -203,46 +206,46 @@ size_t readLength = fread(buffer, 1, filesize, fp);
buffer[readLength] = '\0';
fclose(fp);
-// 原ä½è§£æžbuffer至d,buffer内容会被修改。
+// 原ä½è§£æž buffer 至 d,buffer 内容会被修改。
Document d;
d.ParseInsitu(buffer);
-// 在此查询ã€ä¿®æ”¹DOM……
+// 在此查询ã€ä¿®æ”¹ DOM……
free(buffer);
-// 注æ„:在这个ä½ç½®ï¼Œdå¯èƒ½å«æœ‰æŒ‡å‘已被释放的buffer的悬空指针
+// 注æ„:在这个ä½ç½®ï¼Œd å¯èƒ½å«æœ‰æŒ‡å‘已被释放的 buffer 的悬空指针
~~~~~~~~~~
-JSON string会被打上const-string的标志。但它们å¯èƒ½å¹¶éžçœŸæ­£çš„「常数ã€ã€‚它的生命周期å–决于存储JSON的缓冲区。
+JSON string 会被打上 const-string 的标志。但它们å¯èƒ½å¹¶éžçœŸæ­£çš„「常数ã€ã€‚它的生命周期å–决于存储 JSON 的缓冲区。
原ä½è§£æžæŠŠåˆ†é…开销åŠå†…å­˜å¤åˆ¶å‡è‡³æœ€å°ã€‚通常这样åšèƒ½æ”¹å–„缓存一致性,而这对现代计算机æ¥è¯´æ˜¯ä¸€ä¸ªé‡è¦çš„性能因素。
原ä½è§£æžæœ‰ä»¥ä¸‹é™åˆ¶ï¼š
-1. 整个JSON须存储在内存之中。
+1. 整个 JSON 须存储在内存之中。
2. æµçš„æ¥æºç¼“ç ä¸Žæ–‡æ¡£çš„目标编ç å¿…须相åŒã€‚
3. 需è¦ä¿ç•™ç¼“冲区,直至文档ä¸å†è¢«ä½¿ç”¨ã€‚
-4. è‹¥DOM需è¦åœ¨è§£æžåŽè¢«é•¿æœŸä½¿ç”¨ï¼Œè€ŒDOM内åªæœ‰å¾ˆå°‘JSON string,ä¿ç•™ç¼“冲区å¯èƒ½é€ æˆå†…存浪费。
+4. è‹¥ DOM 需è¦åœ¨è§£æžåŽè¢«é•¿æœŸä½¿ç”¨ï¼Œè€Œ DOM 内åªæœ‰å¾ˆå°‘ JSON string,ä¿ç•™ç¼“冲区å¯èƒ½é€ æˆå†…存浪费。
-原ä½è§£æžæœ€é€‚åˆç”¨äºŽçŸ­æœŸçš„ã€ç”¨å®Œå³å¼ƒçš„JSON。实际应用中,这些场åˆæ˜¯éžå¸¸æ™®é的,例如ååºåˆ—化JSON至C++对象ã€å¤„ç†ä»¥JSON表示的web请求等。
+原ä½è§£æžæœ€é€‚åˆç”¨äºŽçŸ­æœŸçš„ã€ç”¨å®Œå³å¼ƒçš„ JSON。实际应用中,这些场åˆæ˜¯éžå¸¸æ™®é的,例如ååºåˆ—化 JSON 至 C++ 对象ã€å¤„ç†ä»¥ JSON 表示的 web 请求等。
## 转ç ä¸Žæ ¡éªŒ {#TranscodingAndValidation}
-RapidJSON内部支æŒä¸åŒUnicodeæ ¼å¼ï¼ˆæ­£å¼çš„术语是UCSå˜æ¢æ ¼å¼ï¼‰é—´çš„转æ¢ã€‚在DOM解æžæ—¶ï¼Œæµçš„æ¥æºç¼–ç ä¸ŽDOMçš„ç¼–ç å¯ä»¥ä¸åŒã€‚例如,æ¥æºæµå¯èƒ½å«æœ‰UTF-8çš„JSON,而DOM则使用UTF-16ç¼–ç ã€‚在[EncodedInputStream](doc/stream.md)一节里有一个例å­ã€‚
+RapidJSON 内部支æŒä¸åŒ Unicode æ ¼å¼ï¼ˆæ­£å¼çš„术语是 UCS å˜æ¢æ ¼å¼ï¼‰é—´çš„转æ¢ã€‚在 DOM 解æžæ—¶ï¼Œæµçš„æ¥æºç¼–ç ä¸Ž DOM çš„ç¼–ç å¯ä»¥ä¸åŒã€‚例如,æ¥æºæµå¯èƒ½å«æœ‰ UTF-8 çš„ JSON,而 DOM 则使用 UTF-16 ç¼–ç ã€‚在 [EncodedInputStream](doc/stream.zh-cn.md) 一节里有一个例å­ã€‚
-当从DOM输出一个JSON至输出æµä¹‹æ—¶ï¼Œä¹Ÿå¯ä»¥ä½¿ç”¨è½¬ç åŠŸèƒ½ã€‚在[EncodedOutputStream](doc/stream.md)一节里有一个例å­ã€‚
+当从 DOM 输出一个 JSON 至输出æµä¹‹æ—¶ï¼Œä¹Ÿå¯ä»¥ä½¿ç”¨è½¬ç åŠŸèƒ½ã€‚在 [EncodedOutputStream](doc/stream.zh-cn.md) 一节里有一个例å­ã€‚
-在转ç è¿‡ç¨‹ä¸­ï¼Œä¼šæŠŠæ¥æºstring解ç æˆUnicodeç ç‚¹ï¼Œç„¶åŽæŠŠç ç‚¹ç¼–ç æˆç›®æ ‡æ ¼å¼ã€‚在解ç æ—¶ï¼Œå®ƒä¼šæ ¡éªŒæ¥æºstring的字节åºåˆ—是å¦åˆæ³•ã€‚è‹¥é‡ä¸Šéžåˆæ³•åºåˆ—,解æžå™¨ä¼šåœæ­¢å¹¶è¿”回`kParseErrorStringInvalidEncoding`错误。
+在转ç è¿‡ç¨‹ä¸­ï¼Œä¼šæŠŠæ¥æº string 解ç æˆ Unicode ç ç‚¹ï¼Œç„¶åŽæŠŠç ç‚¹ç¼–ç æˆç›®æ ‡æ ¼å¼ã€‚在解ç æ—¶ï¼Œå®ƒä¼šæ ¡éªŒæ¥æº string 的字节åºåˆ—是å¦åˆæ³•ã€‚è‹¥é‡ä¸Šéžåˆæ³•åºåˆ—,解æžå™¨ä¼šåœæ­¢å¹¶è¿”回 `kParseErrorStringInvalidEncoding` 错误。
-当æ¥æºç¼–ç ä¸ŽDOMçš„ç¼–ç ç›¸åŒï¼Œè§£æžå™¨ç¼ºçœåœ°*ä¸ä¼š*校验åºåˆ—。使用者å¯å¼€å¯`kParseValidateEncodingFlag`去强制校验。
+当æ¥æºç¼–ç ä¸Ž DOM çš„ç¼–ç ç›¸åŒï¼Œè§£æžå™¨ç¼ºçœåœ° * ä¸ä¼š * 校验åºåˆ—。使用者å¯å¼€å¯ `kParseValidateEncodingFlag` 去强制校验。
# 技巧 {#Techniques}
-这里讨论一些DOM API的使用技巧。
+这里讨论一些 DOM API 的使用技巧。
-## 把DOM作为SAX事件å‘表者
+## 把 DOM 作为 SAX 事件å‘表者
-在RapidJSON中,利用`Writer`把DOM生æˆJSONçš„åšæ³•ï¼Œçœ‹æ¥æœ‰ç‚¹å¥‡æ€ªã€‚
+在 RapidJSON 中,利用 `Writer` 把 DOM ç”Ÿæˆ JSON çš„åšæ³•ï¼Œçœ‹æ¥æœ‰ç‚¹å¥‡æ€ªã€‚
~~~~~~~~~~cpp
// ...
@@ -250,19 +253,19 @@ Writer<StringBuffer> writer(buffer);
d.Accept(writer);
~~~~~~~~~~
-实际上,`Value::Accept()`是负责å‘布该值相关的SAX事件至处ç†å™¨çš„。通过这个设计,`Value`åŠ`Writer`解除了å¶åˆã€‚`Value`å¯ç”ŸæˆSAX事件,而`Writer`则å¯ä»¥å¤„ç†è¿™äº›äº‹ä»¶ã€‚
+实际上,`Value::Accept()` 是负责å‘布该值相关的 SAX 事件至处ç†å™¨çš„。通过这个设计,`Value` åŠ `Writer` 解除了å¶åˆã€‚`Value` å¯ç”Ÿæˆ SAX 事件,而 `Writer` 则å¯ä»¥å¤„ç†è¿™äº›äº‹ä»¶ã€‚
-使用者å¯ä»¥åˆ›å»ºè‡ªå®šä¹‰çš„处ç†å™¨ï¼ŒåŽ»æŠŠDOM转æ¢æˆå…¶å®ƒæ ¼å¼ã€‚例如,一个把DOM转æ¢æˆXML的处ç†å™¨ã€‚
+使用者å¯ä»¥åˆ›å»ºè‡ªå®šä¹‰çš„处ç†å™¨ï¼ŒåŽ»æŠŠ DOM 转æ¢æˆå…¶å®ƒæ ¼å¼ã€‚例如,一个把 DOM 转æ¢æˆ XML 的处ç†å™¨ã€‚
-è¦çŸ¥é“更多关于SAX事件与处ç†å™¨ï¼Œå¯å‚阅[SAX](doc/sax.md)。
+è¦çŸ¥é“更多关于 SAX 事件与处ç†å™¨ï¼Œå¯å‚阅 [SAX](doc/sax.zh-cn.md)。
-## 使用者缓冲区{ #UserBuffer}
+## 使用者缓冲区 {#UserBuffer}
许多应用软件å¯èƒ½éœ€è¦å°½é‡å‡å°‘内存分é…。
-`MemoryPoolAllocator`å¯ä»¥å¸®åŠ©è¿™æ–¹é¢ï¼Œå®ƒå®¹è®¸ä½¿ç”¨è€…æ供一个缓冲区。该缓冲区å¯èƒ½ç½®äºŽç¨‹åºå †æ ˆï¼Œæˆ–是一个é™æ€åˆ†é…的「è‰ç¨¿ç¼“冲区(scratch buffer)ã€ï¼ˆä¸€ä¸ªé™æ€ï¼å…¨å±€çš„数组),用于储存临时数æ®ã€‚
+`MemoryPoolAllocator` å¯ä»¥å¸®åŠ©è¿™æ–¹é¢ï¼Œå®ƒå®¹è®¸ä½¿ç”¨è€…æ供一个缓冲区。该缓冲区å¯èƒ½ç½®äºŽç¨‹åºå †æ ˆï¼Œæˆ–是一个é™æ€åˆ†é…的「è‰ç¨¿ç¼“冲区(scratch buffer)ã€ï¼ˆä¸€ä¸ªé™æ€ï¼å…¨å±€çš„数组),用于储存临时数æ®ã€‚
-`MemoryPoolAllocator`会先用使用者缓冲区去解决分é…请求。当使用者缓冲区用完,就会从基础分é…器(缺çœä¸º`CrtAllocator`)分é…一å—内存。
+`MemoryPoolAllocator` 会先用使用者缓冲区去解决分é…请求。当使用者缓冲区用完,就会从基础分é…器(缺çœä¸º `CrtAllocator`)分é…一å—内存。
以下是使用堆栈内存的例å­ï¼Œç¬¬ä¸€ä¸ªåˆ†é…器用于存储值,第二个用于解æžæ—¶çš„临时缓冲。
@@ -276,6 +279,6 @@ DocumentType d(&valueAllocator, sizeof(parseBuffer), &parseAllocator);
d.Parse(json);
~~~~~~~~~~
-若解æžæ—¶åˆ†é…总é‡å°‘于4096+1024字节时,这段代ç ä¸ä¼šé€ æˆä»»ä½•å †å†…存分é…(ç»`new`或`malloc()`)。
+若解æžæ—¶åˆ†é…总é‡å°‘于 4096+1024 字节时,这段代ç ä¸ä¼šé€ æˆä»»ä½•å †å†…存分é…ï¼ˆç» `new` 或 `malloc()`)。
-使用者å¯ä»¥é€šè¿‡`MemoryPoolAllocator::Size()`查询当å‰å·²åˆ†çš„内存大å°ã€‚那么使用者å¯ä»¥æ‹Ÿå®šä½¿ç”¨è€…缓冲区的åˆé€‚大å°ã€‚
+使用者å¯ä»¥é€šè¿‡ `MemoryPoolAllocator::Size()` 查询当å‰å·²åˆ†çš„内存大å°ã€‚那么使用者å¯ä»¥æ‹Ÿå®šä½¿ç”¨è€…缓冲区的åˆé€‚大å°ã€‚
diff --git a/doc/encoding.md b/doc/encoding.md
index e9ebe8f2..e663aeac 100644
--- a/doc/encoding.md
+++ b/doc/encoding.md
@@ -10,7 +10,7 @@ The earlier [RFC4627](http://www.ietf.org/rfc/rfc4627.txt) stated that,
> (in §6) JSON may be represented using UTF-8, UTF-16, or UTF-32. When JSON is written in UTF-8, JSON is 8bit compatible. When JSON is written in UTF-16 or UTF-32, the binary content-transfer-encoding must be used.
-RapidJSON supports various encodings. It can also validate the encodings of JSON, and transconding JSON among encodings. All these features are implemented internally, without the need for external libraries (e.g. [ICU](http://site.icu-project.org/)).
+RapidJSON supports various encodings. It can also validate the encodings of JSON, and transcoding JSON among encodings. All these features are implemented internally, without the need for external libraries (e.g. [ICU](http://site.icu-project.org/)).
[TOC]
@@ -131,8 +131,8 @@ StringStream source(s);
GenericStringBuffer<UTF16<> > target;
bool hasError = false;
-while (source.Peak() != '\0')
- if (!Transcoder::Transcode<UTF8<>, UTF16<> >(source, target)) {
+while (source.Peek() != '\0')
+ if (!Transcoder<UTF8<>, UTF16<> >::Transcode(source, target)) {
hasError = true;
break;
}
diff --git a/doc/encoding.zh-cn.md b/doc/encoding.zh-cn.md
index 3435c3ab..808ba525 100644
--- a/doc/encoding.zh-cn.md
+++ b/doc/encoding.zh-cn.md
@@ -1,45 +1,45 @@
# ç¼–ç 
-æ ¹æ®[ECMA-404](http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf):
+æ ¹æ® [ECMA-404](http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf):
> (in Introduction) JSON text is a sequence of Unicode code points.
>
-> 翻译:JSON文本是Unicodeç ç‚¹çš„åºåˆ—。
+> 翻译:JSON 文本是 Unicode ç ç‚¹çš„åºåˆ—。
-较早的[RFC4627](http://www.ietf.org/rfc/rfc4627.txt)申明:
+较早的 [RFC4627](http://www.ietf.org/rfc/rfc4627.txt) 申明:
> (in §3) JSON text SHALL be encoded in Unicode. The default encoding is UTF-8.
>
-> 翻译:JSON文本应该以Unicodeç¼–ç ã€‚缺çœçš„ç¼–ç ä¸ºUTF-8。
+> 翻译:JSON 文本应该以 Unicode ç¼–ç ã€‚缺çœçš„ç¼–ç ä¸º UTF-8。
> (in §6) JSON may be represented using UTF-8, UTF-16, or UTF-32. When JSON is written in UTF-8, JSON is 8bit compatible. When JSON is written in UTF-16 or UTF-32, the binary content-transfer-encoding must be used.
>
-> 翻译:JSONå¯ä½¿ç”¨UTF-8ã€UTF-16或UTF-18表示。当JSON以UTF-8写入,该JSON是8ä½å…¼å®¹çš„。当JSON以UTF-16或UTF-32写入,就必须使用二进制的内容传é€ç¼–ç ã€‚
+> 翻译:JSON å¯ä½¿ç”¨ UTF-8ã€UTF-16 或 UTF-32 表示。当 JSON 以 UTF-8 写入,该 JSON 是 8 ä½å…¼å®¹çš„。当 JSON 以 UTF-16 或 UTF-32 写入,就必须使用二进制的内容传é€ç¼–ç ã€‚
-RapidJSON支æŒå¤šç§ç¼–ç ã€‚它也能检查JSONçš„ç¼–ç ï¼Œä»¥åŠåœ¨ä¸åŒç¼–ç ä¸­è¿›è¡Œè½¬ç ã€‚所有这些功能都是在内部实现,无需使用外部的程åºåº“(如[ICU](http://site.icu-project.org/))。
+RapidJSON 支æŒå¤šç§ç¼–ç ã€‚它也能检查 JSON çš„ç¼–ç ï¼Œä»¥åŠåœ¨ä¸åŒç¼–ç ä¸­è¿›è¡Œè½¬ç ã€‚所有这些功能都是在内部实现,无需使用外部的程åºåº“(如 [ICU](http://site.icu-project.org/))。
[TOC]
# Unicode {#Unicode}
-æ ¹æ® [Unicode的官方网站](http://www.unicode.org/standard/translations/t-chinese.html):
->Unicodeç»™æ¯ä¸ªå­—符æ供了一个唯一的数字,
+æ ¹æ® [Unicode 的官方网站](http://www.unicode.org/standard/translations/t-chinese.html):
+>Unicode ç»™æ¯ä¸ªå­—符æ供了一个唯一的数字,
ä¸è®ºæ˜¯ä»€ä¹ˆå¹³å°ã€
ä¸è®ºæ˜¯ä»€ä¹ˆç¨‹åºã€
ä¸è®ºæ˜¯ä»€ä¹ˆè¯­è¨€ã€‚
-这些唯一数字称为ç ç‚¹ï¼ˆcode point),其范围介乎`0x0`至`0x10FFFF`之间。
+这些唯一数字称为ç ç‚¹ï¼ˆcode point),其范围介乎 `0x0` 至 `0x10FFFF` 之间。
-## Unicode转æ¢æ ¼å¼ {#UTF}
+## Unicode 转æ¢æ ¼å¼ {#UTF}
-存储Unicodeç ç‚¹æœ‰å¤šç§ç¼–ç æ–¹å¼ã€‚这些称为Unicode转æ¢æ ¼å¼ï¼ˆUnicode Transformation Format, UTF)。RapidJSON支æŒæœ€å¸¸ç”¨çš„UTF,包括:
+存储 Unicode ç ç‚¹æœ‰å¤šç§ç¼–ç æ–¹å¼ã€‚这些称为 Unicode 转æ¢æ ¼å¼ï¼ˆUnicode Transformation Format, UTF)。RapidJSON 支æŒæœ€å¸¸ç”¨çš„ UTF,包括:
-* UTF-8:8ä½å¯å˜é•¿åº¦ç¼–ç ã€‚它把一个ç ç‚¹æ˜ å°„至1至4个字节。
-* UTF-16:16ä½å¯å˜é•¿åº¦ç¼–ç ã€‚它把一个ç ç‚¹æ˜ å°„至1至2个16ä½ç¼–ç å•å…ƒï¼ˆå³2至4个字节)。
-* UTF-32:32ä½å›ºå®šé•¿åº¦ç¼–ç ã€‚它直接把ç ç‚¹æ˜ å°„至å•ä¸ª32ä½ç¼–ç å•å…ƒï¼ˆå³4字节)。
+* UTF-8:8 ä½å¯å˜é•¿åº¦ç¼–ç ã€‚它把一个ç ç‚¹æ˜ å°„至 1 至 4 个字节。
+* UTF-16:16 ä½å¯å˜é•¿åº¦ç¼–ç ã€‚它把一个ç ç‚¹æ˜ å°„至 1 至 2 个 16 ä½ç¼–ç å•å…ƒï¼ˆå³ 2 至 4 个字节)。
+* UTF-32:32 ä½å›ºå®šé•¿åº¦ç¼–ç ã€‚它直接把ç ç‚¹æ˜ å°„至å•ä¸ª 32 ä½ç¼–ç å•å…ƒï¼ˆå³ 4 字节)。
-对于UTF-16åŠUTF-32æ¥è¯´ï¼Œå­—节åºï¼ˆendianness)是有影å“的。在内存中,它们通常都是以该计算机的字节åºæ¥å­˜å‚¨ã€‚然而,当è¦å‚¨å­˜åœ¨æ–‡ä»¶ä¸­æˆ–在网上传输,我们需è¦æŒ‡æ˜Žå­—节åºåˆ—的字节åºï¼Œæ˜¯å°ç«¯ï¼ˆlittle endian, LE)还是大端(big-endian, BE)。
+对于 UTF-16 åŠ UTF-32 æ¥è¯´ï¼Œå­—节åºï¼ˆendianness)是有影å“的。在内存中,它们通常都是以该计算机的字节åºæ¥å­˜å‚¨ã€‚然而,当è¦å‚¨å­˜åœ¨æ–‡ä»¶ä¸­æˆ–在网上传输,我们需è¦æŒ‡æ˜Žå­—节åºåˆ—的字节åºï¼Œæ˜¯å°ç«¯ï¼ˆlittle endian, LE)还是大端(big-endian, BE)。
-RapidJSON通过`rapidjson/encodings.h`中的struct去æä¾›å„ç§ç¼–ç ï¼š
+RapidJSON 通过 `rapidjson/encodings.h` 中的 struct 去æä¾›å„ç§ç¼–ç ï¼š
~~~~~~~~~~cpp
namespace rapidjson {
@@ -68,38 +68,38 @@ struct UTF32BE;
} // namespace rapidjson
~~~~~~~~~~
-对于在内存中的文本,我们正常会使用`UTF8`ã€`UTF16`或`UTF32`。对于处ç†ç»è¿‡I/O的文本,我们å¯ä½¿ç”¨`UTF8`ã€`UTF16LE`ã€`UTF16BE`ã€`UTF32LE`或`UTF32BE`。
+对于在内存中的文本,我们正常会使用 `UTF8`ã€`UTF16` 或 `UTF32`。对于处ç†ç»è¿‡ I/O 的文本,我们å¯ä½¿ç”¨ `UTF8`ã€`UTF16LE`ã€`UTF16BE`ã€`UTF32LE` 或 `UTF32BE`。
-当使用DOM风格的API,`GenericValue<Encoding>`åŠ`GenericDocument<Encoding>`里的`Encoding`模æ¿å‚数是用于指明内存中存储的JSON字符串使用哪ç§ç¼–ç ã€‚因此通常我们会在此å‚数中使用`UTF8`ã€`UTF16`或`UTF32`。如何选择,视乎应用软件所使用的æ“作系统åŠå…¶ä»–程åºåº“。例如,Windows API使用UTF-16表示Unicode字符,而多数的Linuxå‘行版本åŠåº”用软件则更喜欢UTF-8。
+当使用 DOM 风格的 API,`GenericValue<Encoding>` åŠ `GenericDocument<Encoding>` 里的 `Encoding` 模æ¿å‚数是用于指明内存中存储的 JSON 字符串使用哪ç§ç¼–ç ã€‚因此通常我们会在此å‚数中使用 `UTF8`ã€`UTF16` 或 `UTF32`。如何选择,视乎应用软件所使用的æ“作系统åŠå…¶ä»–程åºåº“。例如,Windows API 使用 UTF-16 表示 Unicode 字符,而多数的 Linux å‘行版本åŠåº”用软件则更喜欢 UTF-8。
-使用UTF-16çš„DOM声明例å­ï¼š
+使用 UTF-16 çš„ DOM 声明例å­ï¼š
~~~~~~~~~~cpp
typedef GenericDocument<UTF16<> > WDocument;
typedef GenericValue<UTF16<> > WValue;
~~~~~~~~~~
-å¯ä»¥åœ¨[DOM's Encoding](doc/stream.md)一节看到更详细的使用例å­ã€‚
+å¯ä»¥åœ¨ [DOM's Encoding](doc/stream.zh-cn.md) 一节看到更详细的使用例å­ã€‚
## 字符类型 {#CharacterType}
-从之å‰çš„声明中å¯ä»¥çœ‹åˆ°ï¼Œæ¯ä¸ªç¼–ç éƒ½æœ‰ä¸€ä¸ª`CharType`模æ¿å‚数。这å¯èƒ½æ¯”较容易混淆,实际上,æ¯ä¸ª`CharType`存储一个编ç å•å…ƒï¼Œè€Œä¸æ˜¯ä¸€ä¸ªå­—符(ç ç‚¹ï¼‰ã€‚如之å‰æ‰€è°ˆåŠï¼Œåœ¨UTF-8中一个ç ç‚¹å¯èƒ½ä¼šç¼–ç æˆ1至4个编ç å•å…ƒã€‚
+从之å‰çš„声明中å¯ä»¥çœ‹åˆ°ï¼Œæ¯ä¸ªç¼–ç éƒ½æœ‰ä¸€ä¸ª `CharType` 模æ¿å‚数。这å¯èƒ½æ¯”较容易混淆,实际上,æ¯ä¸ª `CharType` 存储一个编ç å•å…ƒï¼Œè€Œä¸æ˜¯ä¸€ä¸ªå­—符(ç ç‚¹ï¼‰ã€‚如之å‰æ‰€è°ˆåŠï¼Œåœ¨ UTF-8 中一个ç ç‚¹å¯èƒ½ä¼šç¼–ç æˆ 1 至 4 个编ç å•å…ƒã€‚
-对于`UTF16(LE|BE)`åŠ`UTF32(LE|BE)`æ¥è¯´ï¼Œ`CharType`必须分别是一个至少2åŠ4字节的整数类型。
+对于 `UTF16(LE|BE)` åŠ `UTF32(LE|BE)` æ¥è¯´ï¼Œ`CharType` 必须分别是一个至少 2 åŠ 4 字节的整数类型。
-注æ„C++11新添了`char16_t`åŠ`char32_t`类型,也å¯åˆ†åˆ«ç”¨äºŽ`UTF16`åŠ`UTF32`。
+æ³¨æ„ C++11 新添了 `char16_t` åŠ `char32_t` 类型,也å¯åˆ†åˆ«ç”¨äºŽ `UTF16` åŠ `UTF32`。
## AutoUTF {#AutoUTF}
上述所介ç»çš„ç¼–ç éƒ½æ˜¯åœ¨ç¼–译期é™æ€æŒ·å®šçš„。æ¢å¥è¯è¯´ï¼Œä½¿ç”¨è€…必须知é“内存或æµä¹‹ä¸­ä½¿ç”¨äº†å“ªç§ç¼–ç ã€‚然而,有时候我们å¯èƒ½éœ€è¦è¯»å†™ä¸åŒç¼–ç çš„文件,而且这些编ç éœ€è¦åœ¨è¿è¡Œæ—¶æ‰èƒ½å†³å®šã€‚
-`AutoUTF`是为此而设计的编ç ã€‚它根æ®è¾“入或输出æµæ¥é€‰æ‹©ä½¿ç”¨å“ªç§ç¼–ç ã€‚ç›®å‰å®ƒåº”该与`EncodedInputStream`åŠ`EncodedOutputStream`结åˆä½¿ç”¨ã€‚
+`AutoUTF` 是为此而设计的编ç ã€‚它根æ®è¾“入或输出æµæ¥é€‰æ‹©ä½¿ç”¨å“ªç§ç¼–ç ã€‚ç›®å‰å®ƒåº”该与 `EncodedInputStream` åŠ `EncodedOutputStream` 结åˆä½¿ç”¨ã€‚
## ASCII {#ASCII}
-虽然JSON标准并未æåŠ[ASCII](http://en.wikipedia.org/wiki/ASCII),有时候我们希望写入7ä½çš„ASCII JSON,以供未能处ç†UTF-8的应用程åºä½¿ç”¨ã€‚由于任JSON都å¯ä»¥æŠŠUnicode字符表示为`\uXXXX`转义åºåˆ—,JSON总是å¯ç”¨ASCIIæ¥ç¼–ç ã€‚
+虽然 JSON 标准并未æåŠ [ASCII](http://en.wikipedia.org/wiki/ASCII),有时候我们希望写入 7 ä½çš„ ASCII JSONï¼Œä»¥ä¾›æœªèƒ½å¤„ç† UTF-8 的应用程åºä½¿ç”¨ã€‚由于任 JSON 都å¯ä»¥æŠŠ Unicode 字符表示为 `\uXXXX` 转义åºåˆ—,JSON 总是å¯ç”¨ ASCII æ¥ç¼–ç ã€‚
-以下的例å­æŠŠUTF-8çš„DOM写æˆASCIIçš„JSON:
+以下的例å­æŠŠ UTF-8 çš„ DOM å†™æˆ ASCII çš„ JSON:
~~~~~~~~~~cpp
using namespace rapidjson;
@@ -111,21 +111,21 @@ d.Accept(writer);
std::cout << buffer.GetString();
~~~~~~~~~~
-ASCIIå¯ç”¨äºŽè¾“å…¥æµã€‚当输入æµåŒ…å«å¤§äºŽ127的字节,就会导致`kParseErrorStringInvalidEncoding`错误。
+ASCII å¯ç”¨äºŽè¾“å…¥æµã€‚当输入æµåŒ…å«å¤§äºŽ 127 的字节,就会导致 `kParseErrorStringInvalidEncoding` 错误。
-ASCII *ä¸èƒ½* 用于内存(`Document`çš„ç¼–ç ï¼Œæˆ–`Reader`的目标编ç ),因为它ä¸èƒ½è¡¨ç¤ºUnicodeç ç‚¹ã€‚
+ASCII * ä¸èƒ½ * 用于内存(`Document` çš„ç¼–ç ï¼Œæˆ– `Reader` 的目标编ç ),因为它ä¸èƒ½è¡¨ç¤º Unicode ç ç‚¹ã€‚
# 校验åŠè½¬ç  {#ValidationTranscoding}
-当RapidJSON解æžä¸€ä¸ªJSON时,它能校验输入JSON,判断它是å¦æ‰€æ ‡æ˜Žç¼–ç çš„åˆæ³•åºåˆ—。è¦å¼€å¯æ­¤é€‰é¡¹ï¼Œè¯·æŠŠ`kParseValidateEncodingFlag`加入`parseFlags`模æ¿å‚数。
+当 RapidJSON 解æžä¸€ä¸ª JSON 时,它能校验输入 JSON,判断它是å¦æ‰€æ ‡æ˜Žç¼–ç çš„åˆæ³•åºåˆ—。è¦å¼€å¯æ­¤é€‰é¡¹ï¼Œè¯·æŠŠ `kParseValidateEncodingFlag` 加入 `parseFlags` 模æ¿å‚数。
-若输入编ç å’Œè¾“出编ç å¹¶ä¸ç›¸åŒï¼Œ`Reader`åŠ`Writer`会算把文本转ç ã€‚在这ç§æƒ…况下,并ä¸éœ€è¦`kParseValidateEncodingFlag`,因为它必须解ç è¾“å…¥åºåˆ—。若åºåˆ—ä¸èƒ½è¢«è§£ç ï¼Œå®ƒå¿…然是ä¸åˆæ³•çš„。
+若输入编ç å’Œè¾“出编ç å¹¶ä¸ç›¸åŒï¼Œ`Reader` åŠ `Writer` 会算把文本转ç ã€‚在这ç§æƒ…况下,并ä¸éœ€è¦ `kParseValidateEncodingFlag`,因为它必须解ç è¾“å…¥åºåˆ—。若åºåˆ—ä¸èƒ½è¢«è§£ç ï¼Œå®ƒå¿…然是ä¸åˆæ³•çš„。
## 转ç å™¨ {#Transcoder}
-虽然RapidJSONçš„ç¼–ç åŠŸèƒ½æ˜¯ä¸ºJSON解æžï¼ç”Ÿæˆè€Œè®¾è®¡ï¼Œä½¿ç”¨è€…也å¯ä»¥â€œæ»¥ç”¨â€å®ƒä»¬æ¥ä¸ºéžJSON字符串转ç ã€‚
+虽然 RapidJSON çš„ç¼–ç åŠŸèƒ½æ˜¯ä¸º JSON 解æžï¼ç”Ÿæˆè€Œè®¾è®¡ï¼Œä½¿ç”¨è€…也å¯ä»¥â€œæ»¥ç”¨â€å®ƒä»¬æ¥ä¸ºéž JSON 字符串转ç ã€‚
-以下的例å­æŠŠUTF-8字符串转ç æˆUTF-16:
+以下的例å­æŠŠ UTF-8 字符串转ç æˆ UTF-16:
~~~~~~~~~~cpp
#include "rapidjson/encodings.h"
@@ -137,8 +137,8 @@ StringStream source(s);
GenericStringBuffer<UTF16<> > target;
bool hasError = false;
-while (source.Peak() != '\0')
- if (!Transcoder::Transcode<UTF8<>, UTF16<> >(source, target)) {
+while (source.Peek() != '\0')
+ if (!Transcoder<UTF8<>, UTF16<> >::Transcode(source, target)) {
hasError = true;
break;
}
@@ -149,4 +149,4 @@ if (!hasError) {
}
~~~~~~~~~~
-你也å¯ä»¥ç”¨`AutoUTF`åŠå¯¹åº”çš„æµæ¥åœ¨è¿è¡Œæ—¶è®¾ç½®å†…æºï¼ç›®çš„之编ç ã€‚
+你也å¯ä»¥ç”¨ `AutoUTF` åŠå¯¹åº”çš„æµæ¥åœ¨è¿è¡Œæ—¶è®¾ç½®å†…æºï¼ç›®çš„之编ç ã€‚
diff --git a/doc/faq.md b/doc/faq.md
index 1b0541c2..d5697ff4 100644
--- a/doc/faq.md
+++ b/doc/faq.md
@@ -18,7 +18,7 @@
4. Is RapidJSON free?
- Yes, it is free under MIT license. It can be used in commercial applications. Please check the details in [license.txt](https://github.com/miloyip/rapidjson/blob/master/license.txt).
+ Yes, it is free under MIT license. It can be used in commercial applications. Please check the details in [license.txt](https://github.com/Tencent/rapidjson/blob/master/license.txt).
5. Is RapidJSON small? What are its dependencies?
@@ -44,7 +44,7 @@
10. How RapidJSON is tested?
- RapidJSON contains a unit test suite for automatic testing. [Travis](https://travis-ci.org/miloyip/rapidjson/)(for Linux) and [AppVeyor](https://ci.appveyor.com/project/miloyip/rapidjson/)(for Windows) will compile and run the unit test suite for all modifications. The test process also uses Valgrind (in Linux) to detect memory leaks.
+ RapidJSON contains a unit test suite for automatic testing. [Travis](https://travis-ci.org/Tencent/rapidjson/)(for Linux) and [AppVeyor](https://ci.appveyor.com/project/Tencent/rapidjson/)(for Windows) will compile and run the unit test suite for all modifications. The test process also uses Valgrind (in Linux) to detect memory leaks.
11. Is RapidJSON well documented?
@@ -70,7 +70,7 @@
3. Does RapidJSON support relaxed syntax?
- Currently no. RapidJSON only support the strict standardized format. Support on related syntax is under discussion in this [issue](https://github.com/miloyip/rapidjson/issues/36).
+ Currently no. RapidJSON only support the strict standardized format. Support on related syntax is under discussion in this [issue](https://github.com/Tencent/rapidjson/issues/36).
## DOM and SAX
@@ -116,7 +116,7 @@
~~~~~~~~~~cpp
Value(kObjectType).Swap(d);
~~~~~~~~~~
- or equivalent, but sightly longer to type:
+ or equivalent, but slightly longer to type:
~~~~~~~~~~cpp
d.Swap(Value(kObjectType).Move());
~~~~~~~~~~
@@ -140,11 +140,11 @@
}
~~~~~~~~~~
- The most important requirement to take care of document and value life-cycle as well as consistent memory managent using the right allocator during the value transfer.
+ The most important requirement to take care of document and value life-cycle as well as consistent memory management using the right allocator during the value transfer.
Simple yet most efficient way to achieve that is to modify the `address` definition above to initialize it with allocator of the `person` document, then we just add the root member of the value:
~~~~~~~~~~cpp
- Documnet address(person.GetAllocator());
+ Document address(person.GetAllocator());
...
person["person"].AddMember("address", address["address"], person.GetAllocator());
~~~~~~~~~~
@@ -174,7 +174,7 @@ Alternatively, if we don't want to explicitly refer to the root value of `addres
3. Why do I need to provide the length of string?
- Since C string is null-terminated, the length of string needs to be computed via `strlen()`, with linear runtime complexity. This incurs an unncessary overhead of many operations, if the user already knows the length of string.
+ Since C string is null-terminated, the length of string needs to be computed via `strlen()`, with linear runtime complexity. This incurs an unnecessary overhead of many operations, if the user already knows the length of string.
Also, RapidJSON can handle `\u0000` (null character) within a string. If a string contains null characters, `strlen()` cannot return the true length of it. In such case user must provide the length of string explicitly.
@@ -204,7 +204,7 @@ Alternatively, if we don't want to explicitly refer to the root value of `addres
2. Can it validate the encoding?
- Yes, just pass `kParseValidateEncodingFlag` to `Parse()`. If there is invalid encoding in the stream, it wil generate `kParseErrorStringInvalidEncoding` error.
+ Yes, just pass `kParseValidateEncodingFlag` to `Parse()`. If there is invalid encoding in the stream, it will generate `kParseErrorStringInvalidEncoding` error.
3. What is surrogate pair? Does RapidJSON support it?
@@ -248,7 +248,7 @@ Alternatively, if we don't want to explicitly refer to the root value of `addres
1. Is RapidJSON really fast?
- Yes. It may be the fastest open source JSON library. There is a [benchmark](https://github.com/miloyip/nativejson-benchmark) for evaluating performance of C/C++ JSON libaries.
+ Yes. It may be the fastest open source JSON library. There is a [benchmark](https://github.com/miloyip/nativejson-benchmark) for evaluating performance of C/C++ JSON libraries.
2. Why is it fast?
@@ -256,19 +256,19 @@ Alternatively, if we don't want to explicitly refer to the root value of `addres
3. What is SIMD? How it is applied in RapidJSON?
- [SIMD](http://en.wikipedia.org/wiki/SIMD) instructions can perform parallel computation in modern CPUs. RapidJSON support Intel's SSE2/SSE4.2 to accelerate whitespace skipping. This improves performance of parsing indent formatted JSON. Define `RAPIDJSON_SSE2` or `RAPIDJSON_SSE42` macro to enable this feature. However, running the executable on a machine without such instruction set support will make it crash.
+ [SIMD](http://en.wikipedia.org/wiki/SIMD) instructions can perform parallel computation in modern CPUs. RapidJSON support Intel's SSE2/SSE4.2 and ARM's Neon to accelerate whitespace/tabspace/carriage-return/line-feed skipping. This improves performance of parsing indent formatted JSON. Define `RAPIDJSON_SSE2`, `RAPIDJSON_SSE42` or `RAPIDJSON_NEON` macro to enable this feature. However, running the executable on a machine without such instruction set support will make it crash.
4. Does it consume a lot of memory?
The design of RapidJSON aims at reducing memory footprint.
- In the SAX API, `Reader` consumes memory portional to maximum depth of JSON tree, plus maximum length of JSON string.
+ In the SAX API, `Reader` consumes memory proportional to maximum depth of JSON tree, plus maximum length of JSON string.
In the DOM API, each `Value` consumes exactly 16/24 bytes for 32/64-bit architecture respectively. RapidJSON also uses a special memory allocator to minimize overhead of allocations.
5. What is the purpose of being high performance?
- Some applications need to process very large JSON files. Some server-side applications need to process huge amount of JSONs. Being high performance can improve both latency and throuput. In a broad sense, it will also save energy.
+ Some applications need to process very large JSON files. Some server-side applications need to process huge amount of JSONs. Being high performance can improve both latency and throughput. In a broad sense, it will also save energy.
## Gossip
diff --git a/doc/faq.zh-cn.md b/doc/faq.zh-cn.md
index 7127283b..f279acfe 100644
--- a/doc/faq.zh-cn.md
+++ b/doc/faq.zh-cn.md
@@ -4,107 +4,107 @@
## 一般问题
-1. RapidJSON是什么?
+1. RapidJSON 是什么?
- RapidJSON是一个C++库,用于解æžåŠç”ŸæˆJSON。读者å¯å‚考它的所有[特点](doc/features.zh-cn.md)。
+ RapidJSON 是一个 C++ 库,用于解æžåŠç”Ÿæˆ JSON。读者å¯å‚考它的所有 [特点](doc/features.zh-cn.md)。
-2. 为什么称作RapidJSON?
+2. 为什么称作 RapidJSON?
- 它的çµæ„Ÿæ¥è‡ªäºŽ[RapidXML](http://rapidxml.sourceforge.net/),RapidXML是一个高速的XML DOM解æžå™¨ã€‚
+ 它的çµæ„Ÿæ¥è‡ªäºŽ [RapidXML](http://rapidxml.sourceforge.net/),RapidXML 是一个高速的 XML DOM 解æžå™¨ã€‚
-3. RapidJSON与RapidXML相似么?
+3. RapidJSON 与 RapidXML 相似么?
- RapidJSON借镜了RapidXML的一些设计, 包括原ä½ï¼ˆ*in situ*)解æžã€åªæœ‰å¤´æ–‡ä»¶çš„库。但两者的API是完全ä¸åŒçš„。此外RapidJSON也æ供许多RapidXML没有的特点。
+ RapidJSON 借镜了 RapidXML 的一些设计, 包括原ä½ï¼ˆ*in situ*)解æžã€åªæœ‰å¤´æ–‡ä»¶çš„库。但两者的 API 是完全ä¸åŒçš„。此外 RapidJSON 也æ供许多 RapidXML 没有的特点。
-4. RapidJSON是å…费的么?
+4. RapidJSON 是å…费的么?
- 是的,它在MIT特許æ¢æ¬¾ä¸‹å…费。它å¯ç”¨äºŽå•†ä¸šè½¯ä»¶ã€‚详情请å‚看[license.txt](https://github.com/miloyip/rapidjson/blob/master/license.txt)。
+ 是的,它在 MIT 特許æ¢æ¬¾ä¸‹å…费。它å¯ç”¨äºŽå•†ä¸šè½¯ä»¶ã€‚详情请å‚看 [license.txt](https://github.com/Tencent/rapidjson/blob/master/license.txt)。
-5. RapidJSON很å°ä¹ˆï¼Ÿå®ƒæœ‰ä½•ä¾èµ–?
+5. RapidJSON 很å°ä¹ˆï¼Ÿå®ƒæœ‰ä½•ä¾èµ–?
- 是的。在Windows上,一个解æžJSON并打å°å‡ºç»Ÿè®¡çš„å¯æ‰§è¡Œæ–‡ä»¶å°‘于30KB。
+ 是的。在 Windows ä¸Šï¼Œä¸€ä¸ªè§£æž JSON 并打å°å‡ºç»Ÿè®¡çš„å¯æ‰§è¡Œæ–‡ä»¶å°‘于 30KB。
- RapidJSONä»…ä¾èµ–于C++标准库。
+ RapidJSON ä»…ä¾èµ–于 C++ 标准库。
-6. 怎样安装RapidJSON?
+6. 怎样安装 RapidJSON?
- è§[安装一节](../readme.zh-cn.md#安装)。
+ è§ [安装一节](../readme.zh-cn.md#安装)。
-7. RapidJSON能å¦è¿è¡ŒäºŽæˆ‘çš„å¹³å°ï¼Ÿ
+7. RapidJSON 能å¦è¿è¡ŒäºŽæˆ‘çš„å¹³å°ï¼Ÿ
- 社区已在多个æ“作系统ï¼ç¼–译器ï¼CPU架构的组åˆä¸Šæµ‹è¯•RapidJSON。但我们无法确ä¿å®ƒèƒ½è¿è¡ŒäºŽä½ ç‰¹å®šçš„å¹³å°ä¸Šã€‚åªéœ€è¦ç”ŸæˆåŠæ‰§è¡Œå•å…ƒæµ‹è¯•ä¾¿èƒ½èŽ·å–答案。
+ 社区已在多个æ“作系统ï¼ç¼–译器ï¼CPU 架构的组åˆä¸Šæµ‹è¯• RapidJSON。但我们无法确ä¿å®ƒèƒ½è¿è¡ŒäºŽä½ ç‰¹å®šçš„å¹³å°ä¸Šã€‚åªéœ€è¦ç”ŸæˆåŠæ‰§è¡Œå•å…ƒæµ‹è¯•ä¾¿èƒ½èŽ·å–答案。
-8. RapidJSON支æŒC++03么?C++11呢?
+8. RapidJSON æ”¯æŒ C++03 么?C++11 呢?
- RapidJSON开始时在C++03上实现。åŽæ¥åŠ å…¥äº†å¯é€‰çš„C++11特性支æŒï¼ˆå¦‚转移构造函数ã€`noexcept`)。RapidJSON应该兼容所有éµä»ŽC++03或C++11的编译器。
+ RapidJSON 开始时在 C++03 上实现。åŽæ¥åŠ å…¥äº†å¯é€‰çš„ C++11 特性支æŒï¼ˆå¦‚转移构造函数ã€`noexcept`)。RapidJSON 应该兼容所有éµä»Ž C++03 或 C++11 的编译器。
-9. RapidJSON是å¦çœŸçš„用于实际应用?
+9. RapidJSON 是å¦çœŸçš„用于实际应用?
- 是的。它被é…置于å‰å°åŠåŽå°çš„真实应用中。一个社区æˆå‘˜è¯´RapidJSON在他们的系统中æ¯æ—¥è§£æž5åƒä¸‡ä¸ªJSON。
+ 是的。它被é…置于å‰å°åŠåŽå°çš„真实应用中。一个社区æˆå‘˜è¯´ RapidJSON 在他们的系统中æ¯æ—¥è§£æž 5 åƒä¸‡ä¸ª JSON。
-10. RapidJSON是如何被测试的?
+10. RapidJSON 是如何被测试的?
- RapidJSON包å«ä¸€ç»„å•å…ƒæµ‹è¯•åŽ»æ‰§è¡Œè‡ªåŠ¨æµ‹è¯•ã€‚[Travis](https://travis-ci.org/miloyip/rapidjson/)(供Linuxå¹³å°ï¼‰åŠ[AppVeyor](https://ci.appveyor.com/project/miloyip/rapidjson/)(供Windowså¹³å°ï¼‰ä¼šå¯¹æ‰€æœ‰ä¿®æ”¹è¿›è¡Œç¼–译åŠæ‰§è¡Œå•å…ƒæµ‹è¯•ã€‚在Linux下还会使用Valgrind去检测内存泄æ¼ã€‚
+ RapidJSON 包å«ä¸€ç»„å•å…ƒæµ‹è¯•åŽ»æ‰§è¡Œè‡ªåŠ¨æµ‹è¯•ã€‚[Travis](https://travis-ci.org/Tencent/rapidjson/)(供 Linux å¹³å°ï¼‰åŠ [AppVeyor](https://ci.appveyor.com/project/Tencent/rapidjson/)(供 Windows å¹³å°ï¼‰ä¼šå¯¹æ‰€æœ‰ä¿®æ”¹è¿›è¡Œç¼–译åŠæ‰§è¡Œå•å…ƒæµ‹è¯•ã€‚在 Linux 下还会使用 Valgrind 去检测内存泄æ¼ã€‚
-11. RapidJSON是å¦æœ‰å®Œæ•´çš„文档?
+11. RapidJSON 是å¦æœ‰å®Œæ•´çš„文档?
- RapidJSONæ供了使用手册åŠAPI说明文档。
+ RapidJSON æä¾›äº†ä½¿ç”¨æ‰‹å†ŒåŠ API 说明文档。
12. 有没有其他替代å“?
- 有许多替代å“。例如[nativejson-benchmark](https://github.com/miloyip/nativejson-benchmark)列出了一些开æºçš„C/C++ JSON库。[json.org](http://www.json.org/)也有一个列表。
+ 有许多替代å“。例如 [nativejson-benchmark](https://github.com/miloyip/nativejson-benchmark) 列出了一些开æºçš„ C/C++ JSON 库。[json.org](http://www.json.org/) 也有一个列表。
## JSON
-1. 什么是JSON?
+1. 什么是 JSON?
- JSON (JavaScript Object Notation)是一个轻é‡çš„æ•°æ®äº¤æ¢æ ¼å¼ã€‚它使用人类å¯è¯»çš„文本格å¼ã€‚更多关于JSON的细节å¯è€ƒ[RFC7159](http://www.ietf.org/rfc/rfc7159.txt)åŠ[ECMA-404](http://www.ecma-international.org/publications/standards/Ecma-404.htm)。
+ JSON (JavaScript Object Notation) 是一个轻é‡çš„æ•°æ®äº¤æ¢æ ¼å¼ã€‚它使用人类å¯è¯»çš„文本格å¼ã€‚更多关于 JSON 的细节å¯è€ƒ [RFC7159](http://www.ietf.org/rfc/rfc7159.txt) åŠ [ECMA-404](http://www.ecma-international.org/publications/standards/Ecma-404.htm)。
-2. JSON有什么应用场åˆï¼Ÿ
+2. JSON 有什么应用场åˆï¼Ÿ
- JSON常用于网页应用程åºï¼Œä»¥ä¼ é€ç»“构化数æ®ã€‚它也å¯ä½œä¸ºæ–‡ä»¶æ ¼å¼ç”¨äºŽæ•°æ®æŒä¹…化。
+ JSON 常用于网页应用程åºï¼Œä»¥ä¼ é€ç»“构化数æ®ã€‚它也å¯ä½œä¸ºæ–‡ä»¶æ ¼å¼ç”¨äºŽæ•°æ®æŒä¹…化。
-2. RapidJSON是å¦ç¬¦åˆJSON标准?
+2. RapidJSON 是å¦ç¬¦åˆ JSON 标准?
- 是。RapidJSON完全符åˆ[RFC7159](http://www.ietf.org/rfc/rfc7159.txt)åŠ[ECMA-404](http://www.ecma-international.org/publications/standards/Ecma-404.htm)。它能处ç†ä¸€äº›ç‰¹æ®Šæƒ…况,例如支æŒJSON字符串中å«æœ‰ç©ºå­—符åŠä»£ç†å¯¹ï¼ˆsurrogate pair)。
+ 是。RapidJSON å®Œå…¨ç¬¦åˆ [RFC7159](http://www.ietf.org/rfc/rfc7159.txt) åŠ [ECMA-404](http://www.ecma-international.org/publications/standards/Ecma-404.htm)。它能处ç†ä¸€äº›ç‰¹æ®Šæƒ…å†µï¼Œä¾‹å¦‚æ”¯æŒ JSON 字符串中å«æœ‰ç©ºå­—符åŠä»£ç†å¯¹ï¼ˆsurrogate pair)。
-3. RapidJSON是å¦æ”¯æŒå®½æ¾çš„语法?
+3. RapidJSON 是å¦æ”¯æŒå®½æ¾çš„语法?
- 现时ä¸æ”¯æŒã€‚RapidJSONåªæ”¯æŒä¸¥æ ¼çš„标准格å¼ã€‚宽æ¾è¯­æ³•çŽ°æ—¶åœ¨è¿™[issue](https://github.com/miloyip/rapidjson/issues/36)中进行讨论。
+ 现时ä¸æ”¯æŒã€‚RapidJSON åªæ”¯æŒä¸¥æ ¼çš„标准格å¼ã€‚宽æ¾è¯­æ³•çŽ°æ—¶åœ¨è¿™ [issue](https://github.com/Tencent/rapidjson/issues/36) 中进行讨论。
-## DOM与SAX
+## DOM 与 SAX
-1. 什么是DOM风格API?
+1. 什么是 DOM 风格 API?
- Document Object Model(DOM)是一个储存于内存的JSON表示方å¼ï¼Œç”¨äºŽæŸ¥è¯¢åŠä¿®æ”¹JSON。
+ Document Object Model(DOM)是一个储存于内存的 JSON 表示方å¼ï¼Œç”¨äºŽæŸ¥è¯¢åŠä¿®æ”¹ JSON。
-2. 什么是SAX风格API?
+2. 什么是 SAX 风格 API?
- SAX是一个事件驱动的API,用于解æžåŠç”ŸæˆJSON。
+ SAX 是一个事件驱动的 API,用于解æžåŠç”Ÿæˆ JSON。
-3. 我应用DOM还是SAX?
+3. 我应用 DOM 还是 SAX?
- DOM易于查询åŠä¿®æ”¹ã€‚SAX则是éžå¸¸å¿«åŠçœå†…存的,但通常较难使用。
+ DOM 易于查询åŠä¿®æ”¹ã€‚SAX 则是éžå¸¸å¿«åŠçœå†…存的,但通常较难使用。
4. 什么是原ä½ï¼ˆ*in situ*)解æžï¼Ÿ
- 原ä½è§£æžä¼šæŠŠJSON字符串直接解ç è‡³è¾“入的JSON中。这是一个优化,å¯å‡å°‘内存消耗åŠæå‡æ€§èƒ½ï¼Œä½†è¾“入的JSON会被更改。进一步细节请å‚考[原ä½è§£æž](doc/dom.md) 。
+ 原ä½è§£æžä¼šæŠŠ JSON 字符串直接解ç è‡³è¾“入的 JSON 中。这是一个优化,å¯å‡å°‘内存消耗åŠæå‡æ€§èƒ½ï¼Œä½†è¾“入的 JSON 会被更改。进一步细节请å‚考 [原ä½è§£æž](doc/dom.zh-cn.md) 。
5. 什么时候会产生解æžé”™è¯¯ï¼Ÿ
- 当输入的JSON包å«éžæ³•è¯­æ³•ï¼Œæˆ–ä¸èƒ½è¡¨ç¤ºä¸€ä¸ªå€¼ï¼ˆå¦‚Number太大),或解æžå™¨çš„处ç†å™¨ä¸­æ–­è§£æžè¿‡ç¨‹ï¼Œè§£æžå™¨éƒ½ä¼šäº§ç”Ÿä¸€ä¸ªé”™è¯¯ã€‚详情请å‚考[解æžé”™è¯¯](doc/dom.md)。
+ 当输入的 JSON 包å«éžæ³•è¯­æ³•ï¼Œæˆ–ä¸èƒ½è¡¨ç¤ºä¸€ä¸ªå€¼ï¼ˆå¦‚ Number 太大),或解æžå™¨çš„处ç†å™¨ä¸­æ–­è§£æžè¿‡ç¨‹ï¼Œè§£æžå™¨éƒ½ä¼šäº§ç”Ÿä¸€ä¸ªé”™è¯¯ã€‚详情请å‚考 [解æžé”™è¯¯](doc/dom.zh-cn.md)。
6. 有什么错误信æ¯ï¼Ÿ
- 错误信æ¯å­˜å‚¨åœ¨`ParseResult`,它包å«é”™è¯¯ä»£å·åŠå移值(从JSON开始至错误处的字符数目)。å¯ä»¥æŠŠé”™è¯¯ä»£å·ç¿»è¯‘为人类å¯è¯»çš„错误讯æ¯ã€‚
+ 错误信æ¯å­˜å‚¨åœ¨ `ParseResult`,它包å«é”™è¯¯ä»£å·åŠå移值(从 JSON 开始至错误处的字符数目)。å¯ä»¥æŠŠé”™è¯¯ä»£å·ç¿»è¯‘为人类å¯è¯»çš„错误讯æ¯ã€‚
-7. 为何ä¸åªä½¿ç”¨`double`去表示JSON number?
+7. 为何ä¸åªä½¿ç”¨ `double` 去表示 JSON number?
- 一些应用需è¦ä½¿ç”¨64ä½æ— å·ï¼æœ‰å·æ•´æ•°ã€‚这些整数ä¸èƒ½æ— æŸåœ°è½¬æ¢æˆ`double`。因此解æžå™¨ä¼šæ£€æµ‹ä¸€ä¸ªJSON number是å¦èƒ½è½¬æ¢è‡³å„ç§æ•´æ•°ç±»åž‹åŠ`double`。
+ 一些应用需è¦ä½¿ç”¨ 64 ä½æ— å·ï¼æœ‰å·æ•´æ•°ã€‚这些整数ä¸èƒ½æ— æŸåœ°è½¬æ¢æˆ `double`。因此解æžå™¨ä¼šæ£€æµ‹ä¸€ä¸ª JSON number 是å¦èƒ½è½¬æ¢è‡³å„ç§æ•´æ•°ç±»åž‹åŠ `double`。
-8. 如何清空并最å°åŒ–`document`或`value`的容é‡ï¼Ÿ
+8. 如何清空并最å°åŒ– `document` 或 `value` 的容é‡ï¼Ÿ
- 调用 `SetXXX()` 方法 - 这些方法会调用æžæž„函数,并é‡å»ºç©ºçš„Object或Array:
+ 调用 `SetXXX()` 方法 - 这些方法会调用æžæž„函数,并é‡å»ºç©ºçš„ Object 或 Array:
~~~~~~~~~~cpp
Document d;
@@ -112,7 +112,7 @@
d.SetObject(); // clear and minimize
~~~~~~~~~~
- å¦å¤–,也å¯ä»¥å‚考在 [C++ swap with temporary idiom](https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Clear-and-minimize)中的一ç§ç­‰ä»·çš„方法:
+ å¦å¤–,也å¯ä»¥å‚考在 [C++ swap with temporary idiom](https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Clear-and-minimize) 中的一ç§ç­‰ä»·çš„方法:
~~~~~~~~~~cpp
Value(kObjectType).Swap(d);
~~~~~~~~~~
@@ -121,9 +121,9 @@
d.Swap(Value(kObjectType).Move());
~~~~~~~~~~
-9. 如何将一个`document`节点æ’入到å¦ä¸€ä¸ª`document`中?
+9. 如何将一个 `document` 节点æ’入到å¦ä¸€ä¸ª `document` 中?
- 比如有以下两个document(DOM):
+ 比如有以下两个 document(DOM):
~~~~~~~~~~cpp
Document person;
person.Parse("{\"person\":{\"name\":{\"first\":\"Adam\",\"last\":\"Thomas\"}}}");
@@ -131,7 +131,7 @@
Document address;
address.Parse("{\"address\":{\"city\":\"Moscow\",\"street\":\"Quiet\"}}");
~~~~~~~~~~
- å‡è®¾æˆ‘们希望将整个 `address` æ’入到`person`中,作为其的一个å­èŠ‚点:
+ å‡è®¾æˆ‘们希望将整个 `address` æ’入到 `person` 中,作为其的一个å­èŠ‚点:
~~~~~~~~~~js
{ "person": {
"name": { "first": "Adam", "last": "Thomas" },
@@ -140,22 +140,22 @@
}
~~~~~~~~~~
- 在æ’入节点的过程中需è¦æ³¨æ„`document`å’Œ`value`的生命周期并且正确地使用allocator进行内存分é…和管ç†ã€‚
+ 在æ’入节点的过程中需è¦æ³¨æ„ `document` å’Œ `value` 的生命周期并且正确地使用 allocator 进行内存分é…和管ç†ã€‚
- 一个简å•æœ‰æ•ˆçš„方法就是修改上述`address`å˜é‡çš„定义,让其使用`person`çš„allocatoråˆå§‹åŒ–,然åŽå°†å…¶æ·»åŠ åˆ°æ ¹èŠ‚点。
+ 一个简å•æœ‰æ•ˆçš„方法就是修改上述 `address` å˜é‡çš„定义,让其使用 `person` çš„ allocator åˆå§‹åŒ–,然åŽå°†å…¶æ·»åŠ åˆ°æ ¹èŠ‚点。
~~~~~~~~~~cpp
Documnet address(person.GetAllocator());
...
person["person"].AddMember("address", address["address"], person.GetAllocator());
~~~~~~~~~~
- 当然,如果你ä¸æƒ³é€šè¿‡æ˜¾å¼åœ°å†™å‡º`address`çš„keyæ¥å¾—到其值,å¯ä»¥ä½¿ç”¨è¿­ä»£å™¨æ¥å®žçŽ°:
+ 当然,如果你ä¸æƒ³é€šè¿‡æ˜¾å¼åœ°å†™å‡º `address` çš„ key æ¥å¾—到其值,å¯ä»¥ä½¿ç”¨è¿­ä»£å™¨æ¥å®žçŽ°:
~~~~~~~~~~cpp
auto addressRoot = address.MemberBegin();
person["person"].AddMember(addressRoot->name, addressRoot->value, person.GetAllocator());
~~~~~~~~~~
- 此外,还å¯ä»¥é€šè¿‡æ·±æ‹·è´address documentæ¥å®žçŽ°:
+ 此外,还å¯ä»¥é€šè¿‡æ·±æ‹·è´ address document æ¥å®žçŽ°:
~~~~~~~~~~cpp
Value addressValue = Value(address["address"], person.GetAllocator());
person["person"].AddMember("address", addressValue, person.GetAllocator());
@@ -163,128 +163,128 @@
## Document/Value (DOM)
-1. 什么是转移语æ„?为什么?
+1. 什么是转移语义?为什么?
- `Value`ä¸ç”¨å¤åˆ¶è¯­æ„,而使用了转移语æ„。这是指,当把æ¥æºå€¼èµ‹å€¼äºŽç›®æ ‡å€¼æ—¶ï¼Œæ¥æºå€¼çš„所有æƒä¼šè½¬ç§»è‡³ç›®æ ‡å€¼ã€‚
+ `Value` ä¸ç”¨å¤åˆ¶è¯­ä¹‰ï¼Œè€Œä½¿ç”¨äº†è½¬ç§»è¯­ä¹‰ã€‚这是指,当把æ¥æºå€¼èµ‹å€¼äºŽç›®æ ‡å€¼æ—¶ï¼Œæ¥æºå€¼çš„所有æƒä¼šè½¬ç§»è‡³ç›®æ ‡å€¼ã€‚
由于转移快于å¤åˆ¶ï¼Œæ­¤è®¾è®¡å†³å®šå¼ºè¿«ä½¿ç”¨è€…注æ„到å¤åˆ¶çš„消耗。
2. 怎样去å¤åˆ¶ä¸€ä¸ªå€¼ï¼Ÿ
- 有两个APIå¯ç”¨ï¼šå«allocator的构造函数,以åŠ`CopyFrom()`。å¯å‚考[æ·±å¤åˆ¶Value](doc/tutorial.md)里的用例。
+ 有两个 API å¯ç”¨ï¼šå« allocator çš„æž„é€ å‡½æ•°ï¼Œä»¥åŠ `CopyFrom()`。å¯å‚考 [æ·±å¤åˆ¶ Value](doc/tutorial.zh-cn.md) 里的用例。
3. 为什么我需è¦æ供字符串的长度?
- 由于C字符串是空字符结尾的,需è¦ä½¿ç”¨`strlen()`去计算其长度,这是线性å¤æ‚度的æ“作。若使用者已知字符串的长度,对很多æ“作æ¥è¯´ä¼šé€ æˆä¸å¿…è¦çš„消耗。
+ 由于 C 字符串是空字符结尾的,需è¦ä½¿ç”¨ `strlen()` 去计算其长度,这是线性å¤æ‚度的æ“作。若使用者已知字符串的长度,对很多æ“作æ¥è¯´ä¼šé€ æˆä¸å¿…è¦çš„消耗。
- 此外,RapidJSONå¯å¤„ç†å«æœ‰`\u0000`(空字符)的字符串。若一个字符串å«æœ‰ç©ºå­—符,`strlen()`便ä¸èƒ½è¿”回真正的字符串长度。在这ç§æƒ…况下使用者必须明确地æ供字符串长度。
+ 此外,RapidJSON å¯å¤„ç†å«æœ‰ `\u0000`(空字符)的字符串。若一个字符串å«æœ‰ç©ºå­—符,`strlen()` 便ä¸èƒ½è¿”回真正的字符串长度。在这ç§æƒ…况下使用者必须明确地æ供字符串长度。
-4. 为什么在许多DOMæ“作API中è¦æ供分é…器作为å‚数?
+4. 为什么在许多 DOM æ“作 API 中è¦æ供分é…器作为å‚数?
- 由于这些API是`Value`çš„æˆå‘˜å‡½æ•°ï¼Œæˆ‘们ä¸å¸Œæœ›ä¸ºæ¯ä¸ª`Value`储存一个分é…器指针。
+ 由于这些 API 是 `Value` çš„æˆå‘˜å‡½æ•°ï¼Œæˆ‘们ä¸å¸Œæœ›ä¸ºæ¯ä¸ª `Value` 储存一个分é…器指针。
5. 它会转æ¢å„ç§æ•°å€¼ç±»åž‹ä¹ˆï¼Ÿ
- 当使用`GetInt()`ã€`GetUint()`ç­‰API时,å¯èƒ½ä¼šå‘生转æ¢ã€‚对于整数至整数转æ¢ï¼Œä»…当ä¿è¯è½¬æ¢å®‰å…¨æ‰ä¼šè½¬æ¢ï¼ˆå¦åˆ™ä¼šæ–­è¨€å¤±è´¥ï¼‰ã€‚然而,当把一个64ä½æœ‰å·ï¼æ— å·æ•´æ•°è½¬æ¢è‡³double时,它会转æ¢ï¼Œä½†æœ‰å¯èƒ½ä¼šæŸå¤±ç²¾åº¦ã€‚å«æœ‰å°æ•°çš„æ•°å­—ã€æˆ–大于64ä½çš„整数,都åªèƒ½ä½¿ç”¨`GetDouble()`获å–其值。
+ 当使用 `GetInt()`ã€`GetUint()` ç­‰ API 时,å¯èƒ½ä¼šå‘生转æ¢ã€‚对于整数至整数转æ¢ï¼Œä»…当ä¿è¯è½¬æ¢å®‰å…¨æ‰ä¼šè½¬æ¢ï¼ˆå¦åˆ™ä¼šæ–­è¨€å¤±è´¥ï¼‰ã€‚然而,当把一个 64 ä½æœ‰å·ï¼æ— å·æ•´æ•°è½¬æ¢è‡³ double 时,它会转æ¢ï¼Œä½†æœ‰å¯èƒ½ä¼šæŸå¤±ç²¾åº¦ã€‚å«æœ‰å°æ•°çš„æ•°å­—ã€æˆ–大于 64 ä½çš„整数,都åªèƒ½ä½¿ç”¨ `GetDouble()` 获å–其值。
## Reader/Writer (SAX)
-1. 为什么ä¸ä»…仅用`printf`输出一个JSON?为什么需è¦`Writer`?
+1. 为什么ä¸ä»…仅用 `printf` 输出一个 JSONï¼Ÿä¸ºä»€ä¹ˆéœ€è¦ `Writer`?
- 最é‡è¦çš„是,`Writer`能确ä¿è¾“出的JSON是格å¼æ­£ç¡®çš„。错误地调用SAX事件(如`StartObject()`é”™é…`EndArray()`)会造æˆæ–­è¨€å¤±è´¥ã€‚此外,`Writer`会把字符串进行转义(如`\n`)。最åŽï¼Œ`printf()`的数值输出å¯èƒ½å¹¶ä¸æ˜¯ä¸€ä¸ªåˆæ³•çš„JSON number,特别是æŸäº›locale会有数字分隔符。而且`Writer`的数值字符串转æ¢æ˜¯ä½¿ç”¨éžå¸¸å¿«çš„算法æ¥å®žçŽ°çš„,胜过`printf()`åŠ`iostream`。
+ 最é‡è¦çš„是,`Writer` 能确ä¿è¾“出的 JSON 是格å¼æ­£ç¡®çš„。错误地调用 SAX 事件(如 `StartObject()` é”™é… `EndArray()`)会造æˆæ–­è¨€å¤±è´¥ã€‚此外,`Writer` 会把字符串进行转义(如 `\n`)。最åŽï¼Œ`printf()` 的数值输出å¯èƒ½å¹¶ä¸æ˜¯ä¸€ä¸ªåˆæ³•çš„ JSON number,特别是æŸäº› locale 会有数字分隔符。而且 `Writer` 的数值字符串转æ¢æ˜¯ä½¿ç”¨éžå¸¸å¿«çš„算法æ¥å®žçŽ°çš„,胜过 `printf()` åŠ `iostream`。
2. 我能å¦æš‚åœè§£æžè¿‡ç¨‹ï¼Œå¹¶åœ¨ç¨åŽç»§ç»­ï¼Ÿ
- 基于性能考虑,目å‰ç‰ˆæœ¬å¹¶ä¸ç›´æŽ¥æ”¯æŒæ­¤åŠŸèƒ½ã€‚然而,若执行环境支æŒå¤šçº¿ç¨‹ï¼Œä½¿ç”¨è€…å¯ä»¥åœ¨å¦ä¸€çº¿ç¨‹è§£æžJSON,并通过阻塞输入æµåŽ»æš‚åœã€‚
+ 基于性能考虑,目å‰ç‰ˆæœ¬å¹¶ä¸ç›´æŽ¥æ”¯æŒæ­¤åŠŸèƒ½ã€‚然而,若执行环境支æŒå¤šçº¿ç¨‹ï¼Œä½¿ç”¨è€…å¯ä»¥åœ¨å¦ä¸€çº¿ç¨‹è§£æž JSON,并通过阻塞输入æµåŽ»æš‚åœã€‚
## Unicode
-1. 它是å¦æ”¯æŒUTF-8ã€UTF-16åŠå…¶ä»–æ ¼å¼ï¼Ÿ
+1. 它是å¦æ”¯æŒ UTF-8ã€UTF-16 åŠå…¶ä»–æ ¼å¼ï¼Ÿ
- 是。它完全支æŒUTF-8ã€UTF-16(大端ï¼å°ç«¯ï¼‰ã€UTF-32(大端ï¼å°ç«¯ï¼‰åŠASCII。
+ æ˜¯ã€‚å®ƒå®Œå…¨æ”¯æŒ UTF-8ã€UTF-16(大端ï¼å°ç«¯ï¼‰ã€UTF-32(大端ï¼å°ç«¯ï¼‰åŠ ASCII。
2. 它能å¦æ£€æµ‹ç¼–ç çš„åˆæ³•æ€§ï¼Ÿ
- 能。åªéœ€æŠŠ`kParseValidateEncodingFlag`å‚考传给`Parse()`。若å‘现在输入æµä¸­æœ‰éžæ³•çš„ç¼–ç ï¼Œå®ƒå°±ä¼šäº§ç”Ÿ`kParseErrorStringInvalidEncoding`错误。
+ 能。åªéœ€æŠŠ `kParseValidateEncodingFlag` å‚考传给 `Parse()`。若å‘现在输入æµä¸­æœ‰éžæ³•çš„ç¼–ç ï¼Œå®ƒå°±ä¼šäº§ç”Ÿ `kParseErrorStringInvalidEncoding` 错误。
-3. 什么是代ç†å¯¹ï¼ˆsurrogate pair)?RapidJSON是å¦æ”¯æŒï¼Ÿ
+3. 什么是代ç†å¯¹ï¼ˆsurrogate pair)?RapidJSON 是å¦æ”¯æŒï¼Ÿ
- JSON使用UTF-16ç¼–ç åŽ»è½¬ä¹‰Unicode字符,例如`\u5927`表示中文字“大â€ã€‚è¦å¤„ç†åŸºæœ¬å¤šæ–‡ç§å¹³é¢ï¼ˆbasic multilingual plane,BMP)以外的字符时,UTF-16会把那些字符编ç æˆä¸¤ä¸ª16ä½å€¼ï¼Œè¿™ç§°ä¸ºUTF-16代ç†å¯¹ã€‚例如,绘文字字符U+1F602在JSON中å¯è¢«ç¼–ç æˆ`\uD83D\uDE02`。
+ JSON 使用 UTF-16 ç¼–ç åŽ»è½¬ä¹‰ Unicode 字符,例如 `\u5927` 表示中文字“大â€ã€‚è¦å¤„ç†åŸºæœ¬å¤šæ–‡ç§å¹³é¢ï¼ˆbasic multilingual plane,BMP)以外的字符时,UTF-16 会把那些字符编ç æˆä¸¤ä¸ª 16 ä½å€¼ï¼Œè¿™ç§°ä¸º UTF-16 代ç†å¯¹ã€‚例如,绘文字字符 U+1F602 在 JSON 中å¯è¢«ç¼–ç æˆ `\uD83D\uDE02`。
- RapidJSON完全支æŒè§£æžåŠç”ŸæˆUTF-16代ç†å¯¹ã€‚
+ RapidJSON 完全支æŒè§£æžåŠç”Ÿæˆ UTF-16 代ç†å¯¹ã€‚
-4. 它能å¦å¤„ç†JSON字符串中的`\u0000`(空字符)?
+4. 它能å¦å¤„ç† JSON 字符串中的 `\u0000`(空字符)?
- 能。RapidJSON完全支æŒJSON字符串中的空字符。然而,使用者需è¦æ³¨æ„到这件事,并使用`GetStringLength()`åŠç›¸å…³API去å–得字符串真正长度。
+ 能。RapidJSON å®Œå…¨æ”¯æŒ JSON 字符串中的空字符。然而,使用者需è¦æ³¨æ„到这件事,并使用 `GetStringLength()` åŠç›¸å…³ API 去å–得字符串真正长度。
-5. 能å¦å¯¹æ‰€æœ‰éžASCII字符输出æˆ`\uxxxx`å½¢å¼ï¼Ÿ
+5. 能å¦å¯¹æ‰€æœ‰éž ASCII å­—ç¬¦è¾“å‡ºæˆ `\uxxxx` å½¢å¼ï¼Ÿ
- å¯ä»¥ã€‚åªè¦åœ¨`Writer`中使用`ASCII<>`作为输出编ç å‚数,就å¯ä»¥å¼ºé€¼è½¬ä¹‰é‚£äº›å­—符。
+ å¯ä»¥ã€‚åªè¦åœ¨ `Writer` 中使用 `ASCII<>` 作为输出编ç å‚数,就å¯ä»¥å¼ºé€¼è½¬ä¹‰é‚£äº›å­—符。
## æµ
-1. 我有一个很大的JSON文件。我应å¦æŠŠå®ƒæ•´ä¸ªè½½å…¥å†…存中?
+1. 我有一个很大的 JSON 文件。我应å¦æŠŠå®ƒæ•´ä¸ªè½½å…¥å†…存中?
- 使用者å¯ä½¿ç”¨`FileReadStream`去é€å—读入文件。但若使用于原ä½è§£æžï¼Œå¿…须载入整个文件。
+ 使用者å¯ä½¿ç”¨ `FileReadStream` 去é€å—读入文件。但若使用于原ä½è§£æžï¼Œå¿…须载入整个文件。
-2. 我能å¦è§£æžä¸€ä¸ªä»Žç½‘络上串æµè¿›æ¥çš„JSON?
+2. 我能å¦è§£æžä¸€ä¸ªä»Žç½‘络上串æµè¿›æ¥çš„ JSON?
- å¯ä»¥ã€‚使用者å¯æ ¹æ®`FileReadStream`的实现,去实现一个自定义的æµã€‚
+ å¯ä»¥ã€‚使用者å¯æ ¹æ® `FileReadStream` 的实现,去实现一个自定义的æµã€‚
-3. 我ä¸çŸ¥é“一些JSON将会使用哪ç§ç¼–ç ã€‚怎样处ç†å®ƒä»¬ï¼Ÿ
+3. 我ä¸çŸ¥é“一些 JSON 将会使用哪ç§ç¼–ç ã€‚怎样处ç†å®ƒä»¬ï¼Ÿ
- ä½ å¯ä»¥ä½¿ç”¨`AutoUTFInputStream`,它能自动检测输入æµçš„ç¼–ç ã€‚然而,它会带æ¥ä¸€äº›æ€§èƒ½å¼€é”€ã€‚
+ ä½ å¯ä»¥ä½¿ç”¨ `AutoUTFInputStream`,它能自动检测输入æµçš„ç¼–ç ã€‚然而,它会带æ¥ä¸€äº›æ€§èƒ½å¼€é”€ã€‚
-4. 什么是BOM?RapidJSON怎样处ç†å®ƒï¼Ÿ
+4. 什么是 BOM?RapidJSON 怎样处ç†å®ƒï¼Ÿ
- [字节顺åºæ ‡è®°ï¼ˆbyte order mark, BOM)](http://en.wikipedia.org/wiki/Byte_order_mark)有时会出现于文件ï¼æµçš„开始,以表示其UTFç¼–ç ç±»åž‹ã€‚
+ [字节顺åºæ ‡è®°ï¼ˆbyte order mark, BOM)](http://en.wikipedia.org/wiki/Byte_order_mark) 有时会出现于文件ï¼æµçš„开始,以表示其 UTF ç¼–ç ç±»åž‹ã€‚
- RapidJSONçš„`EncodedInputStream`å¯æ£€æµ‹ï¼è·³è¿‡BOM。`EncodedOutputStream`å¯é€‰æ‹©æ˜¯å¦å†™å…¥BOM。å¯å‚考[ç¼–ç æµ](doc/stream.md)中的例å­ã€‚
+ RapidJSON çš„ `EncodedInputStream` å¯æ£€æµ‹ï¼è·³è¿‡ BOM。`EncodedOutputStream` å¯é€‰æ‹©æ˜¯å¦å†™å…¥ BOM。å¯å‚考 [ç¼–ç æµ](doc/stream.zh-cn.md) 中的例å­ã€‚
5. 为什么会涉åŠå¤§ç«¯ï¼å°ç«¯ï¼Ÿ
- æµçš„大端ï¼å°ç«¯æ˜¯UTF-16åŠUTF-32æµè¦å¤„ç†çš„问题,而UTF-8ä¸éœ€è¦å¤„ç†ã€‚
+ æµçš„大端ï¼å°ç«¯æ˜¯ UTF-16 åŠ UTF-32 æµè¦å¤„ç†çš„问题,而 UTF-8 ä¸éœ€è¦å¤„ç†ã€‚
## 性能
-1. RapidJSON是å¦çœŸçš„快?
+1. RapidJSON 是å¦çœŸçš„快?
- 是。它å¯èƒ½æ˜¯æœ€å¿«çš„å¼€æºJSON库。有一个[评测](https://github.com/miloyip/nativejson-benchmark)评估C/C++ JSON库的性能。
+ 是。它å¯èƒ½æ˜¯æœ€å¿«çš„å¼€æº JSON 库。有一个 [评测](https://github.com/miloyip/nativejson-benchmark) 评估 C/C++ JSON 库的性能。
2. 为什么它会快?
- RapidJSON的许多设计是针对时间ï¼ç©ºé—´æ€§èƒ½æ¥è®¾è®¡çš„,这些决定å¯èƒ½ä¼šå½±å“API的易用性。此外,它也使用了许多底层优化(内部函数ï¼intrinsicã€SIMD)åŠç‰¹åˆ«çš„算法(自定义的double至字符串转æ¢ã€å­—符串至double的转æ¢ï¼‰ã€‚
+ RapidJSON 的许多设计是针对时间ï¼ç©ºé—´æ€§èƒ½æ¥è®¾è®¡çš„,这些决定å¯èƒ½ä¼šå½±å“ API 的易用性。此外,它也使用了许多底层优化(内部函数ï¼intrinsicã€SIMD)åŠç‰¹åˆ«çš„算法(自定义的 double 至字符串转æ¢ã€å­—符串至 double 的转æ¢ï¼‰ã€‚
-3. 什是是SIMD?它如何用于RapidJSON?
+3. 什是是 SIMD?它如何用于 RapidJSON?
- [SIMD](http://en.wikipedia.org/wiki/SIMD)指令å¯ä»¥åœ¨çŽ°ä»£CPU中执行并行è¿ç®—。RapidJSON支æŒäº†Intelçš„SSE2/SSE4.2去加速跳过空白字符。在解æžå«ç¼©è¿›çš„JSON时,这能æå‡æ€§èƒ½ã€‚åªè¦å®šä¹‰å为`RAPIDJSON_SSE2`或`RAPIDJSON_SSE42`çš„å®ï¼Œå°±èƒ½å¯åŠ¨è¿™ä¸ªåŠŸèƒ½ã€‚然而,若在ä¸æ”¯æŒè¿™äº›æŒ‡ä»¤é›†çš„机器上执行这些å¯æ‰§è¡Œæ–‡ä»¶ï¼Œä¼šå¯¼è‡´å´©æºƒã€‚
+ [SIMD](http://en.wikipedia.org/wiki/SIMD) 指令å¯ä»¥åœ¨çŽ°ä»£ CPU 中执行并行è¿ç®—。RapidJSON 支æŒä½¿ç”¨ Intel çš„ SSE2/SSE4.2 å’Œ ARM çš„ Neon æ¥åŠ é€Ÿå¯¹ç©ºç™½ç¬¦ã€åˆ¶è¡¨ç¬¦ã€å›žè½¦ç¬¦å’Œæ¢è¡Œç¬¦çš„过滤处ç†ã€‚在解æžå«ç¼©è¿›çš„ JSON 时,这能æå‡æ€§èƒ½ã€‚åªè¦å®šä¹‰å为 `RAPIDJSON_SSE2` ,`RAPIDJSON_SSE42` 或 `RAPIDJSON_NEON` çš„å®ï¼Œå°±èƒ½å¯åŠ¨è¿™ä¸ªåŠŸèƒ½ã€‚然而,若在ä¸æ”¯æŒè¿™äº›æŒ‡ä»¤é›†çš„机器上执行这些å¯æ‰§è¡Œæ–‡ä»¶ï¼Œä¼šå¯¼è‡´å´©æºƒã€‚
4. 它会消耗许多内存么?
- RapidJSON的设计目标是å‡ä½Žå†…å­˜å ç”¨ã€‚
+ RapidJSON 的设计目标是å‡ä½Žå†…å­˜å ç”¨ã€‚
- 在SAX API中,`Reader`消耗的内存与JSON树深度加上最长JSON字符æˆæ­£æ¯”。
+ 在 SAX API 中,`Reader` 消耗的内存与 JSON 树深度加上最长 JSON 字符æˆæ­£æ¯”。
- 在DOM API中,æ¯ä¸ª`Value`在32/64ä½æž¶æž„下分别消耗16/24字节。RapidJSON也使用一个特殊的内存分é…器去å‡å°‘分é…çš„é¢å¤–开销。
+ 在 DOM API 中,æ¯ä¸ª `Value` 在 32/64 ä½æž¶æž„下分别消耗 16/24 字节。RapidJSON 也使用一个特殊的内存分é…器去å‡å°‘分é…çš„é¢å¤–开销。
5. 高性能的æ„义何在?
- 有些应用程åºéœ€è¦å¤„ç†éžå¸¸å¤§çš„JSON文件。而有些åŽå°åº”用程åºéœ€è¦å¤„ç†å¤§é‡çš„JSON。达到高性能åŒæ—¶æ”¹å–„延时åŠåžåé‡ã€‚更广义æ¥è¯´ï¼Œè¿™ä¹Ÿå¯ä»¥èŠ‚çœèƒ½æºã€‚
+ 有些应用程åºéœ€è¦å¤„ç†éžå¸¸å¤§çš„ JSON 文件。而有些åŽå°åº”用程åºéœ€è¦å¤„ç†å¤§é‡çš„ JSON。达到高性能åŒæ—¶æ”¹å–„延时åŠåžåé‡ã€‚更广义æ¥è¯´ï¼Œè¿™ä¹Ÿå¯ä»¥èŠ‚çœèƒ½æºã€‚
## 八挂
-1. è°æ˜¯RapidJSONçš„å¼€å‘者?
+1. è°æ˜¯ RapidJSON çš„å¼€å‘者?
- å¶åŠ²å³°ï¼ˆMilo Yip,[miloyip](https://github.com/miloyip))是RapidJSON的原作者。全世界许多贡献者一直在改善RapidJSON。Philipp A. Hartmann([pah](https://github.com/pah))实现了许多改进,也设置了自动化测试,而且还å‚与许多社区讨论。ä¸æ¬§å—(Don Ding,[thebusytypist](https://github.com/thebusytypist))实现了迭代å¼è§£æžå™¨ã€‚Andrii Senkovych([jollyroger](https://github.com/jollyroger))完æˆäº†å‘CMakeçš„è¿ç§»ã€‚Kosta([Kosta-Github](https://github.com/Kosta-Github))æ供了一个éžå¸¸çµå·§çš„短字符串优化。也需è¦æ„Ÿè°¢å…¶ä»–献者åŠç¤¾åŒºæˆå‘˜ã€‚
+ å¶åŠ²å³°ï¼ˆMilo Yip,[miloyip](https://github.com/miloyip))是 RapidJSON 的原作者。全世界许多贡献者一直在改善 RapidJSON。Philipp A. Hartmann([pah](https://github.com/pah))实现了许多改进,也设置了自动化测试,而且还å‚与许多社区讨论。ä¸æ¬§å—(Don Ding,[thebusytypist](https://github.com/thebusytypist))实现了迭代å¼è§£æžå™¨ã€‚Andrii Senkovych([jollyroger](https://github.com/jollyroger))完æˆäº†å‘ CMake çš„è¿ç§»ã€‚Kosta([Kosta-Github](https://github.com/Kosta-Github))æ供了一个éžå¸¸çµå·§çš„短字符串优化。也需è¦æ„Ÿè°¢å…¶ä»–献者åŠç¤¾åŒºæˆå‘˜ã€‚
-2. 为何你è¦å¼€å‘RapidJSON?
+2. 为何你è¦å¼€å‘ RapidJSON?
- 在2011年开始这项目是,它仅一个兴趣项目。Milo Yip是一个游æˆç¨‹åºå‘˜ï¼Œä»–在那时候认识到JSON并希望在未æ¥çš„项目中使用。由于JSON好åƒå¾ˆç®€å•ï¼Œä»–希望写一个仅有头文件并且快速的程åºåº“。
+ 在 2011 年开始这项目是,它仅一个兴趣项目。Milo Yip 是一个游æˆç¨‹åºå‘˜ï¼Œä»–在那时候认识到 JSON 并希望在未æ¥çš„项目中使用。由于 JSON 好åƒå¾ˆç®€å•ï¼Œä»–希望写一个仅有头文件并且快速的程åºåº“。
3. 为什么开å‘中段有一段长期空档?
- 主è¦æ˜¯ä¸ªäººå› ç´ ï¼Œä¾‹å¦‚加入新家庭æˆå‘˜ã€‚å¦å¤–,Milo Yip也花了许多业馀时间去翻译Jason Gregory的《Game Engine Architecture》至中文版《游æˆå¼•æ“Žæž¶æž„》。
+ 主è¦æ˜¯ä¸ªäººå› ç´ ï¼Œä¾‹å¦‚加入新家庭æˆå‘˜ã€‚å¦å¤–,Milo Yip 也花了许多业馀时间去翻译 Jason Gregory 的《Game Engine Architecture》至中文版《游æˆå¼•æ“Žæž¶æž„》。
-4. 为什么这个项目从Google Codeæ¬åˆ°GitHub?
+4. 为什么这个项目从 Google Code æ¬åˆ° GitHub?
- 这是大势所趋,而且GitHub更为强大åŠæ–¹ä¾¿ã€‚
+ 这是大势所趋,而且 GitHub 更为强大åŠæ–¹ä¾¿ã€‚
diff --git a/doc/features.md b/doc/features.md
index 6b529a77..0d79e7f8 100644
--- a/doc/features.md
+++ b/doc/features.md
@@ -20,11 +20,16 @@
## Standard compliance
* RapidJSON should be fully RFC4627/ECMA-404 compliance.
+* Support JSON Pointer (RFC6901).
+* Support JSON Schema Draft v4.
* Support Unicode surrogate.
* Support null character (`"\u0000"`)
* For example, `["Hello\u0000World"]` can be parsed and handled gracefully. There is API for getting/setting lengths of string.
* Support optional relaxed syntax.
- * Single line (`// ...`) and multiple line (`/* ... */`) comments.
+ * Single line (`// ...`) and multiple line (`/* ... */`) comments (`kParseCommentsFlag`).
+ * Trailing commas at the end of objects and arrays (`kParseTrailingCommasFlag`).
+ * `NaN`, `Inf`, `Infinity`, `-Inf` and `-Infinity` as `double` values (`kParseNanAndInfFlag`)
+* [NPM compliant](http://github.com/Tencent/rapidjson/blob/master/doc/npm.md).
## Unicode
@@ -68,7 +73,7 @@
* Only store pointer instead of copying
* Optimization for "short" strings
* Store short string in `Value` internally without additional allocation.
- * For UTF-8 string: maximum 11 characters in 32-bit, 15 characters in 64-bit.
+ * For UTF-8 string: maximum 11 characters in 32-bit, 21 characters in 64-bit (13 characters in x86-64).
* Optionally support `std::string` (define `RAPIDJSON_HAS_STDSTRING=1`)
## Generation
@@ -96,3 +101,4 @@
* Some C++11 support (optional)
* Rvalue reference
* `noexcept` specifier
+ * Range-based for loop
diff --git a/doc/features.zh-cn.md b/doc/features.zh-cn.md
index 85a7db16..7662cc13 100644
--- a/doc/features.zh-cn.md
+++ b/doc/features.zh-cn.md
@@ -3,49 +3,54 @@
## 总体
* 跨平å°
- * 编译器:Visual Studioã€gccã€clangç­‰
- * 架构:x86ã€x64ã€ARMç­‰
- * æ“作系统:Windowsã€Mac OS Xã€Linuxã€iOSã€Androidç­‰
+ * 编译器:Visual Studioã€gccã€clang ç­‰
+ * 架构:x86ã€x64ã€ARM ç­‰
+ * æ“作系统:Windowsã€Mac OS Xã€Linuxã€iOSã€Android ç­‰
* 容易安装
* åªæœ‰å¤´æ–‡ä»¶çš„库。åªéœ€æŠŠå¤´æ–‡ä»¶å¤åˆ¶è‡³ä½ çš„项目中。
* 独立ã€æœ€å°ä¾èµ–
- * ä¸éœ€ä¾èµ–STLã€BOOST等。
- * åªåŒ…å«`<cstdio>`, `<cstdlib>`, `<cstring>`, `<inttypes.h>`, `<new>`, `<stdint.h>`。
-* 没使用C++异常ã€RTTI
+ * ä¸éœ€ä¾èµ– STLã€BOOST 等。
+ * åªåŒ…å« `<cstdio>`, `<cstdlib>`, `<cstring>`, `<inttypes.h>`, `<new>`, `<stdint.h>`。
+* 没使用 C++ 异常ã€RTTI
* 高性能
* 使用模版åŠå†…è”函数去é™ä½Žå‡½æ•°è°ƒç”¨å¼€é”€ã€‚
- * 内部ç»ä¼˜åŒ–çš„Grisu2åŠæµ®ç‚¹æ•°è§£æžå®žçŽ°ã€‚
- * å¯é€‰çš„SSE2/SSE4.2支æŒã€‚
+ * 内部ç»ä¼˜åŒ–çš„ Grisu2 åŠæµ®ç‚¹æ•°è§£æžå®žçŽ°ã€‚
+ * å¯é€‰çš„ SSE2/SSE4.2 支æŒã€‚
## 符åˆæ ‡å‡†
-* RapidJSON应完全符åˆRFC4627/ECMA-404标准。
-* 支æŒUnicod代ç†å¯¹ï¼ˆsurrogate pair)。
+* RapidJSON åº”å®Œå…¨ç¬¦åˆ RFC4627/ECMA-404 标准。
+* æ”¯æŒ JSON Pointer (RFC6901).
+* æ”¯æŒ JSON Schema Draft v4.
+* æ”¯æŒ Unicode 代ç†å¯¹ï¼ˆsurrogate pair)。
* 支æŒç©ºå­—符(`"\u0000"`)。
- * 例如,å¯ä»¥ä¼˜é›…地解æžåŠå¤„ç†`["Hello\u0000World"]`。å«è¯»å†™å­—符串长度的API。
-* 支æŒæ”¾å®½çš„å¯é€‰è¯­æ³•
- * å•è¡Œï¼ˆ`// ...`)åŠå¤šè¡Œï¼ˆ`/* ... */`) 注释。
+ * 例如,å¯ä»¥ä¼˜é›…地解æžåŠå¤„ç† `["Hello\u0000World"]`。å«è¯»å†™å­—符串长度的 API。
+* 支æŒå¯é€‰çš„放宽语法
+ * å•è¡Œï¼ˆ`// ...`)åŠå¤šè¡Œï¼ˆ`/* ... */`) 注释 (`kParseCommentsFlag`)。
+ * 在对象和数组结æŸå‰å«é€—å· (`kParseTrailingCommasFlag`)。
+ * `NaN`ã€`Inf`ã€`Infinity`ã€`-Inf` åŠ `-Infinity` 作为 `double` 值 (`kParseNanAndInfFlag`)
+* [NPM 兼容](https://github.com/Tencent/rapidjson/blob/master/doc/npm.md).
## Unicode
-* 支æŒUTF-8ã€UTF-16ã€UTF-32ç¼–ç ï¼ŒåŒ…括å°ç«¯åºå’Œå¤§ç«¯åºã€‚
+* æ”¯æŒ UTF-8ã€UTF-16ã€UTF-32 ç¼–ç ï¼ŒåŒ…括å°ç«¯åºå’Œå¤§ç«¯åºã€‚
* 这些编ç ç”¨äºŽè¾“入输出æµï¼Œä»¥åŠå†…存中的表示。
* 支æŒä»Žè¾“å…¥æµè‡ªåŠ¨æ£€æµ‹ç¼–ç ã€‚
* 内部支æŒç¼–ç çš„转æ¢ã€‚
- * 例如,你å¯ä»¥è¯»å–一个UTF-8文件,让RapidJSON把JSON字符串转æ¢è‡³UTF-16çš„DOM。
+ * 例如,你å¯ä»¥è¯»å–一个 UTF-8 文件,让 RapidJSON 把 JSON 字符串转æ¢è‡³ UTF-16 çš„ DOM。
* 内部支æŒç¼–ç æ ¡éªŒã€‚
- * 例如,你å¯ä»¥è¯»å–一个UTF-8文件,让RapidJSON检查是å¦æ‰€æœ‰JSON字符串是åˆæ³•çš„UTF-8字节åºåˆ—。
+ * 例如,你å¯ä»¥è¯»å–一个 UTF-8 文件,让 RapidJSON 检查是å¦æ‰€æœ‰ JSON 字符串是åˆæ³•çš„ UTF-8 字节åºåˆ—。
* 支æŒè‡ªå®šä¹‰çš„字符类型。
- * 预设的字符类型是:UTF-8为`char`,UTF-16为`wchar_t`,UTF32为`uint32_t`。
+ * 预设的字符类型是:UTF-8 为 `char`,UTF-16 为 `wchar_t`,UTF32 为 `uint32_t`。
* 支æŒè‡ªå®šä¹‰çš„ç¼–ç ã€‚
-## API风格
+## API 风格
-* SAX(Simple API for XML)风格API
- * 类似于[SAX](http://en.wikipedia.org/wiki/Simple_API_for_XML), RapidJSONæ供一个事件循åºè®¿é—®çš„解æžå™¨API(`rapidjson::GenericReader`)。RapidJSON也æ供一个生æˆå™¨API(`rapidjson::Writer`),å¯ä»¥å¤„ç†ç›¸åŒçš„事件集åˆã€‚
-* DOM(Document Object Model)风格API
- * 类似于HTMLï¼XMLçš„[DOM](http://en.wikipedia.org/wiki/Document_Object_Model),RapidJSONå¯æŠŠJSON解æžè‡³ä¸€ä¸ªDOM表示方å¼ï¼ˆ`rapidjson::GenericDocument`),以方便æ“作。如有需è¦ï¼Œå¯æŠŠDOM转æ¢ï¼ˆstringify)回JSON。
- * DOM风格API(`rapidjson::GenericDocument`)实际上是由SAX风格API(`rapidjson::GenericReader`)实现的。SAX更快,但有时DOM更易用。用户å¯æ ¹æ®æƒ…况作出选择。
+* SAX(Simple API for XML)风格 API
+ * 类似于 [SAX](http://en.wikipedia.org/wiki/Simple_API_for_XML), RapidJSON æ供一个事件循åºè®¿é—®çš„解æžå™¨ API(`rapidjson::GenericReader`)。RapidJSON 也æ供一个生æˆå™¨ API(`rapidjson::Writer`),å¯ä»¥å¤„ç†ç›¸åŒçš„事件集åˆã€‚
+* DOM(Document Object Model)风格 API
+ * 类似于 HTMLï¼XML çš„ [DOM](http://en.wikipedia.org/wiki/Document_Object_Model),RapidJSON å¯æŠŠ JSON 解æžè‡³ä¸€ä¸ª DOM 表示方å¼ï¼ˆ`rapidjson::GenericDocument`),以方便æ“作。如有需è¦ï¼Œå¯æŠŠ DOM 转æ¢ï¼ˆstringify)回 JSON。
+ * DOM 风格 API(`rapidjson::GenericDocument`)实际上是由 SAX 风格 API(`rapidjson::GenericReader`)实现的。SAX 更快,但有时 DOM 更易用。用户å¯æ ¹æ®æƒ…况作出选择。
## 解æž
@@ -53,45 +58,46 @@
* 递归å¼è§£æžå™¨è¾ƒå¿«ï¼Œä½†åœ¨æžç«¯æƒ…况下å¯å‡ºçŽ°å †æ ˆæº¢å‡ºã€‚
* 迭代å¼è§£æžå™¨ä½¿ç”¨è‡ªå®šä¹‰çš„堆栈去维æŒè§£æžçŠ¶æ€ã€‚
* 支æŒåŽŸä½ï¼ˆ*in situ*)解æžã€‚
- * 把JSON字符串的值解æžè‡³åŽŸJSON之中,然åŽè®©DOM指å‘那些字符串。
+ * 把 JSON 字符串的值解æžè‡³åŽŸ JSON 之中,然åŽè®© DOM 指å‘那些字符串。
* 比常规分æžæ›´å¿«ï¼šä¸éœ€å­—符串的内存分é…ã€ä¸éœ€å¤åˆ¶ï¼ˆå¦‚字符串ä¸å«è½¬ä¹‰ç¬¦ï¼‰ã€ç¼“å­˜å‹å¥½ã€‚
-* 对于JSON数字类型,支æŒ32-bit/64-bit的有å·ï¼æ— å·æ•´æ•°ï¼Œä»¥åŠ`double`。
+* 对于 JSON æ•°å­—ç±»åž‹ï¼Œæ”¯æŒ 32-bit/64-bit 的有å·ï¼æ— å·æ•´æ•°ï¼Œä»¥åŠ `double`。
* 错误处ç†
* 支æŒè¯¦å°½çš„解æžé”™è¯¯ä»£å·ã€‚
* 支æŒæœ¬åœ°åŒ–错误信æ¯ã€‚
## DOM (Document)
-* RapidJSON在类型转æ¢æ—¶ä¼šæ£€æŸ¥æ•°å€¼çš„范围。
+* RapidJSON 在类型转æ¢æ—¶ä¼šæ£€æŸ¥æ•°å€¼çš„范围。
* 字符串字é¢é‡çš„优化
* åªå‚¨å­˜æŒ‡é’ˆï¼Œä¸ä½œå¤åˆ¶
* 优化“短â€å­—符串
- * 在`Value`内储存短字符串,无需é¢å¤–分é…。
- * 对UTF-8字符串æ¥è¯´ï¼Œ32ä½æž¶æž„下å¯å­˜å‚¨æœ€å¤š11字符,64ä½ä¸‹15字符。
-* å¯é€‰åœ°æ”¯æŒ`std::string`(定义`RAPIDJSON_HAS_STDSTRING=1`)
+ * 在 `Value` 内储存短字符串,无需é¢å¤–分é…。
+ * 对 UTF-8 字符串æ¥è¯´ï¼Œ32 ä½æž¶æž„下å¯å­˜å‚¨æœ€å¤š 11 字符,64 ä½ä¸‹ 21 字符(x86-64 下 13 字符)。
+* å¯é€‰åœ°æ”¯æŒ `std::string`(定义 `RAPIDJSON_HAS_STDSTRING=1`)
## 生æˆ
-* 支æŒ`rapidjson::PrettyWriter`去加入æ¢è¡ŒåŠç¼©è¿›ã€‚
+* æ”¯æŒ `rapidjson::PrettyWriter` 去加入æ¢è¡ŒåŠç¼©è¿›ã€‚
## 输入输出æµ
-* 支æŒ`rapidjson::GenericStringBuffer`,把输出的JSON储存于字符串内。
-* 支æŒ`rapidjson::FileReadStream`åŠ`rapidjson::FileWriteStream`,使用`FILE`对象作输入输出。
+* æ”¯æŒ `rapidjson::GenericStringBuffer`,把输出的 JSON 储存于字符串内。
+* æ”¯æŒ `rapidjson::FileReadStream` åŠ `rapidjson::FileWriteStream`,使用 `FILE` 对象作输入输出。
* 支æŒè‡ªå®šä¹‰è¾“入输出æµã€‚
## 内存
-* 最å°åŒ–DOM的内存开销。
- * 对大部分32ï¼64ä½æœºå™¨è€Œè¨€ï¼Œæ¯ä¸ªJSON值åªå 16或20字节(ä¸åŒ…å«å­—符串)。
+* 最å°åŒ– DOM 的内存开销。
+ * 对大部分 32ï¼64 ä½æœºå™¨è€Œè¨€ï¼Œæ¯ä¸ª JSON 值åªå  16 或 20 字节(ä¸åŒ…å«å­—符串)。
* 支æŒå¿«é€Ÿçš„预设分é…器。
* 它是一个堆栈形å¼çš„分é…器(顺åºåˆ†é…,ä¸å®¹è®¸å•ç‹¬é‡Šæ”¾ï¼Œé€‚åˆè§£æžè¿‡ç¨‹ä¹‹ç”¨ï¼‰ã€‚
- * 使用者也å¯æ供一个预分é…的缓冲区。(有å¯èƒ½è¾¾è‡³æ— éœ€CRT分é…就能解æžå¤šä¸ªJSON)
-* 支æŒæ ‡å‡†CRT(C-runtime)分é…器。
+ * 使用者也å¯æ供一个预分é…的缓冲区。(有å¯èƒ½è¾¾è‡³æ— éœ€ CRT 分é…就能解æžå¤šä¸ª JSON)
+* 支æŒæ ‡å‡† CRT(C-runtime)分é…器。
* 支æŒè‡ªå®šä¹‰åˆ†é…器。
## 其他
-* 一些C++11的支æŒï¼ˆå¯é€‰ï¼‰
+* 一些 C++11 的支æŒï¼ˆå¯é€‰ï¼‰
* å³å€¼å¼•ç”¨ï¼ˆrvalue reference)
- * `noexcept`修饰符
+ * `noexcept` 修饰符
+ * 范围 for 循环
diff --git a/doc/internals.md b/doc/internals.md
index 174a03a2..9b94d7ff 100644
--- a/doc/internals.md
+++ b/doc/internals.md
@@ -114,7 +114,7 @@ Number is a bit more complicated. For normal integer values, it can contains `kI
## Short-String Optimization {#ShortString}
- Kosta (@Kosta-Github) provided a very neat short-string optimization. The optimization idea is given as follow. Excluding the `flags_`, a `Value` has 12 or 16 bytes (32-bit or 64-bit) for storing actual data. Instead of storing a pointer to a string, it is possible to store short strings in these space internally. For encoding with 1-byte character type (e.g. `char`), it can store maximum 11 or 15 characters string inside the `Value` type.
+ [Kosta](https://github.com/Kosta-Github) provided a very neat short-string optimization. The optimization idea is given as follow. Excluding the `flags_`, a `Value` has 12 or 16 bytes (32-bit or 64-bit) for storing actual data. Instead of storing a pointer to a string, it is possible to store short strings in these space internally. For encoding with 1-byte character type (e.g. `char`), it can store maximum 11 or 15 characters string inside the `Value` type.
| ShortString (Ch=char) | |32-bit|64-bit|
|---------------------|-------------------------------------|:----:|:----:|
@@ -126,7 +126,7 @@ A special technique is applied. Instead of storing the length of string directly
This optimization can reduce memory usage for copy-string. It can also improve cache-coherence thus improve runtime performance.
-# Allocator {#Allocator}
+# Allocator {#InternalAllocator}
`Allocator` is a concept in RapidJSON:
~~~cpp
@@ -158,7 +158,7 @@ Note that `Malloc()` and `Realloc()` are member functions but `Free()` is static
Internally, it allocates chunks of memory from the base allocator (by default `CrtAllocator`) and stores the chunks as a singly linked list. When user requests an allocation, it allocates memory from the following order:
-1. User supplied buffer if it is available. (See [User Buffer section in DOM](dom.md))
+1. User supplied buffer if it is available. (See [User Buffer section in DOM](doc/dom.md))
2. If user supplied buffer is full, use the current memory chunk.
3. If the current block is full, allocate a new block of memory.
@@ -183,17 +183,20 @@ void SkipWhitespace(InputStream& s) {
However, this requires 4 comparisons and a few branching for each character. This was found to be a hot spot.
-To accelerate this process, SIMD was applied to compare 16 characters with 4 white spaces for each iteration. Currently RapidJSON only supports SSE2 and SSE4.2 instructions for this. And it is only activated for UTF-8 memory streams, including string stream or *in situ* parsing.
+To accelerate this process, SIMD was applied to compare 16 characters with 4 white spaces for each iteration. Currently RapidJSON supports SSE2, SSE4.2 and ARM Neon instructions for this. And it is only activated for UTF-8 memory streams, including string stream or *in situ* parsing.
-To enable this optimization, need to define `RAPIDJSON_SSE2` or `RAPIDJSON_SSE42` before including `rapidjson.h`. Some compilers can detect the setting, as in `perftest.h`:
+To enable this optimization, need to define `RAPIDJSON_SSE2`, `RAPIDJSON_SSE42` or `RAPIDJSON_NEON` before including `rapidjson.h`. Some compilers can detect the setting, as in `perftest.h`:
~~~cpp
// __SSE2__ and __SSE4_2__ are recognized by gcc, clang, and the Intel compiler.
// We use -march=native with gmake to enable -msse2 and -msse4.2, if supported.
+// Likewise, __ARM_NEON is used to detect Neon.
#if defined(__SSE4_2__)
# define RAPIDJSON_SSE42
#elif defined(__SSE2__)
# define RAPIDJSON_SSE2
+#elif defined(__ARM_NEON)
+# define RAPIDJSON_NEON
#endif
~~~
@@ -211,7 +214,7 @@ In [Intel® 64 and IA-32 Architectures Optimization Reference Manual
This is not feasible as RapidJSON should not enforce such requirement.
-To fix this issue, currently the routine process bytes up to the next aligned address. After tha, use aligned read to perform SIMD processing. Also see [#85](https://github.com/miloyip/rapidjson/issues/85).
+To fix this issue, currently the routine process bytes up to the next aligned address. After tha, use aligned read to perform SIMD processing. Also see [#85](https://github.com/Tencent/rapidjson/issues/85).
## Local Stream Copy {#LocalStreamCopy}
diff --git a/doc/internals.zh-cn.md b/doc/internals.zh-cn.md
new file mode 100644
index 00000000..ca3d297a
--- /dev/null
+++ b/doc/internals.zh-cn.md
@@ -0,0 +1,363 @@
+# 内部架构
+
+本部分记录了一些设计和实现细节。
+
+[TOC]
+
+# 架构 {#Architecture}
+
+## SAX 和 DOM
+
+下é¢çš„ UML 图显示了 SAX å’Œ DOM 的基本关系。
+
+![架构 UML 类图](diagram/architecture.png)
+
+关系的核心是 `Handler` 概念。在 SAX 一边,`Reader` 从æµè§£æž JSON 并将事件å‘é€åˆ° `Handler`。`Writer` 实现了 `Handler` 概念,用于处ç†ç›¸åŒçš„事件。在 DOM 一边,`Document` 实现了 `Handler` 概念,用于通过这些时间æ¥æž„建 DOM。`Value` 支æŒäº† `Value::Accept(Handler&)` 函数,它å¯ä»¥å°† DOM 转æ¢ä¸ºäº‹ä»¶è¿›è¡Œå‘é€ã€‚
+
+在这个设计,SAX 是ä¸ä¾èµ–于 DOM 的。甚至 `Reader` å’Œ `Writer` 之间也没有ä¾èµ–。这æ供了连接事件å‘é€å™¨å’Œå¤„ç†å™¨çš„çµæ´»æ€§ã€‚除此之外,`Value` 也是ä¸ä¾èµ–于 SAX 的。所以,除了将 DOM åºåˆ—化为 JSON 之外,用户也å¯ä»¥å°†å…¶åºåˆ—化为 XML,或者åšä»»ä½•å…¶ä»–事情。
+
+## 工具类
+
+SAX å’Œ DOM API 都ä¾èµ–于3个é¢å¤–的概念:`Allocator`ã€`Encoding` å’Œ `Stream`。它们的继承层次结构如下图所示。
+
+![工具类 UML 类图](diagram/utilityclass.png)
+
+# 值(Value) {#Value}
+
+`Value` (实际上被定义为 `GenericValue<UTF8<>>`)是 DOM API 的核心。本部分æ述了它的设计。
+
+## æ•°æ®å¸ƒå±€ {#DataLayout}
+
+`Value` 是[å¯å˜ç±»åž‹](http://en.wikipedia.org/wiki/Variant_type)。在 RapidJSON 的上下文中,一个 `Value` 的实例å¯ä»¥åŒ…å«6ç§ JSON æ•°æ®ç±»åž‹ä¹‹ä¸€ã€‚通过使用 `union` ,这是å¯èƒ½å®žçŽ°çš„。æ¯ä¸€ä¸ª `Value` 包å«ä¸¤ä¸ªæˆå‘˜ï¼š`union Data data_` å’Œ `unsigned flags_`。`flags_` 表明了 JSON 类型,以åŠé™„加的信æ¯ã€‚
+
+下表显示了所有类型的数æ®å¸ƒå±€ã€‚32ä½/64ä½åˆ—表明了字段所å ç”¨çš„字节数。
+
+| Null | | 32ä½ | 64ä½ |
+|-------------------|----------------------------------|:----:|:----:|
+| (未使用) | |4 |8 |
+| (未使用) | |4 |4 |
+| (未使用) | |4 |4 |
+| `unsigned flags_` | `kNullType kNullFlag` |4 |4 |
+
+| Bool | | 32ä½ | 64ä½ |
+|-------------------|----------------------------------------------------|:----:|:----:|
+| (未使用) | |4 |8 |
+| (未使用) | |4 |4 |
+| (未使用) | |4 |4 |
+| `unsigned flags_` | `kBoolType` (either `kTrueFlag` or `kFalseFlag`) |4 |4 |
+
+| String | | 32ä½ | 64ä½ |
+|---------------------|-------------------------------------|:----:|:----:|
+| `Ch* str` | 指å‘字符串的指针(å¯èƒ½æ‹¥æœ‰æ‰€æœ‰æƒï¼‰ |4 |8 |
+| `SizeType length` | 字符串长度 |4 |4 |
+| (未使用) | |4 |4 |
+| `unsigned flags_` | `kStringType kStringFlag ...` |4 |4 |
+
+| Object | | 32ä½ | 64ä½ |
+|---------------------|-------------------------------------|:----:|:----:|
+| `Member* members` | 指å‘æˆå‘˜æ•°ç»„的指针(拥有所有æƒï¼‰ |4 |8 |
+| `SizeType size` | æˆå‘˜æ•°é‡ |4 |4 |
+| `SizeType capacity` | æˆå‘˜å®¹é‡ |4 |4 |
+| `unsigned flags_` | `kObjectType kObjectFlag` |4 |4 |
+
+| Array | | 32ä½ | 64ä½ |
+|---------------------|-------------------------------------|:----:|:----:|
+| `Value* values` | 指å‘值数组的指针(拥有所有æƒï¼‰ |4 |8 |
+| `SizeType size` | å€¼æ•°é‡ |4 |4 |
+| `SizeType capacity` | å€¼å®¹é‡ |4 |4 |
+| `unsigned flags_` | `kArrayType kArrayFlag` |4 |4 |
+
+| Number (Int) | | 32ä½ | 64ä½ |
+|---------------------|-------------------------------------|:----:|:----:|
+| `int i` | 32ä½æœ‰ç¬¦å·æ•´æ•° |4 |4 |
+| (零填充) | 0 |4 |4 |
+| (未使用) | |4 |8 |
+| `unsigned flags_` | `kNumberType kNumberFlag kIntFlag kInt64Flag ...` |4 |4 |
+
+| Number (UInt) | | 32ä½ | 64ä½ |
+|---------------------|-------------------------------------|:----:|:----:|
+| `unsigned u` | 32ä½æ— ç¬¦å·æ•´æ•° |4 |4 |
+| (零填充) | 0 |4 |4 |
+| (未使用) | |4 |8 |
+| `unsigned flags_` | `kNumberType kNumberFlag kUIntFlag kUInt64Flag ...` |4 |4 |
+
+| Number (Int64) | | 32ä½ | 64ä½ |
+|---------------------|-------------------------------------|:----:|:----:|
+| `int64_t i64` | 64ä½æœ‰ç¬¦å·æ•´æ•° |8 |8 |
+| (未使用) | |4 |8 |
+| `unsigned flags_` | `kNumberType kNumberFlag kInt64Flag ...` |4 |4 |
+
+| Number (Uint64) | | 32ä½ | 64ä½ |
+|---------------------|-------------------------------------|:----:|:----:|
+| `uint64_t i64` | 64ä½æ— ç¬¦å·æ•´æ•° |8 |8 |
+| (未使用) | |4 |8 |
+| `unsigned flags_` | `kNumberType kNumberFlag kInt64Flag ...` |4 |4 |
+
+| Number (Double) | | 32ä½ | 64ä½ |
+|---------------------|-------------------------------------|:----:|:----:|
+| `uint64_t i64` | åŒç²¾åº¦æµ®ç‚¹æ•° |8 |8 |
+| (未使用) | |4 |8 |
+| `unsigned flags_` |`kNumberType kNumberFlag kDoubleFlag`|4 |4 |
+
+这里有一些需è¦æ³¨æ„的地方:
+* 为了å‡å°‘在64ä½æž¶æž„上的内存消耗,`SizeType` 被定义为 `unsigned` 而ä¸æ˜¯ `size_t`。
+* 32ä½æ•´æ•°çš„零填充å¯èƒ½è¢«æ”¾åœ¨å®žé™…类型的å‰é¢æˆ–åŽé¢ï¼Œè¿™ä¾èµ–于字节åºã€‚这使得它å¯ä»¥å°†32ä½æ•´æ•°ä¸ç»è¿‡ä»»ä½•è½¬æ¢å°±å¯ä»¥è§£é‡Šä¸º64ä½æ•´æ•°ã€‚
+* `Int` 永远是 `Int64`,å之ä¸ç„¶ã€‚
+
+## 标志 {#Flags}
+
+32ä½çš„ `flags_` 包å«äº† JSON 类型和其他信æ¯ã€‚如å‰æ–‡ä¸­çš„表所述,æ¯ä¸€ç§ JSON 类型包å«äº†å†—余的 `kXXXType` å’Œ `kXXXFlag`。这个设计是为了优化测试ä½æ ‡å¿—(`IsNumber()`)和获å–æ¯ä¸€ç§ç±»åž‹çš„åºåˆ—å·ï¼ˆ`GetType()`)。
+
+字符串有两个å¯é€‰çš„标志。`kCopyFlag` 表明这个字符串拥有字符串拷è´çš„所有æƒã€‚而 `kInlineStrFlag` æ„味ç€ä½¿ç”¨äº†[短字符串优化](#ShortString)。
+
+数字更加å¤æ‚一些。对于普通的整数值,它å¯ä»¥åŒ…å« `kIntFlag`ã€`kUintFlag`〠`kInt64Flag` å’Œ/或 `kUint64Flag`,这由整数的范围决定。带有å°æ•°æˆ–者超过64ä½æ‰€èƒ½è¡¨è¾¾çš„范围的整数的数字会被存储为带有 `kDoubleFlag` çš„ `double`。
+
+## 短字符串优化 {#ShortString}
+
+[Kosta](https://github.com/Kosta-Github) æ供了很棒的短字符串优化。这个优化的xxx如下所述。除去 `flags_` ,`Value` 有12或16字节(对于32ä½æˆ–64ä½ï¼‰æ¥å­˜å‚¨å®žé™…çš„æ•°æ®ã€‚这为在其内部直接存储短字符串而ä¸æ˜¯å­˜å‚¨å­—符串的指针创造了å¯èƒ½ã€‚对于1字节的字符类型(例如 `char`),它å¯ä»¥åœ¨ `Value` 类型内部存储至多11或15个字符的字符串。
+
+|ShortString (Ch=char)| | 32ä½ | 64ä½ |
+|---------------------|-------------------------------------|:----:|:----:|
+| `Ch str[MaxChars]` | 字符串缓冲区 |11 |15 |
+| `Ch invLength` | MaxChars - Length |1 |1 |
+| `unsigned flags_` | `kStringType kStringFlag ...` |4 |4 |
+
+这里使用了一项特殊的技术。它存储了 (MaxChars - length) 而ä¸ç›´æŽ¥å­˜å‚¨å­—符串的长度。这使得存储11个字符并且带有åŽç¼€ `\0` æˆä¸ºå¯èƒ½ã€‚
+
+这个优化å¯ä»¥å‡å°‘字符串拷è´å†…å­˜å ç”¨ã€‚它也改善了缓存一致性,并进一步æ高了è¿è¡Œæ—¶æ€§èƒ½ã€‚
+
+# 分é…器(Allocator) {#InternalAllocator}
+
+`Allocator` 是 RapidJSON 中的概念:
+~~~cpp
+concept Allocator {
+ static const bool kNeedFree; //!< 表明这个分é…器是å¦éœ€è¦è°ƒç”¨ Free()。
+
+ // 申请内存å—。
+ // \param size 内存å—的大å°ï¼Œä»¥å­—节记。
+ // \returns 指å‘内存å—的指针。
+ void* Malloc(size_t size);
+
+ // 调整内存å—的大å°ã€‚
+ // \param originalPtr 当å‰å†…å­˜å—的指针。空指针是被å…许的。
+ // \param originalSize 当å‰å¤§å°ï¼Œä»¥å­—节记。(设计问题:因为有些分é…器å¯èƒ½ä¸ä¼šè®°å½•å®ƒï¼Œæ˜¾ç¤ºçš„传递它å¯ä»¥èŠ‚约内存。)
+ // \param newSize 新大å°ï¼Œä»¥å­—节记。
+ void* Realloc(void* originalPtr, size_t originalSize, size_t newSize);
+
+ // 释放内存å—。
+ // \param ptr 指å‘内存å—的指针。空指针是被å…许的。
+ static void Free(void *ptr);
+};
+~~~
+
+需è¦æ³¨æ„的是 `Malloc()` å’Œ `Realloc()` 是æˆå‘˜å‡½æ•°è€Œ `Free()` 是é™æ€æˆå‘˜å‡½æ•°ã€‚
+
+## MemoryPoolAllocator {#MemoryPoolAllocator}
+
+`MemoryPoolAllocator` 是 DOM 的默认内存分é…器。它åªç”³è¯·å†…存而ä¸é‡Šæ”¾å†…存。这对于构建 DOM æ ‘éžå¸¸åˆé€‚。
+
+在它的内部,它从基础的内存分é…器申请内存å—(默认为 `CrtAllocator`)并将这些内存å—存储为å•å‘链表。当用户请求申请内存,它会éµå¾ªä¸‹åˆ—步骤æ¥ç”³è¯·å†…存:
+
+1. 如果å¯ç”¨ï¼Œä½¿ç”¨ç”¨æˆ·æä¾›çš„ç¼“å†²åŒºã€‚ï¼ˆè§ [User Buffer section in DOM](doc/dom.md))
+2. 如果用户æ供的缓冲区已满,使用当å‰å†…å­˜å—。
+3. 如果当å‰å†…å­˜å—已满,申请新的内存å—。
+
+# 解æžä¼˜åŒ– {#ParsingOptimization}
+
+## 使用 SIMD 跳过空格 {#SkipwhitespaceWithSIMD}
+
+当从æµä¸­è§£æž JSON 时,解æžå™¨éœ€è¦è·³è¿‡4ç§ç©ºæ ¼å­—符:
+
+1. 空格 (`U+0020`)
+2. 制表符 (`U+000B`)
+3. æ¢è¡Œ (`U+000A`)
+4. 回车 (`U+000D`)
+
+这是一份简å•çš„实现:
+~~~cpp
+void SkipWhitespace(InputStream& s) {
+ while (s.Peek() == ' ' || s.Peek() == '\n' || s.Peek() == '\r' || s.Peek() == '\t')
+ s.Take();
+}
+~~~
+
+但是,这需è¦å¯¹æ¯ä¸ªå­—符进行4次比较以åŠä¸€äº›åˆ†æ”¯ã€‚这被å‘现是一个热点。
+
+为了加速这一处ç†ï¼ŒRapidJSON 使用 SIMD æ¥åœ¨ä¸€æ¬¡è¿­ä»£ä¸­æ¯”较16个字符和4ä¸ªç©ºæ ¼ã€‚ç›®å‰ RapidJSON æ”¯æŒ SSE2 , SSE4.2 å’Œ ARM Neon 指令。åŒæ—¶å®ƒä¹Ÿåªä¼šå¯¹ UTF-8 内存æµå¯ç”¨ï¼ŒåŒ…括字符串æµæˆ– *原ä½* 解æžã€‚
+
+ä½ å¯ä»¥é€šè¿‡åœ¨åŒ…å« `rapidjson.h` 之å‰å®šä¹‰ `RAPIDJSON_SSE2` , `RAPIDJSON_SSE42` 或 `RAPIDJSON_NEON` æ¥å¯ç”¨è¿™ä¸ªä¼˜åŒ–。一些编译器å¯ä»¥æ£€æµ‹è¿™ä¸ªè®¾ç½®ï¼Œå¦‚ `perftest.h`:
+
+~~~cpp
+// __SSE2__ å’Œ __SSE4_2__ å¯è¢« gccã€clang å’Œ Intel 编译器识别:
+// 如果支æŒçš„è¯ï¼Œæˆ‘们在 gmake 中使用了 -march=native æ¥å¯ç”¨ -msse2 å’Œ -msse4.2
+// åŒæ ·çš„, __ARM_NEON 被用于识别Neon
+#if defined(__SSE4_2__)
+# define RAPIDJSON_SSE42
+#elif defined(__SSE2__)
+# define RAPIDJSON_SSE2
+#elif defined(__ARM_NEON)
+# define RAPIDJSON_NEON
+#endif
+~~~
+
+需è¦æ³¨æ„的是,这是编译期的设置。在ä¸æ”¯æŒè¿™äº›æŒ‡ä»¤çš„机器上è¿è¡Œå¯æ‰§è¡Œæ–‡ä»¶ä¼šä½¿å®ƒå´©æºƒã€‚
+
+### 页é¢å¯¹é½é—®é¢˜
+
+在 RapidJSON 的早期版本中,被报告了[一个问题](https://code.google.com/archive/p/rapidjson/issues/104):`SkipWhitespace_SIMD()` 会罕è§åœ°å¯¼è‡´å´©æºƒï¼ˆçº¦äº”å万分之一的几率)。在调查之åŽï¼Œæ€€ç–‘是 `_mm_loadu_si128()` 访问了 `'\0'` 之åŽçš„内存,并越过被ä¿æŠ¤çš„页é¢è¾¹ç•Œã€‚
+
+在 [Intel® 64 and IA-32 Architectures Optimization Reference Manual
+](http://www.intel.com/content/www/us/en/architecture-and-technology/64-ia-32-architectures-optimization-manual.html) 中,章节 10.2.1:
+
+> 为了支æŒéœ€è¦è´¹å¯¹é½çš„128ä½ SIMD 内存访问的算法,调用者的内存缓冲区申请应当考虑添加一些填充空间,这样被调用的函数å¯ä»¥å®‰å…¨åœ°å°†åœ°å€æŒ‡é’ˆç”¨äºŽæœªå¯¹é½çš„128ä½ SIMD 内存æ“作。
+> 在结åˆéžå¯¹é½çš„ SIMD 内存æ“作中,最å°çš„对é½å¤§å°åº”该等于 SIMD 寄存器的大å°ã€‚
+
+对于 RapidJSON æ¥è¯´ï¼Œè¿™æ˜¾ç„¶æ˜¯ä¸å¯è¡Œçš„,因为 RapidJSON ä¸åº”当强迫用户进行内存对é½ã€‚
+
+为了修å¤è¿™ä¸ªé—®é¢˜ï¼Œå½“å‰çš„代ç ä¼šå…ˆæŒ‰å­—节处ç†ç›´åˆ°ä¸‹ä¸€ä¸ªå¯¹é½çš„地å€ã€‚在这之åŽï¼Œä½¿ç”¨å¯¹é½è¯»å–æ¥è¿›è¡Œ SIMD 处ç†ã€‚è§ [#85](https://github.com/Tencent/rapidjson/issues/85)。
+
+## 局部æµæ‹·è´ {#LocalStreamCopy}
+
+在优化的过程中,我们å‘现一些编译器ä¸èƒ½å°†è®¿é—®æµçš„一些æˆå‘˜æ•°æ®æ”¾å…¥å±€éƒ¨å˜é‡æˆ–者寄存器中。测试结果显示,对于一些æµç±»åž‹ï¼Œåˆ›å»ºæµçš„æ‹·è´å¹¶å°†å…¶ç”¨äºŽå†…层循环中å¯ä»¥æ”¹å–„æ€§èƒ½ã€‚ä¾‹å¦‚ï¼Œå®žé™…ï¼ˆéž SIMD)的 `SkipWhitespace()` 被实现为:
+
+~~~cpp
+template<typename InputStream>
+void SkipWhitespace(InputStream& is) {
+ internal::StreamLocalCopy<InputStream> copy(is);
+ InputStream& s(copy.s);
+
+ while (s.Peek() == ' ' || s.Peek() == '\n' || s.Peek() == '\r' || s.Peek() == '\t')
+ s.Take();
+}
+~~~
+
+基于æµçš„特å¾ï¼Œ`StreamLocalCopy` 会创建(或ä¸åˆ›å»ºï¼‰æµå¯¹è±¡çš„æ‹·è´ï¼Œåœ¨å±€éƒ¨ä½¿ç”¨å®ƒå¹¶å°†æµçš„状æ€æ‹·è´å›žåŽŸæ¥çš„æµã€‚
+
+## 解æžä¸ºåŒç²¾åº¦æµ®ç‚¹æ•° {#ParsingDouble}
+
+将字符串解æžä¸º `double` 并ä¸ç®€å•ã€‚标准库函数 `strtod()` å¯ä»¥èƒœä»»è¿™é¡¹å·¥ä½œï¼Œä½†å®ƒæ¯”较缓慢。默认情况下,解æžå™¨ä½¿ç”¨é»˜è®¤çš„精度设置。这最多有 3[ULP](http://en.wikipedia.org/wiki/Unit_in_the_last_place) 的误差,并实现在 `internal::StrtodNormalPrecision()` 中。
+
+当使用 `kParseFullPrecisionFlag` 时,编译器会改为调用 `internal::StrtodFullPrecision()` ,这个函数会自动调用三个版本的转æ¢ã€‚
+1. [Fast-Path](http://www.exploringbinary.com/fast-path-decimal-to-floating-point-conversion/)。
+2. [double-conversion](https://github.com/floitsch/double-conversion) 中的自定义 DIY-FP 实现。
+3. (Clinger, William D. How to read floating point numbers accurately. Vol. 25. No. 6. ACM, 1990) 中的大整数算法。
+
+如果第一个转æ¢æ–¹æ³•å¤±è´¥ï¼Œåˆ™å°è¯•ä½¿ç”¨ç¬¬äºŒç§æ–¹æ³•ï¼Œä»¥æ­¤ç±»æŽ¨ã€‚
+
+# 生æˆä¼˜åŒ– {#GenerationOptimization}
+
+## æ•´æ•°åˆ°å­—ç¬¦ä¸²çš„è½¬æ¢ {#itoa}
+
+整数到字符串转æ¢çš„朴素算法需è¦å¯¹æ¯ä¸€ä¸ªå进制ä½è¿›è¡Œä¸€æ¬¡å¤„罚。我们实现了若干版本并在 [itoa-benchmark](https://github.com/miloyip/itoa-benchmark) 中对它们进行了评估。
+
+虽然 SSE2 版本是最快的,但它和第二快的 `branchlut` å·®è·ä¸å¤§ã€‚而且 `branchlut` 是纯C++实现,所以我们在 RapidJSON 中使用了 `branchlut`。
+
+## åŒç²¾åº¦æµ®ç‚¹æ•°åˆ°å­—ç¬¦ä¸²çš„è½¬æ¢ {#dtoa}
+
+åŽŸæ¥ RapidJSON 使用 `snprintf(..., ..., "%g")` æ¥è¿›è¡ŒåŒç²¾åº¦æµ®ç‚¹æ•°åˆ°å­—符串的转æ¢ã€‚这是ä¸å‡†ç¡®çš„,因为默认的精度是6。éšåŽæˆ‘们å‘现它很缓慢,而且有其它的替代å“。
+
+Google çš„ V8 [double-conversion](https://github.com/floitsch/double-conversion
+) 实现了更新的ã€å¿«é€Ÿçš„被称为 Grisu3 的算法(Loitsch, Florian. "Printing floating-point numbers quickly and accurately with integers." ACM Sigplan Notices 45.6 (2010): 233-243.)。
+
+然而,这个实现ä¸æ˜¯ä»…头文件的,所以我们实现了一个仅头文件的 Grisu2 版本。这个算法ä¿è¯äº†ç»“果永远精确。而且在大多数情况下,它会生æˆæœ€çŸ­çš„(å¯é€‰ï¼‰å­—符串表示。
+
+这个仅头文件的转æ¢å‡½æ•°åœ¨ [dtoa-benchmark](https://github.com/miloyip/dtoa-benchmark) 中进行评估。
+
+# 解æžå™¨ {#Parser}
+
+## è¿­ä»£è§£æž {#IterativeParser}
+
+迭代解æžå™¨æ˜¯ä¸€ä¸ªä»¥éžé€’å½’æ–¹å¼å®žçŽ°çš„递归下é™çš„ LL(1) 解æžå™¨ã€‚
+
+### 语法 {#IterativeParserGrammar}
+
+解æžå™¨ä½¿ç”¨çš„语法是基于严格 JSON 语法的:
+~~~~~~~~~~
+S -> array | object
+array -> [ values ]
+object -> { members }
+values -> non-empty-values | ε
+non-empty-values -> value addition-values
+addition-values -> ε | , non-empty-values
+members -> non-empty-members | ε
+non-empty-members -> member addition-members
+addition-members -> ε | , non-empty-members
+member -> STRING : value
+value -> STRING | NUMBER | NULL | BOOLEAN | object | array
+~~~~~~~~~~
+
+注æ„到左因å­è¢«åŠ å…¥äº†éžç»ˆç»“符的 `values` å’Œ `members` æ¥ä¿è¯è¯­æ³•æ˜¯ LL(1) 的。
+
+### 解æžè¡¨ {#IterativeParserParsingTable}
+
+基于这份语法,我们å¯ä»¥æž„造 FIRST å’Œ FOLLOW 集åˆã€‚
+
+éžç»ˆç»“符的 FIRST 集åˆå¦‚下所示:
+
+| NON-TERMINAL | FIRST |
+|:-----------------:|:--------------------------------:|
+| array | [ |
+| object | { |
+| values | ε STRING NUMBER NULL BOOLEAN { [ |
+| addition-values | ε COMMA |
+| members | ε STRING |
+| addition-members | ε COMMA |
+| member | STRING |
+| value | STRING NUMBER NULL BOOLEAN { [ |
+| S | [ { |
+| non-empty-members | STRING |
+| non-empty-values | STRING NUMBER NULL BOOLEAN { [ |
+
+FOLLOW 集åˆå¦‚下所示:
+
+| NON-TERMINAL | FOLLOW |
+|:-----------------:|:-------:|
+| S | $ |
+| array | , $ } ] |
+| object | , $ } ] |
+| values | ] |
+| non-empty-values | ] |
+| addition-values | ] |
+| members | } |
+| non-empty-members | } |
+| addition-members | } |
+| member | , } |
+| value | , } ] |
+
+最终å¯ä»¥ä»Ž FIRST å’Œ FOLLOW 集åˆç”Ÿæˆè§£æžè¡¨ï¼š
+
+| NON-TERMINAL | [ | { | , | : | ] | } | STRING | NUMBER | NULL | BOOLEAN |
+|:-----------------:|:---------------------:|:---------------------:|:-------------------:|:-:|:-:|:-:|:-----------------------:|:---------------------:|:---------------------:|:---------------------:|
+| S | array | object | | | | | | | | |
+| array | [ values ] | | | | | | | | | |
+| object | | { members } | | | | | | | | |
+| values | non-empty-values | non-empty-values | | | ε | | non-empty-values | non-empty-values | non-empty-values | non-empty-values |
+| non-empty-values | value addition-values | value addition-values | | | | | value addition-values | value addition-values | value addition-values | value addition-values |
+| addition-values | | | , non-empty-values | | ε | | | | | |
+| members | | | | | | ε | non-empty-members | | | |
+| non-empty-members | | | | | | | member addition-members | | | |
+| addition-members | | | , non-empty-members | | | ε | | | | |
+| member | | | | | | | STRING : value | | | |
+| value | array | object | | | | | STRING | NUMBER | NULL | BOOLEAN |
+
+对于上é¢çš„语法分æžï¼Œè¿™é‡Œæœ‰ä¸€ä¸ªå¾ˆæ£’çš„[工具](http://hackingoff.com/compilers/predict-first-follow-set)。
+
+### 实现 {#IterativeParserImplementation}
+
+基于这份解æžè¡¨ï¼Œä¸€ä¸ªç›´æŽ¥çš„(常规的)将规则åå‘入栈的实现å¯ä»¥æ­£å¸¸å·¥ä½œã€‚
+
+在 RapidJSON 中,对直接的实现进行了一些修改:
+
+首先,在 RapidJSON 中,这份解æžè¡¨è¢«ç¼–ç ä¸ºçŠ¶æ€æœºã€‚
+规则由头部和主体组æˆã€‚
+状æ€è½¬æ¢ç”±è§„则构造。
+除此之外,é¢å¤–的状æ€è¢«æ·»åŠ åˆ°ä¸Ž `array` å’Œ `object` 有关的规则。
+通过这ç§æ–¹å¼ï¼Œç”Ÿæˆæ•°ç»„值或对象æˆå‘˜å¯ä»¥åªç”¨ä¸€æ¬¡çŠ¶æ€è½¬ç§»ä¾¿å¯å®Œæˆï¼Œ
+而ä¸éœ€è¦åœ¨ç›´æŽ¥çš„实现中的多次出栈/入栈æ“作。
+这也使得估计栈的大å°æ›´åŠ å®¹æ˜“。
+
+状æ€å›¾å¦‚如下所示:
+
+![状æ€å›¾](diagram/iterative-parser-states-diagram.png)
+
+第二,迭代解æžå™¨ä¹Ÿåœ¨å†…部栈ä¿å­˜äº†æ•°ç»„的值个数和对象æˆå‘˜çš„æ•°é‡ï¼Œè¿™ä¹Ÿä¸Žä¼ ç»Ÿçš„实现ä¸åŒã€‚
diff --git a/doc/misc/footer.html b/doc/misc/footer.html
index 843aa110..77f11311 100644
--- a/doc/misc/footer.html
+++ b/doc/misc/footer.html
@@ -7,21 +7,5 @@
</ul>
</div>
<!--END GENERATE_TREEVIEW-->
-<script type="text/javascript">
- /* * * CONFIGURATION VARIABLES * * */
- var disqus_shortname = 'rapidjson-doc';
-
- /* * * DON'T EDIT BELOW THIS LINE * * */
- (function() {
- var dt = document.createElement('div');
- dt.id = "disqus_thread";
- (document.getElementsByClassName('contents')[0]).appendChild(dt);
-
- var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
- dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
- (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
- })();
-</script>
-
</body>
</html>
diff --git a/doc/misc/header.html b/doc/misc/header.html
index d43f2aaf..a89ba46b 100644
--- a/doc/misc/header.html
+++ b/doc/misc/header.html
@@ -16,18 +16,9 @@ $mathjax
<link href="$relpath^$stylesheet" rel="stylesheet" type="text/css" />
$extrastylesheet
</head>
-<script>
- (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
- (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
- m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
- })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
-
- ga('create', 'UA-63929386-1', 'auto');
- ga('send', 'pageview');
-</script>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
-<div id="topbanner"><a href="https://github.com/miloyip/rapidjson" title="RapidJSON GitHub"><i class="githublogo"></i></a></div>
+<div id="topbanner"><a href="https://github.com/Tencent/rapidjson" title="RapidJSON GitHub"><i class="githublogo"></i></a></div>
$searchbox
<!--END TITLEAREA-->
<!-- end header part -->
diff --git a/doc/npm.md b/doc/npm.md
new file mode 100644
index 00000000..6f4e85ad
--- /dev/null
+++ b/doc/npm.md
@@ -0,0 +1,31 @@
+## NPM
+
+# package.json {#package}
+
+~~~~~~~~~~js
+{
+ ...
+ "dependencies": {
+ ...
+ "rapidjson": "git@github.com:Tencent/rapidjson.git"
+ },
+ ...
+ "gypfile": true
+}
+~~~~~~~~~~
+
+# binding.gyp {#binding}
+
+~~~~~~~~~~js
+{
+ ...
+ 'targets': [
+ {
+ ...
+ 'include_dirs': [
+ '<!(node -e \'require("rapidjson")\')'
+ ]
+ }
+ ]
+}
+~~~~~~~~~~
diff --git a/doc/performance.md b/doc/performance.md
index 702ca723..6f9e1bf8 100644
--- a/doc/performance.md
+++ b/doc/performance.md
@@ -1,6 +1,6 @@
# Performance
-There is a [native JSON benchmark collection] [1] which evaluates speed, memory usage and code size of various operations among 20 JSON libaries.
+There is a [native JSON benchmark collection] [1] which evaluates speed, memory usage and code size of various operations among 37 JSON libraries.
[1]: https://github.com/miloyip/nativejson-benchmark
@@ -15,12 +15,12 @@ Additionally, you may refer to the following third-party benchmarks.
* [json_spirit](https://github.com/cierelabs/json_spirit)
* [jsoncpp](http://jsoncpp.sourceforge.net/)
* [libjson](http://sourceforge.net/projects/libjson/)
- * [rapidjson](https://github.com/miloyip/rapidjson/)
+ * [rapidjson](https://github.com/Tencent/rapidjson/)
* [QJsonDocument](http://qt-project.org/doc/qt-5.0/qtcore/qjsondocument.html)
* [JSON Parser Benchmarking](http://chadaustin.me/2013/01/json-parser-benchmarking/) by Chad Austin (Jan 2013)
* [sajson](https://github.com/chadaustin/sajson)
- * [rapidjson](https://github.com/miloyip/rapidjson/)
+ * [rapidjson](https://github.com/Tencent/rapidjson/)
* [vjson](https://code.google.com/p/vjson/)
* [YAJL](http://lloyd.github.com/yajl/)
* [Jansson](http://www.digip.org/jansson/)
diff --git a/doc/performance.zh-cn.md b/doc/performance.zh-cn.md
index b590fe0c..2322c9c4 100644
--- a/doc/performance.zh-cn.md
+++ b/doc/performance.zh-cn.md
@@ -1,10 +1,10 @@
# 性能
-有一个[native JSON benchmark collection][1]项目,能评估20个JSON库在ä¸åŒæ“作下的速度ã€å…§å­˜ç”¨é‡åŠä»£ç å¤§å°ã€‚
+有一个 [native JSON benchmark collection][1] 项目,能评估 37 个 JSON 库在ä¸åŒæ“作下的速度ã€å…§å­˜ç”¨é‡åŠä»£ç å¤§å°ã€‚
[1]: https://github.com/miloyip/nativejson-benchmark
-RapidJSON 0.1版本的性能测试文章ä½äºŽ[这里](https://code.google.com/p/rapidjson/wiki/Performance).
+RapidJSON 0.1 版本的性能测试文章ä½äºŽ [这里](https://code.google.com/p/rapidjson/wiki/Performance).
此外,你也å¯ä»¥å‚考以下这些第三方的评测。
@@ -15,12 +15,12 @@ RapidJSON 0.1版本的性能测试文章ä½äºŽ[这里](https://code.google.com/p
* [json_spirit](https://github.com/cierelabs/json_spirit)
* [jsoncpp](http://jsoncpp.sourceforge.net/)
* [libjson](http://sourceforge.net/projects/libjson/)
- * [rapidjson](https://github.com/miloyip/rapidjson/)
+ * [rapidjson](https://github.com/Tencent/rapidjson/)
* [QJsonDocument](http://qt-project.org/doc/qt-5.0/qtcore/qjsondocument.html)
* [JSON Parser Benchmarking](http://chadaustin.me/2013/01/json-parser-benchmarking/) by Chad Austin (Jan 2013)
* [sajson](https://github.com/chadaustin/sajson)
- * [rapidjson](https://github.com/miloyip/rapidjson/)
+ * [rapidjson](https://github.com/Tencent/rapidjson/)
* [vjson](https://code.google.com/p/vjson/)
* [YAJL](http://lloyd.github.com/yajl/)
* [Jansson](http://www.digip.org/jansson/)
diff --git a/doc/pointer.md b/doc/pointer.md
index 3927a12e..9a0e5ca0 100644
--- a/doc/pointer.md
+++ b/doc/pointer.md
@@ -1,6 +1,6 @@
# Pointer
-## Status: experimental, shall be included in v1.1
+(This feature was released in v1.1.0)
JSON Pointer is a standardized ([RFC6901]) way to select a value inside a JSON Document (DOM). This can be analogous to XPath for XML document. However, JSON Pointer is much simpler, and a single JSON Pointer only pointed to a single value.
@@ -211,7 +211,7 @@ p.Stringify(sb);
std::cout << sb.GetString() << std::endl;
~~~
-It can also stringify to URI fragment reprsentation by `StringifyUriFragment()`.
+It can also stringify to URI fragment representation by `StringifyUriFragment()`.
# User-Supplied Tokens {#UserSuppliedTokens}
diff --git a/doc/pointer.zh-cn.md b/doc/pointer.zh-cn.md
index b340debf..f58f55f3 100644
--- a/doc/pointer.zh-cn.md
+++ b/doc/pointer.zh-cn.md
@@ -1,6 +1,6 @@
# Pointer
-## 状æ€: 实验性,应该会åˆè¿› v1.1
+(本功能于 v1.1.0 å‘布)
JSON Pointer 是一个标准化([RFC6901])的方å¼åŽ»é€‰å–一个 JSON Document(DOM)中的值。这类似于 XML çš„ XPath。然而,JSON Pointer 简å•å¾—多,而且æ¯ä¸ª JSON Pointer 仅指å‘å•ä¸ªå€¼ã€‚
@@ -25,7 +25,7 @@ JSON Pointer 是一个标准化([RFC6901])的方å¼åŽ»é€‰å–一个 JSON Docu
3. `"/foo/1"` → `"baz"`
4. `"/pi"` → `3.1416`
-è¦æ³¨æ„,一个空 JSON Pointer `""` (零个token)解æžä¸ºæ•´ä¸ª JSON。
+è¦æ³¨æ„,一个空 JSON Pointer `""` (零个 token)解æžä¸ºæ•´ä¸ª JSON。
# 基本使用方法 {#BasicUsage}
@@ -123,7 +123,7 @@ assert(success);
Token `"0"` 在第一个 pointer 中被当作æˆå‘˜å字。它在第二个 pointer 中被当作æˆæ•°ç»„索引。
-å…¶ä»–å‡½æ•°ä¼šæ”¹å˜ DOM,包括`Create()`ã€`GetWithDefault()`ã€`Set()`ã€`Swap()`。这些函数总是æˆåŠŸçš„。若一些父值ä¸å­˜åœ¨ï¼Œå°±ä¼šåˆ›å»ºå®ƒä»¬ã€‚若父值类型ä¸åŒ¹é… token,也会强行改å˜å…¶ç±»åž‹ã€‚改å˜ç±»åž‹ä¹Ÿæ„味ç€å®Œå…¨ç§»é™¤å…¶ DOM å­æ ‘的内容。
+å…¶ä»–å‡½æ•°ä¼šæ”¹å˜ DOM,包括 `Create()`ã€`GetWithDefault()`ã€`Set()`ã€`Swap()`。这些函数总是æˆåŠŸçš„。若一些父值ä¸å­˜åœ¨ï¼Œå°±ä¼šåˆ›å»ºå®ƒä»¬ã€‚若父值类型ä¸åŒ¹é… token,也会强行改å˜å…¶ç±»åž‹ã€‚改å˜ç±»åž‹ä¹Ÿæ„味ç€å®Œå…¨ç§»é™¤å…¶ DOM å­æ ‘的内容。
例如,把上é¢çš„ JSON 解译至 `d` 之åŽï¼Œ
@@ -185,7 +185,7 @@ private:
# URI ç‰‡æ®µè¡¨ç¤ºæ–¹å¼ {#URIFragment}
-除了我们一直在使用的字符串方å¼è¡¨ç¤º JSON pointer,[RFC6901]也定义了一个 JSON Pointer çš„ URI 片段(fragment)表示方å¼ã€‚URI 片段是定义于 [RFC3986] "Uniform Resource Identifier (URI): Generic Syntax"。
+除了我们一直在使用的字符串方å¼è¡¨ç¤º JSON pointer,[RFC6901] 也定义了一个 JSON Pointer çš„ URI 片段(fragment)表示方å¼ã€‚URI 片段是定义于 [RFC3986] "Uniform Resource Identifier (URI): Generic Syntax"。
URI 片段的主è¦åˆ†åˆ«æ˜¯å¿…然以 `#` (pound sign)开头,而一些字符也会以百分比编ç æˆ UTF-8 åºåˆ—。例如,以下的表展示了ä¸åŒè¡¨ç¤ºæ³•ä¸‹çš„ C/C++ 字符串常数。
diff --git a/doc/sax.md b/doc/sax.md
index 9a6d8148..874361ff 100644
--- a/doc/sax.md
+++ b/doc/sax.md
@@ -8,7 +8,7 @@ In RapidJSON, `Reader` (typedef of `GenericReader<...>`) is the SAX-style parser
# Reader {#Reader}
-`Reader` parses a JSON from a stream. While it reads characters from the stream, it analyze the characters according to the syntax of JSON, and publish events to a handler.
+`Reader` parses a JSON from a stream. While it reads characters from the stream, it analyzes the characters according to the syntax of JSON, and publishes events to a handler.
For example, here is a JSON.
@@ -24,7 +24,7 @@ For example, here is a JSON.
}
~~~~~~~~~~
-While a `Reader` parses this JSON, it publishes the following events to the handler sequentially:
+When a `Reader` parses this JSON, it publishes the following events to the handler sequentially:
~~~~~~~~~~
StartObject()
@@ -50,7 +50,7 @@ EndArray(4)
EndObject(7)
~~~~~~~~~~
-These events can be easily matched with the JSON, except some event parameters need further explanation. Let's see the `simplereader` example which produces exactly the same output as above:
+These events can be easily matched with the JSON, but some event parameters need further explanation. Let's see the `simplereader` example which produces exactly the same output as above:
~~~~~~~~~~cpp
#include "rapidjson/reader.h"
@@ -59,7 +59,7 @@ These events can be easily matched with the JSON, except some event parameters n
using namespace rapidjson;
using namespace std;
-struct MyHandler {
+struct MyHandler : public BaseReaderHandler<UTF8<>, MyHandler> {
bool Null() { cout << "Null()" << endl; return true; }
bool Bool(bool b) { cout << "Bool(" << boolalpha << b << ")" << endl; return true; }
bool Int(int i) { cout << "Int(" << i << ")" << endl; return true; }
@@ -91,11 +91,11 @@ void main() {
}
~~~~~~~~~~
-Note that, RapidJSON uses template to statically bind the `Reader` type and the handler type, instead of using class with virtual functions. This paradigm can improve the performance by inlining functions.
+Note that RapidJSON uses templates to statically bind the `Reader` type and the handler type, instead of using classes with virtual functions. This paradigm can improve performance by inlining functions.
## Handler {#Handler}
-As the previous example showed, user needs to implement a handler, which consumes the events (function calls) from `Reader`. The handler must contain the following member functions.
+As shown in the previous example, the user needs to implement a handler which consumes the events (via function calls) from the `Reader`. The handler must contain the following member functions.
~~~~~~~~~~cpp
class Handler {
@@ -106,6 +106,7 @@ class Handler {
bool Int64(int64_t i);
bool Uint64(uint64_t i);
bool Double(double d);
+ bool RawNumber(const Ch* str, SizeType length, bool copy);
bool String(const Ch* str, SizeType length, bool copy);
bool StartObject();
bool Key(const Ch* str, SizeType length, bool copy);
@@ -119,17 +120,17 @@ class Handler {
`Bool(bool)` is called when the `Reader` encounters a JSON true or false value.
-When the `Reader` encounters a JSON number, it chooses a suitable C++ type mapping. And then it calls *one* function out of `Int(int)`, `Uint(unsigned)`, `Int64(int64_t)`, `Uint64(uint64_t)` and `Double(double)`.
+When the `Reader` encounters a JSON number, it chooses a suitable C++ type mapping. And then it calls *one* function out of `Int(int)`, `Uint(unsigned)`, `Int64(int64_t)`, `Uint64(uint64_t)` and `Double(double)`. If `kParseNumbersAsStrings` is enabled, `Reader` will always calls `RawNumber()` instead.
-`String(const char* str, SizeType length, bool copy)` is called when the `Reader` encounters a string. The first parameter is pointer to the string. The second parameter is the length of the string (excluding the null terminator). Note that RapidJSON supports null character `'\0'` inside a string. If such situation happens, `strlen(str) < length`. The last `copy` indicates whether the handler needs to make a copy of the string. For normal parsing, `copy = true`. Only when *insitu* parsing is used, `copy = false`. And beware that, the character type depends on the target encoding, which will be explained later.
+`String(const char* str, SizeType length, bool copy)` is called when the `Reader` encounters a string. The first parameter is pointer to the string. The second parameter is the length of the string (excluding the null terminator). Note that RapidJSON supports null character `\0` inside a string. If such situation happens, `strlen(str) < length`. The last `copy` indicates whether the handler needs to make a copy of the string. For normal parsing, `copy = true`. Only when *insitu* parsing is used, `copy = false`. And be aware that the character type depends on the target encoding, which will be explained later.
-When the `Reader` encounters the beginning of an object, it calls `StartObject()`. An object in JSON is a set of name-value pairs. If the object contains members it first calls `Key()` for the name of member, and then calls functions depending on the type of the value. These calls of name-value pairs repeats until calling `EndObject(SizeType memberCount)`. Note that the `memberCount` parameter is just an aid for the handler, user may not need this parameter.
+When the `Reader` encounters the beginning of an object, it calls `StartObject()`. An object in JSON is a set of name-value pairs. If the object contains members it first calls `Key()` for the name of member, and then calls functions depending on the type of the value. These calls of name-value pairs repeat until calling `EndObject(SizeType memberCount)`. Note that the `memberCount` parameter is just an aid for the handler; users who do not need this parameter may ignore it.
-Array is similar to object but simpler. At the beginning of an array, the `Reader` calls `BeginArary()`. If there is elements, it calls functions according to the types of element. Similarly, in the last call `EndArray(SizeType elementCount)`, the parameter `elementCount` is just an aid for the handler.
+Arrays are similar to objects, but simpler. At the beginning of an array, the `Reader` calls `BeginArray()`. If there is elements, it calls functions according to the types of element. Similarly, in the last call `EndArray(SizeType elementCount)`, the parameter `elementCount` is just an aid for the handler.
-Every handler functions returns a `bool`. Normally it should returns `true`. If the handler encounters an error, it can return `false` to notify event publisher to stop further processing.
+Every handler function returns a `bool`. Normally it should return `true`. If the handler encounters an error, it can return `false` to notify the event publisher to stop further processing.
-For example, when we parse a JSON with `Reader` and the handler detected that the JSON does not conform to the required schema, then the handler can return `false` and let the `Reader` stop further parsing. And the `Reader` will be in error state with error code `kParseErrorTermination`.
+For example, when we parse a JSON with `Reader` and the handler detects that the JSON does not conform to the required schema, the handler can return `false` and let the `Reader` stop further parsing. This will place the `Reader` in an error state, with error code `kParseErrorTermination`.
## GenericReader {#GenericReader}
@@ -148,19 +149,19 @@ typedef GenericReader<UTF8<>, UTF8<> > Reader;
} // namespace rapidjson
~~~~~~~~~~
-The `Reader` uses UTF-8 as both source and target encoding. The source encoding means the encoding in the JSON stream. The target encoding means the encoding of the `str` parameter in `String()` calls. For example, to parse a UTF-8 stream and outputs UTF-16 string events, you can define a reader by:
+The `Reader` uses UTF-8 as both source and target encoding. The source encoding means the encoding in the JSON stream. The target encoding means the encoding of the `str` parameter in `String()` calls. For example, to parse a UTF-8 stream and output UTF-16 string events, you can define a reader by:
~~~~~~~~~~cpp
GenericReader<UTF8<>, UTF16<> > reader;
~~~~~~~~~~
-Note that, the default character type of `UTF16` is `wchar_t`. So this `reader`needs to call `String(const wchar_t*, SizeType, bool)` of the handler.
+Note that, the default character type of `UTF16` is `wchar_t`. So this `reader` needs to call `String(const wchar_t*, SizeType, bool)` of the handler.
The third template parameter `Allocator` is the allocator type for internal data structure (actually a stack).
-## Parsing {#Parsing}
+## Parsing {#SaxParsing}
-The one and only one function of `Reader` is to parse JSON.
+The main function of `Reader` is used to parse JSON.
~~~~~~~~~~cpp
template <unsigned parseFlags, typename InputStream, typename Handler>
@@ -171,7 +172,30 @@ template <typename InputStream, typename Handler>
bool Parse(InputStream& is, Handler& handler);
~~~~~~~~~~
-If an error occurs during parsing, it will return `false`. User can also calls `bool HasParseEror()`, `ParseErrorCode GetParseErrorCode()` and `size_t GetErrorOffset()` to obtain the error states. Actually `Document` uses these `Reader` functions to obtain parse errors. Please refer to [DOM](doc/dom.md) for details about parse error.
+If an error occurs during parsing, it will return `false`. User can also call `bool HasParseError()`, `ParseErrorCode GetParseErrorCode()` and `size_t GetErrorOffset()` to obtain the error states. In fact, `Document` uses these `Reader` functions to obtain parse errors. Please refer to [DOM](doc/dom.md) for details about parse errors.
+
+## Token-by-Token Parsing {#TokenByTokenParsing}
+
+Some users may wish to parse a JSON input stream a single token at a time, instead of immediately parsing an entire document without stopping. To parse JSON this way, instead of calling `Parse`, you can use the `IterativeParse` set of functions:
+
+~~~~~~~~~~cpp
+ void IterativeParseInit();
+
+ template <unsigned parseFlags, typename InputStream, typename Handler>
+ bool IterativeParseNext(InputStream& is, Handler& handler);
+
+ bool IterativeParseComplete();
+~~~~~~~~~~
+
+Here is an example of iteratively parsing JSON, token by token:
+
+~~~~~~~~~~cpp
+ reader.IterativeParseInit();
+ while (!reader.IterativeParseComplete()) {
+ reader.IterativeParseNext<kParseDefaultFlags>(is, handler);
+ // Your handler has been called once.
+ }
+~~~~~~~~~~
# Writer {#Writer}
@@ -243,7 +267,7 @@ Anyway, using `Writer` API is even simpler than generating a JSON by ad hoc meth
~~~~~~~~~~cpp
namespace rapidjson {
-template<typename OutputStream, typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename Allocator = CrtAllocator<> >
+template<typename OutputStream, typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename Allocator = CrtAllocator<>, unsigned writeFlags = kWriteDefaultFlags>
class Writer {
public:
Writer(OutputStream& os, Allocator* allocator = 0, size_t levelDepth = kDefaultLevelDepth)
@@ -259,7 +283,16 @@ The `SourceEncoding` template parameter specifies the encoding to be used in `St
The `TargetEncoding` template parameter specifies the encoding in the output stream.
-The last one, `Allocator` is the type of allocator, which is used for allocating internal data structure (a stack).
+The `Allocator` is the type of allocator, which is used for allocating internal data structure (a stack).
+
+The `writeFlags` are combination of the following bit-flags:
+
+Parse flags | Meaning
+------------------------------|-----------------------------------
+`kWriteNoFlags` | No flag is set.
+`kWriteDefaultFlags` | Default write flags. It is equal to macro `RAPIDJSON_WRITE_DEFAULT_FLAGS`, which is defined as `kWriteNoFlags`.
+`kWriteValidateEncodingFlag` | Validate encoding of JSON strings.
+`kWriteNanAndInfFlag` | Allow writing of `Infinity`, `-Infinity` and `NaN`.
Besides, the constructor of `Writer` has a `levelDepth` parameter. This parameter affects the initial memory allocated for storing information per hierarchy level.
@@ -277,7 +310,7 @@ A `Writer` can only output a single JSON, which can be any JSON type at the root
When a JSON is complete, the `Writer` cannot accept any new events. Otherwise the output will be invalid (i.e. having more than one root). To reuse the `Writer` object, user can call `Writer::Reset(OutputStream& os)` to reset all internal states of the `Writer` with a new output stream.
-# Techniques {#Techniques}
+# Techniques {#SaxTechniques}
## Parsing JSON to Custom Data Structure {#CustomDataStructure}
@@ -419,6 +452,7 @@ struct CapitalizeFilter {
bool Int64(int64_t i) { return out_.Int64(i); }
bool Uint64(uint64_t u) { return out_.Uint64(u); }
bool Double(double d) { return out_.Double(d); }
+ bool RawNumber(const char* str, SizeType length, bool copy) { return out_.RawNumber(str, length, copy); }
bool String(const char* str, SizeType length, bool) {
buffer_.clear();
for (SizeType i = 0; i < length; i++)
diff --git a/doc/sax.zh-cn.md b/doc/sax.zh-cn.md
index f8dc7b9a..740c339f 100644
--- a/doc/sax.zh-cn.md
+++ b/doc/sax.zh-cn.md
@@ -1,16 +1,16 @@
# SAX
-"SAX"此术语æºäºŽ[Simple API for XML](http://en.wikipedia.org/wiki/Simple_API_for_XML)。我们借了此术语去套用在JSON的解æžåŠç”Ÿæˆã€‚
+"SAX" 此术语æºäºŽ [Simple API for XML](http://en.wikipedia.org/wiki/Simple_API_for_XML)。我们借了此术语去套用在 JSON 的解æžåŠç”Ÿæˆã€‚
-在RapidJSON中,`Reader`(`GenericReader<...>`çš„typedef)是JSONçš„SAX风格解æžå™¨ï¼Œè€Œ`Writer`(`GenericWriter<...>`çš„typedef)则是JSONçš„SAX风格生æˆå™¨ã€‚
+在 RapidJSON 中,`Reader`(`GenericReader<...>` çš„ typedef)是 JSON çš„ SAX 风格解æžå™¨ï¼Œè€Œ `Writer`(`GenericWriter<...>` çš„ typedef)则是 JSON çš„ SAX 风格生æˆå™¨ã€‚
[TOC]
# Reader {#Reader}
-`Reader`从输入æµè§£æžä¸€ä¸ªJSON。当它从æµä¸­è¯»å–字符时,它会基于JSON的语法去分æžå­—符,并å‘处ç†å™¨å‘é€äº‹ä»¶ã€‚
+`Reader` 从输入æµè§£æžä¸€ä¸ª JSON。当它从æµä¸­è¯»å–字符时,它会基于 JSON 的语法去分æžå­—符,并å‘处ç†å™¨å‘é€äº‹ä»¶ã€‚
-例如,以下是一个JSON。
+例如,以下是一个 JSON。
~~~~~~~~~~js
{
@@ -24,7 +24,7 @@
}
~~~~~~~~~~
-当一个`Reader`解æžæ­¤JSON时,它会顺åºåœ°å‘处ç†å™¨å‘é€ä»¥ä¸‹çš„事件:
+当一个 `Reader` 解æžæ­¤ JSON 时,它会顺åºåœ°å‘处ç†å™¨å‘é€ä»¥ä¸‹çš„事件:
~~~~~~~~~~
StartObject()
@@ -50,7 +50,7 @@ EndArray(4)
EndObject(7)
~~~~~~~~~~
-除了一些事件å‚数需è¦å†ä½œè§£é‡Šï¼Œè¿™äº›äº‹ä»¶å¯ä»¥è½»æ¾åœ°ä¸ŽJSON对上。我们å¯ä»¥çœ‹çœ‹`simplereader`例å­æ€Žæ ·äº§ç”Ÿå’Œä»¥ä¸Šå®Œå…¨ç›¸åŒçš„结果:
+除了一些事件å‚数需è¦å†ä½œè§£é‡Šï¼Œè¿™äº›äº‹ä»¶å¯ä»¥è½»æ¾åœ°ä¸Ž JSON 对上。我们å¯ä»¥çœ‹çœ‹ `simplereader` 例å­æ€Žæ ·äº§ç”Ÿå’Œä»¥ä¸Šå®Œå…¨ç›¸åŒçš„结果:
~~~~~~~~~~cpp
#include "rapidjson/reader.h"
@@ -59,7 +59,7 @@ EndObject(7)
using namespace rapidjson;
using namespace std;
-struct MyHandler {
+struct MyHandler : public BaseReaderHandler<UTF8<>, MyHandler> {
bool Null() { cout << "Null()" << endl; return true; }
bool Bool(bool b) { cout << "Bool(" << boolalpha << b << ")" << endl; return true; }
bool Int(int i) { cout << "Int(" << i << ")" << endl; return true; }
@@ -91,11 +91,11 @@ void main() {
}
~~~~~~~~~~
-注æ„RapidJSON使用模æ¿åŽ»é™æ€æŒ·å®š`Reader`类型åŠå¤„ç†å™¨çš„类形,而ä¸æ˜¯ä½¿ç”¨å«è™šå‡½æ•°çš„类。这个范å¼å¯ä»¥é€šè¿‡æŠŠå‡½æ•°å†…è”而改善性能。
+æ³¨æ„ RapidJSON 使用模æ¿åŽ»é™æ€æŒ·å®š `Reader` 类型åŠå¤„ç†å™¨çš„类形,而ä¸æ˜¯ä½¿ç”¨å«è™šå‡½æ•°çš„类。这个范å¼å¯ä»¥é€šè¿‡æŠŠå‡½æ•°å†…è”而改善性能。
## 处ç†å™¨ {#Handler}
-如å‰ä¾‹æ‰€ç¤ºï¼Œä½¿ç”¨è€…需è¦å®žçŽ°ä¸€ä¸ªå¤„ç†å™¨ï¼ˆhandler),用于处ç†æ¥è‡ª`Reader`的事件(函数调用)。处ç†å™¨å¿…须包å«ä»¥ä¸‹çš„æˆå‘˜å‡½æ•°ã€‚
+如å‰ä¾‹æ‰€ç¤ºï¼Œä½¿ç”¨è€…需è¦å®žçŽ°ä¸€ä¸ªå¤„ç†å™¨ï¼ˆhandler),用于处ç†æ¥è‡ª `Reader` 的事件(函数调用)。处ç†å™¨å¿…须包å«ä»¥ä¸‹çš„æˆå‘˜å‡½æ•°ã€‚
~~~~~~~~~~cpp
class Handler {
@@ -106,6 +106,7 @@ class Handler {
bool Int64(int64_t i);
bool Uint64(uint64_t i);
bool Double(double d);
+ bool RawNumber(const Ch* str, SizeType length, bool copy);
bool String(const Ch* str, SizeType length, bool copy);
bool StartObject();
bool Key(const Ch* str, SizeType length, bool copy);
@@ -115,25 +116,25 @@ class Handler {
};
~~~~~~~~~~
-当`Reader`é‡åˆ°JSON null值时会调用`Null()`。
+当 `Reader` é‡åˆ° JSON null 值时会调用 `Null()`。
-当`Reader`é‡åˆ°JSON true或false值时会调用`Bool(bool)`。
+当 `Reader` é‡åˆ° JSON true 或 false 值时会调用 `Bool(bool)`。
-当`Reader`é‡åˆ°JSON number,它会选择一个åˆé€‚çš„C++类型映射,然åŽè°ƒç”¨`Int(int)`ã€`Uint(unsigned)`ã€`Int64(int64_t)`ã€`Uint64(uint64_t)`åŠ`Double(double)`çš„*其中之一个*。
+当 `Reader` é‡åˆ° JSON number,它会选择一个åˆé€‚çš„ C++ 类型映射,然åŽè°ƒç”¨ `Int(int)`ã€`Uint(unsigned)`ã€`Int64(int64_t)`ã€`Uint64(uint64_t)` åŠ `Double(double)` çš„ * 其中之一个 *。 若开å¯äº† `kParseNumbersAsStrings` 选项,`Reader` 便会改为调用 `RawNumber()`。
-当`Reader`é‡åˆ°JSON string,它会调用`String(const char* str, SizeType length, bool copy)`。第一个å‚数是字符串的指针。第二个å‚数是字符串的长度(ä¸åŒ…å«ç©ºç»ˆæ­¢ç¬¦å·ï¼‰ã€‚注æ„RapidJSON支æŒå­—串中å«æœ‰ç©ºå­—符`'\0'`。若出现这ç§æƒ…况,便会有`strlen(str) < length`。最åŽçš„`copy`å‚数表示处ç†å™¨æ˜¯å¦éœ€è¦å¤åˆ¶è¯¥å­—符串。在正常解æžæ—¶ï¼Œ`copy = true`。仅当使用原ä½è§£æžæ—¶ï¼Œ`copy = false`。此外,还è¦æ³¨æ„字符的类型与目标编ç ç›¸å…³ï¼Œæˆ‘们ç¨åŽä¼šå†è°ˆè¿™ä¸€ç‚¹ã€‚
+当 `Reader` é‡åˆ° JSON string,它会调用 `String(const char* str, SizeType length, bool copy)`。第一个å‚数是字符串的指针。第二个å‚数是字符串的长度(ä¸åŒ…å«ç©ºç»ˆæ­¢ç¬¦å·ï¼‰ã€‚æ³¨æ„ RapidJSON 支æŒå­—串中å«æœ‰ç©ºå­—符 `\0`。若出现这ç§æƒ…况,便会有 `strlen(str) < length`。最åŽçš„ `copy` å‚数表示处ç†å™¨æ˜¯å¦éœ€è¦å¤åˆ¶è¯¥å­—符串。在正常解æžæ—¶ï¼Œ`copy = true`。仅当使用原ä½è§£æžæ—¶ï¼Œ`copy = false`。此外,还è¦æ³¨æ„字符的类型与目标编ç ç›¸å…³ï¼Œæˆ‘们ç¨åŽä¼šå†è°ˆè¿™ä¸€ç‚¹ã€‚
-当`Reader`é‡åˆ°JSON object的开始之时,它会调用`StartObject()`。JSONçš„object是一个键值对(æˆå‘˜ï¼‰çš„集åˆã€‚è‹¥object包å«æˆå‘˜ï¼Œå®ƒä¼šå…ˆä¸ºæˆå‘˜çš„å字调用`Key()`,然åŽå†æŒ‰å€¼çš„类型调用函数。它ä¸æ–­è°ƒç”¨è¿™äº›é”®å€¼å¯¹ï¼Œç›´è‡³æœ€ç»ˆè°ƒç”¨`EndObject(SizeType memberCount)`。注æ„`memberCount`å‚数对处ç†å™¨æ¥è¯´åªæ˜¯å助性质,使用者å¯èƒ½ä¸éœ€è¦æ­¤å‚数。
+当 `Reader` é‡åˆ° JSON object 的开始之时,它会调用 `StartObject()`。JSON çš„ object 是一个键值对(æˆå‘˜ï¼‰çš„集åˆã€‚è‹¥ object 包å«æˆå‘˜ï¼Œå®ƒä¼šå…ˆä¸ºæˆå‘˜çš„å字调用 `Key()`,然åŽå†æŒ‰å€¼çš„类型调用函数。它ä¸æ–­è°ƒç”¨è¿™äº›é”®å€¼å¯¹ï¼Œç›´è‡³æœ€ç»ˆè°ƒç”¨ `EndObject(SizeType memberCount)`ã€‚æ³¨æ„ `memberCount` å‚数对处ç†å™¨æ¥è¯´åªæ˜¯å助性质,使用者å¯èƒ½ä¸éœ€è¦æ­¤å‚数。
-JSON array与object相似,但更简å•ã€‚在array开始时,`Reader`会调用`BeginArary()`。若arrayå«æœ‰å…ƒç´ ï¼Œå®ƒä¼šæŒ‰å…ƒç´ çš„类型æ¥è¯»ç”¨å‡½æ•°ã€‚相似地,最åŽå®ƒä¼šè°ƒç”¨`EndArray(SizeType elementCount)`,其中`elementCount`å‚数对处ç†å™¨æ¥è¯´åªæ˜¯å助性质。
+JSON array 与 object 相似,但更简å•ã€‚在 array 开始时,`Reader` 会调用 `BeginArary()`。若 array å«æœ‰å…ƒç´ ï¼Œå®ƒä¼šæŒ‰å…ƒç´ çš„类型æ¥è¯»ç”¨å‡½æ•°ã€‚相似地,最åŽå®ƒä¼šè°ƒç”¨ `EndArray(SizeType elementCount)`,其中 `elementCount` å‚数对处ç†å™¨æ¥è¯´åªæ˜¯å助性质。
-æ¯ä¸ªå¤„ç†å™¨å‡½æ•°éƒ½è¿”回一个`bool`。正常它们应返回`true`。若处ç†å™¨é‡åˆ°é”™è¯¯ï¼Œå®ƒå¯ä»¥è¿”回`false`去通知事件å‘é€æ–¹åœæ­¢ç»§ç»­å¤„ç†ã€‚
+æ¯ä¸ªå¤„ç†å™¨å‡½æ•°éƒ½è¿”回一个 `bool`。正常它们应返回 `true`。若处ç†å™¨é‡åˆ°é”™è¯¯ï¼Œå®ƒå¯ä»¥è¿”回 `false` 去通知事件å‘é€æ–¹åœæ­¢ç»§ç»­å¤„ç†ã€‚
-例如,当我们用`Reader`解æžä¸€ä¸ªJSON时,处ç†å™¨æ£€æµ‹åˆ°è¯¥JSON并ä¸ç¬¦åˆæ‰€éœ€çš„schema,那么处ç†å™¨å¯ä»¥è¿”回`false`,令`Reader`åœæ­¢ä¹‹åŽçš„解æžå·¥ä½œã€‚而`Reader`会进入一个错误状æ€ï¼Œå¹¶ä»¥`kParseErrorTermination`错误ç æ ‡è¯†ã€‚
+例如,当我们用 `Reader` 解æžä¸€ä¸ª JSON 时,处ç†å™¨æ£€æµ‹åˆ°è¯¥ JSON 并ä¸ç¬¦åˆæ‰€éœ€çš„ schema,那么处ç†å™¨å¯ä»¥è¿”回 `false`,令 `Reader` åœæ­¢ä¹‹åŽçš„解æžå·¥ä½œã€‚而 `Reader` 会进入一个错误状æ€ï¼Œå¹¶ä»¥ `kParseErrorTermination` 错误ç æ ‡è¯†ã€‚
## GenericReader {#GenericReader}
-å‰é¢æåŠï¼Œ`Reader`是`GenericReader`模æ¿ç±»çš„typedef:
+å‰é¢æåŠï¼Œ`Reader` 是 `GenericReader` 模æ¿ç±»çš„ typedef:
~~~~~~~~~~cpp
namespace rapidjson {
@@ -148,19 +149,19 @@ typedef GenericReader<UTF8<>, UTF8<> > Reader;
} // namespace rapidjson
~~~~~~~~~~
-`Reader`使用UTF-8作为æ¥æºåŠç›®æ ‡ç¼–ç ã€‚æ¥æºç¼–ç æ˜¯æŒ‡JSONæµçš„ç¼–ç ã€‚目标编ç æ˜¯æŒ‡`String()`çš„`str`å‚数所用的编ç ã€‚例如,è¦è§£æžä¸€ä¸ªUTF-8æµå¹¶è¾“出至UTF-16 string事件,你需è¦è¿™ä¹ˆå®šä¹‰ä¸€ä¸ªreader:
+`Reader` 使用 UTF-8 作为æ¥æºåŠç›®æ ‡ç¼–ç ã€‚æ¥æºç¼–ç æ˜¯æŒ‡ JSON æµçš„ç¼–ç ã€‚目标编ç æ˜¯æŒ‡ `String()` çš„ `str` å‚数所用的编ç ã€‚例如,è¦è§£æžä¸€ä¸ª UTF-8 æµå¹¶è¾“出至 UTF-16 string 事件,你需è¦è¿™ä¹ˆå®šä¹‰ä¸€ä¸ª reader:
~~~~~~~~~~cpp
GenericReader<UTF8<>, UTF16<> > reader;
~~~~~~~~~~
-注æ„到`UTF16`的缺çœç±»åž‹æ˜¯`wchar_t`。因此这个`reader`需è¦è°ƒç”¨å¤„ç†å™¨çš„`String(const wchar_t*, SizeType, bool)`。
+注æ„到 `UTF16` 的缺çœç±»åž‹æ˜¯ `wchar_t`。因此这个 `reader` 需è¦è°ƒç”¨å¤„ç†å™¨çš„ `String(const wchar_t*, SizeType, bool)`。
-第三个模æ¿å‚æ•°`Allocator`是内部数æ®ç»“构(实际上是一个堆栈)的分é…器类型。
+第三个模æ¿å‚æ•° `Allocator` 是内部数æ®ç»“构(实际上是一个堆栈)的分é…器类型。
-## è§£æž {#Parsing}
+## è§£æž {#SaxParsing}
-`Reader`的唯一功能就是解æžJSON。
+`Reader` çš„å”¯ä¸€åŠŸèƒ½å°±æ˜¯è§£æž JSON。
~~~~~~~~~~cpp
template <unsigned parseFlags, typename InputStream, typename Handler>
@@ -171,15 +172,15 @@ template <typename InputStream, typename Handler>
bool Parse(InputStream& is, Handler& handler);
~~~~~~~~~~
-若在解æžä¸­å‡ºçŽ°é”™è¯¯ï¼Œå®ƒä¼šè¿”回`false`。使用者å¯è°ƒç”¨`bool HasParseEror()`, `ParseErrorCode GetParseErrorCode()`åŠ`size_t GetErrorOffset()`获å–错误状æ€ã€‚实际上`Document`使用这些`Reader`函数去获å–解æžé”™è¯¯ã€‚请å‚考[DOM](doc/dom.md)去了解有关解æžé”™è¯¯çš„细节。
+若在解æžä¸­å‡ºçŽ°é”™è¯¯ï¼Œå®ƒä¼šè¿”回 `false`。使用者å¯è°ƒç”¨ `bool HasParseEror()`, `ParseErrorCode GetParseErrorCode()` åŠ `size_t GetErrorOffset()` 获å–错误状æ€ã€‚实际上 `Document` 使用这些 `Reader` 函数去获å–解æžé”™è¯¯ã€‚请å‚考 [DOM](doc/dom.zh-cn.md) 去了解有关解æžé”™è¯¯çš„细节。
# Writer {#Writer}
-`Reader`把JSON转æ¢ï¼ˆè§£æžï¼‰æˆä¸ºäº‹ä»¶ã€‚`Writer`åšå®Œå…¨ç›¸å的事情。它把事件转æ¢æˆJSON。
+`Reader` 把 JSON 转æ¢ï¼ˆè§£æžï¼‰æˆä¸ºäº‹ä»¶ã€‚`Writer` åšå®Œå…¨ç›¸å的事情。它把事件转æ¢æˆ JSON。
-`Writer`是éžå¸¸å®¹æ˜“使用的。若你的应用程åºåªéœ€æŠŠä¸€äº›æ•°æ®è½¬æ¢æˆJSON,å¯èƒ½ç›´æŽ¥ä½¿ç”¨`Writer`,会比建立一个`Document`然åŽç”¨`Writer`把它转æ¢æˆJSON更加方便。
+`Writer` 是éžå¸¸å®¹æ˜“使用的。若你的应用程åºåªéœ€æŠŠä¸€äº›æ•°æ®è½¬æ¢æˆ JSON,å¯èƒ½ç›´æŽ¥ä½¿ç”¨ `Writer`,会比建立一个 `Document` 然åŽç”¨ `Writer` 把它转æ¢æˆ JSON 更加方便。
-在`simplewriter`例å­é‡Œï¼Œæˆ‘们åš`simplereader`完全相å的事情。
+在 `simplewriter` 例å­é‡Œï¼Œæˆ‘ä»¬åš `simplereader` 完全相å的事情。
~~~~~~~~~~cpp
#include "rapidjson/writer.h"
@@ -221,24 +222,24 @@ void main() {
{"hello":"world","t":true,"f":false,"n":null,"i":123,"pi":3.1416,"a":[0,1,2,3]}
~~~~~~~~~~
-`String()`åŠ`Key()`å„有两个é‡è½½ã€‚一个是如处ç†å™¨concept般,有3个å‚数。它能处ç†å«ç©ºå­—符的字符串。å¦ä¸€ä¸ªæ˜¯å¦‚上中使用的较简å•ç‰ˆæœ¬ã€‚
+`String()` åŠ `Key()` å„有两个é‡è½½ã€‚一个是如处ç†å™¨ concept 般,有 3 个å‚数。它能处ç†å«ç©ºå­—符的字符串。å¦ä¸€ä¸ªæ˜¯å¦‚上中使用的较简å•ç‰ˆæœ¬ã€‚
-注æ„到,例å­ä»£ç ä¸­çš„`EndArray()`åŠ`EndObject()`并没有å‚数。å¯ä»¥ä¼ é€’一个`SizeType`çš„å‚数,但它会被`Writer`忽略。
+注æ„到,例å­ä»£ç ä¸­çš„ `EndArray()` åŠ `EndObject()` 并没有å‚数。å¯ä»¥ä¼ é€’一个 `SizeType` çš„å‚数,但它会被 `Writer` 忽略。
-ä½ å¯èƒ½ä¼šæ€€ç–‘,为什么ä¸ä½¿ç”¨`sprintf()`或`std::stringstream`去建立一个JSON?
+ä½ å¯èƒ½ä¼šæ€€ç–‘,为什么ä¸ä½¿ç”¨ `sprintf()` 或 `std::stringstream` 去建立一个 JSON?
这有几个原因:
-1. `Writer`必然会输出一个结构良好(well-formed)的JSON。若然有错误的事件次åºï¼ˆå¦‚`Int()`ç´§éš`StartObject()`出现),它会在调试模å¼ä¸­äº§ç”Ÿæ–­è¨€å¤±è´¥ã€‚
-2. `Writer::String()`å¯å¤„ç†å­—符串转义(如把ç ç‚¹`U+000A`转æ¢æˆ`\n`)åŠè¿›è¡ŒUnicode转ç ã€‚
-3. `Writer`一致地处ç†number的输出。
-4. `Writer`实现了事件处ç†å™¨concept。å¯ç”¨äºŽå¤„ç†æ¥è‡ª`Reader`ã€`Document`或其他事件å‘生器。
-5. `Writer`å¯å¯¹ä¸åŒå¹³å°è¿›è¡Œä¼˜åŒ–。
+1. `Writer` 必然会输出一个结构良好(well-formed)的 JSON。若然有错误的事件次åºï¼ˆå¦‚ `Int()` ç´§éš `StartObject()` 出现),它会在调试模å¼ä¸­äº§ç”Ÿæ–­è¨€å¤±è´¥ã€‚
+2. `Writer::String()` å¯å¤„ç†å­—符串转义(如把ç ç‚¹ `U+000A` 转æ¢æˆ `\n`)åŠè¿›è¡Œ Unicode 转ç ã€‚
+3. `Writer` ä¸€è‡´åœ°å¤„ç† number 的输出。
+4. `Writer` 实现了事件处ç†å™¨ concept。å¯ç”¨äºŽå¤„ç†æ¥è‡ª `Reader`ã€`Document` 或其他事件å‘生器。
+5. `Writer` å¯å¯¹ä¸åŒå¹³å°è¿›è¡Œä¼˜åŒ–。
-无论如何,使用`Writer` API去生æˆJSON甚至乎比这些临时方法更简å•ã€‚
+无论如何,使用 `Writer` API åŽ»ç”Ÿæˆ JSON 甚至乎比这些临时方法更简å•ã€‚
## æ¨¡æ¿ {#WriterTemplate}
-`Writer`与`Reader`有少许设计区别。`Writer`是一个模æ¿ç±»ï¼Œè€Œä¸æ˜¯ä¸€ä¸ªtypedef。 并没有`GenericWriter`。以下是`Writer`的声明。
+`Writer` 与 `Reader` 有少许设计区别。`Writer` 是一个模æ¿ç±»ï¼Œè€Œä¸æ˜¯ä¸€ä¸ª typedef。 并没有 `GenericWriter`。以下是 `Writer` 的声明。
~~~~~~~~~~cpp
namespace rapidjson {
@@ -253,39 +254,48 @@ public:
} // namespace rapidjson
~~~~~~~~~~
-`OutputStream`模æ¿å‚数是输出æµçš„类型。它的类型ä¸å¯ä»¥è¢«è‡ªåŠ¨æŽ¨æ–­ï¼Œå¿…须由使用者æ供。
+`OutputStream` 模æ¿å‚数是输出æµçš„类型。它的类型ä¸å¯ä»¥è¢«è‡ªåŠ¨æŽ¨æ–­ï¼Œå¿…须由使用者æ供。
-`SourceEncoding`模æ¿å‚数指定了`String(const Ch*, ...)`çš„ç¼–ç ã€‚
+`SourceEncoding` 模æ¿å‚数指定了 `String(const Ch*, ...)` çš„ç¼–ç ã€‚
-`TargetEncoding`模æ¿å‚数指定输出æµçš„ç¼–ç ã€‚
+`TargetEncoding` 模æ¿å‚数指定输出æµçš„ç¼–ç ã€‚
-最åŽä¸€ä¸ª`Allocator`是分é…器的类型,用于分é…内部数æ®ç»“构(一个堆栈)。
+`Allocator` 是分é…器的类型,用于分é…内部数æ®ç»“构(一个堆栈)。
-此外,`Writer`的构造函数有一`levelDepth`å‚数。存储æ¯å±‚阶信æ¯çš„åˆå§‹å†…存分é…é‡å—æ­¤å‚æ•°å½±å“。
+`writeFlags` 是以下ä½æ ‡å¿—的组åˆï¼š
+
+写入ä½æ ‡å¿— | æ„义
+------------------------------|-----------------------------------
+`kWriteNoFlags` | 没有任何标志。
+`kWriteDefaultFlags` | 缺çœçš„解æžé€‰é¡¹ã€‚它等于 `RAPIDJSON_WRITE_DEFAULT_FLAGS` å®ï¼Œæ­¤å®å®šä¹‰ä¸º `kWriteNoFlags`。
+`kWriteValidateEncodingFlag` | 校验 JSON 字符串的编ç ã€‚
+`kWriteNanAndInfFlag` | 容许写入 `Infinity`, `-Infinity` åŠ `NaN`。
+
+此外,`Writer` 的构造函数有一 `levelDepth` å‚数。存储æ¯å±‚阶信æ¯çš„åˆå§‹å†…存分é…é‡å—æ­¤å‚æ•°å½±å“。
## PrettyWriter {#PrettyWriter}
-`Writer`所输出的是没有空格字符的最紧凑JSON,适åˆç½‘络传输或储存,但ä¸é€‚åˆäººç±»é˜…读。
+`Writer` 所输出的是没有空格字符的最紧凑 JSON,适åˆç½‘络传输或储存,但ä¸é€‚åˆäººç±»é˜…读。
-因此,RapidJSONæ供了一个`PrettyWriter`,它在输出中加入缩进åŠæ¢è¡Œã€‚
+因此,RapidJSON æ供了一个 `PrettyWriter`,它在输出中加入缩进åŠæ¢è¡Œã€‚
-`PrettyWriter`的用法与`Writer`几乎一样,ä¸åŒä¹‹å¤„是`PrettyWriter`æ供了一个`SetIndent(Ch indentChar, unsigned indentCharCount)`函数。缺çœçš„缩进是4个空格。
+`PrettyWriter` 的用法与 `Writer` 几乎一样,ä¸åŒä¹‹å¤„是 `PrettyWriter` æ供了一个 `SetIndent(Ch indentChar, unsigned indentCharCount)` 函数。缺çœçš„缩进是 4 个空格。
## 完整性åŠé‡ç½® {#CompletenessReset}
-一个`Writer`åªå¯è¾“出å•ä¸ªJSON,其根节点å¯ä»¥æ˜¯ä»»ä½•JSON类型。当处ç†å®Œå•ä¸ªæ ¹èŠ‚点事件(如`String()`),或匹é…的最åŽ`EndObject()`或`EndArray()`事件,输出的JSON是结构完整(well-formed)åŠå®Œæ•´çš„。使用者å¯è°ƒç”¨`Writer::IsComplete()`去检测完整性。
+一个 `Writer` åªå¯è¾“出å•ä¸ª JSON,其根节点å¯ä»¥æ˜¯ä»»ä½• JSON 类型。当处ç†å®Œå•ä¸ªæ ¹èŠ‚点事件(如 `String()`),或匹é…çš„æœ€åŽ `EndObject()` 或 `EndArray()` 事件,输出的 JSON 是结构完整(well-formed)åŠå®Œæ•´çš„。使用者å¯è°ƒç”¨ `Writer::IsComplete()` 去检测完整性。
-当JSON完整时,`Writer`ä¸èƒ½å†æŽ¥å—新的事件。ä¸ç„¶å…¶è¾“出便会是ä¸åˆæ³•çš„(例如有超过一个根节点)。为了é‡æ–°åˆ©ç”¨`Writer`对象,使用者å¯è°ƒç”¨`Writer::Reset(OutputStream& os)`去é‡ç½®å…¶æ‰€æœ‰å†…部状æ€åŠè®¾ç½®æ–°çš„输出æµã€‚
+当 JSON 完整时,`Writer` ä¸èƒ½å†æŽ¥å—新的事件。ä¸ç„¶å…¶è¾“出便会是ä¸åˆæ³•çš„(例如有超过一个根节点)。为了é‡æ–°åˆ©ç”¨ `Writer` 对象,使用者å¯è°ƒç”¨ `Writer::Reset(OutputStream& os)` 去é‡ç½®å…¶æ‰€æœ‰å†…部状æ€åŠè®¾ç½®æ–°çš„输出æµã€‚
-# 技巧 {#Techniques}
+# 技巧 {#SaxTechniques}
-## 解æžJSON至自定义结构 {#CustomDataStructure}
+## è§£æž JSON 至自定义结构 {#CustomDataStructure}
-`Document`的解æžåŠŸèƒ½å®Œå…¨ä¾é `Reader`。实际上`Document`是一个处ç†å™¨ï¼Œåœ¨è§£æžJSON时接收事件去建立一个DOM。
+`Document` 的解æžåŠŸèƒ½å®Œå…¨ä¾é  `Reader`。实际上 `Document` 是一个处ç†å™¨ï¼Œåœ¨è§£æž JSON 时接收事件去建立一个 DOM。
-使用者å¯ä»¥ç›´æŽ¥ä½¿ç”¨`Reader`去建立其他数æ®ç»“构。这消除了建立DOM的步骤,从而å‡å°‘了内存开销并改善性能。
+使用者å¯ä»¥ç›´æŽ¥ä½¿ç”¨ `Reader` 去建立其他数æ®ç»“构。这消除了建立 DOM 的步骤,从而å‡å°‘了内存开销并改善性能。
-在以下的`messagereader`例å­ä¸­ï¼Œ`ParseMessages()`解æžä¸€ä¸ªJSON,该JSON应该是一个å«é”®å€¼å¯¹çš„object。
+在以下的 `messagereader` 例å­ä¸­ï¼Œ`ParseMessages()` 解æžä¸€ä¸ª JSON,该 JSON 应该是一个å«é”®å€¼å¯¹çš„ object。
~~~~~~~~~~cpp
#include "rapidjson/reader.h"
@@ -386,15 +396,15 @@ Error: Terminate parsing due to Handler error.
at offset 59 near '} }...'
~~~~~~~~~~
-第一个JSON(`json1`)被æˆåŠŸåœ°è§£æžè‡³`MessageMap`。由于`MessageMap`是一个`std::map`,打å°æ¬¡åºæŒ‰é”®å€¼æŽ’åºã€‚此次åºä¸ŽJSON中的次åºä¸åŒã€‚
+第一个 JSON(`json1`)被æˆåŠŸåœ°è§£æžè‡³ `MessageMap`。由于 `MessageMap` 是一个 `std::map`,打å°æ¬¡åºæŒ‰é”®å€¼æŽ’åºã€‚此次åºä¸Ž JSON 中的次åºä¸åŒã€‚
-在第二个JSON(`json2`)中,`foo`的值是一个空object。由于它是一个object,`MessageHandler::StartObject()`会被调用。然而,在`state_ = kExpectValue`的情况下,该函数会返回`false`,并导致解æžè¿‡ç¨‹ç»ˆæ­¢ã€‚错误代ç æ˜¯`kParseErrorTermination`。
+在第二个 JSON(`json2`)中,`foo` 的值是一个空 object。由于它是一个 object,`MessageHandler::StartObject()` 会被调用。然而,在 `state_ = kExpectValue` 的情况下,该函数会返回 `false`,并导致解æžè¿‡ç¨‹ç»ˆæ­¢ã€‚错误代ç æ˜¯ `kParseErrorTermination`。
-## 过滤JSON {#Filtering}
+## 过滤 JSON {#Filtering}
-如å‰é¢æåŠè¿‡ï¼Œ`Writer`å¯å¤„ç†`Reader`å‘出的事件。`example/condense/condense.cpp`例å­ç®€å•åœ°è®¾ç½®`Writer`作为一个`Reader`的处ç†å™¨ï¼Œå› æ­¤å®ƒèƒ½ç§»é™¤JSON中的所有空白字符。`example/pretty/pretty.cpp`例å­ä½¿ç”¨åŒæ ·çš„关系,åªæ˜¯ä»¥`PrettyWriter`å–代`Writer`。因此`pretty`能够é‡æ–°æ ¼å¼åŒ–JSON,加入缩进åŠæ¢è¡Œã€‚
+如å‰é¢æåŠè¿‡ï¼Œ`Writer` å¯å¤„ç† `Reader` å‘出的事件。`example/condense/condense.cpp` 例å­ç®€å•åœ°è®¾ç½® `Writer` 作为一个 `Reader` 的处ç†å™¨ï¼Œå› æ­¤å®ƒèƒ½ç§»é™¤ JSON 中的所有空白字符。`example/pretty/pretty.cpp` 例å­ä½¿ç”¨åŒæ ·çš„关系,åªæ˜¯ä»¥ `PrettyWriter` å–代 `Writer`。因此 `pretty` 能够é‡æ–°æ ¼å¼åŒ– JSON,加入缩进åŠæ¢è¡Œã€‚
-实际上,我们å¯ä»¥ä½¿ç”¨SAX风格API去加入(多个)中间层去过滤JSON的内容。例如`capitalize`例å­å¯ä»¥æŠŠæ‰€æœ‰JSON string改为大写。
+实际上,我们å¯ä»¥ä½¿ç”¨ SAX 风格 API 去加入(多个)中间层去过滤 JSON 的内容。例如 `capitalize` 例å­å¯ä»¥æŠŠæ‰€æœ‰ JSON string 改为大写。
~~~~~~~~~~cpp
#include "rapidjson/reader.h"
@@ -419,6 +429,7 @@ struct CapitalizeFilter {
bool Int64(int64_t i) { return out_.Int64(i); }
bool Uint64(uint64_t u) { return out_.Uint64(u); }
bool Double(double d) { return out_.Double(d); }
+ bool RawNumber(const char* str, SizeType length, bool copy) { return out_.RawNumber(str, length, copy); }
bool String(const char* str, SizeType length, bool) {
buffer_.clear();
for (SizeType i = 0; i < length; i++)
@@ -457,20 +468,20 @@ int main(int, char*[]) {
}
~~~~~~~~~~
-注æ„到,ä¸å¯ç®€å•åœ°æŠŠJSON当作字符串去改为大写。例如:
+注æ„到,ä¸å¯ç®€å•åœ°æŠŠ JSON 当作字符串去改为大写。例如:
~~~~~~~~~~
["Hello\nWorld"]
~~~~~~~~~~
-简å•åœ°æŠŠæ•´ä¸ªJSON转为大写的è¯ä¼šäº§ç”Ÿé”™è¯¯çš„转义符:
+简å•åœ°æŠŠæ•´ä¸ª JSON 转为大写的è¯ä¼šäº§ç”Ÿé”™è¯¯çš„转义符:
~~~~~~~~~~
["HELLO\NWORLD"]
~~~~~~~~~~
-而`capitalize`就会产生正确的结果:
+而 `capitalize` 就会产生正确的结果:
~~~~~~~~~~
["HELLO\nWORLD"]
~~~~~~~~~~
-我们还å¯ä»¥å¼€å‘æ›´å¤æ‚的过滤器。然而,由于SAX风格API在æŸä¸€æ—¶é—´ç‚¹åªèƒ½æä¾›å•ä¸€äº‹ä»¶çš„ä¿¡æ¯ï¼Œä½¿ç”¨è€…需è¦è‡ªè¡Œè®°å½•ä¸€äº›ä¸Šä¸‹æ–‡ä¿¡æ¯ï¼ˆä¾‹å¦‚从根节点起的路径ã€å‚¨å­˜å…¶ä»–相关值)。对于处ç†æŸäº›æƒ…况,用DOM会比SAX更容易实现。
+我们还å¯ä»¥å¼€å‘æ›´å¤æ‚的过滤器。然而,由于 SAX 风格 API 在æŸä¸€æ—¶é—´ç‚¹åªèƒ½æä¾›å•ä¸€äº‹ä»¶çš„ä¿¡æ¯ï¼Œä½¿ç”¨è€…需è¦è‡ªè¡Œè®°å½•ä¸€äº›ä¸Šä¸‹æ–‡ä¿¡æ¯ï¼ˆä¾‹å¦‚从根节点起的路径ã€å‚¨å­˜å…¶ä»–相关值)。对于处ç†æŸäº›æƒ…况,用 DOM 会比 SAX 更容易实现。
diff --git a/doc/schema.md b/doc/schema.md
index 053fc236..b4542251 100644
--- a/doc/schema.md
+++ b/doc/schema.md
@@ -1,6 +1,6 @@
# Schema
-## Status: experimental, shall be included in v1.1
+(This feature was released in v1.1.0)
JSON Schema is a draft standard for describing the format of JSON data. The schema itself is also JSON data. By validating a JSON structure with JSON Schema, your code can safely access the DOM without manually checking types, or whether a key exists, etc. It can also ensure that the serialized JSON conform to a specified schema.
@@ -8,7 +8,7 @@ RapidJSON implemented a JSON Schema validator for [JSON Schema Draft v4](http://
[TOC]
-## Basic Usage
+# Basic Usage {#BasicUsage}
First of all, you need to parse a JSON Schema into `Document`, and then compile the `Document` into a `SchemaDocument`.
@@ -20,7 +20,7 @@ Secondly, construct a `SchemaValidator` with the `SchemaDocument`. It is similar
// ...
Document sd;
-if (!sd.Parse(schemaJson)) {
+if (sd.Parse(schemaJson).HasParseError()) {
// the schema is not a valid JSON.
// ...
}
@@ -28,7 +28,7 @@ SchemaDocument schema(sd); // Compile a Document to SchemaDocument
// sd is no longer needed here.
Document d;
-if (!d.Parse(inputJson)) {
+if (d.Parse(inputJson).HasParseError()) {
// the input is not a valid JSON.
// ...
}
@@ -49,14 +49,14 @@ if (!d.Accept(validator)) {
Some notes:
-* One `SchemaDocment` can be referenced by multiple `SchemaValidator`s. It will not be modified by `SchemaValidator`s.
+* One `SchemaDocument` can be referenced by multiple `SchemaValidator`s. It will not be modified by `SchemaValidator`s.
* A `SchemaValidator` may be reused to validate multiple documents. To run it for other documents, call `validator.Reset()` first.
-## Validation during parsing/serialization
+# Validation during parsing/serialization {#ParsingSerialization}
Unlike most JSON Schema validator implementations, RapidJSON provides a SAX-based schema validator. Therefore, you can parse a JSON from a stream while validating it on the fly. If the validator encounters a JSON value that invalidates the supplied schema, the parsing will be terminated immediately. This design is especially useful for parsing large JSON files.
-### DOM parsing
+## DOM parsing {#DomParsing}
For using DOM in parsing, `Document` needs some preparation and finalizing tasks, in addition to receiving SAX events, thus it needs some work to route the reader, validator and the document. `SchemaValidatingReader` is a helper class that doing such work.
@@ -97,7 +97,7 @@ if (!reader.GetParseResult()) {
}
~~~
-### SAX parsing
+## SAX parsing {#SaxParsing}
For using SAX in parsing, it is much simpler. If it only need to validate the JSON without further processing, it is simply:
@@ -126,7 +126,7 @@ if (!reader.Parse(ss, validator)) {
}
~~~
-### Serialization
+## Serialization {#Serialization}
It is also possible to do validation during serializing. This can ensure the result JSON is valid according to the JSON schema.
@@ -144,20 +144,20 @@ if (!d.Accept(validator)) {
Of course, if your application only needs SAX-style serialization, it can simply send SAX events to `SchemaValidator` instead of `Writer`.
-## Remote Schema
+# Remote Schema {#RemoteSchema}
-JSON Schema supports [`$ref` keyword](http://spacetelescope.github.io/understanding-json-schema/structuring.html), which is a [JSON pointer](pointer.md) referencing to a local or remote schema. Local pointer is prefixed with `#`, while remote pointer is an relative or absolute URI. For example:
+JSON Schema supports [`$ref` keyword](http://spacetelescope.github.io/understanding-json-schema/structuring.html), which is a [JSON pointer](doc/pointer.md) referencing to a local or remote schema. Local pointer is prefixed with `#`, while remote pointer is an relative or absolute URI. For example:
~~~js
{ "$ref": "definitions.json#/address" }
~~~
-As `SchemaValidator` does not know how to resolve such URI, it needs a user-provided `IRemoteSchemaDocumentProvider` instance to do so.
+As `SchemaDocument` does not know how to resolve such URI, it needs a user-provided `IRemoteSchemaDocumentProvider` instance to do so.
~~~
class MyRemoteSchemaDocumentProvider : public IRemoteSchemaDocumentProvider {
public:
- virtual const SchemaDocument* GetRemoteDocument(const char* uri, SizeTyp length) {
+ virtual const SchemaDocument* GetRemoteDocument(const char* uri, SizeType length) {
// Resolve the uri and returns a pointer to that schema.
}
};
@@ -165,10 +165,10 @@ public:
// ...
MyRemoteSchemaDocumentProvider provider;
-SchemaValidator validator(schema, &provider);
+SchemaDocument schema(sd, &provider);
~~~
-## Conformance
+# Conformance {#Conformance}
RapidJSON passed 262 out of 263 tests in [JSON Schema Test Suite](https://github.com/json-schema/JSON-Schema-Test-Suite) (Json Schema draft 4).
@@ -176,7 +176,7 @@ The failed test is "changed scope ref invalid" of "change resolution scope" in `
Besides, the `format` schema keyword for string values is ignored, since it is not required by the specification.
-### Regular Expression
+## Regular Expression {#RegEx}
The schema keyword `pattern` and `patternProperties` uses regular expression to match the required pattern.
@@ -184,34 +184,34 @@ RapidJSON implemented a simple NFA regular expression engine, which is used by d
|Syntax|Description|
|------|-----------|
-|`ab` | Concatenation
-|`a|b` | Alternation
-|`a?` | Zero or one
-|`a*` | Zero or more
-|`a+` | One or more
-|`a{3}` | Exactly 3 times
-|`a{3,}` | At least 3 times
-|`a{3,5}`| 3 to 5 times
-|`(ab)` | Grouping
-|`^a` | At the beginning
-|`a$` | At the end
-|`.` | Any character
-|`[abc]` | Character classes
-|`[a-c]` | Character class range
-|`[a-z0-9_]` | Character class combination
-|`[^abc]` | Negated character classes
-|`[^a-c]` | Negated character class range
-|`[\b]` | Backspace (U+0008)
-|`\|`, `\\`, ... | Escape characters
-|`\f` | Form feed (U+000C)
-|`\n` | Line feed (U+000A)
-|`\r` | Carriage return (U+000D)
-|`\t` | Tab (U+0009)
-|`\v` | Vertical tab (U+000B)
+|`ab` | Concatenation |
+|<code>a&#124;b</code> | Alternation |
+|`a?` | Zero or one |
+|`a*` | Zero or more |
+|`a+` | One or more |
+|`a{3}` | Exactly 3 times |
+|`a{3,}` | At least 3 times |
+|`a{3,5}`| 3 to 5 times |
+|`(ab)` | Grouping |
+|`^a` | At the beginning |
+|`a$` | At the end |
+|`.` | Any character |
+|`[abc]` | Character classes |
+|`[a-c]` | Character class range |
+|`[a-z0-9_]` | Character class combination |
+|`[^abc]` | Negated character classes |
+|`[^a-c]` | Negated character class range |
+|`[\b]` | Backspace (U+0008) |
+|<code>\\&#124;</code>, `\\`, ... | Escape characters |
+|`\f` | Form feed (U+000C) |
+|`\n` | Line feed (U+000A) |
+|`\r` | Carriage return (U+000D) |
+|`\t` | Tab (U+0009) |
+|`\v` | Vertical tab (U+000B) |
For C++11 compiler, it is also possible to use the `std::regex` by defining `RAPIDJSON_SCHEMA_USE_INTERNALREGEX=0` and `RAPIDJSON_SCHEMA_USE_STDREGEX=1`. If your schemas do not need `pattern` and `patternProperties`, you can set both macros to zero to disable this feature, which will reduce some code size.
-## Performance
+# Performance {#Performance}
Most C++ JSON libraries do not yet support JSON Schema. So we tried to evaluate the performance of RapidJSON's JSON Schema validator according to [json-schema-benchmark](https://github.com/ebdrup/json-schema-benchmark), which tests 11 JavaScript libraries running on Node.js.
diff --git a/doc/schema.zh-cn.md b/doc/schema.zh-cn.md
index dd0ec73b..c85177f9 100644
--- a/doc/schema.zh-cn.md
+++ b/doc/schema.zh-cn.md
@@ -1,6 +1,6 @@
# Schema
-## 状æ€: 实验性,应该会åˆè¿› v1.1
+(本功能于 v1.1.0 å‘布)
JSON Schema 是æè¿° JSON æ ¼å¼çš„一个标准è‰æ¡ˆã€‚一个 schema 本身也是一个 JSON。使用 JSON Schema 去校验 JSON,å¯ä»¥è®©ä½ çš„代ç å®‰å…¨åœ°è®¿é—® DOM,而无须检查类型或键值是å¦å­˜åœ¨ç­‰ã€‚这也能确ä¿è¾“出的 JSON 是符åˆæŒ‡å®šçš„ schema。
@@ -8,7 +8,7 @@ RapidJSON 实现了一个 [JSON Schema Draft v4](http://json-schema.org/document
[TOC]
-## 基本用法
+# 基本用法 {#BasicUsage}
首先,你è¦æŠŠ JSON Schema 解æžæˆ `Document`,å†æŠŠå®ƒç¼–译æˆä¸€ä¸ª `SchemaDocument`。
@@ -20,23 +20,23 @@ RapidJSON 实现了一个 [JSON Schema Draft v4](http://json-schema.org/document
// ...
Document sd;
-if (!sd.Parse(schemaJson)) {
- // the schema is not a valid JSON.
+if (sd.Parse(schemaJson).HasParseError()) {
+ // æ­¤ schema ä¸æ˜¯åˆæ³•çš„ JSON
// ...
}
-SchemaDocument schema(sd); // Compile a Document to SchemaDocument
-// sd is no longer needed here.
+SchemaDocument schema(sd); // 把一个 Document 编译至 SchemaDocument
+// 之åŽä¸å†éœ€è¦ sd
Document d;
-if (!d.Parse(inputJson)) {
- // the input is not a valid JSON.
+if (d.Parse(inputJson).HasParseError()) {
+ // 输入ä¸æ˜¯ä¸€ä¸ªåˆæ³•çš„ JSON
// ...
}
SchemaValidator validator(schema);
if (!d.Accept(validator)) {
- // Input JSON is invalid according to the schema
- // Output diagnostic information
+ // 输入的 JSON ä¸åˆä¹Ž schema
+ // 打å°è¯Šæ–­ä¿¡æ¯
StringBuffer sb;
validator.GetInvalidSchemaPointer().StringifyUriFragment(sb);
printf("Invalid schema: %s\n", sb.GetString());
@@ -49,14 +49,14 @@ if (!d.Accept(validator)) {
一些注æ„点:
-* 一个 `SchemaDocment` 能被多个 `SchemaValidator` åŠç”¨ã€‚它ä¸ä¼šè¢« `SchemaValidator` 修改。
-* 一个 `SchemaValidator` å¯ä»¥é‡å¤ä½¿ç”¨æ¥æ ¡éªŒå¤šä¸ªæ–‡ä»¶ã€‚在校验其他文件å‰ï¼Œå…ˆè°ƒç”¨ `validator.Reset()`。
+* 一个 `SchemaDocment` 能被多个 `SchemaValidator` 引用。它ä¸ä¼šè¢« `SchemaValidator` 修改。
+* å¯ä»¥é‡å¤ä½¿ç”¨ä¸€ä¸ª `SchemaValidator` æ¥æ ¡éªŒå¤šä¸ªæ–‡ä»¶ã€‚在校验其他文件å‰ï¼Œé¡»å…ˆè°ƒç”¨ `validator.Reset()`。
-## 在解æžï¼ç”Ÿæˆæ—¶è¿›è¡Œæ ¡éªŒ
+# 在解æžï¼ç”Ÿæˆæ—¶è¿›è¡Œæ ¡éªŒ {#ParsingSerialization}
与大部分 JSON Schema 校验器有所ä¸åŒï¼ŒRapidJSON æ供了一个基于 SAX çš„ schema 校验器实现。因此,你å¯ä»¥åœ¨è¾“å…¥æµè§£æž JSON çš„åŒæ—¶è¿›è¡Œæ ¡éªŒã€‚若校验器é‡åˆ°ä¸€ä¸ªä¸Ž schema ä¸ç¬¦çš„值,就会立å³ç»ˆæ­¢è§£æžã€‚这设计对于解æžå¤§åž‹ JSON 文件时特别有用。
-### DOM 解æž
+## DOM è§£æž {#DomParsing}
在使用 DOM 进行解æžæ—¶ï¼Œ`Document` 除了接收 SAX 事件外,还需åšä¸€äº›å‡†å¤‡åŠç»“æŸå·¥ä½œï¼Œå› æ­¤ï¼Œä¸ºäº†è¿žæŽ¥ `Reader`ã€`SchemaValidator` å’Œ `Document` è¦åšå¤šä¸€ç‚¹äº‹æƒ…。`SchemaValidatingReader` 是一个辅助类去åšé‚£äº›å·¥ä½œã€‚
@@ -64,28 +64,28 @@ if (!d.Accept(validator)) {
#include "rapidjson/filereadstream.h"
// ...
-SchemaDocument schema(sd); // Compile a Document to SchemaDocument
+SchemaDocument schema(sd); // 把一个 Document 编译至 SchemaDocument
-// Use reader to parse the JSON
+// 使用 reader è§£æž JSON
FILE* fp = fopen("big.json", "r");
FileReadStream is(fp, buffer, sizeof(buffer));
-// Parse JSON from reader, validate the SAX events, and store in d.
+// 用 reader è§£æž JSON,校验它的 SAX 事件,并存储至 d
Document d;
SchemaValidatingReader<kParseDefaultFlags, FileReadStream, UTF8<> > reader(is, schema);
d.Populate(reader);
if (!reader.GetParseResult()) {
- // Not a valid JSON
- // When reader.GetParseResult().Code() == kParseErrorTermination,
- // it may be terminated by:
- // (1) the validator found that the JSON is invalid according to schema; or
- // (2) the input stream has I/O error.
+ // ä¸æ˜¯ä¸€ä¸ªåˆæ³•çš„ JSON
+ // 当 reader.GetParseResult().Code() == kParseErrorTermination,
+ // 它å¯èƒ½æ˜¯è¢«ä»¥ä¸‹åŽŸå› ä¸­æ­¢ï¼š
+ // (1) 校验器å‘现 JSON ä¸åˆä¹Ž schema;或
+ // (2) 输入æµæœ‰ I/O 错误。
- // Check the validation result
+ // 检查校验结果
if (!reader.IsValid()) {
- // Input JSON is invalid according to the schema
- // Output diagnostic information
+ // 输入的 JSON ä¸åˆä¹Ž schema
+ // 打å°è¯Šæ–­ä¿¡æ¯
StringBuffer sb;
reader.GetInvalidSchemaPointer().StringifyUriFragment(sb);
printf("Invalid schema: %s\n", sb.GetString());
@@ -97,7 +97,7 @@ if (!reader.GetParseResult()) {
}
~~~
-### SAX 解æž
+## SAX è§£æž {#SaxParsing}
使用 SAX 解æžæ—¶ï¼Œæƒ…况就简å•å¾—多。若åªéœ€è¦æ ¡éªŒ JSON 而无需进一步处ç†ï¼Œé‚£ä¹ˆä»…需è¦ï¼š
@@ -126,7 +126,7 @@ if (!reader.Parse(ss, validator)) {
}
~~~
-### 生æˆ
+## ç”Ÿæˆ {#Serialization}
我们也å¯ä»¥åœ¨ç”Ÿæˆï¼ˆserialization)的时候进行校验。这能确ä¿è¾“出的 JSON 符åˆä¸€ä¸ª JSON Schema。
@@ -144,20 +144,20 @@ if (!d.Accept(validator)) {
å½“ç„¶ï¼Œå¦‚æžœä½ çš„åº”ç”¨ä»…éœ€è¦ SAX 风格的生æˆï¼Œé‚£ä¹ˆåªéœ€è¦æŠŠ SAX 事件由原æ¥å‘é€åˆ° `Writer`,改为å‘é€åˆ° `SchemaValidator`。
-## 远程 Schema
+# 远程 Schema {#RemoteSchema}
-JSON Schema æ”¯æŒ [`$ref` 关键字](http://spacetelescope.github.io/understanding-json-schema/structuring.html),它是一个[JSON pointer](pointer.md) 引用至一个本地(local)或远程(remote) schema。本地指针的首字符是 `#`,而远程指针是一个相对或ç»å¯¹ URI。例如:
+JSON Schema æ”¯æŒ [`$ref` 关键字](http://spacetelescope.github.io/understanding-json-schema/structuring.html),它是一个 [JSON pointer](doc/pointer.zh-cn.md) 引用至一个本地(local)或远程(remote) schema。本地指针的首字符是 `#`,而远程指针是一个相对或ç»å¯¹ URI。例如:
~~~js
{ "$ref": "definitions.json#/address" }
~~~
-由于 `SchemaValidator` 并ä¸çŸ¥é“如何处ç†é‚£äº› URI,它需è¦ä½¿ç”¨è€…æ供一个 `IRemoteSchemaDocumentProvider` 的实例去处ç†ã€‚
+由于 `SchemaDocument` 并ä¸çŸ¥é“如何处ç†é‚£äº› URI,它需è¦ä½¿ç”¨è€…æ供一个 `IRemoteSchemaDocumentProvider` 的实例去处ç†ã€‚
~~~
class MyRemoteSchemaDocumentProvider : public IRemoteSchemaDocumentProvider {
public:
- virtual const SchemaDocument* GetRemoteDocument(const char* uri, SizeTyp length) {
+ virtual const SchemaDocument* GetRemoteDocument(const char* uri, SizeType length) {
// Resolve the uri and returns a pointer to that schema.
}
};
@@ -165,10 +165,10 @@ public:
// ...
MyRemoteSchemaDocumentProvider provider;
-SchemaValidator validator(schema, &provider);
+SchemaDocument schema(sd, &provider);
~~~
-## 标准的符åˆç¨‹åº¦
+# 标准的符åˆç¨‹åº¦ {#Conformance}
RapidJSON 通过了 [JSON Schema Test Suite](https://github.com/json-schema/JSON-Schema-Test-Suite) (Json Schema draft 4) 中 263 个测试的 262 个。
@@ -176,7 +176,7 @@ RapidJSON 通过了 [JSON Schema Test Suite](https://github.com/json-schema/JSON
除此以外,关于字符串类型的 `format` schema 关键字也会被忽略,因为标准中并没需求必须实现。
-### 正则表达å¼
+## æ­£åˆ™è¡¨è¾¾å¼ {#RegEx}
`pattern` åŠ `patternProperties` 这两个 schema 关键字使用了正则表达å¼åŽ»åŒ¹é…所需的模å¼ã€‚
@@ -184,34 +184,34 @@ RapidJSON 实现了一个简å•çš„ NFA 正则表达å¼å¼•æ“Žï¼Œå¹¶é¢„设使用ã€
|语法|æè¿°|
|------|-----------|
-|`ab` | 串è”
-|`a|b` | 交替
-|`a?` | 零或一次
-|`a*` | 零或多次
-|`a+` | 一或多次
-|`a{3}` | 刚好 3 次
-|`a{3,}` | 至少 3 次
-|`a{3,5}`| 3 至 5 次
-|`(ab)` | 分组
-|`^a` | 在开始处
-|`a$` | 在结æŸå¤„
-|`.` | 任何字符
-|`[abc]` | 字符组
-|`[a-c]` | 字符组范围
-|`[a-z0-9_]` | 字符组组åˆ
-|`[^abc]` | 字符组å–å
-|`[^a-c]` | 字符组范围å–å
-|`[\b]` | 退格符 (U+0008)
-|`\|`, `\\`, ... | 转义字符
-|`\f` | 馈页 (U+000C)
-|`\n` | 馈行 (U+000A)
-|`\r` | 回车 (U+000D)
-|`\t` | 制表 (U+0009)
-|`\v` | 垂直制表 (U+000B)
+|`ab` | ä¸²è” |
+|<code>a&#124;b</code> | 交替 |
+|`a?` | 零或一次 |
+|`a*` | 零或多次 |
+|`a+` | 一或多次 |
+|`a{3}` | 刚好 3 次 |
+|`a{3,}` | 至少 3 次 |
+|`a{3,5}`| 3 至 5 次 |
+|`(ab)` | 分组 |
+|`^a` | 在开始处 |
+|`a$` | 在结æŸå¤„ |
+|`.` | 任何字符 |
+|`[abc]` | 字符组 |
+|`[a-c]` | 字符组范围 |
+|`[a-z0-9_]` | å­—ç¬¦ç»„ç»„åˆ |
+|`[^abc]` | 字符组å–å |
+|`[^a-c]` | 字符组范围å–å |
+|`[\b]` | 退格符 (U+0008) |
+|<code>\\&#124;</code>, `\\`, ... | 转义字符 |
+|`\f` | 馈页 (U+000C) |
+|`\n` | 馈行 (U+000A) |
+|`\r` | 回车 (U+000D) |
+|`\t` | 制表 (U+0009) |
+|`\v` | 垂直制表 (U+000B) |
对于使用 C++11 编译器的使用者,也å¯ä½¿ç”¨ `std::regex`,åªéœ€å®šä¹‰ `RAPIDJSON_SCHEMA_USE_INTERNALREGEX=0` åŠ `RAPIDJSON_SCHEMA_USE_STDREGEX=1`。若你的 schema 无需使用 `pattern` 或 `patternProperties`,å¯ä»¥æŠŠä¸¤ä¸ªå®éƒ½è®¾ä¸ºé›¶ï¼Œä»¥ç¦ç”¨æ­¤åŠŸèƒ½ï¼Œè¿™æ ·åšå¯èŠ‚çœä¸€äº›ä»£ç ä½“积。
-## 性能
+# 性能 {#Performance}
大部分 C++ JSON åº“éƒ½æœªæ”¯æŒ JSON Schema。因此我们å°è¯•æŒ‰ç…§ [json-schema-benchmark](https://github.com/ebdrup/json-schema-benchmark) 去评估 RapidJSON çš„ JSON Schema 校验器。该评测测试了 11 个è¿è¡Œåœ¨ node.js 上的 JavaScript 库。
diff --git a/doc/stream.zh-cn.md b/doc/stream.zh-cn.md
index 5cc9c0d9..f2c54f79 100644
--- a/doc/stream.zh-cn.md
+++ b/doc/stream.zh-cn.md
@@ -1,16 +1,16 @@
# æµ
-在RapidJSON中,`rapidjson::Stream`是用於读写JSON的概念(概念是指C++çš„concept)。在这里我们先介ç»å¦‚何使用RapidJSONæ供的å„ç§æµã€‚然åŽå†çœ‹çœ‹å¦‚何自行定义æµã€‚
+在 RapidJSON 中,`rapidjson::Stream` 是用於读写 JSON 的概念(概念是指 C++ çš„ concept)。在这里我们先介ç»å¦‚何使用 RapidJSON æ供的å„ç§æµã€‚然åŽå†çœ‹çœ‹å¦‚何自行定义æµã€‚
[TOC]
# å†…å­˜æµ {#MemoryStreams}
-内存æµæŠŠJSON存储在内存之中。
+内存æµæŠŠ JSON 存储在内存之中。
## StringStream(输入){#StringStream}
-`StringStream`是最基本的输入æµï¼Œå®ƒè¡¨ç¤ºä¸€ä¸ªå®Œæ•´çš„ã€åªè¯»çš„ã€å­˜å‚¨äºŽå†…存的JSON。它在`rapidjson/rapidjson.h`中定义。
+`StringStream` 是最基本的输入æµï¼Œå®ƒè¡¨ç¤ºä¸€ä¸ªå®Œæ•´çš„ã€åªè¯»çš„ã€å­˜å‚¨äºŽå†…存的 JSON。它在 `rapidjson/rapidjson.h` 中定义。
~~~~~~~~~~cpp
#include "rapidjson/document.h" // ä¼šåŒ…å« "rapidjson/rapidjson.h"
@@ -25,7 +25,7 @@ Document d;
d.ParseStream(s);
~~~~~~~~~~
-由于这是éžå¸¸å¸¸ç”¨çš„用法,RapidJSONæä¾›`Document::Parse(const char*)`去åšå®Œå…¨ç›¸åŒçš„事情:
+由于这是éžå¸¸å¸¸ç”¨çš„用法,RapidJSON æä¾› `Document::Parse(const char*)` 去åšå®Œå…¨ç›¸åŒçš„事情:
~~~~~~~~~~cpp
// ...
@@ -34,11 +34,11 @@ Document d;
d.Parse(json);
~~~~~~~~~~
-需è¦æ³¨æ„,`StringStream`是`GenericStringStream<UTF8<> >`çš„typedef,使用者å¯ç”¨å…¶ä»–ç¼–ç ç±»åŽ»ä»£è¡¨æµæ‰€ä½¿ç”¨çš„字符集。
+需è¦æ³¨æ„,`StringStream` 是 `GenericStringStream<UTF8<> >` çš„ typedef,使用者å¯ç”¨å…¶ä»–ç¼–ç ç±»åŽ»ä»£è¡¨æµæ‰€ä½¿ç”¨çš„字符集。
## StringBuffer(输出){#StringBuffer}
-`StringBuffer`是一个简å•çš„输出æµã€‚它分é…一个内存缓冲区,供写入整个JSON。å¯ä½¿ç”¨`GetString()`æ¥èŽ·å–该缓冲区。
+`StringBuffer` 是一个简å•çš„输出æµã€‚它分é…一个内存缓冲区,供写入整个 JSON。å¯ä½¿ç”¨ `GetString()` æ¥èŽ·å–该缓冲区。
~~~~~~~~~~cpp
#include "rapidjson/stringbuffer.h"
@@ -50,26 +50,26 @@ d.Accept(writer);
const char* output = buffer.GetString();
~~~~~~~~~~
-当缓冲区满溢,它将自动增加容é‡ã€‚缺çœå®¹é‡æ˜¯256个字符(UTF8是256字节,UTF16是512字节等)。使用者能自行æ供分é…器åŠåˆå§‹å®¹é‡ã€‚
+当缓冲区满溢,它将自动增加容é‡ã€‚缺çœå®¹é‡æ˜¯ 256 个字符(UTF8 是 256 字节,UTF16 是 512 字节等)。使用者能自行æ供分é…器åŠåˆå§‹å®¹é‡ã€‚
~~~~~~~~~~cpp
StringBuffer buffer1(0, 1024); // 使用它的分é…器,åˆå§‹å¤§å° = 1024
StringBuffer buffer2(allocator, 1024);
~~~~~~~~~~
-如无设置分é…器,`StringBuffer`会自行实例化一个内部分é…器。
+如无设置分é…器,`StringBuffer` 会自行实例化一个内部分é…器。
-相似地,`StringBuffer`是`GenericStringBuffer<UTF8<> >`的typedef。
+相似地,`StringBuffer` 是 `GenericStringBuffer<UTF8<> >` 的 typedef。
# æ–‡ä»¶æµ {#FileStreams}
-当è¦ä»Žæ–‡ä»¶è§£æžä¸€ä¸ªJSON,你å¯ä»¥æŠŠæ•´ä¸ªJSON读入内存并使用上述的`StringStream`。
+当è¦ä»Žæ–‡ä»¶è§£æžä¸€ä¸ª JSON,你å¯ä»¥æŠŠæ•´ä¸ª JSON 读入内存并使用上述的 `StringStream`。
-然而,若JSON很大,或是内存有é™ï¼Œä½ å¯ä»¥æ”¹ç”¨`FileReadStream`。它åªä¼šä»Žæ–‡ä»¶è¯»å–一部分至缓冲区,然åŽè®©é‚£éƒ¨åˆ†è¢«è§£æžã€‚若缓冲区的字符都被读完,它会å†ä»Žæ–‡ä»¶è¯»å–下一部分。
+然而,若 JSON 很大,或是内存有é™ï¼Œä½ å¯ä»¥æ”¹ç”¨ `FileReadStream`。它åªä¼šä»Žæ–‡ä»¶è¯»å–一部分至缓冲区,然åŽè®©é‚£éƒ¨åˆ†è¢«è§£æžã€‚若缓冲区的字符都被读完,它会å†ä»Žæ–‡ä»¶è¯»å–下一部分。
## FileReadStream(输入) {#FileReadStream}
-`FileReadStream`通过`FILE`指针读å–文件。使用者需è¦æ供一个缓冲区。
+`FileReadStream` 通过 `FILE` 指针读å–文件。使用者需è¦æ供一个缓冲区。
~~~~~~~~~~cpp
#include "rapidjson/filereadstream.h"
@@ -77,7 +77,7 @@ StringBuffer buffer2(allocator, 1024);
using namespace rapidjson;
-FILE* fp = fopen("big.json", "rb"); // éžWindowså¹³å°ä½¿ç”¨"r"
+FILE* fp = fopen("big.json", "rb"); // éž Windows å¹³å°ä½¿ç”¨ "r"
char readBuffer[65536];
FileReadStream is(fp, readBuffer, sizeof(readBuffer));
@@ -88,13 +88,13 @@ d.ParseStream(is);
fclose(fp);
~~~~~~~~~~
-与`StringStreams`ä¸ä¸€æ ·ï¼Œ`FileReadStream`是一个字节æµã€‚它ä¸å¤„ç†ç¼–ç ã€‚若文件并éžUTF-8ç¼–ç ï¼Œå¯ä»¥æŠŠå­—节æµç”¨`EncodedInputStream`包装。我们很快会讨论这个问题。
+与 `StringStreams` ä¸ä¸€æ ·ï¼Œ`FileReadStream` 是一个字节æµã€‚它ä¸å¤„ç†ç¼–ç ã€‚è‹¥æ–‡ä»¶å¹¶éž UTF-8 ç¼–ç ï¼Œå¯ä»¥æŠŠå­—节æµç”¨ `EncodedInputStream` 包装。我们很快会讨论这个问题。
-除了读å–文件,使用者也å¯ä»¥ä½¿ç”¨`FileReadStream`æ¥è¯»å–`stdin`。
+除了读å–文件,使用者也å¯ä»¥ä½¿ç”¨ `FileReadStream` æ¥è¯»å– `stdin`。
## FileWriteStream(输出){#FileWriteStream}
-`FileWriteStream`是一个å«ç¼“冲功能的输出æµã€‚它的用法与`FileReadStream`éžå¸¸ç›¸ä¼¼ã€‚
+`FileWriteStream` 是一个å«ç¼“冲功能的输出æµã€‚它的用法与 `FileReadStream` éžå¸¸ç›¸ä¼¼ã€‚
~~~~~~~~~~cpp
#include "rapidjson/filewritestream.h"
@@ -106,7 +106,7 @@ Document d;
d.Parse(json);
// ...
-FILE* fp = fopen("output.json", "wb"); // éžWindowså¹³å°ä½¿ç”¨"w"
+FILE* fp = fopen("output.json", "wb"); // éž Windows å¹³å°ä½¿ç”¨ "w"
char writeBuffer[65536];
FileWriteStream os(fp, writeBuffer, sizeof(writeBuffer));
@@ -117,11 +117,11 @@ d.Accept(writer);
fclose(fp);
~~~~~~~~~~
-它也å¯ä»¥æŠŠè¾“出导å‘`stdout`。
+它也å¯ä»¥æŠŠè¾“å‡ºå¯¼å‘ `stdout`。
# iostream 包装类 {#iostreamWrapper}
-基于用户的è¦æ±‚,RapidJSONæ供了正å¼çš„ `std::basic_istream` å’Œ `std::basic_ostream` 包装类。然而,请注æ„其性能会大大低于以上的其他æµã€‚
+基于用户的è¦æ±‚,RapidJSON æ供了正å¼çš„ `std::basic_istream` å’Œ `std::basic_ostream` 包装类。然而,请注æ„其性能会大大低于以上的其他æµã€‚
## IStreamWrapper {#IStreamWrapper}
@@ -173,19 +173,19 @@ d.Accept(writer);
# ç¼–ç æµ {#EncodedStreams}
-ç¼–ç æµï¼ˆencoded streams)本身ä¸å­˜å‚¨JSON,它们是通过包装字节æµæ¥æ供基本的编ç ï¼è§£ç åŠŸèƒ½ã€‚
+ç¼–ç æµï¼ˆencoded streams)本身ä¸å­˜å‚¨ JSON,它们是通过包装字节æµæ¥æ供基本的编ç ï¼è§£ç åŠŸèƒ½ã€‚
-如上所述,我们å¯ä»¥ç›´æŽ¥è¯»å…¥UTF-8字节æµã€‚然而,UTF-16åŠUTF-32有字节åºï¼ˆendian)问题。è¦æ­£ç¡®åœ°å¤„ç†å­—节åºï¼Œéœ€è¦åœ¨è¯»å–时把字节转æ¢æˆå­—符(如对UTF-16使用`wchar_t`),以åŠåœ¨å†™å…¥æ—¶æŠŠå­—符转æ¢ä¸ºå­—节。
+如上所述,我们å¯ä»¥ç›´æŽ¥è¯»å…¥ UTF-8 字节æµã€‚然而,UTF-16 åŠ UTF-32 有字节åºï¼ˆendian)问题。è¦æ­£ç¡®åœ°å¤„ç†å­—节åºï¼Œéœ€è¦åœ¨è¯»å–时把字节转æ¢æˆå­—符(如对 UTF-16 使用 `wchar_t`),以åŠåœ¨å†™å…¥æ—¶æŠŠå­—符转æ¢ä¸ºå­—节。
-除此以外,我们也需è¦å¤„ç†[字节顺åºæ ‡è®°ï¼ˆbyte order mark, BOM)](http://en.wikipedia.org/wiki/Byte_order_mark)。当从一个字节æµè¯»å–时,需è¦æ£€æµ‹BOM,或者仅仅是把存在的BOM消去。当把JSON写入字节æµæ—¶ï¼Œä¹Ÿå¯é€‰æ‹©å†™å…¥BOM。
+除此以外,我们也需è¦å¤„ç† [字节顺åºæ ‡è®°ï¼ˆbyte order mark, BOM)](http://en.wikipedia.org/wiki/Byte_order_mark)。当从一个字节æµè¯»å–时,需è¦æ£€æµ‹ BOM,或者仅仅是把存在的 BOM 消去。当把 JSON 写入字节æµæ—¶ï¼Œä¹Ÿå¯é€‰æ‹©å†™å…¥ BOM。
-若一个æµçš„ç¼–ç åœ¨ç¼–译期已知,你å¯ä½¿ç”¨`EncodedInputStream`åŠ`EncodedOutputStream`。若一个æµå¯èƒ½å­˜å‚¨UTF-8ã€UTF-16LEã€UTF-16BEã€UTF-32LEã€UTF-32BEçš„JSON,并且编ç åªèƒ½åœ¨è¿è¡Œæ—¶å¾—知,你便å¯ä»¥ä½¿ç”¨`AutoUTFInputStream`åŠ`AutoUTFOutputStream`。这些æµå®šä¹‰åœ¨`rapidjson/encodedstream.h`。
+若一个æµçš„ç¼–ç åœ¨ç¼–译期已知,你å¯ä½¿ç”¨ `EncodedInputStream` åŠ `EncodedOutputStream`。若一个æµå¯èƒ½å­˜å‚¨ UTF-8ã€UTF-16LEã€UTF-16BEã€UTF-32LEã€UTF-32BE çš„ JSON,并且编ç åªèƒ½åœ¨è¿è¡Œæ—¶å¾—知,你便å¯ä»¥ä½¿ç”¨ `AutoUTFInputStream` åŠ `AutoUTFOutputStream`。这些æµå®šä¹‰åœ¨ `rapidjson/encodedstream.h`。
注æ„到,这些编ç æµå¯ä»¥æ–½äºŽæ–‡ä»¶ä»¥å¤–çš„æµã€‚例如,你å¯ä»¥ç”¨ç¼–ç æµåŒ…装内存中的文件或自定义的字节æµã€‚
## EncodedInputStream {#EncodedInputStream}
-`EncodedInputStream`å«ä¸¤ä¸ªæ¨¡æ¿å‚数。第一个是`Encoding`类型,例如定义于`rapidjson/encodings.h`çš„`UTF8`ã€`UTF16LE`。第二个å‚数是被包装的æµçš„类型。
+`EncodedInputStream` å«ä¸¤ä¸ªæ¨¡æ¿å‚数。第一个是 `Encoding` 类型,例如定义于 `rapidjson/encodings.h` çš„ `UTF8`ã€`UTF16LE`。第二个å‚数是被包装的æµçš„类型。
~~~~~~~~~~cpp
#include "rapidjson/document.h"
@@ -195,50 +195,50 @@ d.Accept(writer);
using namespace rapidjson;
-FILE* fp = fopen("utf16le.json", "rb"); // éžWindowså¹³å°ä½¿ç”¨"r"
+FILE* fp = fopen("utf16le.json", "rb"); // éž Windows å¹³å°ä½¿ç”¨ "r"
char readBuffer[256];
FileReadStream bis(fp, readBuffer, sizeof(readBuffer));
-EncodedInputStream<UTF16LE<>, FileReadStream> eis(bis); // 用eis包装bis
+EncodedInputStream<UTF16LE<>, FileReadStream> eis(bis); // 用 eis 包装 bis
-Document d; // Document为GenericDocument<UTF8<> >
-d.ParseStream<0, UTF16LE<> >(eis); // 把UTF-16LE文件解æžè‡³å†…存中的UTF-8
+Document d; // Document 为 GenericDocument<UTF8<> >
+d.ParseStream<0, UTF16LE<> >(eis); // 把 UTF-16LE 文件解æžè‡³å†…存中的 UTF-8
fclose(fp);
~~~~~~~~~~
## EncodedOutputStream {#EncodedOutputStream}
-`EncodedOutputStream`也是相似的,但它的构造函数有一个`bool putBOM`å‚数,用于控制是å¦åœ¨è¾“出字节æµå†™å…¥BOM。
+`EncodedOutputStream` 也是相似的,但它的构造函数有一个 `bool putBOM` å‚数,用于控制是å¦åœ¨è¾“出字节æµå†™å…¥ BOM。
~~~~~~~~~~cpp
#include "rapidjson/filewritestream.h" // FileWriteStream
#include "rapidjson/encodedstream.h" // EncodedOutputStream
#include <cstdio>
-Document d; // Document为GenericDocument<UTF8<> >
+Document d; // Document 为 GenericDocument<UTF8<> >
// ...
-FILE* fp = fopen("output_utf32le.json", "wb"); // éžWindowså¹³å°ä½¿ç”¨"w"
+FILE* fp = fopen("output_utf32le.json", "wb"); // éž Windows å¹³å°ä½¿ç”¨ "w"
char writeBuffer[256];
FileWriteStream bos(fp, writeBuffer, sizeof(writeBuffer));
typedef EncodedOutputStream<UTF32LE<>, FileWriteStream> OutputStream;
-OutputStream eos(bos, true); // 写入BOM
+OutputStream eos(bos, true); // 写入 BOM
Writer<OutputStream, UTF32LE<>, UTF8<>> writer(eos);
-d.Accept(writer); // 这里从内存的UTF-8生æˆUTF32-LE文件
+d.Accept(writer); // 这里从内存的 UTF-8 ç”Ÿæˆ UTF32-LE 文件
fclose(fp);
~~~~~~~~~~
## AutoUTFInputStream {#AutoUTFInputStream}
-有时候,应用软件å¯èƒ½éœ€è¦ã²ƒç†æ‰€æœ‰å¯æ”¯æŒçš„JSONç¼–ç ã€‚`AutoUTFInputStream`会先使用BOMæ¥æ£€æµ‹ç¼–ç ã€‚è‹¥BOMä¸å­˜åœ¨ï¼Œå®ƒä¾¿ä¼šä½¿ç”¨åˆæ³•JSON的特性æ¥æ£€æµ‹ã€‚若两ç§æ–¹æ³•éƒ½å¤±è´¥ï¼Œå®ƒå°±ä¼šå€’退至构造函数æ供的UTF类型。
+有时候,应用软件å¯èƒ½éœ€è¦ã²ƒç†æ‰€æœ‰å¯æ”¯æŒçš„ JSON ç¼–ç ã€‚`AutoUTFInputStream` 会先使用 BOM æ¥æ£€æµ‹ç¼–ç ã€‚è‹¥ BOM ä¸å­˜åœ¨ï¼Œå®ƒä¾¿ä¼šä½¿ç”¨åˆæ³• JSON 的特性æ¥æ£€æµ‹ã€‚若两ç§æ–¹æ³•éƒ½å¤±è´¥ï¼Œå®ƒå°±ä¼šå€’退至构造函数æ供的 UTF 类型。
-由于字符(编ç å•å…ƒï¼code unit)å¯èƒ½æ˜¯8ä½ã€16ä½æˆ–32ä½ï¼Œ`AutoUTFInputStream` 需è¦ä¸€ä¸ªèƒ½è‡³å°‘储存32ä½çš„字符类型。我们å¯ä»¥ä½¿ç”¨`unsigned`作为模æ¿å‚数:
+由于字符(编ç å•å…ƒï¼code unit)å¯èƒ½æ˜¯ 8 ä½ã€16 ä½æˆ– 32 ä½ï¼Œ`AutoUTFInputStream` 需è¦ä¸€ä¸ªèƒ½è‡³å°‘储存 32 ä½çš„字符类型。我们å¯ä»¥ä½¿ç”¨ `unsigned` 作为模æ¿å‚数:
~~~~~~~~~~cpp
#include "rapidjson/document.h"
@@ -248,26 +248,26 @@ fclose(fp);
using namespace rapidjson;
-FILE* fp = fopen("any.json", "rb"); // éžWindowså¹³å°ä½¿ç”¨"r"
+FILE* fp = fopen("any.json", "rb"); // éž Windows å¹³å°ä½¿ç”¨ "r"
char readBuffer[256];
FileReadStream bis(fp, readBuffer, sizeof(readBuffer));
-AutoUTFInputStream<unsigned, FileReadStream> eis(bis); // 用eis包装bis
+AutoUTFInputStream<unsigned, FileReadStream> eis(bis); // 用 eis 包装 bis
-Document d; // Document为GenericDocument<UTF8<> >
-d.ParseStream<0, AutoUTF<unsigned> >(eis); // 把任何UTFç¼–ç çš„文件解æžè‡³å†…存中的UTF-8
+Document d; // Document 为 GenericDocument<UTF8<> >
+d.ParseStream<0, AutoUTF<unsigned> >(eis); // 把任何 UTF ç¼–ç çš„文件解æžè‡³å†…存中的 UTF-8
fclose(fp);
~~~~~~~~~~
-当è¦æŒ‡å®šæµçš„ç¼–ç ï¼Œå¯ä½¿ç”¨ä¸Šé¢ä¾‹å­ä¸­`ParseStream()`çš„å‚æ•°`AutoUTF<CharType>`。
+当è¦æŒ‡å®šæµçš„ç¼–ç ï¼Œå¯ä½¿ç”¨ä¸Šé¢ä¾‹å­ä¸­ `ParseStream()` çš„å‚æ•° `AutoUTF<CharType>`。
-ä½ å¯ä»¥ä½¿ç”¨`UTFType GetType()`去获å–UTF类型,并且用`HasBOM()`检测输入æµæ˜¯å¦å«æœ‰BOM。
+ä½ å¯ä»¥ä½¿ç”¨ `UTFType GetType()` åŽ»èŽ·å– UTF 类型,并且用 `HasBOM()` 检测输入æµæ˜¯å¦å«æœ‰ BOM。
## AutoUTFOutputStream {#AutoUTFOutputStream}
-相似地,è¦åœ¨è¿è¡Œæ—¶é€‰æ‹©è¾“出的编ç ï¼Œæˆ‘们å¯ä½¿ç”¨`AutoUTFOutputStream`。这个类本身并éžã€Œè‡ªåŠ¨ã€ã€‚你需è¦åœ¨è¿è¡Œæ—¶æŒ‡å®šUTF类型,以åŠæ˜¯å¦å†™å…¥BOM。
+相似地,è¦åœ¨è¿è¡Œæ—¶é€‰æ‹©è¾“出的编ç ï¼Œæˆ‘们å¯ä½¿ç”¨ `AutoUTFOutputStream`。这个类本身并éžã€Œè‡ªåŠ¨ã€ã€‚你需è¦åœ¨è¿è¡Œæ—¶æŒ‡å®š UTF 类型,以åŠæ˜¯å¦å†™å…¥ BOM。
~~~~~~~~~~cpp
using namespace rapidjson;
@@ -284,13 +284,13 @@ void WriteJSONFile(FILE* fp, UTFType type, bool putBOM, const Document& d) {
}
~~~~~~~~~~
-`AutoUTFInputStream`ï¼`AutoUTFOutputStream`是比`EncodedInputStream`ï¼`EncodedOutputStream`方便。但å‰è€…会产生一点è¿è¡ŒæœŸé¢å¤–开销。
+`AutoUTFInputStream`ï¼`AutoUTFOutputStream` 是比 `EncodedInputStream`ï¼`EncodedOutputStream` 方便。但å‰è€…会产生一点è¿è¡ŒæœŸé¢å¤–开销。
# è‡ªå®šä¹‰æµ {#CustomStream}
-除了内存ï¼æ–‡ä»¶æµï¼Œä½¿ç”¨è€…å¯åˆ›å»ºè‡ªè¡Œå®šä¹‰é€‚é…RapidJSON APIçš„æµç±»ã€‚例如,你å¯ä»¥åˆ›å»ºç½‘络æµã€ä»ŽåŽ‹ç¼©æ–‡ä»¶è¯»å–çš„æµç­‰ç­‰ã€‚
+除了内存ï¼æ–‡ä»¶æµï¼Œä½¿ç”¨è€…å¯åˆ›å»ºè‡ªè¡Œå®šä¹‰é€‚é… RapidJSON API çš„æµç±»ã€‚例如,你å¯ä»¥åˆ›å»ºç½‘络æµã€ä»ŽåŽ‹ç¼©æ–‡ä»¶è¯»å–çš„æµç­‰ç­‰ã€‚
-RapidJSON利用模æ¿ç»“åˆä¸åŒçš„类型。åªè¦ä¸€ä¸ªç±»åŒ…å«æ‰€æœ‰æ‰€éœ€çš„接å£ï¼Œå°±å¯ä»¥ä½œä¸ºä¸€ä¸ªæµã€‚æµçš„接åˆå®šä¹‰åœ¨`rapidjson/rapidjson.h`的注释里:
+RapidJSON 利用模æ¿ç»“åˆä¸åŒçš„类型。åªè¦ä¸€ä¸ªç±»åŒ…å«æ‰€æœ‰æ‰€éœ€çš„接å£ï¼Œå°±å¯ä»¥ä½œä¸ºä¸€ä¸ªæµã€‚æµçš„接åˆå®šä¹‰åœ¨ `rapidjson/rapidjson.h` 的注释里:
~~~~~~~~~~cpp
concept Stream {
@@ -317,19 +317,19 @@ concept Stream {
void Flush();
//! 完æˆå†™ä½œæ“作。
- //! \param begin PutBegin()返回的开始写入指针。
+ //! \param begin PutBegin() 返回的开始写入指针。
//! \return 已写入的字符数é‡ã€‚
size_t PutEnd(Ch* begin);
}
~~~~~~~~~~
-输入æµå¿…须实现`Peek()`ã€`Take()`åŠ`Tell()`。
-输出æµå¿…须实现`Put()`åŠ`Flush()`。
-`PutBegin()`åŠ`PutEnd()`是特殊的接å£ï¼Œä»…用于原ä½ï¼ˆ*in situ*)解æžã€‚一般的æµä¸éœ€å®žçŽ°å®ƒä»¬ã€‚然而,å³ä½¿æŽ¥å£ä¸éœ€ç”¨äºŽæŸäº›æµï¼Œä»ç„¶éœ€è¦æ供空实现,å¦åˆ™ä¼šäº§ç”Ÿç¼–译错误。
+输入æµå¿…须实现 `Peek()`ã€`Take()` åŠ `Tell()`。
+输出æµå¿…须实现 `Put()` åŠ `Flush()`。
+`PutBegin()` åŠ `PutEnd()` 是特殊的接å£ï¼Œä»…用于原ä½ï¼ˆ*in situ*)解æžã€‚一般的æµä¸éœ€å®žçŽ°å®ƒä»¬ã€‚然而,å³ä½¿æŽ¥å£ä¸éœ€ç”¨äºŽæŸäº›æµï¼Œä»ç„¶éœ€è¦æ供空实现,å¦åˆ™ä¼šäº§ç”Ÿç¼–译错误。
-## 例å­ï¼šistream的包装类 {#ExampleIStreamWrapper}
+## 例å­ï¼šistream 的包装类 {#ExampleIStreamWrapper}
-以下的简å•ä¾‹å­æ˜¯`std::istream`的包装类,它åªéœ€çŽ°3个函数。
+以下的简å•ä¾‹å­æ˜¯ `std::istream` 的包装类,它åªéœ€çŽ° 3 个函数。
~~~~~~~~~~cpp
class MyIStreamWrapper {
@@ -364,7 +364,7 @@ private:
};
~~~~~~~~~~
-使用者能用它æ¥åŒ…装`std::stringstream`ã€`std::ifstream`的实例。
+使用者能用它æ¥åŒ…装 `std::stringstream`ã€`std::ifstream` 的实例。
~~~~~~~~~~cpp
const char* json = "[1,2,3,4]";
@@ -375,11 +375,11 @@ Document d;
d.ParseStream(is);
~~~~~~~~~~
-但è¦æ³¨æ„,由于标准库的内部开销问,此实现的性能å¯èƒ½ä¸å¦‚RapidJSON的内存ï¼æ–‡ä»¶æµã€‚
+但è¦æ³¨æ„,由于标准库的内部开销问,此实现的性能å¯èƒ½ä¸å¦‚ RapidJSON 的内存ï¼æ–‡ä»¶æµã€‚
-## 例å­ï¼šostream的包装类 {#ExampleOStreamWrapper}
+## 例å­ï¼šostream 的包装类 {#ExampleOStreamWrapper}
-以下的例å­æ˜¯`std::istream`的包装类,它åªéœ€å®žçŽ°2个函数。
+以下的例å­æ˜¯ `std::istream` 的包装类,它åªéœ€å®žçŽ° 2 个函数。
~~~~~~~~~~cpp
class MyOStreamWrapper {
@@ -406,7 +406,7 @@ private:
};
~~~~~~~~~~
-使用者能用它æ¥åŒ…装`std::stringstream`ã€`std::ofstream`的实例。
+使用者能用它æ¥åŒ…装 `std::stringstream`ã€`std::ofstream` 的实例。
~~~~~~~~~~cpp
Document d;
@@ -419,8 +419,8 @@ Writer<MyOStreamWrapper> writer(os);
d.Accept(writer);
~~~~~~~~~~
-但è¦æ³¨æ„,由于标准库的内部开销问,此实现的性能å¯èƒ½ä¸å¦‚RapidJSON的内存ï¼æ–‡ä»¶æµã€‚
+但è¦æ³¨æ„,由于标准库的内部开销问,此实现的性能å¯èƒ½ä¸å¦‚ RapidJSON 的内存ï¼æ–‡ä»¶æµã€‚
# 总结 {#Summary}
-本节æ述了RapidJSONæ供的å„ç§æµçš„类。内存æµå¾ˆç®€å•ã€‚è‹¥JSON存储在文件中,文件æµå¯å‡å°‘JSON解æžåŠç”Ÿæˆæ‰€éœ€çš„内存é‡ã€‚ç¼–ç æµåœ¨å­—节æµå’Œå­—符æµä¹‹é—´ä½œè½¬æ¢ã€‚最åŽï¼Œä½¿ç”¨è€…å¯ä½¿ç”¨ä¸€ä¸ªç®€å•æŽ¥å£åˆ›å»ºè‡ªå®šä¹‰çš„æµã€‚
+本节æ述了 RapidJSON æ供的å„ç§æµçš„类。内存æµå¾ˆç®€å•ã€‚è‹¥ JSON 存储在文件中,文件æµå¯å‡å°‘ JSON 解æžåŠç”Ÿæˆæ‰€éœ€çš„内存é‡ã€‚ç¼–ç æµåœ¨å­—节æµå’Œå­—符æµä¹‹é—´ä½œè½¬æ¢ã€‚最åŽï¼Œä½¿ç”¨è€…å¯ä½¿ç”¨ä¸€ä¸ªç®€å•æŽ¥å£åˆ›å»ºè‡ªå®šä¹‰çš„æµã€‚
diff --git a/doc/tutorial.md b/doc/tutorial.md
index 12110234..3fa63c93 100644
--- a/doc/tutorial.md
+++ b/doc/tutorial.md
@@ -2,7 +2,7 @@
This tutorial introduces the basics of the Document Object Model(DOM) API.
-As shown in [Usage at a glance](@ref index), a JSON can be parsed into DOM, and then the DOM can be queried and modified easily, and finally be converted back to JSON.
+As shown in [Usage at a glance](@ref index), JSON can be parsed into a DOM, and then the DOM can be queried and modified easily, and finally be converted back to JSON.
[TOC]
@@ -14,7 +14,7 @@ Each JSON value is stored in a type called `Value`. A `Document`, representing t
In this section, we will use excerpt of `example/tutorial/tutorial.cpp`.
-Assumes we have a JSON stored in a C string (`const char* json`):
+Assume we have the following JSON stored in a C string (`const char* json`):
~~~~~~~~~~js
{
"hello": "world",
@@ -55,7 +55,7 @@ printf("hello = %s\n", document["hello"].GetString());
~~~~~~~~~~
~~~~~~~~~~
-world
+hello = world
~~~~~~~~~~
JSON true/false values are represented as `bool`.
@@ -65,16 +65,16 @@ printf("t = %s\n", document["t"].GetBool() ? "true" : "false");
~~~~~~~~~~
~~~~~~~~~~
-true
+t = true
~~~~~~~~~~
-JSON null can be queryed by `IsNull()`.
+JSON null can be queried with `IsNull()`.
~~~~~~~~~~cpp
printf("n = %s\n", document["n"].IsNull() ? "null" : "?");
~~~~~~~~~~
~~~~~~~~~~
-null
+n = null
~~~~~~~~~~
JSON number type represents all numeric values. However, C++ needs more specific type for manipulation.
@@ -115,15 +115,15 @@ a[3] = 4
Note that, RapidJSON does not automatically convert values between JSON types. If a value is a string, it is invalid to call `GetInt()`, for example. In debug mode it will fail an assertion. In release mode, the behavior is undefined.
-In the following, details about querying individual types are discussed.
+In the following sections we discuss details about querying individual types.
## Query Array {#QueryArray}
-By default, `SizeType` is typedef of `unsigned`. In most systems, array is limited to store up to 2^32-1 elements.
+By default, `SizeType` is typedef of `unsigned`. In most systems, an array is limited to store up to 2^32-1 elements.
-You may access the elements in array by integer literal, for example, `a[0]`, `a[1]`, `a[2]`.
+You may access the elements in an array by integer literal, for example, `a[0]`, `a[1]`, `a[2]`.
-Array is similar to `std::vector`, instead of using indices, you may also use iterator to access all the elements.
+Array is similar to `std::vector`: instead of using indices, you may also use iterator to access all the elements.
~~~~~~~~~~cpp
for (Value::ConstValueIterator itr = a.Begin(); itr != a.End(); ++itr)
printf("%d ", itr->GetInt());
@@ -133,9 +133,18 @@ And other familiar query functions:
* `SizeType Capacity() const`
* `bool Empty() const`
+### Range-based For Loop (New in v1.1.0)
+
+When C++11 is enabled, you can use range-based for loop to access all elements in an array.
+
+~~~~~~~~~~cpp
+for (auto& v : a.GetArray())
+ printf("%d ", v.GetInt());
+~~~~~~~~~~
+
## Query Object {#QueryObject}
-Similar to array, we can access all object members by iterator:
+Similar to Array, we can access all object members by iterator:
~~~~~~~~~~cpp
static const char* kTypeNames[] =
@@ -166,16 +175,26 @@ If we are unsure whether a member exists, we need to call `HasMember()` before c
~~~~~~~~~~cpp
Value::ConstMemberIterator itr = document.FindMember("hello");
if (itr != document.MemberEnd())
- printf("%s %s\n", itr->value.GetString());
+ printf("%s\n", itr->value.GetString());
+~~~~~~~~~~
+
+### Range-based For Loop (New in v1.1.0)
+
+When C++11 is enabled, you can use range-based for loop to access all members in an object.
+
+~~~~~~~~~~cpp
+for (auto& m : document.GetObject())
+ printf("Type of member %s is %s\n",
+ m.name.GetString(), kTypeNames[m.value.GetType()]);
~~~~~~~~~~
## Querying Number {#QueryNumber}
-JSON provide a single numerical type called Number. Number can be integer or real numbers. RFC 4627 says the range of Number is specified by parser.
+JSON provides a single numerical type called Number. Number can be an integer or a real number. RFC 4627 says the range of Number is specified by the parser implementation.
-As C++ provides several integer and floating point number types, the DOM tries to handle these with widest possible range and good performance.
+As C++ provides several integer and floating point number types, the DOM tries to handle these with the widest possible range and good performance.
-When a Number is parsed, it is stored in the DOM as either one of the following type:
+When a Number is parsed, it is stored in the DOM as one of the following types:
Type | Description
-----------|---------------------------------------
@@ -185,7 +204,7 @@ Type | Description
`int64_t` | 64-bit signed integer
`double` | 64-bit double precision floating point
-When querying a number, you can check whether the number can be obtained as target type:
+When querying a number, you can check whether the number can be obtained as the target type:
Checking | Obtaining
------------------|---------------------
@@ -196,9 +215,9 @@ Checking | Obtaining
`bool IsInt64()` | `int64_t GetInt64()`
`bool IsDouble()` | `double GetDouble()`
-Note that, an integer value may be obtained in various ways without conversion. For example, A value `x` containing 123 will make `x.IsInt() == x.IsUint() == x.IsInt64() == x.IsUint64() == true`. But a value `y` containing -3000000000 will only makes `x.IsInt64() == true`.
+Note that, an integer value may be obtained in various ways without conversion. For example, A value `x` containing 123 will make `x.IsInt() == x.IsUint() == x.IsInt64() == x.IsUint64() == true`. But a value `y` containing -3000000000 will only make `x.IsInt64() == true`.
-When obtaining the numeric values, `GetDouble()` will convert internal integer representation to a `double`. Note that, `int` and `unsigned` can be safely convert to `double`, but `int64_t` and `uint64_t` may lose precision (since mantissa of `double` is only 52-bits).
+When obtaining the numeric values, `GetDouble()` will convert internal integer representation to a `double`. Note that, `int` and `unsigned` can be safely converted to `double`, but `int64_t` and `uint64_t` may lose precision (since mantissa of `double` is only 52-bits).
## Query String {#QueryString}
@@ -206,7 +225,7 @@ In addition to `GetString()`, the `Value` class also contains `GetStringLength()
According to RFC 4627, JSON strings can contain Unicode character `U+0000`, which must be escaped as `"\u0000"`. The problem is that, C/C++ often uses null-terminated string, which treats ``\0'` as the terminator symbol.
-To conform RFC 4627, RapidJSON supports string containing `U+0000`. If you need to handle this, you can use `GetStringLength()` API to obtain the correct length of string.
+To conform RFC 4627, RapidJSON supports string containing `U+0000`. If you need to handle this, you can use `GetStringLength()` to obtain the correct string length.
For example, after parsing a the following JSON to `Document d`:
@@ -341,14 +360,14 @@ a.PushBack(Value(42).Move(), allocator); // same as above
~~~~~~~~~~
## Create String {#CreateString}
-RapidJSON provide two strategies for storing string.
+RapidJSON provides two strategies for storing string.
1. copy-string: allocates a buffer, and then copy the source data into it.
2. const-string: simply store a pointer of string.
-Copy-string is always safe because it owns a copy of the data. Const-string can be used for storing string literal, and in-situ parsing which we will mentioned in Document section.
+Copy-string is always safe because it owns a copy of the data. Const-string can be used for storing a string literal, and for in-situ parsing which will be mentioned in the DOM section.
-To make memory allocation customizable, RapidJSON requires user to pass an instance of allocator, whenever an operation may require allocation. This design is needed to prevent storing a allocator (or Document) pointer per Value.
+To make memory allocation customizable, RapidJSON requires users to pass an instance of allocator, whenever an operation may require allocation. This design is needed to prevent storing a allocator (or Document) pointer per Value.
Therefore, when we assign a copy-string, we call this overloaded `SetString()` with allocator:
@@ -366,7 +385,7 @@ In this example, we get the allocator from a `Document` instance. This is a comm
Besides, the above `SetString()` requires length. This can handle null characters within a string. There is another `SetString()` overloaded function without the length parameter. And it assumes the input is null-terminated and calls a `strlen()`-like function to obtain the length.
-Finally, for string literal or string with safe life-cycle can use const-string version of `SetString()`, which lacks allocator parameter. For string literals (or constant character arrays), simply passing the literal as parameter is safe and efficient:
+Finally, for a string literal or string with a safe life-cycle one can use the const-string version of `SetString()`, which lacks an allocator parameter. For string literals (or constant character arrays), simply passing the literal as parameter is safe and efficient:
~~~~~~~~~~cpp
Value s;
@@ -374,7 +393,7 @@ s.SetString("rapidjson"); // can contain null character, length derived at co
s = "rapidjson"; // shortcut, same as above
~~~~~~~~~~
-For character pointer, the RapidJSON requires to mark it as safe before using it without copying. This can be achieved by using the `StringRef` function:
+For a character pointer, RapidJSON requires it to be marked as safe before using it without copying. This can be achieved by using the `StringRef` function:
~~~~~~~~~cpp
const char * cstr = getenv("USER");
@@ -389,7 +408,7 @@ s = StringRef(cstr,cstr_len); // shortcut, same as above
~~~~~~~~~
## Modify Array {#ModifyArray}
-Value with array type provides similar APIs as `std::vector`.
+Value with array type provides an API similar to `std::vector`.
* `Clear()`
* `Reserve(SizeType, Allocator&)`
@@ -399,7 +418,7 @@ Value with array type provides similar APIs as `std::vector`.
* `ValueIterator Erase(ConstValueIterator pos)`
* `ValueIterator Erase(ConstValueIterator first, ConstValueIterator last)`
-Note that, `Reserve(...)` and `PushBack(...)` may allocate memory for the array elements, therefore require an allocator.
+Note that, `Reserve(...)` and `PushBack(...)` may allocate memory for the array elements, therefore requiring an allocator.
Here is an example of `PushBack()`:
@@ -414,7 +433,7 @@ for (int i = 5; i <= 10; i++)
a.PushBack("Lua", allocator).PushBack("Mio", allocator);
~~~~~~~~~~
-Differs from STL, `PushBack()`/`PopBack()` returns the array reference itself. This is called _fluent interface_.
+This API differs from STL in that `PushBack()`/`PopBack()` return the array reference itself. This is called _fluent interface_.
If you want to add a non-constant string or a string without sufficient lifetime (see [Create String](#CreateString)) to the array, you need to create a string Value by using the copy-string API. To avoid the need for an intermediate variable, you can use a [temporary value](#TemporaryValues) in place:
@@ -429,7 +448,7 @@ contact.PushBack(val, document.GetAllocator());
~~~~~~~~~~
## Modify Object {#ModifyObject}
-Object is a collection of key-value pairs (members). Each key must be a string value. To modify an object, either add or remove members. THe following APIs are for adding members:
+The Object class is a collection of key-value pairs (members). Each key must be a string value. To modify an object, either add or remove members. The following API is for adding members:
* `Value& AddMember(Value&, Value&, Allocator& allocator)`
* `Value& AddMember(StringRefType, Value&, Allocator&)`
@@ -443,7 +462,7 @@ contact.AddMember("name", "Milo", document.GetAllocator());
contact.AddMember("married", true, document.GetAllocator());
~~~~~~~~~~
-The name parameter with `StringRefType` is similar to the interface of `SetString` function for string values. These overloads are used to avoid the need for copying the `name` string, as constant key names are very common in JSON objects.
+The name parameter with `StringRefType` is similar to the interface of the `SetString` function for string values. These overloads are used to avoid the need for copying the `name` string, since constant key names are very common in JSON objects.
If you need to create a name from a non-constant string or a string without sufficient lifetime (see [Create String](#CreateString)), you need to create a string Value by using the copy-string API. To avoid the need for an intermediate variable, you can use a [temporary value](#TemporaryValues) in place:
diff --git a/doc/tutorial.zh-cn.md b/doc/tutorial.zh-cn.md
index 37808b08..6b2588f7 100644
--- a/doc/tutorial.zh-cn.md
+++ b/doc/tutorial.zh-cn.md
@@ -2,19 +2,19 @@
本教程简介文件对象模型(Document Object Model, DOM)API。
-如[用法一览](../readme.zh-cn.md#用法一览)中所示,å¯ä»¥è§£æžä¸€ä¸ªJSON至DOM,然åŽå°±å¯ä»¥è½»æ¾æŸ¥è¯¢åŠä¿®æ”¹DOM,并最终转æ¢å›žJSON。
+如 [用法一览](../readme.zh-cn.md#用法一览) 中所示,å¯ä»¥è§£æžä¸€ä¸ª JSON 至 DOM,然åŽå°±å¯ä»¥è½»æ¾æŸ¥è¯¢åŠä¿®æ”¹ DOM,并最终转æ¢å›ž JSON。
[TOC]
# Value åŠ Document {#ValueDocument}
-æ¯ä¸ªJSON值都储存为`Value`类,而`Document`类则表示整个DOM,它存储了一个DOMæ ‘çš„æ ¹`Value`。RapidJSON的所有公开类型åŠå‡½æ•°éƒ½åœ¨`rapidjson`命å空间中。
+æ¯ä¸ª JSON 值都储存为 `Value` 类,而 `Document` 类则表示整个 DOM,它存储了一个 DOM æ ‘çš„æ ¹ `Value`。RapidJSON 的所有公开类型åŠå‡½æ•°éƒ½åœ¨ `rapidjson` 命å空间中。
-# 查询Value {#QueryValue}
+# 查询 Value {#QueryValue}
-在本节中,我们会使用到`example/tutorial/tutorial.cpp`中的代ç ç‰‡æ®µã€‚
+在本节中,我们会使用到 `example/tutorial/tutorial.cpp` 中的代ç ç‰‡æ®µã€‚
-å‡è®¾æˆ‘们用C语言的字符串储存一个JSON(`const char* json`):
+å‡è®¾æˆ‘们用 C 语言的字符串储存一个 JSON(`const char* json`):
~~~~~~~~~~js
{
"hello": "world",
@@ -27,7 +27,7 @@
}
~~~~~~~~~~
-把它解æžè‡³ä¸€ä¸ª`Document`:
+把它解æžè‡³ä¸€ä¸ª `Document`:
~~~~~~~~~~cpp
#include "rapidjson/document.h"
@@ -38,16 +38,16 @@ Document document;
document.Parse(json);
~~~~~~~~~~
-那么现在该JSON就会被解æžè‡³`document`中,æˆä¸ºä¸€æ£µ*DOMæ ‘*:
+那么现在该 JSON 就会被解æžè‡³ `document` 中,æˆä¸ºä¸€æ£µ *DOM æ ‘ *:
-![教程中的DOM](diagram/tutorial.png)
+![教程中的 DOM](diagram/tutorial.png)
-自从RFC 7159作出更新,åˆæ³•JSON文件的根å¯ä»¥æ˜¯ä»»ä½•ç±»åž‹çš„JSON值。而在较早的RFC 4627中,根值åªå…许是Object或Array。而在上述例å­ä¸­ï¼Œæ ¹æ˜¯ä¸€ä¸ªObject。
+自从 RFC 7159 作出更新,åˆæ³• JSON 文件的根å¯ä»¥æ˜¯ä»»ä½•ç±»åž‹çš„ JSON 值。而在较早的 RFC 4627 中,根值åªå…许是 Object 或 Array。而在上述例å­ä¸­ï¼Œæ ¹æ˜¯ä¸€ä¸ª Object。
~~~~~~~~~~cpp
assert(document.IsObject());
~~~~~~~~~~
-让我们查询一下根Object中有没有`"hello"`æˆå‘˜ã€‚由于一个`Value`å¯åŒ…å«ä¸åŒç±»åž‹çš„值,我们å¯èƒ½éœ€è¦éªŒè¯å®ƒçš„类型,并使用åˆé€‚çš„API去获å–其值。在此例中,`"hello"`æˆå‘˜å…³è”到一个JSON String。
+让我们查询一下根 Object 中有没有 `"hello"` æˆå‘˜ã€‚由于一个 `Value` å¯åŒ…å«ä¸åŒç±»åž‹çš„值,我们å¯èƒ½éœ€è¦éªŒè¯å®ƒçš„类型,并使用åˆé€‚çš„ API 去获å–其值。在此例中,`"hello"` æˆå‘˜å…³è”到一个 JSON String。
~~~~~~~~~~cpp
assert(document.HasMember("hello"));
assert(document["hello"].IsString());
@@ -58,7 +58,7 @@ printf("hello = %s\n", document["hello"].GetString());
world
~~~~~~~~~~
-JSON True/False值是以`bool`表示的。
+JSON True/False 值是以 `bool` 表示的。
~~~~~~~~~~cpp
assert(document["t"].IsBool());
printf("t = %s\n", document["t"].GetBool() ? "true" : "false");
@@ -68,7 +68,7 @@ printf("t = %s\n", document["t"].GetBool() ? "true" : "false");
true
~~~~~~~~~~
-JSON Null值å¯ç”¨`IsNull()`查询。
+JSON Null 值å¯ç”¨ `IsNull()` 查询。
~~~~~~~~~~cpp
printf("n = %s\n", document["n"].IsNull() ? "null" : "?");
~~~~~~~~~~
@@ -77,12 +77,12 @@ printf("n = %s\n", document["n"].IsNull() ? "null" : "?");
null
~~~~~~~~~~
-JSON Number类型表示所有数值。然而,C++需è¦ä½¿ç”¨æ›´ä¸“门的类型。
+JSON Number 类型表示所有数值。然而,C++ 需è¦ä½¿ç”¨æ›´ä¸“门的类型。
~~~~~~~~~~cpp
assert(document["i"].IsNumber());
-// 在此情况下,IsUint()/IsInt64()/IsUInt64()也会返回 true
+// 在此情况下,IsUint()/IsInt64()/IsUInt64() 也会返回 true
assert(document["i"].IsInt());
printf("i = %d\n", document["i"].GetInt());
// å¦ä¸€ç§ç”¨æ³•ï¼š (int)document["i"]
@@ -97,7 +97,7 @@ i = 123
pi = 3.1416
~~~~~~~~~~
-JSON Array包å«ä¸€äº›å…ƒç´ ã€‚
+JSON Array 包å«ä¸€äº›å…ƒç´ ã€‚
~~~~~~~~~~cpp
// 使用引用æ¥è¿žç»­è®¿é—®ï¼Œæ–¹ä¾¿ä¹‹ä½™è¿˜æ›´é«˜æ•ˆã€‚
const Value& a = document["a"];
@@ -113,17 +113,17 @@ a[2] = 3
a[3] = 4
~~~~~~~~~~
-注æ„,RapidJSON并ä¸è‡ªåŠ¨è½¬æ¢å„ç§JSON类型。例如,对一个Stringçš„Value调用`GetInt()`是éžæ³•çš„。在调试模å¼ä¸‹ï¼Œå®ƒä¼šè¢«æ–­è¨€å¤±è´¥ã€‚在å‘布模å¼ä¸‹ï¼Œå…¶è¡Œä¸ºæ˜¯æœªå®šä¹‰çš„。
+注æ„,RapidJSON 并ä¸è‡ªåŠ¨è½¬æ¢å„ç§ JSON 类型。例如,对一个 String çš„ Value 调用 `GetInt()` 是éžæ³•çš„。在调试模å¼ä¸‹ï¼Œå®ƒä¼šè¢«æ–­è¨€å¤±è´¥ã€‚在å‘布模å¼ä¸‹ï¼Œå…¶è¡Œä¸ºæ˜¯æœªå®šä¹‰çš„。
以下将会讨论有关查询å„类型的细节。
-## 查询Array {#QueryArray}
+## 查询 Array {#QueryArray}
-缺çœæƒ…况下,`SizeType`是`unsigned`çš„typedef。在多数系统中,Array最多能存储2^32-1个元素。
+缺çœæƒ…况下,`SizeType` 是 `unsigned` çš„ typedef。在多数系统中,Array 最多能存储 2^32-1 个元素。
-ä½ å¯ä»¥ç”¨æ•´æ•°å­—é¢é‡è®¿é—®å…ƒç´ ï¼Œå¦‚`a[0]`ã€`a[1]`ã€`a[2]`。
+ä½ å¯ä»¥ç”¨æ•´æ•°å­—é¢é‡è®¿é—®å…ƒç´ ï¼Œå¦‚ `a[0]`ã€`a[1]`ã€`a[2]`。
-Array与`std::vector`相似,除了使用索引,也å¯ä½¿ç”¨è¿­ä»£å™¨æ¥è®¿é—®æ‰€æœ‰å…ƒç´ ã€‚
+Array 与 `std::vector` 相似,除了使用索引,也å¯ä½¿ç”¨è¿­ä»£å™¨æ¥è®¿é—®æ‰€æœ‰å…ƒç´ ã€‚
~~~~~~~~~~cpp
for (Value::ConstValueIterator itr = a.Begin(); itr != a.End(); ++itr)
printf("%d ", itr->GetInt());
@@ -133,9 +133,18 @@ for (Value::ConstValueIterator itr = a.Begin(); itr != a.End(); ++itr)
* `SizeType Capacity() const`
* `bool Empty() const`
-## 查询Object {#QueryObject}
+### 范围 for 循环 (v1.1.0 中的新功能)
-å’ŒArray相似,我们å¯ä»¥ç”¨è¿­ä»£å™¨åŽ»è®¿é—®æ‰€æœ‰Objectæˆå‘˜ï¼š
+当使用 C++11 功能时,你å¯ä½¿ç”¨èŒƒå›´ for 循环去访问 Array 内的所有元素。
+
+~~~~~~~~~~cpp
+for (auto& v : a.GetArray())
+ printf("%d ", v.GetInt());
+~~~~~~~~~~
+
+## 查询 Object {#QueryObject}
+
+å’Œ Array 相似,我们å¯ä»¥ç”¨è¿­ä»£å™¨åŽ»è®¿é—®æ‰€æœ‰ Object æˆå‘˜ï¼š
~~~~~~~~~~cpp
static const char* kTypeNames[] =
@@ -159,33 +168,43 @@ Type of member pi is Number
Type of member a is Array
~~~~~~~~~~
-注æ„,当`operator[](const char*)`找ä¸åˆ°æˆå‘˜ï¼Œå®ƒä¼šæ–­è¨€å¤±è´¥ã€‚
+注æ„,当 `operator[](const char*)` 找ä¸åˆ°æˆå‘˜ï¼Œå®ƒä¼šæ–­è¨€å¤±è´¥ã€‚
-若我们ä¸ç¡®å®šä¸€ä¸ªæˆå‘˜æ˜¯å¦å­˜åœ¨ï¼Œä¾¿éœ€è¦åœ¨è°ƒç”¨`operator[](const char*)`å‰å…ˆè°ƒç”¨`HasMember()`。然而,这会导致两次查找。更好的åšæ³•æ˜¯è°ƒç”¨`FindMember()`,它能åŒæ—¶æ£€æŸ¥æˆå‘˜æ˜¯å¦å­˜åœ¨å¹¶è¿”回它的Value:
+若我们ä¸ç¡®å®šä¸€ä¸ªæˆå‘˜æ˜¯å¦å­˜åœ¨ï¼Œä¾¿éœ€è¦åœ¨è°ƒç”¨ `operator[](const char*)` å‰å…ˆè°ƒç”¨ `HasMember()`。然而,这会导致两次查找。更好的åšæ³•æ˜¯è°ƒç”¨ `FindMember()`,它能åŒæ—¶æ£€æŸ¥æˆå‘˜æ˜¯å¦å­˜åœ¨å¹¶è¿”回它的 Value:
~~~~~~~~~~cpp
Value::ConstMemberIterator itr = document.FindMember("hello");
if (itr != document.MemberEnd())
- printf("%s %s\n", itr->value.GetString());
+ printf("%s\n", itr->value.GetString());
+~~~~~~~~~~
+
+### 范围 for 循环 (v1.1.0 中的新功能)
+
+当使用 C++11 功能时,你å¯ä½¿ç”¨èŒƒå›´ for 循环去访问 Object 内的所有æˆå‘˜ã€‚
+
+~~~~~~~~~~cpp
+for (auto& m : document.GetObject())
+ printf("Type of member %s is %s\n",
+ m.name.GetString(), kTypeNames[m.value.GetType()]);
~~~~~~~~~~
-## 查询Number {#QueryNumber}
+## 查询 Number {#QueryNumber}
-JSONåªæ供一ç§æ•°å€¼ç±»åž‹â”€â”€Number。数字å¯ä»¥æ˜¯æ•´æ•°æˆ–实数。RFC 4627规定数字的范围由解æžå™¨æŒ‡å®šã€‚
+JSON åªæ供一ç§æ•°å€¼ç±»åž‹â”€â”€Number。数字å¯ä»¥æ˜¯æ•´æ•°æˆ–实数。RFC 4627 规定数字的范围由解æžå™¨æŒ‡å®šã€‚
-由于C++æ供多ç§æ•´æ•°åŠæµ®ç‚¹æ•°ç±»åž‹ï¼ŒDOMå°è¯•å°½é‡æ供最广的范围åŠè‰¯å¥½æ€§èƒ½ã€‚
+由于 C++ æ供多ç§æ•´æ•°åŠæµ®ç‚¹æ•°ç±»åž‹ï¼ŒDOM å°è¯•å°½é‡æ供最广的范围åŠè‰¯å¥½æ€§èƒ½ã€‚
-当解æžä¸€ä¸ªNumberæ—¶, 它会被存储在DOM之中,æˆä¸ºä¸‹åˆ—其中一个类型:
+当解æžä¸€ä¸ª Number æ—¶, 它会被存储在 DOM 之中,æˆä¸ºä¸‹åˆ—其中一个类型:
类型 | æè¿°
-----------|---------------------------------------
-`unsigned` | 32ä½æ— å·æ•´æ•°
-`int` | 32ä½æœ‰å·æ•´æ•°
-`uint64_t` | 64ä½æ— å·æ•´æ•°
-`int64_t` | 64ä½æœ‰å·æ•´æ•°
-`double` | 64ä½åŒç²¾åº¦æµ®ç‚¹æ•°
+`unsigned` | 32 ä½æ— å·æ•´æ•°
+`int` | 32 ä½æœ‰å·æ•´æ•°
+`uint64_t` | 64 ä½æ— å·æ•´æ•°
+`int64_t` | 64 ä½æœ‰å·æ•´æ•°
+`double` | 64 ä½åŒç²¾åº¦æµ®ç‚¹æ•°
-当查询一个Numberæ—¶, ä½ å¯ä»¥æ£€æŸ¥è¯¥æ•°å­—是å¦èƒ½ä»¥ç›®æ ‡ç±»åž‹æ¥æå–:
+当查询一个 Number æ—¶, ä½ å¯ä»¥æ£€æŸ¥è¯¥æ•°å­—是å¦èƒ½ä»¥ç›®æ ‡ç±»åž‹æ¥æå–:
查检 | æå–
------------------|---------------------
@@ -196,28 +215,28 @@ JSONåªæ供一ç§æ•°å€¼ç±»åž‹â”€â”€Number。数字å¯ä»¥æ˜¯æ•´æ•°æˆ–实数。R
`bool IsInt64()` | `int64_t GetInt64()`
`bool IsDouble()` | `double GetDouble()`
-注æ„,一个整数å¯èƒ½ç”¨å‡ ç§ç±»åž‹æ¥æå–,而无需转æ¢ã€‚例如,一个å为`x`çš„Value包å«123,那么`x.IsInt() == x.IsUint() == x.IsInt64() == x.IsUint64() == true`。但如果一个å为`y`çš„Value包å«-3000000000,那么仅会令`x.IsInt64() == true`。
+注æ„,一个整数å¯èƒ½ç”¨å‡ ç§ç±»åž‹æ¥æå–,而无需转æ¢ã€‚例如,一个å为 `x` çš„ Value åŒ…å« 123,那么 `x.IsInt() == x.IsUint() == x.IsInt64() == x.IsUint64() == true`。但如果一个å为 `y` çš„ Value åŒ…å« -3000000000,那么仅会令 `x.IsInt64() == true`。
-当è¦æå–Number类型,`GetDouble()`是会把内部整数的表示转æ¢æˆ`double`。注æ„`int` å’Œ`unsigned`å¯ä»¥å®‰å…¨åœ°è½¬æ¢è‡³`double`,但`int64_t`åŠ`uint64_t`å¯èƒ½ä¼šä¸§å¤±ç²¾åº¦ï¼ˆå› ä¸º`double`的尾数åªæœ‰52ä½ï¼‰ã€‚
+当è¦æå– Number 类型,`GetDouble()` 是会把内部整数的表示转æ¢æˆ `double`ã€‚æ³¨æ„ `int` å’Œ `unsigned` å¯ä»¥å®‰å…¨åœ°è½¬æ¢è‡³ `double`,但 `int64_t` åŠ `uint64_t` å¯èƒ½ä¼šä¸§å¤±ç²¾åº¦ï¼ˆå› ä¸º `double` 的尾数åªæœ‰ 52 ä½ï¼‰ã€‚
-## 查询String {#QueryString}
+## 查询 String {#QueryString}
-除了`GetString()`,`Value`类也有一个`GetStringLength()`。这里会解释个中原因。
+除了 `GetString()`,`Value` 类也有一个 `GetStringLength()`。这里会解释个中原因。
-æ ¹æ®RFC 4627,JSON Stringå¯åŒ…å«Unicode字符`U+0000`,在JSON中会表示为`"\u0000"`。问题是,C/C++通常使用空字符结尾字符串(null-terminated string),这ç§å­—符串把``\0'`作为结æŸç¬¦å·ã€‚
+æ ¹æ® RFC 4627,JSON String å¯åŒ…å« Unicode 字符 `U+0000`,在 JSON 中会表示为 `"\u0000"`。问题是,C/C++ 通常使用空字符结尾字符串(null-terminated string),这ç§å­—符串把 ``\0'` 作为结æŸç¬¦å·ã€‚
-为了符åˆRFC 4627,RapidJSON支æŒåŒ…å«`U+0000`çš„String。若你需è¦å¤„ç†è¿™äº›String,便å¯ä½¿ç”¨`GetStringLength()`去获得正确的字符串长度。
+ä¸ºäº†ç¬¦åˆ RFC 4627,RapidJSON 支æŒåŒ…å« `U+0000` çš„ String。若你需è¦å¤„ç†è¿™äº› String,便å¯ä½¿ç”¨ `GetStringLength()` 去获得正确的字符串长度。
-例如,当解æžä»¥ä¸‹çš„JSON至`Document d`之åŽï¼š
+例如,当解æžä»¥ä¸‹çš„ JSON 至 `Document d` 之åŽï¼š
~~~~~~~~~~js
{ "s" : "a\u0000b" }
~~~~~~~~~~
-`"a\u0000b"`值的正确长度应该是3。但`strlen()`会返回1。
+`"a\u0000b"` 值的正确长度应该是 3。但 `strlen()` 会返回 1。
-`GetStringLength()`也å¯ä»¥æ高性能,因为用户å¯èƒ½éœ€è¦è°ƒç”¨`strlen()`去分é…缓冲。
+`GetStringLength()` 也å¯ä»¥æ高性能,因为用户å¯èƒ½éœ€è¦è°ƒç”¨ `strlen()` 去分é…缓冲。
-此外,`std::string`也支æŒè¿™ä¸ªæž„造函数:
+此外,`std::string` 也支æŒè¿™ä¸ªæž„造函数:
~~~~~~~~~~cpp
string(const char* s, size_t count);
@@ -225,27 +244,27 @@ string(const char* s, size_t count);
此构造函数接å—字符串长度作为å‚数。它支æŒåœ¨å­—符串中存储空字符,也应该会有更好的性能。
-## 比较两个Value
+## 比较两个 Value
-ä½ å¯ä½¿ç”¨`==`åŠ`!=`去比较两个Value。当且仅当两个Value的类型åŠå†…容相åŒï¼Œå®ƒä»¬æ‰å½“作相等。你也å¯ä»¥æ¯”较Value和它的原生类型值。以下是一个例å­ã€‚
+ä½ å¯ä½¿ç”¨ `==` åŠ `!=` 去比较两个 Value。当且仅当两个 Value 的类型åŠå†…容相åŒï¼Œå®ƒä»¬æ‰å½“作相等。你也å¯ä»¥æ¯”较 Value 和它的原生类型值。以下是一个例å­ã€‚
~~~~~~~~~~cpp
if (document["hello"] == document["n"]) /*...*/; // 比较两个值
if (document["hello"] == "world") /*...*/; // 与字符串家é¢é‡ä½œæ¯”较
if (document["i"] != 123) /*...*/; // 与整数作比较
-if (document["pi"] != 3.14) /*...*/; // 与double作比较
+if (document["pi"] != 3.14) /*...*/; // 与 double 作比较
~~~~~~~~~~
-Arrayï¼Object顺åºä»¥å®ƒä»¬çš„元素ï¼æˆå‘˜ä½œæ¯”较。当且仅当它们的整个å­æ ‘相等,它们æ‰å½“作相等。
+Arrayï¼Object 顺åºä»¥å®ƒä»¬çš„元素ï¼æˆå‘˜ä½œæ¯”较。当且仅当它们的整个å­æ ‘相等,它们æ‰å½“作相等。
-注æ„,现时若一个Objectå«æœ‰é‡å¤å‘½åçš„æˆå‘˜ï¼Œå®ƒä¸Žä»»ä½•Object作比较都总会返回`false`。
+注æ„,现时若一个 Object å«æœ‰é‡å¤å‘½åçš„æˆå‘˜ï¼Œå®ƒä¸Žä»»ä½• Object 作比较都总会返回 `false`。
# 创建ï¼ä¿®æ”¹å€¼ {#CreateModifyValues}
-有多ç§æ–¹æ³•åŽ»åˆ›å»ºå€¼ã€‚ 当一个DOM树被创建或修改åŽï¼Œå¯ä½¿ç”¨`Writer`å†æ¬¡å­˜å‚¨ä¸ºJSON。
+有多ç§æ–¹æ³•åŽ»åˆ›å»ºå€¼ã€‚ 当一个 DOM 树被创建或修改åŽï¼Œå¯ä½¿ç”¨ `Writer` å†æ¬¡å­˜å‚¨ä¸º JSON。
-## 改å˜Value类型 {#ChangeValueType}
-当使用默认构造函数创建一个Value或Document,它的类型便会是Null。è¦æ”¹å˜å…¶ç±»åž‹ï¼Œéœ€è°ƒç”¨`SetXXX()`或赋值æ“作,例如:
+## æ”¹å˜ Value 类型 {#ChangeValueType}
+当使用默认构造函数创建一个 Value 或 Document,它的类型便会是 Null。è¦æ”¹å˜å…¶ç±»åž‹ï¼Œéœ€è°ƒç”¨ `SetXXX()` 或赋值æ“作,例如:
~~~~~~~~~~cpp
Document d; // Null
@@ -260,75 +279,75 @@ v = 10; // 简写,和上é¢çš„相åŒ
几个类型也有é‡è½½æž„造函数:
~~~~~~~~~~cpp
-Value b(true); // 调用Value(bool)
+Value b(true); // 调用 Value(bool)
Value i(-123); // 调用 Value(int)
-Value u(123u); // 调用Value(unsigned)
-Value d(1.5); // 调用Value(double)
+Value u(123u); // 调用 Value(unsigned)
+Value d(1.5); // 调用 Value(double)
~~~~~~~~~~
-è¦é‡å»ºç©ºObject或Array,å¯åœ¨é»˜è®¤æž„造函数åŽä½¿ç”¨ `SetObject()`/`SetArray()`,或一次性使用`Value(Type)`:
+è¦é‡å»ºç©º Object 或 Array,å¯åœ¨é»˜è®¤æž„造函数åŽä½¿ç”¨ `SetObject()`/`SetArray()`,或一次性使用 `Value(Type)`:
~~~~~~~~~~cpp
Value o(kObjectType);
Value a(kArrayType);
~~~~~~~~~~
-## 转移语æ„(Move Semantics) {#MoveSemantics}
+## 转移语义(Move Semantics) {#MoveSemantics}
-在设计RapidJSON时有一个éžå¸¸ç‰¹åˆ«çš„决定,就是Value赋值并ä¸æ˜¯æŠŠæ¥æºValueå¤åˆ¶è‡³ç›®çš„Value,而是把把æ¥æºValue转移(move)至目的Value。例如:
+在设计 RapidJSON 时有一个éžå¸¸ç‰¹åˆ«çš„决定,就是 Value 赋值并ä¸æ˜¯æŠŠæ¥æº Value å¤åˆ¶è‡³ç›®çš„ Value,而是把把æ¥æº Value 转移(move)至目的 Value。例如:
~~~~~~~~~~cpp
Value a(123);
Value b(456);
-b = a; // aå˜æˆNull,bå˜æˆæ•°å­—123。
+b = a; // a å˜æˆ Null,b å˜æˆæ•°å­— 123。
~~~~~~~~~~
-![使用移动语æ„赋值。](diagram/move1.png)
+![使用移动语义赋值。](diagram/move1.png)
-为什么?此语æ„有何优点?
+为什么?此语义有何优点?
-最简å•çš„答案就是性能。对于固定大å°çš„JSON类型(Numberã€Trueã€Falseã€Null),å¤åˆ¶å®ƒä»¬æ˜¯ç®€å•å¿«æ·ã€‚然而,对于å¯å˜å¤§å°çš„JSON类型(Stringã€Arrayã€Object),å¤åˆ¶å®ƒä»¬ä¼šäº§ç”Ÿå¤§é‡å¼€é”€ï¼Œè€Œä¸”这些开销常常ä¸è¢«å¯Ÿè§‰ã€‚尤其是当我们需è¦åˆ›å»ºä¸´æ—¶Object,把它å¤åˆ¶è‡³å¦ä¸€å˜é‡ï¼Œç„¶åŽå†æžæž„它。
+最简å•çš„答案就是性能。对于固定大å°çš„ JSON 类型(Numberã€Trueã€Falseã€Null),å¤åˆ¶å®ƒä»¬æ˜¯ç®€å•å¿«æ·ã€‚然而,对于å¯å˜å¤§å°çš„ JSON 类型(Stringã€Arrayã€Object),å¤åˆ¶å®ƒä»¬ä¼šäº§ç”Ÿå¤§é‡å¼€é”€ï¼Œè€Œä¸”这些开销常常ä¸è¢«å¯Ÿè§‰ã€‚尤其是当我们需è¦åˆ›å»ºä¸´æ—¶ Object,把它å¤åˆ¶è‡³å¦ä¸€å˜é‡ï¼Œç„¶åŽå†æžæž„它。
-例如,若使用正常*å¤åˆ¶*语æ„:
+例如,若使用正常 * å¤åˆ¶ * 语义:
~~~~~~~~~~cpp
Value o(kObjectType);
{
Value contacts(kArrayType);
- // 把元素加进contacts数组。
+ // 把元素加进 contacts 数组。
// ...
- o.AddMember("contacts", contacts, d.GetAllocator()); // 深度å¤åˆ¶contacts (å¯èƒ½æœ‰å¤§é‡å†…存分é…)
- // æžæž„contacts。
+ o.AddMember("contacts", contacts, d.GetAllocator()); // 深度å¤åˆ¶ contacts (å¯èƒ½æœ‰å¤§é‡å†…存分é…)
+ // æžæž„ contacts。
}
~~~~~~~~~~
-![å¤åˆ¶è¯­æ„产生大é‡çš„å¤åˆ¶æ“作。](diagram/move2.png)
+![å¤åˆ¶è¯­ä¹‰äº§ç”Ÿå¤§é‡çš„å¤åˆ¶æ“作。](diagram/move2.png)
-那个`o` Object需è¦åˆ†é…一个和contacts相åŒå¤§å°çš„缓冲区,对conactsåšæ·±åº¦å¤åˆ¶ï¼Œå¹¶æœ€ç»ˆè¦æžæž„contacts。这样会产生大é‡æ— å¿…è¦çš„内存分é…ï¼é‡Šæ”¾ï¼Œä»¥åŠå†…å­˜å¤åˆ¶ã€‚
+那个 `o` Object 需è¦åˆ†é…一个和 contacts 相åŒå¤§å°çš„缓冲区,对 conacts åšæ·±åº¦å¤åˆ¶ï¼Œå¹¶æœ€ç»ˆè¦æžæž„ contacts。这样会产生大é‡æ— å¿…è¦çš„内存分é…ï¼é‡Šæ”¾ï¼Œä»¥åŠå†…å­˜å¤åˆ¶ã€‚
有一些方案å¯é¿å…实质地å¤åˆ¶è¿™äº›æ•°æ®ï¼Œä¾‹å¦‚引用计数(reference counting)ã€åžƒåœ¾å›žæ”¶ï¼ˆgarbage collection, GC)。
-为了使RapidJSON简å•åŠå¿«é€Ÿï¼Œæˆ‘们选择了对赋值采用*转移*语æ„。这方法与`std::auto_ptr`相似,都是在赋值时转移拥有æƒã€‚转移快得多简å•å¾—多,åªéœ€è¦æžæž„原æ¥çš„Value,把æ¥æº`memcpy()`至目标,最åŽæŠŠæ¥æºè®¾ç½®ä¸ºNull类型。
+为了使 RapidJSON 简å•åŠå¿«é€Ÿï¼Œæˆ‘们选择了对赋值采用 * 转移 * 语义。这方法与 `std::auto_ptr` 相似,都是在赋值时转移拥有æƒã€‚转移快得多简å•å¾—多,åªéœ€è¦æžæž„原æ¥çš„ Value,把æ¥æº `memcpy()` 至目标,最åŽæŠŠæ¥æºè®¾ç½®ä¸º Null 类型。
-因此,使用转移语æ„åŽï¼Œä¸Šé¢çš„例å­å˜æˆï¼š
+因此,使用转移语义åŽï¼Œä¸Šé¢çš„例å­å˜æˆï¼š
~~~~~~~~~~cpp
Value o(kObjectType);
{
Value contacts(kArrayType);
// adding elements to contacts array.
- o.AddMember("contacts", contacts, d.GetAllocator()); // åªéœ€ memcpy() contacts本身至新æˆå‘˜çš„Value(16字节)
- // contacts在这里å˜æˆNull。它的æžæž„是平凡的。
+ o.AddMember("contacts", contacts, d.GetAllocator()); // åªéœ€ memcpy() contacts 本身至新æˆå‘˜çš„ Value(16 字节)
+ // contacts 在这里å˜æˆ Null。它的æžæž„是平凡的。
}
~~~~~~~~~~
-![转移语æ„ä¸éœ€å¤åˆ¶ã€‚](diagram/move3.png)
+![转移语义ä¸éœ€å¤åˆ¶ã€‚](diagram/move3.png)
-在C++11中这称为转移赋值æ“作(move assignment operator)。由于RapidJSON 支æŒC++03,它在赋值æ“作采用转移语æ„,其它修改形函数如`AddMember()`, `PushBack()`也采用转移语æ„。
+在 C++11 中这称为转移赋值æ“作(move assignment operator)。由于 RapidJSON æ”¯æŒ C++03,它在赋值æ“作采用转移语义,其它修改型函数如 `AddMember()`, `PushBack()` 也采用转移语义。
-### 转移语æ„åŠä¸´æ—¶å€¼ {#TemporaryValues}
+### 转移语义åŠä¸´æ—¶å€¼ {#TemporaryValues}
-有时候,我们想直接构造一个Value并传递给一个“转移â€å‡½æ•°ï¼ˆå¦‚`PushBack()`ã€`AddMember()`)。由于临时对象是ä¸èƒ½è½¬æ¢ä¸ºæ­£å¸¸çš„Value引用,我们加入了一个方便的`Move()`函数:
+有时候,我们想直接构造一个 Value 并传递给一个“转移â€å‡½æ•°ï¼ˆå¦‚ `PushBack()`ã€`AddMember()`)。由于临时对象是ä¸èƒ½è½¬æ¢ä¸ºæ­£å¸¸çš„ Value 引用,我们加入了一个方便的 `Move()` 函数:
~~~~~~~~~~cpp
Value a(kArrayType);
@@ -338,17 +357,17 @@ a.PushBack(Value().SetInt(42), allocator); // fluent API
a.PushBack(Value(42).Move(), allocator); // 和上一行相åŒ
~~~~~~~~~~
-## 创建String {#CreateString}
-RapidJSONæ供两个String的存储策略。
+## 创建 String {#CreateString}
+RapidJSON æ供两个 String 的存储策略。
1. copy-string: 分é…缓冲区,然åŽæŠŠæ¥æºæ•°æ®å¤åˆ¶è‡³å®ƒã€‚
2. const-string: 简å•åœ°å‚¨å­˜å­—符串的指针。
-Copy-string总是安全的,因为它拥有数æ®çš„克隆。Const-stringå¯ç”¨äºŽå­˜å‚¨å­—符串字é¢é‡ï¼Œä»¥åŠç”¨äºŽåœ¨DOM一节中将会æ到的in-situ解æžä¸­ã€‚
+Copy-string 总是安全的,因为它拥有数æ®çš„克隆。Const-string å¯ç”¨äºŽå­˜å‚¨å­—符串字é¢é‡ï¼Œä»¥åŠç”¨äºŽåœ¨ DOM 一节中将会æ到的 in-situ 解æžä¸­ã€‚
-为了让用户自定义内存分é…æ–¹å¼ï¼Œå½“一个æ“作å¯èƒ½éœ€è¦å†…存分é…时,RapidJSONè¦æ±‚用户传递一个allocator实例作为APIå‚数。此设计é¿å…了在æ¯ä¸ªValue存储allocator(或document)的指针。
+为了让用户自定义内存分é…æ–¹å¼ï¼Œå½“一个æ“作å¯èƒ½éœ€è¦å†…存分é…时,RapidJSON è¦æ±‚用户传递一个 allocator 实例作为 API å‚数。此设计é¿å…了在æ¯ä¸ª Value 存储 allocator(或 document)的指针。
-因此,当我们把一个copy-string赋值时, 调用å«æœ‰allocatorçš„`SetString()`é‡è½½å‡½æ•°ï¼š
+因此,当我们把一个 copy-string 赋值时, 调用å«æœ‰ allocator çš„ `SetString()` é‡è½½å‡½æ•°ï¼š
~~~~~~~~~~cpp
Document document;
@@ -357,14 +376,14 @@ char buffer[10];
int len = sprintf(buffer, "%s %s", "Milo", "Yip"); // 动æ€åˆ›å»ºçš„字符串。
author.SetString(buffer, len, document.GetAllocator());
memset(buffer, 0, sizeof(buffer));
-// 清空bufferåŽauthor.GetString() ä»ç„¶åŒ…å« "Milo Yip"
+// 清空 buffer åŽ author.GetString() ä»ç„¶åŒ…å« "Milo Yip"
~~~~~~~~~~
-在此例å­ä¸­ï¼Œæˆ‘们使用`Document`实例的allocator。这是使用RapidJSON时常用的惯用法。但你也å¯ä»¥ç”¨å…¶ä»–allocator实例。
+在此例å­ä¸­ï¼Œæˆ‘们使用 `Document` 实例的 allocator。这是使用 RapidJSON 时常用的惯用法。但你也å¯ä»¥ç”¨å…¶ä»– allocator 实例。
-å¦å¤–,上é¢çš„`SetString()`需è¦é•¿åº¦å‚数。这个API能处ç†å«æœ‰ç©ºå­—符的字符串。å¦ä¸€ä¸ª`SetString()`é‡è½½å‡½æ•°æ²¡æœ‰é•¿åº¦å‚数,它å‡è®¾è¾“入是空字符结尾的,并会调用类似`strlen()`的函数去获å–长度。
+å¦å¤–,上é¢çš„ `SetString()` 需è¦é•¿åº¦å‚数。这个 API 能处ç†å«æœ‰ç©ºå­—符的字符串。å¦ä¸€ä¸ª `SetString()` é‡è½½å‡½æ•°æ²¡æœ‰é•¿åº¦å‚数,它å‡è®¾è¾“入是空字符结尾的,并会调用类似 `strlen()` 的函数去获å–长度。
-最åŽï¼Œå¯¹äºŽå­—符串字é¢é‡æˆ–有安全生命周期的字符串,å¯ä»¥ä½¿ç”¨const-string版本的`SetString()`,它没有allocatorå‚数。对于字符串家é¢é‡ï¼ˆæˆ–字符数组常é‡ï¼‰ï¼Œåªéœ€ç®€å•åœ°ä¼ é€’å­—é¢é‡ï¼Œåˆå®‰å…¨åˆé«˜æ•ˆï¼š
+最åŽï¼Œå¯¹äºŽå­—符串字é¢é‡æˆ–有安全生命周期的字符串,å¯ä»¥ä½¿ç”¨ const-string 版本的 `SetString()`,它没有 allocator å‚数。对于字符串家é¢é‡ï¼ˆæˆ–字符数组常é‡ï¼‰ï¼Œåªéœ€ç®€å•åœ°ä¼ é€’å­—é¢é‡ï¼Œåˆå®‰å…¨åˆé«˜æ•ˆï¼š
~~~~~~~~~~cpp
Value s;
@@ -372,22 +391,22 @@ s.SetString("rapidjson"); // å¯åŒ…å«ç©ºå­—符,长度在编译è推导
s = "rapidjson"; // 上行的缩写
~~~~~~~~~~
-对于字符指针,RapidJSON需è¦ä½œä¸€ä¸ªæ ‡è®°ï¼Œä»£è¡¨å®ƒä¸å¤åˆ¶ä¹Ÿæ˜¯å®‰å…¨çš„。å¯ä»¥ä½¿ç”¨`StringRef`函数:
+对于字符指针,RapidJSON 需è¦ä½œä¸€ä¸ªæ ‡è®°ï¼Œä»£è¡¨å®ƒä¸å¤åˆ¶ä¹Ÿæ˜¯å®‰å…¨çš„。å¯ä»¥ä½¿ç”¨ `StringRef` 函数:
~~~~~~~~~cpp
const char * cstr = getenv("USER");
size_t cstr_len = ...; // 如果有长度
Value s;
// s.SetString(cstr); // è¿™ä¸èƒ½é€šè¿‡ç¼–译
-s.SetString(StringRef(cstr)); // å¯ä»¥ï¼Œå‡è®¾å®ƒçš„生命周期案全,并且是以空字符结尾的
+s.SetString(StringRef(cstr)); // å¯ä»¥ï¼Œå‡è®¾å®ƒçš„生命周期安全,并且是以空字符结尾的
s = StringRef(cstr); // 上行的缩写
s.SetString(StringRef(cstr, cstr_len));// 更快,å¯å¤„ç†ç©ºå­—符
s = StringRef(cstr, cstr_len); // 上行的缩写
~~~~~~~~~
-## 修改Array {#ModifyArray}
-Array类型的Valueæ供与`std::vector`相似的API。
+## 修改 Array {#ModifyArray}
+Array 类型的 Value æ供与 `std::vector` 相似的 API。
* `Clear()`
* `Reserve(SizeType, Allocator&)`
@@ -397,37 +416,37 @@ Array类型的Valueæ供与`std::vector`相似的API。
* `ValueIterator Erase(ConstValueIterator pos)`
* `ValueIterator Erase(ConstValueIterator first, ConstValueIterator last)`
-注æ„,`Reserve(...)`åŠ`PushBack(...)`å¯èƒ½ä¼šä¸ºæ•°ç»„元素分é…内存,所以需è¦ä¸€ä¸ªallocator。
+注æ„,`Reserve(...)` åŠ `PushBack(...)` å¯èƒ½ä¼šä¸ºæ•°ç»„元素分é…内存,所以需è¦ä¸€ä¸ª allocator。
-以下是`PushBack()`的例å­ï¼š
+以下是 `PushBack()` 的例å­ï¼š
~~~~~~~~~~cpp
Value a(kArrayType);
Document::AllocatorType& allocator = document.GetAllocator();
for (int i = 5; i <= 10; i++)
- a.PushBack(i, allocator); // å¯èƒ½éœ€è¦è°ƒç”¨realloc()所以需è¦allocator
+ a.PushBack(i, allocator); // å¯èƒ½éœ€è¦è°ƒç”¨ realloc() æ‰€ä»¥éœ€è¦ allocator
// æµç•…接å£ï¼ˆFluent interface)
a.PushBack("Lua", allocator).PushBack("Mio", allocator);
~~~~~~~~~~
-与STLä¸ä¸€æ ·çš„是,`PushBack()`/`PopBack()`返回Array本身的引用。这称为æµç•…接å£ï¼ˆ_fluent interface_)。
+与 STL ä¸ä¸€æ ·çš„是,`PushBack()`/`PopBack()` 返回 Array 本身的引用。这称为æµç•…接å£ï¼ˆ_fluent interface_)。
-如果你想在Array中加入一个éžå¸¸é‡å­—符串,或是一个没有足够生命周期的字符串(è§[Create String](#CreateString)),你需è¦ä½¿ç”¨copy-string API去创建一个String。为了é¿å…加入中间å˜é‡ï¼Œå¯ä»¥å°±åœ°ä½¿ç”¨ä¸€ä¸ª[临时值](#TemporaryValues):
+如果你想在 Array 中加入一个éžå¸¸é‡å­—ç¬¦ä¸²ï¼Œæˆ–æ˜¯ä¸€ä¸ªæ²¡æœ‰è¶³å¤Ÿç”Ÿå‘½å‘¨æœŸçš„å­—ç¬¦ä¸²ï¼ˆè§ [Create String](#CreateString)),你需è¦ä½¿ç”¨ copy-string API 去创建一个 String。为了é¿å…加入中间å˜é‡ï¼Œå¯ä»¥å°±åœ°ä½¿ç”¨ä¸€ä¸ª [临时值](#TemporaryValues):
~~~~~~~~~~cpp
-// 就地Valueå‚æ•°
+// 就地 Value å‚æ•°
contact.PushBack(Value("copy", document.GetAllocator()).Move(), // copy string
document.GetAllocator());
-// 显å¼Valueå‚æ•°
+// æ˜¾å¼ Value å‚æ•°
Value val("key", document.GetAllocator()); // copy string
contact.PushBack(val, document.GetAllocator());
~~~~~~~~~~
-## 修改Object {#ModifyObject}
-Object是键值对的集åˆã€‚æ¯ä¸ªé”®å¿…须为String。è¦ä¿®æ”¹Object,方法是增加或移除æˆå‘˜ã€‚以下的API用æ¥å¢žåŠ åŸŽå‘˜ï¼š
+## 修改 Object {#ModifyObject}
+Object 是键值对的集åˆã€‚æ¯ä¸ªé”®å¿…须为 String。è¦ä¿®æ”¹ Object,方法是增加或移除æˆå‘˜ã€‚以下的 API 用æ¥å¢žåŠ åŸŽå‘˜ï¼š
* `Value& AddMember(Value&, Value&, Allocator& allocator)`
* `Value& AddMember(StringRefType, Value&, Allocator&)`
@@ -441,34 +460,34 @@ contact.AddMember("name", "Milo", document.GetAllocator());
contact.AddMember("married", true, document.GetAllocator());
~~~~~~~~~~
-使用`StringRefType`作为nameå‚æ•°çš„é‡è½½ç‰ˆæœ¬ä¸Žå­—符串的`SetString`的接å£ç›¸ä¼¼ã€‚ 这些é‡è½½æ˜¯ä¸ºäº†é¿å…å¤åˆ¶`name`字符串,因为JSON object中ç»å¸¸ä¼šä½¿ç”¨å¸¸æ•°é”®å。
+使用 `StringRefType` 作为 name å‚æ•°çš„é‡è½½ç‰ˆæœ¬ä¸Žå­—符串的 `SetString` 的接å£ç›¸ä¼¼ã€‚ 这些é‡è½½æ˜¯ä¸ºäº†é¿å…å¤åˆ¶ `name` 字符串,因为 JSON object 中ç»å¸¸ä¼šä½¿ç”¨å¸¸æ•°é”®å。
-如果你需è¦ä»Žéžå¸¸æ•°å­—符串或生命周期ä¸è¶³çš„字符串创建键å(è§[创建String](#CreateString)),你需è¦ä½¿ç”¨copy-string API。为了é¿å…中间å˜é‡ï¼Œå¯ä»¥å°±åœ°ä½¿ç”¨[临时值](#TemporaryValues):
+如果你需è¦ä»Žéžå¸¸æ•°å­—符串或生命周期ä¸è¶³çš„字符串创建键åï¼ˆè§ [创建 String](#CreateString)),你需è¦ä½¿ç”¨ copy-string API。为了é¿å…中间å˜é‡ï¼Œå¯ä»¥å°±åœ°ä½¿ç”¨ [临时值](#TemporaryValues):
~~~~~~~~~~cpp
-// 就地Valueå‚æ•°
+// 就地 Value å‚æ•°
contact.AddMember(Value("copy", document.GetAllocator()).Move(), // copy string
Value().Move(), // null value
document.GetAllocator());
// 显å¼å‚æ•°
Value key("key", document.GetAllocator()); // copy string name
-Value val(42); // æŸValue
+Value val(42); // æŸ Value
contact.AddMember(key, val, document.GetAllocator());
~~~~~~~~~~
移除æˆå‘˜æœ‰å‡ ä¸ªé€‰æ‹©ï¼š
* `bool RemoveMember(const Ch* name)`:使用键åæ¥ç§»é™¤æˆå‘˜ï¼ˆçº¿æ€§æ—¶é—´å¤æ‚度)。
-* `bool RemoveMember(const Value& name)`:除了`name`是一个Value,和上一行相åŒã€‚
-* `MemberIterator RemoveMember(MemberIterator)`:使用迭代器移除æˆå‘˜ï¼ˆ_常数_时间å¤æ‚度)。
+* `bool RemoveMember(const Value& name)`:除了 `name` 是一个 Value,和上一行相åŒã€‚
+* `MemberIterator RemoveMember(MemberIterator)`:使用迭代器移除æˆå‘˜ï¼ˆ_ 常数 _ 时间å¤æ‚度)。
* `MemberIterator EraseMember(MemberIterator)`:和上行相似但维æŒæˆå‘˜æ¬¡åºï¼ˆçº¿æ€§æ—¶é—´å¤æ‚度)。
* `MemberIterator EraseMember(MemberIterator first, MemberIterator last)`:移除一个范围内的æˆå‘˜ï¼Œç»´æŒæ¬¡åºï¼ˆçº¿æ€§æ—¶é—´å¤æ‚度)。
-`MemberIterator RemoveMember(MemberIterator)`使用了“转移最åŽâ€æ‰‹æ³•æ¥è¾¾æˆå¸¸æ•°æ—¶é—´å¤æ‚度。基本上就是æžæž„迭代器ä½ç½®çš„æˆå‘˜ï¼Œç„¶åŽæŠŠæœ€åŽçš„æˆå‘˜è½¬ç§»è‡³è¿­ä»£å™¨ä½ç½®ã€‚因此,æˆå‘˜çš„次åºä¼šè¢«æ”¹å˜ã€‚
+`MemberIterator RemoveMember(MemberIterator)` 使用了“转移最åŽâ€æ‰‹æ³•æ¥è¾¾æˆå¸¸æ•°æ—¶é—´å¤æ‚度。基本上就是æžæž„迭代器ä½ç½®çš„æˆå‘˜ï¼Œç„¶åŽæŠŠæœ€åŽçš„æˆå‘˜è½¬ç§»è‡³è¿­ä»£å™¨ä½ç½®ã€‚因此,æˆå‘˜çš„次åºä¼šè¢«æ”¹å˜ã€‚
-## æ·±å¤åˆ¶Value {#DeepCopyValue}
-若我们真的è¦å¤åˆ¶ä¸€ä¸ªDOM树,我们å¯ä½¿ç”¨ä¸¤ä¸ªAPIs作深å¤åˆ¶ï¼šå«allocator的构造函数åŠ`CopyFrom()`。
+## æ·±å¤åˆ¶ Value {#DeepCopyValue}
+若我们真的è¦å¤åˆ¶ä¸€ä¸ª DOM 树,我们å¯ä½¿ç”¨ä¸¤ä¸ª APIs 作深å¤åˆ¶ï¼šå« allocator çš„æž„é€ å‡½æ•°åŠ `CopyFrom()`。
~~~~~~~~~~cpp
Document d;
@@ -477,19 +496,19 @@ Value v1("foo");
// Value v2(v1); // ä¸å®¹è®¸
Value v2(v1, a); // 制造一个克隆
-assert(v1.IsString()); // v1ä¸å˜
+assert(v1.IsString()); // v1 ä¸å˜
d.SetArray().PushBack(v1, a).PushBack(v2, a);
-assert(v1.IsNull() && v2.IsNull()); // 两个都转移动d
+assert(v1.IsNull() && v2.IsNull()); // 两个都转移动 d
-v2.CopyFrom(d, a); // 把整个documentå¤åˆ¶è‡³v2
-assert(d.IsArray() && d.Size() == 2); // dä¸å˜
+v2.CopyFrom(d, a); // 把整个 document å¤åˆ¶è‡³ v2
+assert(d.IsArray() && d.Size() == 2); // d ä¸å˜
v1.SetObject().AddMember("array", v2, a);
d.PushBack(v1, a);
~~~~~~~~~~
-## 交æ¢Value {#SwapValues}
+## äº¤æ¢ Value {#SwapValues}
-RapidJSON也æä¾›`Swap()`。
+RapidJSON 也æä¾› `Swap()`。
~~~~~~~~~~cpp
Value a(123);
@@ -499,17 +518,17 @@ assert(a.IsString());
assert(b.IsInt());
~~~~~~~~~~
-无论两棵DOM树有多å¤æ‚,交æ¢æ˜¯å¾ˆå¿«çš„(常数时间)。
+无论两棵 DOM 树有多å¤æ‚,交æ¢æ˜¯å¾ˆå¿«çš„(常数时间)。
# 下一部分 {#WhatsNext}
-本教程展示了如何询查åŠä¿®æ”¹DOM树。RapidJSON还有一个é‡è¦æ¦‚念:
+本教程展示了如何询查åŠä¿®æ”¹ DOM 树。RapidJSON 还有一个é‡è¦æ¦‚念:
-1. [æµ](doc/stream.zh-cn.md) 是读写JSON的通é“。æµå¯ä»¥æ˜¯å†…存字符串ã€æ–‡ä»¶æµç­‰ã€‚用户也å¯ä»¥è‡ªå®šä¹‰æµã€‚
-2. [ç¼–ç ](doc/encoding.zh-cn.md)定义在æµæˆ–内存中使用的字符编ç ã€‚RapidJSON也在内部æä¾›Unicode转æ¢åŠæ ¡éªŒåŠŸèƒ½ã€‚
-3. [DOM](doc/dom.zh-cn.md)的基本功能已在本教程里介ç»ã€‚还有更高级的功能,如原ä½ï¼ˆ*in situ*)解æžã€å…¶ä»–解æžé€‰é¡¹åŠé«˜çº§ç”¨æ³•ã€‚
-4. [SAX](doc/sax.zh-cn.md) 是RapidJSON解æžï¼ç”ŸæˆåŠŸèƒ½çš„基础。学习使用`Reader`/`Writer`去实现更高性能的应用程åºã€‚也å¯ä»¥ä½¿ç”¨`PrettyWriter`去格å¼åŒ–JSON。
-5. [性能](doc/performance.zh-cn.md)展示一些我们åšçš„åŠç¬¬ä¸‰æ–¹çš„性能测试。
-6. [技术内幕](doc/internals.zh-cn.md)讲述一些RapidJSON内部的设计åŠæŠ€æœ¯ã€‚
+1. [æµ](doc/stream.zh-cn.md) 是读写 JSON 的通é“。æµå¯ä»¥æ˜¯å†…存字符串ã€æ–‡ä»¶æµç­‰ã€‚用户也å¯ä»¥è‡ªå®šä¹‰æµã€‚
+2. [ç¼–ç ](doc/encoding.zh-cn.md) 定义在æµæˆ–内存中使用的字符编ç ã€‚RapidJSON 也在内部æä¾› Unicode 转æ¢åŠæ ¡éªŒåŠŸèƒ½ã€‚
+3. [DOM](doc/dom.zh-cn.md) 的基本功能已在本教程里介ç»ã€‚还有更高级的功能,如原ä½ï¼ˆ*in situ*)解æžã€å…¶ä»–解æžé€‰é¡¹åŠé«˜çº§ç”¨æ³•ã€‚
+4. [SAX](doc/sax.zh-cn.md) 是 RapidJSON 解æžï¼ç”ŸæˆåŠŸèƒ½çš„基础。学习使用 `Reader`/`Writer` 去实现更高性能的应用程åºã€‚也å¯ä»¥ä½¿ç”¨ `PrettyWriter` 去格å¼åŒ– JSON。
+5. [性能](doc/performance.zh-cn.md) 展示一些我们åšçš„åŠç¬¬ä¸‰æ–¹çš„性能测试。
+6. [技术内幕](doc/internals.md) 讲述一些 RapidJSON 内部的设计åŠæŠ€æœ¯ã€‚
-你也å¯ä»¥å‚考[常è§é—®é¢˜](faq.zh-cn.md)ã€API文档ã€ä¾‹å­åŠå•å…ƒæµ‹è¯•ã€‚
+你也å¯ä»¥å‚考 [常è§é—®é¢˜](doc/faq.zh-cn.md)ã€API 文档ã€ä¾‹å­åŠå•å…ƒæµ‹è¯•ã€‚
diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt
index 8c546cf7..ff541993 100644
--- a/example/CMakeLists.txt
+++ b/example/CMakeLists.txt
@@ -1,9 +1,16 @@
cmake_minimum_required(VERSION 2.8)
+if(POLICY CMP0054)
+ cmake_policy(SET CMP0054 NEW)
+endif()
+
set(EXAMPLES
capitalize
condense
+ filterkey
+ filterkeydom
jsonx
+ lookaheadparser
messagereader
parsebyparts
pretty
@@ -12,21 +19,21 @@ set(EXAMPLES
serialize
simpledom
simplereader
+ simplepullreader
simplewriter
tutorial)
include_directories("../include/")
add_definitions(-D__STDC_FORMAT_MACROS)
+set_property(DIRECTORY PROPERTY COMPILE_OPTIONS ${EXTRA_CXX_FLAGS})
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
- set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread -Werror -Wall -Wextra -Weffc++ -Wswitch-default")
-elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
- set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror -Wall -Wextra -Weffc++ -Wswitch-default -Wfloat-equal -Wimplicit-fallthrough -Weverything")
-elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
- add_definitions(-D_CRT_SECURE_NO_WARNINGS=1)
+ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
endif()
+add_executable(archivertest archiver/archiver.cpp archiver/archivertest.cpp)
+
foreach (example ${EXAMPLES})
add_executable(${example} ${example}/${example}.cpp)
endforeach()
diff --git a/example/archiver/archiver.cpp b/example/archiver/archiver.cpp
new file mode 100644
index 00000000..59ae4c41
--- /dev/null
+++ b/example/archiver/archiver.cpp
@@ -0,0 +1,292 @@
+#include "archiver.h"
+#include <cassert>
+#include <stack>
+#include "rapidjson/document.h"
+#include "rapidjson/prettywriter.h"
+#include "rapidjson/stringbuffer.h"
+
+using namespace rapidjson;
+
+struct JsonReaderStackItem {
+ enum State {
+ BeforeStart, //!< An object/array is in the stack but it is not yet called by StartObject()/StartArray().
+ Started, //!< An object/array is called by StartObject()/StartArray().
+ Closed //!< An array is closed after read all element, but before EndArray().
+ };
+
+ JsonReaderStackItem(const Value* value, State state) : value(value), state(state), index() {}
+
+ const Value* value;
+ State state;
+ SizeType index; // For array iteration
+};
+
+typedef std::stack<JsonReaderStackItem> JsonReaderStack;
+
+#define DOCUMENT reinterpret_cast<Document*>(mDocument)
+#define STACK (reinterpret_cast<JsonReaderStack*>(mStack))
+#define TOP (STACK->top())
+#define CURRENT (*TOP.value)
+
+JsonReader::JsonReader(const char* json) : mDocument(), mStack(), mError(false) {
+ mDocument = new Document;
+ DOCUMENT->Parse(json);
+ if (DOCUMENT->HasParseError())
+ mError = true;
+ else {
+ mStack = new JsonReaderStack;
+ STACK->push(JsonReaderStackItem(DOCUMENT, JsonReaderStackItem::BeforeStart));
+ }
+}
+
+JsonReader::~JsonReader() {
+ delete DOCUMENT;
+ delete STACK;
+}
+
+// Archive concept
+JsonReader& JsonReader::StartObject() {
+ if (!mError) {
+ if (CURRENT.IsObject() && TOP.state == JsonReaderStackItem::BeforeStart)
+ TOP.state = JsonReaderStackItem::Started;
+ else
+ mError = true;
+ }
+ return *this;
+}
+
+JsonReader& JsonReader::EndObject() {
+ if (!mError) {
+ if (CURRENT.IsObject() && TOP.state == JsonReaderStackItem::Started)
+ Next();
+ else
+ mError = true;
+ }
+ return *this;
+}
+
+JsonReader& JsonReader::Member(const char* name) {
+ if (!mError) {
+ if (CURRENT.IsObject() && TOP.state == JsonReaderStackItem::Started) {
+ Value::ConstMemberIterator memberItr = CURRENT.FindMember(name);
+ if (memberItr != CURRENT.MemberEnd())
+ STACK->push(JsonReaderStackItem(&memberItr->value, JsonReaderStackItem::BeforeStart));
+ else
+ mError = true;
+ }
+ else
+ mError = true;
+ }
+ return *this;
+}
+
+bool JsonReader::HasMember(const char* name) const {
+ if (!mError && CURRENT.IsObject() && TOP.state == JsonReaderStackItem::Started)
+ return CURRENT.HasMember(name);
+ return false;
+}
+
+JsonReader& JsonReader::StartArray(size_t* size) {
+ if (!mError) {
+ if (CURRENT.IsArray() && TOP.state == JsonReaderStackItem::BeforeStart) {
+ TOP.state = JsonReaderStackItem::Started;
+ if (size)
+ *size = CURRENT.Size();
+
+ if (!CURRENT.Empty()) {
+ const Value* value = &CURRENT[TOP.index];
+ STACK->push(JsonReaderStackItem(value, JsonReaderStackItem::BeforeStart));
+ }
+ else
+ TOP.state = JsonReaderStackItem::Closed;
+ }
+ else
+ mError = true;
+ }
+ return *this;
+}
+
+JsonReader& JsonReader::EndArray() {
+ if (!mError) {
+ if (CURRENT.IsArray() && TOP.state == JsonReaderStackItem::Closed)
+ Next();
+ else
+ mError = true;
+ }
+ return *this;
+}
+
+JsonReader& JsonReader::operator&(bool& b) {
+ if (!mError) {
+ if (CURRENT.IsBool()) {
+ b = CURRENT.GetBool();
+ Next();
+ }
+ else
+ mError = true;
+ }
+ return *this;
+}
+
+JsonReader& JsonReader::operator&(unsigned& u) {
+ if (!mError) {
+ if (CURRENT.IsUint()) {
+ u = CURRENT.GetUint();
+ Next();
+ }
+ else
+ mError = true;
+ }
+ return *this;
+}
+
+JsonReader& JsonReader::operator&(int& i) {
+ if (!mError) {
+ if (CURRENT.IsInt()) {
+ i = CURRENT.GetInt();
+ Next();
+ }
+ else
+ mError = true;
+ }
+ return *this;
+}
+
+JsonReader& JsonReader::operator&(double& d) {
+ if (!mError) {
+ if (CURRENT.IsNumber()) {
+ d = CURRENT.GetDouble();
+ Next();
+ }
+ else
+ mError = true;
+ }
+ return *this;
+}
+
+JsonReader& JsonReader::operator&(std::string& s) {
+ if (!mError) {
+ if (CURRENT.IsString()) {
+ s = CURRENT.GetString();
+ Next();
+ }
+ else
+ mError = true;
+ }
+ return *this;
+}
+
+JsonReader& JsonReader::SetNull() {
+ // This function is for JsonWriter only.
+ mError = true;
+ return *this;
+}
+
+void JsonReader::Next() {
+ if (!mError) {
+ assert(!STACK->empty());
+ STACK->pop();
+
+ if (!STACK->empty() && CURRENT.IsArray()) {
+ if (TOP.state == JsonReaderStackItem::Started) { // Otherwise means reading array item pass end
+ if (TOP.index < CURRENT.Size() - 1) {
+ const Value* value = &CURRENT[++TOP.index];
+ STACK->push(JsonReaderStackItem(value, JsonReaderStackItem::BeforeStart));
+ }
+ else
+ TOP.state = JsonReaderStackItem::Closed;
+ }
+ else
+ mError = true;
+ }
+ }
+}
+
+#undef DOCUMENT
+#undef STACK
+#undef TOP
+#undef CURRENT
+
+////////////////////////////////////////////////////////////////////////////////
+// JsonWriter
+
+#define WRITER reinterpret_cast<PrettyWriter<StringBuffer>*>(mWriter)
+#define STREAM reinterpret_cast<StringBuffer*>(mStream)
+
+JsonWriter::JsonWriter() : mWriter(), mStream() {
+ mStream = new StringBuffer;
+ mWriter = new PrettyWriter<StringBuffer>(*STREAM);
+}
+
+JsonWriter::~JsonWriter() {
+ delete WRITER;
+ delete STREAM;
+}
+
+const char* JsonWriter::GetString() const {
+ return STREAM->GetString();
+}
+
+JsonWriter& JsonWriter::StartObject() {
+ WRITER->StartObject();
+ return *this;
+}
+
+JsonWriter& JsonWriter::EndObject() {
+ WRITER->EndObject();
+ return *this;
+}
+
+JsonWriter& JsonWriter::Member(const char* name) {
+ WRITER->String(name, static_cast<SizeType>(strlen(name)));
+ return *this;
+}
+
+bool JsonWriter::HasMember(const char*) const {
+ // This function is for JsonReader only.
+ assert(false);
+ return false;
+}
+
+JsonWriter& JsonWriter::StartArray(size_t*) {
+ WRITER->StartArray();
+ return *this;
+}
+
+JsonWriter& JsonWriter::EndArray() {
+ WRITER->EndArray();
+ return *this;
+}
+
+JsonWriter& JsonWriter::operator&(bool& b) {
+ WRITER->Bool(b);
+ return *this;
+}
+
+JsonWriter& JsonWriter::operator&(unsigned& u) {
+ WRITER->Uint(u);
+ return *this;
+}
+
+JsonWriter& JsonWriter::operator&(int& i) {
+ WRITER->Int(i);
+ return *this;
+}
+
+JsonWriter& JsonWriter::operator&(double& d) {
+ WRITER->Double(d);
+ return *this;
+}
+
+JsonWriter& JsonWriter::operator&(std::string& s) {
+ WRITER->String(s.c_str(), static_cast<SizeType>(s.size()));
+ return *this;
+}
+
+JsonWriter& JsonWriter::SetNull() {
+ WRITER->Null();
+ return *this;
+}
+
+#undef STREAM
+#undef WRITER
diff --git a/example/archiver/archiver.h b/example/archiver/archiver.h
new file mode 100644
index 00000000..c7e74f0c
--- /dev/null
+++ b/example/archiver/archiver.h
@@ -0,0 +1,139 @@
+#ifndef ARCHIVER_H_
+#define ARCHIVER_H_
+
+#include <cstddef>
+#include <string>
+
+/**
+\class Archiver
+\brief Archiver concept
+
+Archiver can be a reader or writer for serialization or deserialization respectively.
+
+class Archiver {
+public:
+ /// \returns true if the archiver is in normal state. false if it has errors.
+ operator bool() const;
+
+ /// Starts an object
+ Archiver& StartObject();
+
+ /// After calling StartObject(), assign a member with a name
+ Archiver& Member(const char* name);
+
+ /// After calling StartObject(), check if a member presents
+ bool HasMember(const char* name) const;
+
+ /// Ends an object
+ Archiver& EndObject();
+
+ /// Starts an array
+ /// \param size If Archiver::IsReader is true, the size of array is written.
+ Archiver& StartArray(size_t* size = 0);
+
+ /// Ends an array
+ Archiver& EndArray();
+
+ /// Read/Write primitive types.
+ Archiver& operator&(bool& b);
+ Archiver& operator&(unsigned& u);
+ Archiver& operator&(int& i);
+ Archiver& operator&(double& d);
+ Archiver& operator&(std::string& s);
+
+ /// Write primitive types.
+ Archiver& SetNull();
+
+ //! Whether it is a reader.
+ static const bool IsReader;
+
+ //! Whether it is a writer.
+ static const bool IsWriter;
+};
+*/
+
+/// Represents a JSON reader which implements Archiver concept.
+class JsonReader {
+public:
+ /// Constructor.
+ /**
+ \param json A non-const source json string for in-situ parsing.
+ \note in-situ means the source JSON string will be modified after parsing.
+ */
+ JsonReader(const char* json);
+
+ /// Destructor.
+ ~JsonReader();
+
+ // Archive concept
+
+ operator bool() const { return !mError; }
+
+ JsonReader& StartObject();
+ JsonReader& Member(const char* name);
+ bool HasMember(const char* name) const;
+ JsonReader& EndObject();
+
+ JsonReader& StartArray(size_t* size = nullptr);
+ JsonReader& EndArray();
+
+ JsonReader& operator&(bool& b);
+ JsonReader& operator&(unsigned& u);
+ JsonReader& operator&(int& i);
+ JsonReader& operator&(double& d);
+ JsonReader& operator&(std::string& s);
+
+ JsonReader& SetNull();
+
+ static const bool IsReader = true;
+ static const bool IsWriter = !IsReader;
+
+private:
+ void Next();
+
+ // PIMPL
+ void* mDocument; ///< DOM result of parsing.
+ void* mStack; ///< Stack for iterating the DOM
+ bool mError; ///< Whether an error is occured.
+};
+
+class JsonWriter {
+public:
+ /// Constructor.
+ JsonWriter();
+
+ /// Destructor.
+ ~JsonWriter();
+
+ /// Obtains the serialized JSON string.
+ const char* GetString() const;
+
+ // Archive concept
+
+ operator bool() const { return true; }
+
+ JsonWriter& StartObject();
+ JsonWriter& Member(const char* name);
+ bool HasMember(const char* name) const;
+ JsonWriter& EndObject();
+
+ JsonWriter& StartArray(size_t* size = 0);
+ JsonWriter& EndArray();
+
+ JsonWriter& operator&(bool& b);
+ JsonWriter& operator&(unsigned& u);
+ JsonWriter& operator&(int& i);
+ JsonWriter& operator&(double& d);
+ JsonWriter& operator&(std::string& s);
+ JsonWriter& SetNull();
+
+ static const bool IsReader = false;
+ static const bool IsWriter = !IsReader;
+
+private:
+ // PIMPL idiom
+ void* mWriter; ///< JSON writer.
+ void* mStream; ///< Stream buffer.
+};
+
+#endif // ARCHIVER_H__
diff --git a/example/archiver/archivertest.cpp b/example/archiver/archivertest.cpp
new file mode 100644
index 00000000..788db36e
--- /dev/null
+++ b/example/archiver/archivertest.cpp
@@ -0,0 +1,281 @@
+#include "archiver.h"
+#include <iostream>
+#include <vector>
+
+//////////////////////////////////////////////////////////////////////////////
+// Test1: simple object
+
+struct Student {
+ std::string name;
+ unsigned age;
+ double height;
+ bool canSwim;
+};
+
+template <typename Archiver>
+Archiver& operator&(Archiver& ar, Student& s) {
+ ar.StartObject();
+ ar.Member("name") & s.name;
+ ar.Member("age") & s.age;
+ ar.Member("height") & s.height;
+ ar.Member("canSwim") & s.canSwim;
+ return ar.EndObject();
+}
+
+std::ostream& operator<<(std::ostream& os, const Student& s) {
+ return os << s.name << " " << s.age << " " << s.height << " " << s.canSwim;
+}
+
+void test1() {
+ std::string json;
+
+ // Serialize
+ {
+ Student s = { "Lua", 9, 150.5, true };
+
+ JsonWriter writer;
+ writer & s;
+ json = writer.GetString();
+ std::cout << json << std::endl;
+ }
+
+ // Deserialize
+ {
+ Student s;
+ JsonReader reader(json.c_str());
+ reader & s;
+ std::cout << s << std::endl;
+ }
+}
+
+//////////////////////////////////////////////////////////////////////////////
+// Test2: std::vector <=> JSON array
+//
+// You can map a JSON array to other data structures as well
+
+struct Group {
+ std::string groupName;
+ std::vector<Student> students;
+};
+
+template <typename Archiver>
+Archiver& operator&(Archiver& ar, Group& g) {
+ ar.StartObject();
+
+ ar.Member("groupName");
+ ar & g.groupName;
+
+ ar.Member("students");
+ size_t studentCount = g.students.size();
+ ar.StartArray(&studentCount);
+ if (ar.IsReader)
+ g.students.resize(studentCount);
+ for (size_t i = 0; i < studentCount; i++)
+ ar & g.students[i];
+ ar.EndArray();
+
+ return ar.EndObject();
+}
+
+std::ostream& operator<<(std::ostream& os, const Group& g) {
+ os << g.groupName << std::endl;
+ for (std::vector<Student>::const_iterator itr = g.students.begin(); itr != g.students.end(); ++itr)
+ os << *itr << std::endl;
+ return os;
+}
+
+void test2() {
+ std::string json;
+
+ // Serialize
+ {
+ Group g;
+ g.groupName = "Rainbow";
+
+ Student s1 = { "Lua", 9, 150.5, true };
+ Student s2 = { "Mio", 7, 120.0, false };
+ g.students.push_back(s1);
+ g.students.push_back(s2);
+
+ JsonWriter writer;
+ writer & g;
+ json = writer.GetString();
+ std::cout << json << std::endl;
+ }
+
+ // Deserialize
+ {
+ Group g;
+ JsonReader reader(json.c_str());
+ reader & g;
+ std::cout << g << std::endl;
+ }
+}
+
+//////////////////////////////////////////////////////////////////////////////
+// Test3: polymorphism & friend
+//
+// Note that friendship is not necessary but make things simpler.
+
+class Shape {
+public:
+ virtual ~Shape() {}
+ virtual const char* GetType() const = 0;
+ virtual void Print(std::ostream& os) const = 0;
+
+protected:
+ Shape() {}
+ Shape(double x, double y) : x_(x), y_(y) {}
+
+ template <typename Archiver>
+ friend Archiver& operator&(Archiver& ar, Shape& s);
+
+ double x_, y_;
+};
+
+template <typename Archiver>
+Archiver& operator&(Archiver& ar, Shape& s) {
+ ar.Member("x") & s.x_;
+ ar.Member("y") & s.y_;
+ return ar;
+}
+
+class Circle : public Shape {
+public:
+ Circle() {}
+ Circle(double x, double y, double radius) : Shape(x, y), radius_(radius) {}
+ ~Circle() {}
+
+ const char* GetType() const { return "Circle"; }
+
+ void Print(std::ostream& os) const {
+ os << "Circle (" << x_ << ", " << y_ << ")" << " radius = " << radius_;
+ }
+
+private:
+ template <typename Archiver>
+ friend Archiver& operator&(Archiver& ar, Circle& c);
+
+ double radius_;
+};
+
+template <typename Archiver>
+Archiver& operator&(Archiver& ar, Circle& c) {
+ ar & static_cast<Shape&>(c);
+ ar.Member("radius") & c.radius_;
+ return ar;
+}
+
+class Box : public Shape {
+public:
+ Box() {}
+ Box(double x, double y, double width, double height) : Shape(x, y), width_(width), height_(height) {}
+ ~Box() {}
+
+ const char* GetType() const { return "Box"; }
+
+ void Print(std::ostream& os) const {
+ os << "Box (" << x_ << ", " << y_ << ")" << " width = " << width_ << " height = " << height_;
+ }
+
+private:
+ template <typename Archiver>
+ friend Archiver& operator&(Archiver& ar, Box& b);
+
+ double width_, height_;
+};
+
+template <typename Archiver>
+Archiver& operator&(Archiver& ar, Box& b) {
+ ar & static_cast<Shape&>(b);
+ ar.Member("width") & b.width_;
+ ar.Member("height") & b.height_;
+ return ar;
+}
+
+class Canvas {
+public:
+ Canvas() {}
+ ~Canvas() { Clear(); }
+
+ void Clear() {
+ for (std::vector<Shape*>::iterator itr = shapes_.begin(); itr != shapes_.end(); ++itr)
+ delete *itr;
+ }
+
+ void AddShape(Shape* shape) { shapes_.push_back(shape); }
+
+ void Print(std::ostream& os) {
+ for (std::vector<Shape*>::iterator itr = shapes_.begin(); itr != shapes_.end(); ++itr) {
+ (*itr)->Print(os);
+ std::cout << std::endl;
+ }
+ }
+
+private:
+ template <typename Archiver>
+ friend Archiver& operator&(Archiver& ar, Canvas& c);
+
+ std::vector<Shape*> shapes_;
+};
+
+template <typename Archiver>
+Archiver& operator&(Archiver& ar, Shape*& shape) {
+ std::string type = ar.IsReader ? "" : shape->GetType();
+ ar.StartObject();
+ ar.Member("type") & type;
+ if (type == "Circle") {
+ if (ar.IsReader) shape = new Circle;
+ ar & static_cast<Circle&>(*shape);
+ }
+ else if (type == "Box") {
+ if (ar.IsReader) shape = new Box;
+ ar & static_cast<Box&>(*shape);
+ }
+ return ar.EndObject();
+}
+
+template <typename Archiver>
+Archiver& operator&(Archiver& ar, Canvas& c) {
+ size_t shapeCount = c.shapes_.size();
+ ar.StartArray(&shapeCount);
+ if (ar.IsReader) {
+ c.Clear();
+ c.shapes_.resize(shapeCount);
+ }
+ for (size_t i = 0; i < shapeCount; i++)
+ ar & c.shapes_[i];
+ return ar.EndArray();
+}
+
+void test3() {
+ std::string json;
+
+ // Serialize
+ {
+ Canvas c;
+ c.AddShape(new Circle(1.0, 2.0, 3.0));
+ c.AddShape(new Box(4.0, 5.0, 6.0, 7.0));
+
+ JsonWriter writer;
+ writer & c;
+ json = writer.GetString();
+ std::cout << json << std::endl;
+ }
+
+ // Deserialize
+ {
+ Canvas c;
+ JsonReader reader(json.c_str());
+ reader & c;
+ c.Print(std::cout);
+ }
+}
+
+//////////////////////////////////////////////////////////////////////////////
+
+int main() {
+ test1();
+ test2();
+ test3();
+}
diff --git a/example/filterkey/filterkey.cpp b/example/filterkey/filterkey.cpp
new file mode 100644
index 00000000..c34a050d
--- /dev/null
+++ b/example/filterkey/filterkey.cpp
@@ -0,0 +1,135 @@
+// JSON filterkey example with SAX-style API.
+
+// This example parses JSON text from stdin with validation.
+// During parsing, specified key will be filtered using a SAX handler.
+// It re-output the JSON content to stdout without whitespace.
+
+#include "rapidjson/reader.h"
+#include "rapidjson/writer.h"
+#include "rapidjson/filereadstream.h"
+#include "rapidjson/filewritestream.h"
+#include "rapidjson/error/en.h"
+#include <stack>
+
+using namespace rapidjson;
+
+// This handler forwards event into an output handler, with filtering the descendent events of specified key.
+template <typename OutputHandler>
+class FilterKeyHandler {
+public:
+ typedef char Ch;
+
+ FilterKeyHandler(OutputHandler& outputHandler, const Ch* keyString, SizeType keyLength) :
+ outputHandler_(outputHandler), keyString_(keyString), keyLength_(keyLength), filterValueDepth_(), filteredKeyCount_()
+ {}
+
+ bool Null() { return filterValueDepth_ > 0 ? EndValue() : outputHandler_.Null() && EndValue(); }
+ bool Bool(bool b) { return filterValueDepth_ > 0 ? EndValue() : outputHandler_.Bool(b) && EndValue(); }
+ bool Int(int i) { return filterValueDepth_ > 0 ? EndValue() : outputHandler_.Int(i) && EndValue(); }
+ bool Uint(unsigned u) { return filterValueDepth_ > 0 ? EndValue() : outputHandler_.Uint(u) && EndValue(); }
+ bool Int64(int64_t i) { return filterValueDepth_ > 0 ? EndValue() : outputHandler_.Int64(i) && EndValue(); }
+ bool Uint64(uint64_t u) { return filterValueDepth_ > 0 ? EndValue() : outputHandler_.Uint64(u) && EndValue(); }
+ bool Double(double d) { return filterValueDepth_ > 0 ? EndValue() : outputHandler_.Double(d) && EndValue(); }
+ bool RawNumber(const Ch* str, SizeType len, bool copy) { return filterValueDepth_ > 0 ? EndValue() : outputHandler_.RawNumber(str, len, copy) && EndValue(); }
+ bool String (const Ch* str, SizeType len, bool copy) { return filterValueDepth_ > 0 ? EndValue() : outputHandler_.String (str, len, copy) && EndValue(); }
+
+ bool StartObject() {
+ if (filterValueDepth_ > 0) {
+ filterValueDepth_++;
+ return true;
+ }
+ else {
+ filteredKeyCount_.push(0);
+ return outputHandler_.StartObject();
+ }
+ }
+
+ bool Key(const Ch* str, SizeType len, bool copy) {
+ if (filterValueDepth_ > 0)
+ return true;
+ else if (len == keyLength_ && std::memcmp(str, keyString_, len) == 0) {
+ filterValueDepth_ = 1;
+ return true;
+ }
+ else {
+ ++filteredKeyCount_.top();
+ return outputHandler_.Key(str, len, copy);
+ }
+ }
+
+ bool EndObject(SizeType) {
+ if (filterValueDepth_ > 0) {
+ filterValueDepth_--;
+ return EndValue();
+ }
+ else {
+ // Use our own filtered memberCount
+ SizeType memberCount = filteredKeyCount_.top();
+ filteredKeyCount_.pop();
+ return outputHandler_.EndObject(memberCount) && EndValue();
+ }
+ }
+
+ bool StartArray() {
+ if (filterValueDepth_ > 0) {
+ filterValueDepth_++;
+ return true;
+ }
+ else
+ return outputHandler_.StartArray();
+ }
+
+ bool EndArray(SizeType elementCount) {
+ if (filterValueDepth_ > 0) {
+ filterValueDepth_--;
+ return EndValue();
+ }
+ else
+ return outputHandler_.EndArray(elementCount) && EndValue();
+ }
+
+private:
+ FilterKeyHandler(const FilterKeyHandler&);
+ FilterKeyHandler& operator=(const FilterKeyHandler&);
+
+ bool EndValue() {
+ if (filterValueDepth_ == 1) // Just at the end of value after filtered key
+ filterValueDepth_ = 0;
+ return true;
+ }
+
+ OutputHandler& outputHandler_;
+ const char* keyString_;
+ const SizeType keyLength_;
+ unsigned filterValueDepth_;
+ std::stack<SizeType> filteredKeyCount_;
+};
+
+int main(int argc, char* argv[]) {
+ if (argc != 2) {
+ fprintf(stderr, "filterkey key < input.json > output.json\n");
+ return 1;
+ }
+
+ // Prepare JSON reader and input stream.
+ Reader reader;
+ char readBuffer[65536];
+ FileReadStream is(stdin, readBuffer, sizeof(readBuffer));
+
+ // Prepare JSON writer and output stream.
+ char writeBuffer[65536];
+ FileWriteStream os(stdout, writeBuffer, sizeof(writeBuffer));
+ Writer<FileWriteStream> writer(os);
+
+ // Prepare Filter
+ FilterKeyHandler<Writer<FileWriteStream> > filter(writer, argv[1], static_cast<SizeType>(strlen(argv[1])));
+
+ // JSON reader parse from the input stream, filter handler filters the events, and forward to writer.
+ // i.e. the events flow is: reader -> filter -> writer
+ if (!reader.Parse(is, filter)) {
+ fprintf(stderr, "\nError(%u): %s\n", static_cast<unsigned>(reader.GetErrorOffset()), GetParseError_En(reader.GetParseErrorCode()));
+ return 1;
+ }
+
+ return 0;
+}
diff --git a/example/filterkeydom/filterkeydom.cpp b/example/filterkeydom/filterkeydom.cpp
new file mode 100644
index 00000000..732cc81f
--- /dev/null
+++ b/example/filterkeydom/filterkeydom.cpp
@@ -0,0 +1,170 @@
+// JSON filterkey example which populates filtered SAX events into a Document.
+
+// This example parses JSON text from stdin with validation.
+// During parsing, specified key will be filtered using a SAX handler.
+// And finally the filtered events are used to populate a Document.
+// As an example, the document is written to standard output.
+
+#include "rapidjson/document.h"
+#include "rapidjson/writer.h"
+#include "rapidjson/filereadstream.h"
+#include "rapidjson/filewritestream.h"
+#include "rapidjson/error/en.h"
+#include <stack>
+
+using namespace rapidjson;
+
+// This handler forwards event into an output handler, with filtering the descendent events of specified key.
+template <typename OutputHandler>
+class FilterKeyHandler {
+public:
+ typedef char Ch;
+
+ FilterKeyHandler(OutputHandler& outputHandler, const Ch* keyString, SizeType keyLength) :
+ outputHandler_(outputHandler), keyString_(keyString), keyLength_(keyLength), filterValueDepth_(), filteredKeyCount_()
+ {}
+
+ bool Null() { return filterValueDepth_ > 0 ? EndValue() : outputHandler_.Null() && EndValue(); }
+ bool Bool(bool b) { return filterValueDepth_ > 0 ? EndValue() : outputHandler_.Bool(b) && EndValue(); }
+ bool Int(int i) { return filterValueDepth_ > 0 ? EndValue() : outputHandler_.Int(i) && EndValue(); }
+ bool Uint(unsigned u) { return filterValueDepth_ > 0 ? EndValue() : outputHandler_.Uint(u) && EndValue(); }
+ bool Int64(int64_t i) { return filterValueDepth_ > 0 ? EndValue() : outputHandler_.Int64(i) && EndValue(); }
+ bool Uint64(uint64_t u) { return filterValueDepth_ > 0 ? EndValue() : outputHandler_.Uint64(u) && EndValue(); }
+ bool Double(double d) { return filterValueDepth_ > 0 ? EndValue() : outputHandler_.Double(d) && EndValue(); }
+ bool RawNumber(const Ch* str, SizeType len, bool copy) { return filterValueDepth_ > 0 ? EndValue() : outputHandler_.RawNumber(str, len, copy) && EndValue(); }
+ bool String (const Ch* str, SizeType len, bool copy) { return filterValueDepth_ > 0 ? EndValue() : outputHandler_.String (str, len, copy) && EndValue(); }
+
+ bool StartObject() {
+ if (filterValueDepth_ > 0) {
+ filterValueDepth_++;
+ return true;
+ }
+ else {
+ filteredKeyCount_.push(0);
+ return outputHandler_.StartObject();
+ }
+ }
+
+ bool Key(const Ch* str, SizeType len, bool copy) {
+ if (filterValueDepth_ > 0)
+ return true;
+ else if (len == keyLength_ && std::memcmp(str, keyString_, len) == 0) {
+ filterValueDepth_ = 1;
+ return true;
+ }
+ else {
+ ++filteredKeyCount_.top();
+ return outputHandler_.Key(str, len, copy);
+ }
+ }
+
+ bool EndObject(SizeType) {
+ if (filterValueDepth_ > 0) {
+ filterValueDepth_--;
+ return EndValue();
+ }
+ else {
+ // Use our own filtered memberCount
+ SizeType memberCount = filteredKeyCount_.top();
+ filteredKeyCount_.pop();
+ return outputHandler_.EndObject(memberCount) && EndValue();
+ }
+ }
+
+ bool StartArray() {
+ if (filterValueDepth_ > 0) {
+ filterValueDepth_++;
+ return true;
+ }
+ else
+ return outputHandler_.StartArray();
+ }
+
+ bool EndArray(SizeType elementCount) {
+ if (filterValueDepth_ > 0) {
+ filterValueDepth_--;
+ return EndValue();
+ }
+ else
+ return outputHandler_.EndArray(elementCount) && EndValue();
+ }
+
+private:
+ FilterKeyHandler(const FilterKeyHandler&);
+ FilterKeyHandler& operator=(const FilterKeyHandler&);
+
+ bool EndValue() {
+ if (filterValueDepth_ == 1) // Just at the end of value after filtered key
+ filterValueDepth_ = 0;
+ return true;
+ }
+
+ OutputHandler& outputHandler_;
+ const char* keyString_;
+ const SizeType keyLength_;
+ unsigned filterValueDepth_;
+ std::stack<SizeType> filteredKeyCount_;
+};
+
+// Implements a generator for Document::Populate()
+template <typename InputStream>
+class FilterKeyReader {
+public:
+ typedef char Ch;
+
+ FilterKeyReader(InputStream& is, const Ch* keyString, SizeType keyLength) :
+ is_(is), keyString_(keyString), keyLength_(keyLength), parseResult_()
+ {}
+
+ // SAX event flow: reader -> filter -> handler
+ template <typename Handler>
+ bool operator()(Handler& handler) {
+ FilterKeyHandler<Handler> filter(handler, keyString_, keyLength_);
+ Reader reader;
+ parseResult_ = reader.Parse(is_, filter);
+ return parseResult_;
+ }
+
+ const ParseResult& GetParseResult() const { return parseResult_; }
+
+private:
+ FilterKeyReader(const FilterKeyReader&);
+ FilterKeyReader& operator=(const FilterKeyReader&);
+
+ InputStream& is_;
+ const char* keyString_;
+ const SizeType keyLength_;
+ ParseResult parseResult_;
+};
+
+int main(int argc, char* argv[]) {
+ if (argc != 2) {
+ fprintf(stderr, "filterkeydom key < input.json > output.json\n");
+ return 1;
+ }
+
+ // Prepare input stream.
+ char readBuffer[65536];
+ FileReadStream is(stdin, readBuffer, sizeof(readBuffer));
+
+ // Prepare Filter
+ FilterKeyReader<FileReadStream> reader(is, argv[1], static_cast<SizeType>(strlen(argv[1])));
+
+ // Populates the filtered events from reader
+ Document document;
+ document.Populate(reader);
+ ParseResult pr = reader.GetParseResult();
+ if (!pr) {
+ fprintf(stderr, "\nError(%u): %s\n", static_cast<unsigned>(pr.Offset()), GetParseError_En(pr.Code()));
+ return 1;
+ }
+
+ // Prepare JSON writer and output stream.
+ char writeBuffer[65536];
+ FileWriteStream os(stdout, writeBuffer, sizeof(writeBuffer));
+ Writer<FileWriteStream> writer(os);
+
+ // Write the document to standard output
+ document.Accept(writer);
+ return 0;
+}
diff --git a/example/lookaheadparser/lookaheadparser.cpp b/example/lookaheadparser/lookaheadparser.cpp
new file mode 100644
index 00000000..f627f4d8
--- /dev/null
+++ b/example/lookaheadparser/lookaheadparser.cpp
@@ -0,0 +1,350 @@
+#include "rapidjson/reader.h"
+#include "rapidjson/document.h"
+#include <iostream>
+
+RAPIDJSON_DIAG_PUSH
+#ifdef __GNUC__
+RAPIDJSON_DIAG_OFF(effc++)
+#endif
+
+// This example demonstrates JSON token-by-token parsing with an API that is
+// more direct; you don't need to design your logic around a handler object and
+// callbacks. Instead, you retrieve values from the JSON stream by calling
+// GetInt(), GetDouble(), GetString() and GetBool(), traverse into structures
+// by calling EnterObject() and EnterArray(), and skip over unwanted data by
+// calling SkipValue(). When you know your JSON's structure, this can be quite
+// convenient.
+//
+// If you aren't sure of what's next in the JSON data, you can use PeekType() and
+// PeekValue() to look ahead to the next object before reading it.
+//
+// If you call the wrong retrieval method--e.g. GetInt when the next JSON token is
+// not an int, EnterObject or EnterArray when there isn't actually an object or array
+// to read--the stream parsing will end immediately and no more data will be delivered.
+//
+// After calling EnterObject, you retrieve keys via NextObjectKey() and values via
+// the normal getters. When NextObjectKey() returns null, you have exited the
+// object, or you can call SkipObject() to skip to the end of the object
+// immediately. If you fetch the entire object (i.e. NextObjectKey() returned null),
+// you should not call SkipObject().
+//
+// After calling EnterArray(), you must alternate between calling NextArrayValue()
+// to see if the array has more data, and then retrieving values via the normal
+// getters. You can call SkipArray() to skip to the end of the array immediately.
+// If you fetch the entire array (i.e. NextArrayValue() returned null),
+// you should not call SkipArray().
+//
+// This parser uses in-situ strings, so the JSON buffer will be altered during the
+// parse.
+
+using namespace rapidjson;
+
+
+class LookaheadParserHandler {
+public:
+ bool Null() { st_ = kHasNull; v_.SetNull(); return true; }
+ bool Bool(bool b) { st_ = kHasBool; v_.SetBool(b); return true; }
+ bool Int(int i) { st_ = kHasNumber; v_.SetInt(i); return true; }
+ bool Uint(unsigned u) { st_ = kHasNumber; v_.SetUint(u); return true; }
+ bool Int64(int64_t i) { st_ = kHasNumber; v_.SetInt64(i); return true; }
+ bool Uint64(uint64_t u) { st_ = kHasNumber; v_.SetUint64(u); return true; }
+ bool Double(double d) { st_ = kHasNumber; v_.SetDouble(d); return true; }
+ bool RawNumber(const char*, SizeType, bool) { return false; }
+ bool String(const char* str, SizeType length, bool) { st_ = kHasString; v_.SetString(str, length); return true; }
+ bool StartObject() { st_ = kEnteringObject; return true; }
+ bool Key(const char* str, SizeType length, bool) { st_ = kHasKey; v_.SetString(str, length); return true; }
+ bool EndObject(SizeType) { st_ = kExitingObject; return true; }
+ bool StartArray() { st_ = kEnteringArray; return true; }
+ bool EndArray(SizeType) { st_ = kExitingArray; return true; }
+
+protected:
+ LookaheadParserHandler(char* str);
+ void ParseNext();
+
+protected:
+ enum LookaheadParsingState {
+ kInit,
+ kError,
+ kHasNull,
+ kHasBool,
+ kHasNumber,
+ kHasString,
+ kHasKey,
+ kEnteringObject,
+ kExitingObject,
+ kEnteringArray,
+ kExitingArray
+ };
+
+ Value v_;
+ LookaheadParsingState st_;
+ Reader r_;
+ InsituStringStream ss_;
+
+ static const int parseFlags = kParseDefaultFlags | kParseInsituFlag;
+};
+
+LookaheadParserHandler::LookaheadParserHandler(char* str) : v_(), st_(kInit), r_(), ss_(str) {
+ r_.IterativeParseInit();
+ ParseNext();
+}
+
+void LookaheadParserHandler::ParseNext() {
+ if (r_.HasParseError()) {
+ st_ = kError;
+ return;
+ }
+
+ r_.IterativeParseNext<parseFlags>(ss_, *this);
+}
+
+class LookaheadParser : protected LookaheadParserHandler {
+public:
+ LookaheadParser(char* str) : LookaheadParserHandler(str) {}
+
+ bool EnterObject();
+ bool EnterArray();
+ const char* NextObjectKey();
+ bool NextArrayValue();
+ int GetInt();
+ double GetDouble();
+ const char* GetString();
+ bool GetBool();
+ void GetNull();
+
+ void SkipObject();
+ void SkipArray();
+ void SkipValue();
+ Value* PeekValue();
+ int PeekType(); // returns a rapidjson::Type, or -1 for no value (at end of object/array)
+
+ bool IsValid() { return st_ != kError; }
+
+protected:
+ void SkipOut(int depth);
+};
+
+bool LookaheadParser::EnterObject() {
+ if (st_ != kEnteringObject) {
+ st_ = kError;
+ return false;
+ }
+
+ ParseNext();
+ return true;
+}
+
+bool LookaheadParser::EnterArray() {
+ if (st_ != kEnteringArray) {
+ st_ = kError;
+ return false;
+ }
+
+ ParseNext();
+ return true;
+}
+
+const char* LookaheadParser::NextObjectKey() {
+ if (st_ == kHasKey) {
+ const char* result = v_.GetString();
+ ParseNext();
+ return result;
+ }
+
+ if (st_ != kExitingObject) {
+ st_ = kError;
+ return 0;
+ }
+
+ ParseNext();
+ return 0;
+}
+
+bool LookaheadParser::NextArrayValue() {
+ if (st_ == kExitingArray) {
+ ParseNext();
+ return false;
+ }
+
+ if (st_ == kError || st_ == kExitingObject || st_ == kHasKey) {
+ st_ = kError;
+ return false;
+ }
+
+ return true;
+}
+
+int LookaheadParser::GetInt() {
+ if (st_ != kHasNumber || !v_.IsInt()) {
+ st_ = kError;
+ return 0;
+ }
+
+ int result = v_.GetInt();
+ ParseNext();
+ return result;
+}
+
+double LookaheadParser::GetDouble() {
+ if (st_ != kHasNumber) {
+ st_ = kError;
+ return 0.;
+ }
+
+ double result = v_.GetDouble();
+ ParseNext();
+ return result;
+}
+
+bool LookaheadParser::GetBool() {
+ if (st_ != kHasBool) {
+ st_ = kError;
+ return false;
+ }
+
+ bool result = v_.GetBool();
+ ParseNext();
+ return result;
+}
+
+void LookaheadParser::GetNull() {
+ if (st_ != kHasNull) {
+ st_ = kError;
+ return;
+ }
+
+ ParseNext();
+}
+
+const char* LookaheadParser::GetString() {
+ if (st_ != kHasString) {
+ st_ = kError;
+ return 0;
+ }
+
+ const char* result = v_.GetString();
+ ParseNext();
+ return result;
+}
+
+void LookaheadParser::SkipOut(int depth) {
+ do {
+ if (st_ == kEnteringArray || st_ == kEnteringObject) {
+ ++depth;
+ }
+ else if (st_ == kExitingArray || st_ == kExitingObject) {
+ --depth;
+ }
+ else if (st_ == kError) {
+ return;
+ }
+
+ ParseNext();
+ }
+ while (depth > 0);
+}
+
+void LookaheadParser::SkipValue() {
+ SkipOut(0);
+}
+
+void LookaheadParser::SkipArray() {
+ SkipOut(1);
+}
+
+void LookaheadParser::SkipObject() {
+ SkipOut(1);
+}
+
+Value* LookaheadParser::PeekValue() {
+ if (st_ >= kHasNull && st_ <= kHasKey) {
+ return &v_;
+ }
+
+ return 0;
+}
+
+int LookaheadParser::PeekType() {
+ if (st_ >= kHasNull && st_ <= kHasKey) {
+ return v_.GetType();
+ }
+
+ if (st_ == kEnteringArray) {
+ return kArrayType;
+ }
+
+ if (st_ == kEnteringObject) {
+ return kObjectType;
+ }
+
+ return -1;
+}
+
+//-------------------------------------------------------------------------
+
+int main() {
+ using namespace std;
+
+ char json[] = " { \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null,"
+ "\"i\":123, \"pi\": 3.1416, \"a\":[-1, 2, 3, 4, \"array\", []], \"skipArrays\":[1, 2, [[[3]]]], "
+ "\"skipObject\":{ \"i\":0, \"t\":true, \"n\":null, \"d\":123.45 }, "
+ "\"skipNested\":[[[[{\"\":0}, {\"\":[-9.87]}]]], [], []], "
+ "\"skipString\":\"zzz\", \"reachedEnd\":null, \"t\":true }";
+
+ LookaheadParser r(json);
+
+ RAPIDJSON_ASSERT(r.PeekType() == kObjectType);
+
+ r.EnterObject();
+ while (const char* key = r.NextObjectKey()) {
+ if (0 == strcmp(key, "hello")) {
+ RAPIDJSON_ASSERT(r.PeekType() == kStringType);
+ cout << key << ":" << r.GetString() << endl;
+ }
+ else if (0 == strcmp(key, "t") || 0 == strcmp(key, "f")) {
+ RAPIDJSON_ASSERT(r.PeekType() == kTrueType || r.PeekType() == kFalseType);
+ cout << key << ":" << r.GetBool() << endl;
+ continue;
+ }
+ else if (0 == strcmp(key, "n")) {
+ RAPIDJSON_ASSERT(r.PeekType() == kNullType);
+ r.GetNull();
+ cout << key << endl;
+ continue;
+ }
+ else if (0 == strcmp(key, "pi")) {
+ RAPIDJSON_ASSERT(r.PeekType() == kNumberType);
+ cout << key << ":" << r.GetDouble() << endl;
+ continue;
+ }
+ else if (0 == strcmp(key, "a")) {
+ RAPIDJSON_ASSERT(r.PeekType() == kArrayType);
+
+ r.EnterArray();
+
+ cout << key << ":[ ";
+ while (r.NextArrayValue()) {
+ if (r.PeekType() == kNumberType) {
+ cout << r.GetDouble() << " ";
+ }
+ else if (r.PeekType() == kStringType) {
+ cout << r.GetString() << " ";
+ }
+ else {
+ r.SkipArray();
+ break;
+ }
+ }
+
+ cout << "]" << endl;
+ }
+ else {
+ cout << key << ":skipped" << endl;
+ r.SkipValue();
+ }
+ }
+
+ return 0;
+}
+
+RAPIDJSON_DIAG_POP
diff --git a/example/parsebyparts/parsebyparts.cpp b/example/parsebyparts/parsebyparts.cpp
index 919d9083..a377efd4 100644
--- a/example/parsebyparts/parsebyparts.cpp
+++ b/example/parsebyparts/parsebyparts.cpp
@@ -1,7 +1,8 @@
// Example of parsing JSON to document by parts.
// Using C++11 threads
-#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1700)
+// Temporarily disable for clang (older version) due to incompatibility with libstdc++
+#if (__cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1700)) && !defined(__clang__)
#include "rapidjson/document.h"
#include "rapidjson/error/en.h"
@@ -20,12 +21,15 @@ public:
AsyncDocumentParser(Document& d)
: stream_(*this)
, d_(d)
- , parseThread_(&AsyncDocumentParser::Parse, this)
+ , parseThread_()
, mutex_()
, notEmpty_()
, finish_()
, completed_()
- {}
+ {
+ // Create and execute thread after all member variables are initialized.
+ parseThread_ = std::thread(&AsyncDocumentParser::Parse, this);
+ }
~AsyncDocumentParser() {
if (!parseThread_.joinable())
diff --git a/example/simplepullreader/simplepullreader.cpp b/example/simplepullreader/simplepullreader.cpp
new file mode 100644
index 00000000..a4fb1161
--- /dev/null
+++ b/example/simplepullreader/simplepullreader.cpp
@@ -0,0 +1,53 @@
+#include "rapidjson/reader.h"
+#include <iostream>
+#include <sstream>
+
+using namespace rapidjson;
+using namespace std;
+
+// If you can require C++11, you could use std::to_string here
+template <typename T> std::string stringify(T x) {
+ std::stringstream ss;
+ ss << x;
+ return ss.str();
+}
+
+struct MyHandler {
+ const char* type;
+ std::string data;
+
+ MyHandler() : type(), data() {}
+
+ bool Null() { type = "Null"; data.clear(); return true; }
+ bool Bool(bool b) { type = "Bool:"; data = b? "true": "false"; return true; }
+ bool Int(int i) { type = "Int:"; data = stringify(i); return true; }
+ bool Uint(unsigned u) { type = "Uint:"; data = stringify(u); return true; }
+ bool Int64(int64_t i) { type = "Int64:"; data = stringify(i); return true; }
+ bool Uint64(uint64_t u) { type = "Uint64:"; data = stringify(u); return true; }
+ bool Double(double d) { type = "Double:"; data = stringify(d); return true; }
+ bool RawNumber(const char* str, SizeType length, bool) { type = "Number:"; data = std::string(str, length); return true; }
+ bool String(const char* str, SizeType length, bool) { type = "String:"; data = std::string(str, length); return true; }
+ bool StartObject() { type = "StartObject"; data.clear(); return true; }
+ bool Key(const char* str, SizeType length, bool) { type = "Key:"; data = std::string(str, length); return true; }
+ bool EndObject(SizeType memberCount) { type = "EndObject:"; data = stringify(memberCount); return true; }
+ bool StartArray() { type = "StartArray"; data.clear(); return true; }
+ bool EndArray(SizeType elementCount) { type = "EndArray:"; data = stringify(elementCount); return true; }
+private:
+ MyHandler(const MyHandler& noCopyConstruction);
+ MyHandler& operator=(const MyHandler& noAssignment);
+};
+
+int main() {
+ const char json[] = " { \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3, 4] } ";
+
+ MyHandler handler;
+ Reader reader;
+ StringStream ss(json);
+ reader.IterativeParseInit();
+ while (!reader.IterativeParseComplete()) {
+ reader.IterativeParseNext<kParseDefaultFlags>(ss, handler);
+ cout << handler.type << handler.data << endl;
+ }
+
+ return 0;
+}
diff --git a/example/simplewriter/simplewriter.cpp b/example/simplewriter/simplewriter.cpp
index f8891504..8d1275c2 100644
--- a/example/simplewriter/simplewriter.cpp
+++ b/example/simplewriter/simplewriter.cpp
@@ -9,26 +9,27 @@ int main() {
StringBuffer s;
Writer<StringBuffer> writer(s);
- writer.StartObject();
- writer.String("hello");
- writer.String("world");
- writer.String("t");
+ writer.StartObject(); // Between StartObject()/EndObject(),
+ writer.Key("hello"); // output a key,
+ writer.String("world"); // follow by a value.
+ writer.Key("t");
writer.Bool(true);
- writer.String("f");
+ writer.Key("f");
writer.Bool(false);
- writer.String("n");
+ writer.Key("n");
writer.Null();
- writer.String("i");
+ writer.Key("i");
writer.Uint(123);
- writer.String("pi");
+ writer.Key("pi");
writer.Double(3.1416);
- writer.String("a");
- writer.StartArray();
+ writer.Key("a");
+ writer.StartArray(); // Between StartArray()/EndArray(),
for (unsigned i = 0; i < 4; i++)
- writer.Uint(i);
+ writer.Uint(i); // all values are elements of the array.
writer.EndArray();
writer.EndObject();
+ // {"hello":"world","t":true,"f":false,"n":null,"i":123,"pi":3.1416,"a":[0,1,2,3]}
cout << s.GetString() << endl;
return 0;
diff --git a/include/rapidjson/allocators.h b/include/rapidjson/allocators.h
index c7059697..655f4a38 100644
--- a/include/rapidjson/allocators.h
+++ b/include/rapidjson/allocators.h
@@ -179,7 +179,8 @@ public:
size = RAPIDJSON_ALIGN(size);
if (chunkHead_ == 0 || chunkHead_->size + size > chunkHead_->capacity)
- AddChunk(chunk_capacity_ > size ? chunk_capacity_ : size);
+ if (!AddChunk(chunk_capacity_ > size ? chunk_capacity_ : size))
+ return NULL;
void *buffer = reinterpret_cast<char *>(chunkHead_) + RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + chunkHead_->size;
chunkHead_->size += size;
@@ -211,11 +212,13 @@ public:
}
// Realloc process: allocate and copy memory, do not free original buffer.
- void* newBuffer = Malloc(newSize);
- RAPIDJSON_ASSERT(newBuffer != 0); // Do not handle out-of-memory explicitly.
- if (originalSize)
- std::memcpy(newBuffer, originalPtr, originalSize);
- return newBuffer;
+ if (void* newBuffer = Malloc(newSize)) {
+ if (originalSize)
+ std::memcpy(newBuffer, originalPtr, originalSize);
+ return newBuffer;
+ }
+ else
+ return NULL;
}
//! Frees a memory block (concept Allocator)
@@ -229,15 +232,20 @@ private:
//! Creates a new chunk.
/*! \param capacity Capacity of the chunk in bytes.
+ \return true if success.
*/
- void AddChunk(size_t capacity) {
+ bool AddChunk(size_t capacity) {
if (!baseAllocator_)
- ownBaseAllocator_ = baseAllocator_ = RAPIDJSON_NEW(BaseAllocator());
- ChunkHeader* chunk = reinterpret_cast<ChunkHeader*>(baseAllocator_->Malloc(RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + capacity));
- chunk->capacity = capacity;
- chunk->size = 0;
- chunk->next = chunkHead_;
- chunkHead_ = chunk;
+ ownBaseAllocator_ = baseAllocator_ = RAPIDJSON_NEW(BaseAllocator)();
+ if (ChunkHeader* chunk = reinterpret_cast<ChunkHeader*>(baseAllocator_->Malloc(RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + capacity))) {
+ chunk->capacity = capacity;
+ chunk->size = 0;
+ chunk->next = chunkHead_;
+ chunkHead_ = chunk;
+ return true;
+ }
+ else
+ return false;
}
static const int kDefaultChunkCapacity = 64 * 1024; //!< Default chunk capacity.
diff --git a/include/rapidjson/cursorstreamwrapper.h b/include/rapidjson/cursorstreamwrapper.h
new file mode 100644
index 00000000..52c11a7c
--- /dev/null
+++ b/include/rapidjson/cursorstreamwrapper.h
@@ -0,0 +1,78 @@
+// Tencent is pleased to support the open source community by making RapidJSON available.
+//
+// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
+//
+// Licensed under the MIT License (the "License"); you may not use this file except
+// in compliance with the License. You may obtain a copy of the License at
+//
+// http://opensource.org/licenses/MIT
+//
+// Unless required by applicable law or agreed to in writing, software distributed
+// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
+// CONDITIONS OF ANY KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations under the License.
+
+#ifndef RAPIDJSON_CURSORSTREAMWRAPPER_H_
+#define RAPIDJSON_CURSORSTREAMWRAPPER_H_
+
+#include "stream.h"
+
+#if defined(__GNUC__)
+RAPIDJSON_DIAG_PUSH
+RAPIDJSON_DIAG_OFF(effc++)
+#endif
+
+#if defined(_MSC_VER) && _MSC_VER <= 1800
+RAPIDJSON_DIAG_PUSH
+RAPIDJSON_DIAG_OFF(4702) // unreachable code
+RAPIDJSON_DIAG_OFF(4512) // assignment operator could not be generated
+#endif
+
+RAPIDJSON_NAMESPACE_BEGIN
+
+
+//! Cursor stream wrapper for counting line and column number if error exists.
+/*!
+ \tparam InputStream Any stream that implements Stream Concept
+*/
+template <typename InputStream, typename Encoding = UTF8<> >
+class CursorStreamWrapper : public GenericStreamWrapper<InputStream, Encoding> {
+public:
+ typedef typename Encoding::Ch Ch;
+
+ CursorStreamWrapper(InputStream& is):
+ GenericStreamWrapper<InputStream, Encoding>(is), line_(1), col_(0) {}
+
+ // counting line and column number
+ Ch Take() {
+ Ch ch = this->is_.Take();
+ if(ch == '\n') {
+ line_ ++;
+ col_ = 0;
+ } else {
+ col_ ++;
+ }
+ return ch;
+ }
+
+ //! Get the error line number, if error exists.
+ size_t GetLine() const { return line_; }
+ //! Get the error column number, if error exists.
+ size_t GetColumn() const { return col_; }
+
+private:
+ size_t line_; //!< Current Line
+ size_t col_; //!< Current Column
+};
+
+#if defined(_MSC_VER) && _MSC_VER <= 1800
+RAPIDJSON_DIAG_POP
+#endif
+
+#if defined(__GNUC__)
+RAPIDJSON_DIAG_POP
+#endif
+
+RAPIDJSON_NAMESPACE_END
+
+#endif // RAPIDJSON_CURSORSTREAMWRAPPER_H_
diff --git a/include/rapidjson/document.h b/include/rapidjson/document.h
index d1f1b6f5..eb6d7dcb 100644
--- a/include/rapidjson/document.h
+++ b/include/rapidjson/document.h
@@ -23,26 +23,29 @@
#include "memorystream.h"
#include "encodedstream.h"
#include <new> // placement new
+#include <limits>
-#ifdef _MSC_VER
RAPIDJSON_DIAG_PUSH
+#ifdef _MSC_VER
RAPIDJSON_DIAG_OFF(4127) // conditional expression is constant
+RAPIDJSON_DIAG_OFF(4244) // conversion from kXxxFlags to 'uint16_t', possible loss of data
#endif
#ifdef __clang__
-RAPIDJSON_DIAG_PUSH
RAPIDJSON_DIAG_OFF(padded)
RAPIDJSON_DIAG_OFF(switch-enum)
RAPIDJSON_DIAG_OFF(c++98-compat)
#endif
#ifdef __GNUC__
-RAPIDJSON_DIAG_PUSH
RAPIDJSON_DIAG_OFF(effc++)
+#if __GNUC__ >= 6
+RAPIDJSON_DIAG_OFF(terminate) // ignore throwing RAPIDJSON_ASSERT in RAPIDJSON_NOEXCEPT functions
#endif
+#endif // __GNUC__
#ifndef RAPIDJSON_NOMEMBERITERATORCLASS
-#include <iterator> // std::iterator, std::random_access_iterator_tag
+#include <iterator> // std::random_access_iterator_tag
#endif
#if RAPIDJSON_HAS_CXX11_RVALUE_REFS
@@ -95,16 +98,13 @@ struct GenericMember {
\see GenericMember, GenericValue::MemberIterator, GenericValue::ConstMemberIterator
*/
template <bool Const, typename Encoding, typename Allocator>
-class GenericMemberIterator
- : public std::iterator<std::random_access_iterator_tag
- , typename internal::MaybeAddConst<Const,GenericMember<Encoding,Allocator> >::Type> {
+class GenericMemberIterator {
friend class GenericValue<Encoding,Allocator>;
template <bool, typename, typename> friend class GenericMemberIterator;
typedef GenericMember<Encoding,Allocator> PlainType;
typedef typename internal::MaybeAddConst<Const,PlainType>::Type ValueType;
- typedef std::iterator<std::random_access_iterator_tag,ValueType> BaseType;
public:
//! Iterator type itself
@@ -114,12 +114,21 @@ public:
//! Non-constant iterator type
typedef GenericMemberIterator<false,Encoding,Allocator> NonConstIterator;
+ /** \name std::iterator_traits support */
+ //@{
+ typedef ValueType value_type;
+ typedef ValueType * pointer;
+ typedef ValueType & reference;
+ typedef std::ptrdiff_t difference_type;
+ typedef std::random_access_iterator_tag iterator_category;
+ //@}
+
//! Pointer to (const) GenericMember
- typedef typename BaseType::pointer Pointer;
+ typedef pointer Pointer;
//! Reference to (const) GenericMember
- typedef typename BaseType::reference Reference;
+ typedef reference Reference;
//! Signed integer type (e.g. \c ptrdiff_t)
- typedef typename BaseType::difference_type DifferenceType;
+ typedef difference_type DifferenceType;
//! Default constructor (singular value)
/*! Creates an iterator pointing to no element.
@@ -297,7 +306,7 @@ struct GenericStringRef {
*/
#endif
explicit GenericStringRef(const CharType* str)
- : s(str), length(internal::StrLen(str)){ RAPIDJSON_ASSERT(s != 0); }
+ : s(str), length(NotNullStrLen(str)) {}
//! Create constant string reference from pointer and length
#ifndef __clang__ // -Wdocumentation
@@ -309,7 +318,9 @@ struct GenericStringRef {
*/
#endif
GenericStringRef(const CharType* str, SizeType len)
- : s(str), length(len) { RAPIDJSON_ASSERT(s != 0); }
+ : s(RAPIDJSON_LIKELY(str) ? str : emptyString), length(len) { RAPIDJSON_ASSERT(str != 0 || len == 0u); }
+
+ GenericStringRef(const GenericStringRef& rhs) : s(rhs.s), length(rhs.length) {}
//! implicit conversion to plain CharType pointer
operator const Ch *() const { return s; }
@@ -318,11 +329,24 @@ struct GenericStringRef {
const SizeType length; //!< length of the string (excluding the trailing NULL terminator)
private:
+ SizeType NotNullStrLen(const CharType* str) {
+ RAPIDJSON_ASSERT(str != 0);
+ return internal::StrLen(str);
+ }
+
+ /// Empty string - used when passing in a NULL pointer
+ static const Ch emptyString[];
+
//! Disallow construction from non-const array
template<SizeType N>
GenericStringRef(CharType (&str)[N]) /* = delete */;
+ //! Copy assignment operator not permitted - immutable type
+ GenericStringRef& operator=(const GenericStringRef& rhs) /* = delete */;
};
+template<typename CharType>
+const CharType GenericStringRef<CharType>::emptyString[] = { CharType() };
+
//! Mark a character pointer as constant string
/*! Mark a plain character pointer as a "string literal". This function
can be used to avoid copying a character string to be referenced as a
@@ -337,7 +361,7 @@ private:
*/
template<typename CharType>
inline GenericStringRef<CharType> StringRef(const CharType* str) {
- return GenericStringRef<CharType>(str, internal::StrLen(str));
+ return GenericStringRef<CharType>(str);
}
//! Mark a character pointer as constant string
@@ -473,7 +497,7 @@ template<typename ValueType>
struct TypeHelper<ValueType, std::basic_string<typename ValueType::Ch> > {
typedef std::basic_string<typename ValueType::Ch> StringType;
static bool Is(const ValueType& v) { return v.IsString(); }
- static StringType Get(const ValueType& v) { return v.GetString(); }
+ static StringType Get(const ValueType& v) { return StringType(v.GetString(), v.GetStringLength()); }
static ValueType& Set(ValueType& v, const StringType& data, typename ValueType::AllocatorType& a) { return v.SetString(data, a); }
};
#endif
@@ -500,7 +524,7 @@ struct TypeHelper<ValueType, typename ValueType::Object> {
static bool Is(const ValueType& v) { return v.IsObject(); }
static ObjectType Get(ValueType& v) { return v.GetObject(); }
static ValueType& Set(ValueType& v, ObjectType data) { return v = data; }
- static ValueType& Set(ValueType& v, ObjectType data, typename ValueType::AllocatorType&) { v = data; }
+ static ValueType& Set(ValueType& v, ObjectType data, typename ValueType::AllocatorType&) { return v = data; }
};
template<typename ValueType>
@@ -587,7 +611,7 @@ public:
kNullFlag, kFalseFlag, kTrueFlag, kObjectFlag, kArrayFlag, kShortStringFlag,
kNumberAnyFlag
};
- RAPIDJSON_ASSERT(type <= kNumberType);
+ RAPIDJSON_ASSERT(type >= kNullType && type <= kNumberType);
data_.f.flags = defaultFlags[type];
// Use ShortString to store empty string.
@@ -600,10 +624,50 @@ public:
\tparam SourceAllocator allocator of \c rhs
\param rhs Value to copy from (read-only)
\param allocator Allocator for allocating copied elements and buffers. Commonly use GenericDocument::GetAllocator().
+ \param copyConstStrings Force copying of constant strings (e.g. referencing an in-situ buffer)
\see CopyFrom()
*/
- template< typename SourceAllocator >
- GenericValue(const GenericValue<Encoding, SourceAllocator>& rhs, Allocator & allocator);
+ template <typename SourceAllocator>
+ GenericValue(const GenericValue<Encoding,SourceAllocator>& rhs, Allocator& allocator, bool copyConstStrings = false) {
+ switch (rhs.GetType()) {
+ case kObjectType: {
+ SizeType count = rhs.data_.o.size;
+ Member* lm = reinterpret_cast<Member*>(allocator.Malloc(count * sizeof(Member)));
+ const typename GenericValue<Encoding,SourceAllocator>::Member* rm = rhs.GetMembersPointer();
+ for (SizeType i = 0; i < count; i++) {
+ new (&lm[i].name) GenericValue(rm[i].name, allocator, copyConstStrings);
+ new (&lm[i].value) GenericValue(rm[i].value, allocator, copyConstStrings);
+ }
+ data_.f.flags = kObjectFlag;
+ data_.o.size = data_.o.capacity = count;
+ SetMembersPointer(lm);
+ }
+ break;
+ case kArrayType: {
+ SizeType count = rhs.data_.a.size;
+ GenericValue* le = reinterpret_cast<GenericValue*>(allocator.Malloc(count * sizeof(GenericValue)));
+ const GenericValue<Encoding,SourceAllocator>* re = rhs.GetElementsPointer();
+ for (SizeType i = 0; i < count; i++)
+ new (&le[i]) GenericValue(re[i], allocator, copyConstStrings);
+ data_.f.flags = kArrayFlag;
+ data_.a.size = data_.a.capacity = count;
+ SetElementsPointer(le);
+ }
+ break;
+ case kStringType:
+ if (rhs.data_.f.flags == kConstStringFlag && !copyConstStrings) {
+ data_.f.flags = rhs.data_.f.flags;
+ data_ = *reinterpret_cast<const Data*>(&rhs.data_);
+ }
+ else
+ SetStringRaw(StringRef(rhs.GetString(), rhs.GetStringLength()), allocator);
+ break;
+ default:
+ data_.f.flags = rhs.data_.f.flags;
+ data_ = *reinterpret_cast<const Data*>(&rhs.data_);
+ break;
+ }
+ }
//! Constructor for boolean value.
/*! \param b Boolean value
@@ -665,6 +729,9 @@ public:
//! Constructor for double value.
explicit GenericValue(double d) RAPIDJSON_NOEXCEPT : data_() { data_.n.d = d; data_.f.flags = kNumberDoubleFlag; }
+ //! Constructor for float value.
+ explicit GenericValue(float f) RAPIDJSON_NOEXCEPT : data_() { data_.n.d = static_cast<double>(f); data_.f.flags = kNumberDoubleFlag; }
+
//! Constructor for constant string (i.e. do not make a copy of string)
GenericValue(const Ch* s, SizeType length) RAPIDJSON_NOEXCEPT : data_() { SetStringRaw(StringRef(s, length)); }
@@ -793,12 +860,13 @@ public:
\tparam SourceAllocator Allocator type of \c rhs
\param rhs Value to copy from (read-only)
\param allocator Allocator to use for copying
+ \param copyConstStrings Force copying of constant strings (e.g. referencing an in-situ buffer)
*/
template <typename SourceAllocator>
- GenericValue& CopyFrom(const GenericValue<Encoding, SourceAllocator>& rhs, Allocator& allocator) {
+ GenericValue& CopyFrom(const GenericValue<Encoding, SourceAllocator>& rhs, Allocator& allocator, bool copyConstStrings = false) {
RAPIDJSON_ASSERT(static_cast<void*>(this) != static_cast<void const*>(&rhs));
this->~GenericValue();
- new (this) GenericValue(rhs, allocator);
+ new (this) GenericValue(rhs, allocator, copyConstStrings);
return *this;
}
@@ -947,12 +1015,16 @@ public:
if (IsUint64()) {
uint64_t u = GetUint64();
volatile double d = static_cast<double>(u);
- return static_cast<uint64_t>(d) == u;
+ return (d >= 0.0)
+ && (d < static_cast<double>((std::numeric_limits<uint64_t>::max)()))
+ && (u == static_cast<uint64_t>(d));
}
if (IsInt64()) {
int64_t i = GetInt64();
volatile double d = static_cast<double>(i);
- return static_cast< int64_t>(d) == i;
+ return (d >= static_cast<double>((std::numeric_limits<int64_t>::min)()))
+ && (d < static_cast<double>((std::numeric_limits<int64_t>::max)()))
+ && (i == static_cast<int64_t>(d));
}
return true; // double, int, uint are always lossless
}
@@ -968,6 +1040,9 @@ public:
bool IsLosslessFloat() const {
if (!IsNumber()) return false;
double a = GetDouble();
+ if (a < static_cast<double>(-(std::numeric_limits<float>::max)())
+ || a > static_cast<double>((std::numeric_limits<float>::max)()))
+ return false;
double b = static_cast<double>(static_cast<float>(a));
return a >= b && a <= b; // Prevent -Wfloat-equal
}
@@ -1001,6 +1076,9 @@ public:
//! Get the number of members in the object.
SizeType MemberCount() const { RAPIDJSON_ASSERT(IsObject()); return data_.o.size; }
+ //! Get the capacity of object.
+ SizeType MemberCapacity() const { RAPIDJSON_ASSERT(IsObject()); return data_.o.capacity; }
+
//! Check whether the object is empty.
bool ObjectEmpty() const { RAPIDJSON_ASSERT(IsObject()); return data_.o.size == 0; }
@@ -1069,6 +1147,21 @@ public:
/*! \pre IsObject() == true */
MemberIterator MemberEnd() { RAPIDJSON_ASSERT(IsObject()); return MemberIterator(GetMembersPointer() + data_.o.size); }
+ //! Request the object to have enough capacity to store members.
+ /*! \param newCapacity The capacity that the object at least need to have.
+ \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator().
+ \return The value itself for fluent API.
+ \note Linear time complexity.
+ */
+ GenericValue& MemberReserve(SizeType newCapacity, Allocator &allocator) {
+ RAPIDJSON_ASSERT(IsObject());
+ if (newCapacity > data_.o.capacity) {
+ SetMembersPointer(reinterpret_cast<Member*>(allocator.Realloc(GetMembersPointer(), data_.o.capacity * sizeof(Member), newCapacity * sizeof(Member))));
+ data_.o.capacity = newCapacity;
+ }
+ return *this;
+ }
+
//! Check whether a member exists in the object.
/*!
\param name Member name to be searched.
@@ -1155,8 +1248,8 @@ public:
\return Iterator to member, if it exists.
Otherwise returns \ref MemberEnd().
*/
- MemberIterator FindMember(const std::basic_string<Ch>& name) { return FindMember(StringRef(name)); }
- ConstMemberIterator FindMember(const std::basic_string<Ch>& name) const { return FindMember(StringRef(name)); }
+ MemberIterator FindMember(const std::basic_string<Ch>& name) { return FindMember(GenericValue(StringRef(name))); }
+ ConstMemberIterator FindMember(const std::basic_string<Ch>& name) const { return FindMember(GenericValue(StringRef(name))); }
#endif
//! Add a member (name-value pair) to the object.
@@ -1174,17 +1267,8 @@ public:
RAPIDJSON_ASSERT(name.IsString());
ObjectData& o = data_.o;
- if (o.size >= o.capacity) {
- if (o.capacity == 0) {
- o.capacity = kDefaultObjectCapacity;
- SetMembersPointer(reinterpret_cast<Member*>(allocator.Malloc(o.capacity * sizeof(Member))));
- }
- else {
- SizeType oldCapacity = o.capacity;
- o.capacity += (oldCapacity + 1) / 2; // grow by factor 1.5
- SetMembersPointer(reinterpret_cast<Member*>(allocator.Realloc(GetMembersPointer(), oldCapacity * sizeof(Member), o.capacity * sizeof(Member))));
- }
- }
+ if (o.size >= o.capacity)
+ MemberReserve(o.capacity == 0 ? kDefaultObjectCapacity : (o.capacity + (o.capacity + 1) / 2), allocator);
Member* members = GetMembersPointer();
members[o.size].name.RawAssign(name);
members[o.size].value.RawAssign(value);
@@ -1649,7 +1733,6 @@ public:
/*! \note If the value is 64-bit integer type, it may lose precision. Use \c IsLosslessFloat() to check whether the converison is lossless.
*/
float GetFloat() const {
- RAPIDJSON_ASSERT(IsFloat());
return static_cast<float>(GetDouble());
}
@@ -1658,7 +1741,7 @@ public:
GenericValue& SetInt64(int64_t i64) { this->~GenericValue(); new (this) GenericValue(i64); return *this; }
GenericValue& SetUint64(uint64_t u64) { this->~GenericValue(); new (this) GenericValue(u64); return *this; }
GenericValue& SetDouble(double d) { this->~GenericValue(); new (this) GenericValue(d); return *this; }
- GenericValue& SetFloat(float f) { this->~GenericValue(); new (this) GenericValue(f); return *this; }
+ GenericValue& SetFloat(float f) { this->~GenericValue(); new (this) GenericValue(static_cast<double>(f)); return *this; }
//@}
@@ -1697,7 +1780,7 @@ public:
\return The value itself for fluent API.
\post IsString() == true && GetString() != s && strcmp(GetString(),s) == 0 && GetStringLength() == length
*/
- GenericValue& SetString(const Ch* s, SizeType length, Allocator& allocator) { this->~GenericValue(); SetStringRaw(StringRef(s, length), allocator); return *this; }
+ GenericValue& SetString(const Ch* s, SizeType length, Allocator& allocator) { return SetString(StringRef(s, length), allocator); }
//! Set this value as a string by copying from source string.
/*! \param s source string.
@@ -1705,7 +1788,15 @@ public:
\return The value itself for fluent API.
\post IsString() == true && GetString() != s && strcmp(GetString(),s) == 0 && GetStringLength() == length
*/
- GenericValue& SetString(const Ch* s, Allocator& allocator) { return SetString(s, internal::StrLen(s), allocator); }
+ GenericValue& SetString(const Ch* s, Allocator& allocator) { return SetString(StringRef(s), allocator); }
+
+ //! Set this value as a string by copying from source string.
+ /*! \param s source string reference
+ \param allocator Allocator for allocating copied buffer. Commonly use GenericDocument::GetAllocator().
+ \return The value itself for fluent API.
+ \post IsString() == true && GetString() != s.s && strcmp(GetString(),s) == 0 && GetStringLength() == length
+ */
+ GenericValue& SetString(StringRefType s, Allocator& allocator) { this->~GenericValue(); SetStringRaw(s, allocator); return *this; }
#if RAPIDJSON_HAS_STDSTRING
//! Set this value as a string by copying from source string.
@@ -1715,7 +1806,7 @@ public:
\post IsString() == true && GetString() != s.data() && strcmp(GetString(),s.data() == 0 && GetStringLength() == s.size()
\note Requires the definition of the preprocessor symbol \ref RAPIDJSON_HAS_STDSTRING.
*/
- GenericValue& SetString(const std::basic_string<Ch>& s, Allocator& allocator) { return SetString(s.data(), SizeType(s.size()), allocator); }
+ GenericValue& SetString(const std::basic_string<Ch>& s, Allocator& allocator) { return SetString(StringRef(s), allocator); }
#endif
//@}
@@ -2025,7 +2116,7 @@ public:
GenericValue<Encoding, Allocator>(type), allocator_(allocator), ownAllocator_(0), stack_(stackAllocator, stackCapacity), parseResult_()
{
if (!allocator_)
- ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator());
+ ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator)();
}
//! Constructor
@@ -2038,7 +2129,7 @@ public:
allocator_(allocator), ownAllocator_(0), stack_(stackAllocator, stackCapacity), parseResult_()
{
if (!allocator_)
- ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator());
+ ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator)();
}
#if RAPIDJSON_HAS_CXX11_RVALUE_REFS
@@ -2099,6 +2190,10 @@ public:
return *this;
}
+ // Allow Swap with ValueType.
+ // Refer to Effective C++ 3rd Edition/Item 33: Avoid hiding inherited names.
+ using ValueType::Swap;
+
//! free-standing swap function helper
/*!
Helper function to enable support for common swap implementation pattern based on \c std::swap:
@@ -2230,7 +2325,7 @@ public:
template <unsigned parseFlags, typename SourceEncoding>
GenericDocument& Parse(const typename SourceEncoding::Ch* str, size_t length) {
RAPIDJSON_ASSERT(!(parseFlags & kParseInsituFlag));
- MemoryStream ms(static_cast<const char*>(str), length * sizeof(typename SourceEncoding::Ch));
+ MemoryStream ms(reinterpret_cast<const char*>(str), length * sizeof(typename SourceEncoding::Ch));
EncodedInputStream<SourceEncoding, MemoryStream> is(ms);
ParseStream<parseFlags, SourceEncoding>(is);
return *this;
@@ -2388,35 +2483,6 @@ private:
//! GenericDocument with UTF8 encoding
typedef GenericDocument<UTF8<> > Document;
-// defined here due to the dependency on GenericDocument
-template <typename Encoding, typename Allocator>
-template <typename SourceAllocator>
-inline
-GenericValue<Encoding,Allocator>::GenericValue(const GenericValue<Encoding,SourceAllocator>& rhs, Allocator& allocator)
-{
- switch (rhs.GetType()) {
- case kObjectType:
- case kArrayType: { // perform deep copy via SAX Handler
- GenericDocument<Encoding,Allocator> d(&allocator);
- rhs.Accept(d);
- RawAssign(*d.stack_.template Pop<GenericValue>(1));
- }
- break;
- case kStringType:
- if (rhs.data_.f.flags == kConstStringFlag) {
- data_.f.flags = rhs.data_.f.flags;
- data_ = *reinterpret_cast<const Data*>(&rhs.data_);
- } else {
- SetStringRaw(StringRef(rhs.GetString(), rhs.GetStringLength()), allocator);
- }
- break;
- default:
- data_.f.flags = rhs.data_.f.flags;
- data_ = *reinterpret_cast<const Data*>(&rhs.data_);
- break;
- }
-}
-
//! Helper class for accessing Value of array type.
/*!
Instance of this helper class is obtained by \c GenericValue::GetArray().
@@ -2497,6 +2563,7 @@ public:
~GenericObject() {}
SizeType MemberCount() const { return value_.MemberCount(); }
+ SizeType MemberCapacity() const { return value_.MemberCapacity(); }
bool ObjectEmpty() const { return value_.ObjectEmpty(); }
template <typename T> ValueType& operator[](T* name) const { return value_[name]; }
template <typename SourceAllocator> ValueType& operator[](const GenericValue<EncodingType, SourceAllocator>& name) const { return value_[name]; }
@@ -2505,13 +2572,14 @@ public:
#endif
MemberIterator MemberBegin() const { return value_.MemberBegin(); }
MemberIterator MemberEnd() const { return value_.MemberEnd(); }
+ GenericObject MemberReserve(SizeType newCapacity, AllocatorType &allocator) const { value_.MemberReserve(newCapacity, allocator); return *this; }
bool HasMember(const Ch* name) const { return value_.HasMember(name); }
#if RAPIDJSON_HAS_STDSTRING
bool HasMember(const std::basic_string<Ch>& name) const { return value_.HasMember(name); }
#endif
template <typename SourceAllocator> bool HasMember(const GenericValue<EncodingType, SourceAllocator>& name) const { return value_.HasMember(name); }
- MemberIterator FindMember(const Ch* name) const { value_.FindMember(name); }
- template <typename SourceAllocator> MemberIterator FindMember(const GenericValue<EncodingType, SourceAllocator>& name) const { value_.FindMember(name); }
+ MemberIterator FindMember(const Ch* name) const { return value_.FindMember(name); }
+ template <typename SourceAllocator> MemberIterator FindMember(const GenericValue<EncodingType, SourceAllocator>& name) const { return value_.FindMember(name); }
#if RAPIDJSON_HAS_STDSTRING
MemberIterator FindMember(const std::basic_string<Ch>& name) const { return value_.FindMember(name); }
#endif
@@ -2530,7 +2598,7 @@ public:
GenericObject AddMember(StringRefType name, ValueType& value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; }
GenericObject AddMember(StringRefType name, StringRefType value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; }
template <typename T> RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr<internal::IsPointer<T>, internal::IsGenericValue<T> >), (GenericObject)) AddMember(StringRefType name, T value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; }
- void RemoveAllMembers() { return value_.RemoveAllMembers(); }
+ void RemoveAllMembers() { value_.RemoveAllMembers(); }
bool RemoveMember(const Ch* name) const { return value_.RemoveMember(name); }
#if RAPIDJSON_HAS_STDSTRING
bool RemoveMember(const std::basic_string<Ch>& name) const { return value_.RemoveMember(name); }
@@ -2557,17 +2625,6 @@ private:
};
RAPIDJSON_NAMESPACE_END
-
-#ifdef _MSC_VER
RAPIDJSON_DIAG_POP
-#endif
-
-#ifdef __clang__
-RAPIDJSON_DIAG_POP
-#endif
-
-#ifdef __GNUC__
-RAPIDJSON_DIAG_POP
-#endif
#endif // RAPIDJSON_DOCUMENT_H_
diff --git a/include/rapidjson/encodedstream.h b/include/rapidjson/encodedstream.h
index c402e5c3..223601c0 100644
--- a/include/rapidjson/encodedstream.h
+++ b/include/rapidjson/encodedstream.h
@@ -85,6 +85,10 @@ public:
size_t PutEnd(Ch*) { return 0; }
MemoryStream& is_;
+
+private:
+ EncodedInputStream(const EncodedInputStream&);
+ EncodedInputStream& operator=(const EncodedInputStream&);
};
//! Output byte stream wrapper with statically bound encoding.
@@ -196,7 +200,7 @@ private:
// xx xx xx xx UTF-8
if (!hasBOM_) {
- unsigned pattern = (c[0] ? 1 : 0) | (c[1] ? 2 : 0) | (c[2] ? 4 : 0) | (c[3] ? 8 : 0);
+ int pattern = (c[0] ? 1 : 0) | (c[1] ? 2 : 0) | (c[2] ? 4 : 0) | (c[3] ? 8 : 0);
switch (pattern) {
case 0x08: type_ = kUTF32BE; break;
case 0x0A: type_ = kUTF16BE; break;
diff --git a/include/rapidjson/encodings.h b/include/rapidjson/encodings.h
index edfc9901..7903e76a 100644
--- a/include/rapidjson/encodings.h
+++ b/include/rapidjson/encodings.h
@@ -154,7 +154,11 @@ struct UTF8 {
}
unsigned char type = GetRange(static_cast<unsigned char>(c));
- *codepoint = (0xFF >> type) & static_cast<unsigned char>(c);
+ if (type >= 32) {
+ *codepoint = 0;
+ } else {
+ *codepoint = (0xFFu >> type) & static_cast<unsigned char>(c);
+ }
bool result = true;
switch (type) {
case 2: TAIL(); return result;
@@ -279,7 +283,7 @@ struct UTF16 {
RAPIDJSON_ASSERT(codepoint <= 0x10FFFF);
unsigned v = codepoint - 0x10000;
os.Put(static_cast<typename OutputStream::Ch>((v >> 10) | 0xD800));
- os.Put((v & 0x3FF) | 0xDC00);
+ os.Put(static_cast<typename OutputStream::Ch>((v & 0x3FF) | 0xDC00));
}
}
@@ -295,7 +299,7 @@ struct UTF16 {
RAPIDJSON_ASSERT(codepoint <= 0x10FFFF);
unsigned v = codepoint - 0x10000;
PutUnsafe(os, static_cast<typename OutputStream::Ch>((v >> 10) | 0xD800));
- PutUnsafe(os, (v & 0x3FF) | 0xDC00);
+ PutUnsafe(os, static_cast<typename OutputStream::Ch>((v & 0x3FF) | 0xDC00));
}
}
@@ -380,7 +384,7 @@ struct UTF16BE : UTF16<CharType> {
static CharType Take(InputByteStream& is) {
RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1);
unsigned c = static_cast<unsigned>(static_cast<uint8_t>(is.Take())) << 8;
- c |= static_cast<uint8_t>(is.Take());
+ c |= static_cast<unsigned>(static_cast<uint8_t>(is.Take()));
return static_cast<CharType>(c);
}
@@ -616,28 +620,28 @@ struct AutoUTF {
#define RAPIDJSON_ENCODINGS_FUNC(x) UTF8<Ch>::x, UTF16LE<Ch>::x, UTF16BE<Ch>::x, UTF32LE<Ch>::x, UTF32BE<Ch>::x
template<typename OutputStream>
- RAPIDJSON_FORCEINLINE static void Encode(OutputStream& os, unsigned codepoint) {
+ static RAPIDJSON_FORCEINLINE void Encode(OutputStream& os, unsigned codepoint) {
typedef void (*EncodeFunc)(OutputStream&, unsigned);
static const EncodeFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Encode) };
(*f[os.GetType()])(os, codepoint);
}
template<typename OutputStream>
- RAPIDJSON_FORCEINLINE static void EncodeUnsafe(OutputStream& os, unsigned codepoint) {
+ static RAPIDJSON_FORCEINLINE void EncodeUnsafe(OutputStream& os, unsigned codepoint) {
typedef void (*EncodeFunc)(OutputStream&, unsigned);
static const EncodeFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(EncodeUnsafe) };
(*f[os.GetType()])(os, codepoint);
}
template <typename InputStream>
- RAPIDJSON_FORCEINLINE static bool Decode(InputStream& is, unsigned* codepoint) {
+ static RAPIDJSON_FORCEINLINE bool Decode(InputStream& is, unsigned* codepoint) {
typedef bool (*DecodeFunc)(InputStream&, unsigned*);
static const DecodeFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Decode) };
return (*f[is.GetType()])(is, codepoint);
}
template <typename InputStream, typename OutputStream>
- RAPIDJSON_FORCEINLINE static bool Validate(InputStream& is, OutputStream& os) {
+ static RAPIDJSON_FORCEINLINE bool Validate(InputStream& is, OutputStream& os) {
typedef bool (*ValidateFunc)(InputStream&, OutputStream&);
static const ValidateFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Validate) };
return (*f[is.GetType()])(is, os);
@@ -654,7 +658,7 @@ template<typename SourceEncoding, typename TargetEncoding>
struct Transcoder {
//! Take one Unicode codepoint from source encoding, convert it to target encoding and put it to the output stream.
template<typename InputStream, typename OutputStream>
- RAPIDJSON_FORCEINLINE static bool Transcode(InputStream& is, OutputStream& os) {
+ static RAPIDJSON_FORCEINLINE bool Transcode(InputStream& is, OutputStream& os) {
unsigned codepoint;
if (!SourceEncoding::Decode(is, &codepoint))
return false;
@@ -663,7 +667,7 @@ struct Transcoder {
}
template<typename InputStream, typename OutputStream>
- RAPIDJSON_FORCEINLINE static bool TranscodeUnsafe(InputStream& is, OutputStream& os) {
+ static RAPIDJSON_FORCEINLINE bool TranscodeUnsafe(InputStream& is, OutputStream& os) {
unsigned codepoint;
if (!SourceEncoding::Decode(is, &codepoint))
return false;
@@ -673,7 +677,7 @@ struct Transcoder {
//! Validate one Unicode codepoint from an encoded stream.
template<typename InputStream, typename OutputStream>
- RAPIDJSON_FORCEINLINE static bool Validate(InputStream& is, OutputStream& os) {
+ static RAPIDJSON_FORCEINLINE bool Validate(InputStream& is, OutputStream& os) {
return Transcode(is, os); // Since source/target encoding is different, must transcode.
}
};
@@ -686,19 +690,19 @@ inline void PutUnsafe(Stream& stream, typename Stream::Ch c);
template<typename Encoding>
struct Transcoder<Encoding, Encoding> {
template<typename InputStream, typename OutputStream>
- RAPIDJSON_FORCEINLINE static bool Transcode(InputStream& is, OutputStream& os) {
+ static RAPIDJSON_FORCEINLINE bool Transcode(InputStream& is, OutputStream& os) {
os.Put(is.Take()); // Just copy one code unit. This semantic is different from primary template class.
return true;
}
template<typename InputStream, typename OutputStream>
- RAPIDJSON_FORCEINLINE static bool TranscodeUnsafe(InputStream& is, OutputStream& os) {
+ static RAPIDJSON_FORCEINLINE bool TranscodeUnsafe(InputStream& is, OutputStream& os) {
PutUnsafe(os, is.Take()); // Just copy one code unit. This semantic is different from primary template class.
return true;
}
template<typename InputStream, typename OutputStream>
- RAPIDJSON_FORCEINLINE static bool Validate(InputStream& is, OutputStream& os) {
+ static RAPIDJSON_FORCEINLINE bool Validate(InputStream& is, OutputStream& os) {
return Encoding::Validate(is, os); // source/target encoding are the same
}
};
diff --git a/include/rapidjson/error/en.h b/include/rapidjson/error/en.h
index c2315fda..2db838bf 100644
--- a/include/rapidjson/error/en.h
+++ b/include/rapidjson/error/en.h
@@ -38,7 +38,7 @@ inline const RAPIDJSON_ERROR_CHARTYPE* GetParseError_En(ParseErrorCode parseErro
case kParseErrorNone: return RAPIDJSON_ERROR_STRING("No error.");
case kParseErrorDocumentEmpty: return RAPIDJSON_ERROR_STRING("The document is empty.");
- case kParseErrorDocumentRootNotSingular: return RAPIDJSON_ERROR_STRING("The document root must not follow by other values.");
+ case kParseErrorDocumentRootNotSingular: return RAPIDJSON_ERROR_STRING("The document root must not be followed by other values.");
case kParseErrorValueInvalid: return RAPIDJSON_ERROR_STRING("Invalid value.");
diff --git a/include/rapidjson/error/error.h b/include/rapidjson/error/error.h
index 95cb31a7..9311d2f0 100644
--- a/include/rapidjson/error/error.h
+++ b/include/rapidjson/error/error.h
@@ -104,6 +104,8 @@ enum ParseErrorCode {
\see GenericReader::Parse, GenericDocument::Parse
*/
struct ParseResult {
+ //!! Unspecified boolean type
+ typedef bool (ParseResult::*BooleanType)() const;
public:
//! Default constructor, no error.
ParseResult() : code_(kParseErrorNone), offset_(0) {}
@@ -115,8 +117,8 @@ public:
//! Get the error offset, if \ref IsError(), 0 otherwise.
size_t Offset() const { return offset_; }
- //! Conversion to \c bool, returns \c true, iff !\ref IsError().
- operator bool() const { return !IsError(); }
+ //! Explicit conversion to \c bool, returns \c true, iff !\ref IsError().
+ operator BooleanType() const { return !IsError() ? &ParseResult::IsError : NULL; }
//! Whether the result is an error.
bool IsError() const { return code_ != kParseErrorNone; }
@@ -124,6 +126,10 @@ public:
bool operator==(ParseErrorCode code) const { return code_ == code; }
friend bool operator==(ParseErrorCode code, const ParseResult & err) { return code == err.code_; }
+ bool operator!=(const ParseResult& that) const { return !(*this == that); }
+ bool operator!=(ParseErrorCode code) const { return !(*this == code); }
+ friend bool operator!=(ParseErrorCode code, const ParseResult & err) { return err != code; }
+
//! Reset error code.
void Clear() { Set(kParseErrorNone); }
//! Update error code and offset.
diff --git a/include/rapidjson/filereadstream.h b/include/rapidjson/filereadstream.h
index b56ea13b..f1bfb7d0 100644
--- a/include/rapidjson/filereadstream.h
+++ b/include/rapidjson/filereadstream.h
@@ -68,7 +68,7 @@ private:
++current_;
else if (!eof_) {
count_ += readCount_;
- readCount_ = fread(buffer_, 1, bufferSize_, fp_);
+ readCount_ = std::fread(buffer_, 1, bufferSize_, fp_);
bufferLast_ = buffer_ + readCount_ - 1;
current_ = buffer_;
diff --git a/include/rapidjson/filewritestream.h b/include/rapidjson/filewritestream.h
index 6378dd60..8b48fee1 100644
--- a/include/rapidjson/filewritestream.h
+++ b/include/rapidjson/filewritestream.h
@@ -25,7 +25,7 @@ RAPIDJSON_DIAG_OFF(unreachable-code)
RAPIDJSON_NAMESPACE_BEGIN
-//! Wrapper of C file stream for input using fread().
+//! Wrapper of C file stream for output using fwrite().
/*!
\note implements Stream concept
*/
@@ -62,7 +62,7 @@ public:
void Flush() {
if (current_ != buffer_) {
- size_t result = fwrite(buffer_, 1, static_cast<size_t>(current_ - buffer_), fp_);
+ size_t result = std::fwrite(buffer_, 1, static_cast<size_t>(current_ - buffer_), fp_);
if (result < static_cast<size_t>(current_ - buffer_)) {
// failure deliberately ignored at this time
// added to avoid warn_unused_result build errors
diff --git a/include/rapidjson/internal/biginteger.h b/include/rapidjson/internal/biginteger.h
index 9d3e88c9..9d3e88c9 100755..100644
--- a/include/rapidjson/internal/biginteger.h
+++ b/include/rapidjson/internal/biginteger.h
diff --git a/include/rapidjson/internal/diyfp.h b/include/rapidjson/internal/diyfp.h
index c9fefdc6..29abf804 100644
--- a/include/rapidjson/internal/diyfp.h
+++ b/include/rapidjson/internal/diyfp.h
@@ -21,7 +21,7 @@
#include "../rapidjson.h"
-#if defined(_MSC_VER) && defined(_M_AMD64)
+#if defined(_MSC_VER) && defined(_M_AMD64) && !defined(__INTEL_COMPILER)
#include <intrin.h>
#pragma intrinsic(_BitScanReverse64)
#pragma intrinsic(_umul128)
diff --git a/include/rapidjson/internal/dtoa.h b/include/rapidjson/internal/dtoa.h
index d4582845..bf2e9b2e 100644
--- a/include/rapidjson/internal/dtoa.h
+++ b/include/rapidjson/internal/dtoa.h
@@ -41,7 +41,7 @@ inline void GrisuRound(char* buffer, int len, uint64_t delta, uint64_t rest, uin
}
}
-inline unsigned CountDecimalDigit32(uint32_t n) {
+inline int CountDecimalDigit32(uint32_t n) {
// Simple pure C++ implementation was faster than __builtin_clz version in this situation.
if (n < 10) return 1;
if (n < 100) return 2;
@@ -63,7 +63,7 @@ inline void DigitGen(const DiyFp& W, const DiyFp& Mp, uint64_t delta, char* buff
const DiyFp wp_w = Mp - W;
uint32_t p1 = static_cast<uint32_t>(Mp.f >> -one.e);
uint64_t p2 = Mp.f & (one.f - 1);
- unsigned kappa = CountDecimalDigit32(p1); // kappa in [0, 9]
+ int kappa = CountDecimalDigit32(p1); // kappa in [0, 9]
*len = 0;
while (kappa > 0) {
@@ -102,7 +102,8 @@ inline void DigitGen(const DiyFp& W, const DiyFp& Mp, uint64_t delta, char* buff
kappa--;
if (p2 < delta) {
*K += kappa;
- GrisuRound(buffer, *len, delta, p2, one.f, wp_w.f * kPow10[-static_cast<int>(kappa)]);
+ int index = -kappa;
+ GrisuRound(buffer, *len, delta, p2, one.f, wp_w.f * (index < 9 ? kPow10[index] : 0));
return;
}
}
@@ -180,7 +181,7 @@ inline char* Prettify(char* buffer, int length, int k, int maxDecimalPlaces) {
buffer[1] = '.';
for (int i = 2; i < offset; i++)
buffer[i] = '0';
- if (length + offset > maxDecimalPlaces) {
+ if (length - kk > maxDecimalPlaces) {
// When maxDecimalPlaces = 2, 0.123 -> 0.12, 0.102 -> 0.1
// Remove extra trailing zeros (at least one) after truncation.
for (int i = maxDecimalPlaces + 1; i > 2; i--)
diff --git a/include/rapidjson/internal/ieee754.h b/include/rapidjson/internal/ieee754.h
index 82bb0b99..c2684ba2 100644
--- a/include/rapidjson/internal/ieee754.h
+++ b/include/rapidjson/internal/ieee754.h
@@ -48,13 +48,13 @@ public:
int IntegerExponent() const { return (IsNormal() ? Exponent() : kDenormalExponent) - kSignificandSize; }
uint64_t ToBias() const { return (u_ & kSignMask) ? ~u_ + 1 : u_ | kSignMask; }
- static unsigned EffectiveSignificandSize(int order) {
+ static int EffectiveSignificandSize(int order) {
if (order >= -1021)
return 53;
else if (order <= -1074)
return 0;
else
- return static_cast<unsigned>(order) + 1074;
+ return order + 1074;
}
private:
diff --git a/include/rapidjson/internal/regex.h b/include/rapidjson/internal/regex.h
index aeb0e3ef..e1a2faae 100644
--- a/include/rapidjson/internal/regex.h
+++ b/include/rapidjson/internal/regex.h
@@ -29,6 +29,14 @@ RAPIDJSON_DIAG_OFF(implicit-fallthrough)
#ifdef __GNUC__
RAPIDJSON_DIAG_PUSH
RAPIDJSON_DIAG_OFF(effc++)
+#if __GNUC__ >= 7
+RAPIDJSON_DIAG_OFF(implicit-fallthrough)
+#endif
+#endif
+
+#ifdef _MSC_VER
+RAPIDJSON_DIAG_PUSH
+RAPIDJSON_DIAG_OFF(4512) // assignment operator could not be generated
#endif
#ifndef RAPIDJSON_REGEX_VERBOSE
@@ -39,11 +47,39 @@ RAPIDJSON_NAMESPACE_BEGIN
namespace internal {
///////////////////////////////////////////////////////////////////////////////
+// DecodedStream
+
+template <typename SourceStream, typename Encoding>
+class DecodedStream {
+public:
+ DecodedStream(SourceStream& ss) : ss_(ss), codepoint_() { Decode(); }
+ unsigned Peek() { return codepoint_; }
+ unsigned Take() {
+ unsigned c = codepoint_;
+ if (c) // No further decoding when '\0'
+ Decode();
+ return c;
+ }
+
+private:
+ void Decode() {
+ if (!Encoding::Decode(ss_, &codepoint_))
+ codepoint_ = 0;
+ }
+
+ SourceStream& ss_;
+ unsigned codepoint_;
+};
+
+///////////////////////////////////////////////////////////////////////////////
// GenericRegex
static const SizeType kRegexInvalidState = ~SizeType(0); //!< Represents an invalid index in GenericRegex::State::out, out1
static const SizeType kRegexInvalidRange = ~SizeType(0);
+template <typename Encoding, typename Allocator>
+class GenericRegexSearch;
+
//! Regular expression engine with subset of ECMAscript grammar.
/*!
Supported regular expression syntax:
@@ -79,45 +115,25 @@ static const SizeType kRegexInvalidRange = ~SizeType(0);
template <typename Encoding, typename Allocator = CrtAllocator>
class GenericRegex {
public:
+ typedef Encoding EncodingType;
typedef typename Encoding::Ch Ch;
+ template <typename, typename> friend class GenericRegexSearch;
GenericRegex(const Ch* source, Allocator* allocator = 0) :
states_(allocator, 256), ranges_(allocator, 256), root_(kRegexInvalidState), stateCount_(), rangeCount_(),
- stateSet_(), state0_(allocator, 0), state1_(allocator, 0), anchorBegin_(), anchorEnd_()
+ anchorBegin_(), anchorEnd_()
{
GenericStringStream<Encoding> ss(source);
- DecodedStream<GenericStringStream<Encoding> > ds(ss);
+ DecodedStream<GenericStringStream<Encoding>, Encoding> ds(ss);
Parse(ds);
}
- ~GenericRegex() {
- Allocator::Free(stateSet_);
- }
+ ~GenericRegex() {}
bool IsValid() const {
return root_ != kRegexInvalidState;
}
- template <typename InputStream>
- bool Match(InputStream& is) const {
- return SearchWithAnchoring(is, true, true);
- }
-
- bool Match(const Ch* s) const {
- GenericStringStream<Encoding> is(s);
- return Match(is);
- }
-
- template <typename InputStream>
- bool Search(InputStream& is) const {
- return SearchWithAnchoring(is, anchorBegin_, anchorEnd_);
- }
-
- bool Search(const Ch* s) const {
- GenericStringStream<Encoding> is(s);
- return Search(is);
- }
-
private:
enum Operator {
kZeroOrOne,
@@ -152,28 +168,6 @@ private:
SizeType minIndex;
};
- template <typename SourceStream>
- class DecodedStream {
- public:
- DecodedStream(SourceStream& ss) : ss_(ss), codepoint_() { Decode(); }
- unsigned Peek() { return codepoint_; }
- unsigned Take() {
- unsigned c = codepoint_;
- if (c) // No further decoding when '\0'
- Decode();
- return c;
- }
-
- private:
- void Decode() {
- if (!Encoding::Decode(ss_, &codepoint_))
- codepoint_ = 0;
- }
-
- SourceStream& ss_;
- unsigned codepoint_;
- };
-
State& GetState(SizeType index) {
RAPIDJSON_ASSERT(index < stateCount_);
return states_.template Bottom<State>()[index];
@@ -195,7 +189,7 @@ private:
}
template <typename InputStream>
- void Parse(DecodedStream<InputStream>& ds) {
+ void Parse(DecodedStream<InputStream, Encoding>& ds) {
Allocator allocator;
Stack<Allocator> operandStack(&allocator, 256); // Frag
Stack<Allocator> operatorStack(&allocator, 256); // Operator
@@ -322,14 +316,6 @@ private:
printf("\n");
#endif
}
-
- // Preallocate buffer for SearchWithAnchoring()
- RAPIDJSON_ASSERT(stateSet_ == 0);
- if (stateCount_ > 0) {
- stateSet_ = static_cast<unsigned*>(states_.GetAllocator().Malloc(GetStateSetSize()));
- state0_.template Reserve<SizeType>(stateCount_);
- state1_.template Reserve<SizeType>(stateCount_);
- }
}
SizeType NewState(SizeType out, SizeType out1, unsigned codepoint) {
@@ -370,14 +356,14 @@ private:
bool Eval(Stack<Allocator>& operandStack, Operator op) {
switch (op) {
case kConcatenation:
- if (operandStack.GetSize() >= sizeof(Frag) * 2) {
+ RAPIDJSON_ASSERT(operandStack.GetSize() >= sizeof(Frag) * 2);
+ {
Frag e2 = *operandStack.template Pop<Frag>(1);
Frag e1 = *operandStack.template Pop<Frag>(1);
Patch(e1.out, e2.start);
*operandStack.template Push<Frag>() = Frag(e1.start, e2.out, Min(e1.minIndex, e2.minIndex));
- return true;
}
- return false;
+ return true;
case kAlternation:
if (operandStack.GetSize() >= sizeof(Frag) * 2) {
@@ -408,7 +394,8 @@ private:
}
return false;
- case kOneOrMore:
+ default:
+ RAPIDJSON_ASSERT(op == kOneOrMore);
if (operandStack.GetSize() >= sizeof(Frag)) {
Frag e = *operandStack.template Pop<Frag>(1);
SizeType s = NewState(kRegexInvalidState, e.start, 0);
@@ -417,16 +404,12 @@ private:
return true;
}
return false;
-
- default:
- return false;
}
}
bool EvalQuantifier(Stack<Allocator>& operandStack, unsigned n, unsigned m) {
RAPIDJSON_ASSERT(n <= m);
- if (operandStack.GetSize() < sizeof(Frag))
- return false;
+ RAPIDJSON_ASSERT(operandStack.GetSize() >= sizeof(Frag));
if (n == 0) {
if (m == 0) // a{0} not support
@@ -466,22 +449,22 @@ private:
static SizeType Min(SizeType a, SizeType b) { return a < b ? a : b; }
void CloneTopOperand(Stack<Allocator>& operandStack) {
- const Frag *src = operandStack.template Top<Frag>();
- SizeType count = stateCount_ - src->minIndex; // Assumes top operand contains states in [src->minIndex, stateCount_)
+ const Frag src = *operandStack.template Top<Frag>(); // Copy constructor to prevent invalidation
+ SizeType count = stateCount_ - src.minIndex; // Assumes top operand contains states in [src->minIndex, stateCount_)
State* s = states_.template Push<State>(count);
- memcpy(s, &GetState(src->minIndex), count * sizeof(State));
+ memcpy(s, &GetState(src.minIndex), count * sizeof(State));
for (SizeType j = 0; j < count; j++) {
if (s[j].out != kRegexInvalidState)
s[j].out += count;
if (s[j].out1 != kRegexInvalidState)
s[j].out1 += count;
}
- *operandStack.template Push<Frag>() = Frag(src->start + count, src->out + count, src->minIndex + count);
+ *operandStack.template Push<Frag>() = Frag(src.start + count, src.out + count, src.minIndex + count);
stateCount_ += count;
}
template <typename InputStream>
- bool ParseUnsigned(DecodedStream<InputStream>& ds, unsigned* u) {
+ bool ParseUnsigned(DecodedStream<InputStream, Encoding>& ds, unsigned* u) {
unsigned r = 0;
if (ds.Peek() < '0' || ds.Peek() > '9')
return false;
@@ -495,7 +478,7 @@ private:
}
template <typename InputStream>
- bool ParseRange(DecodedStream<InputStream>& ds, SizeType* range) {
+ bool ParseRange(DecodedStream<InputStream, Encoding>& ds, SizeType* range) {
bool isBegin = true;
bool negate = false;
int step = 0;
@@ -573,7 +556,7 @@ private:
}
template <typename InputStream>
- bool CharacterEscape(DecodedStream<InputStream>& ds, unsigned* escapedCodepoint) {
+ bool CharacterEscape(DecodedStream<InputStream, Encoding>& ds, unsigned* escapedCodepoint) {
unsigned codepoint;
switch (codepoint = ds.Take()) {
case '^':
@@ -601,34 +584,93 @@ private:
}
}
+ Stack<Allocator> states_;
+ Stack<Allocator> ranges_;
+ SizeType root_;
+ SizeType stateCount_;
+ SizeType rangeCount_;
+
+ static const unsigned kInfinityQuantifier = ~0u;
+
+ // For SearchWithAnchoring()
+ bool anchorBegin_;
+ bool anchorEnd_;
+};
+
+template <typename RegexType, typename Allocator = CrtAllocator>
+class GenericRegexSearch {
+public:
+ typedef typename RegexType::EncodingType Encoding;
+ typedef typename Encoding::Ch Ch;
+
+ GenericRegexSearch(const RegexType& regex, Allocator* allocator = 0) :
+ regex_(regex), allocator_(allocator), ownAllocator_(0),
+ state0_(allocator, 0), state1_(allocator, 0), stateSet_()
+ {
+ RAPIDJSON_ASSERT(regex_.IsValid());
+ if (!allocator_)
+ ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator)();
+ stateSet_ = static_cast<unsigned*>(allocator_->Malloc(GetStateSetSize()));
+ state0_.template Reserve<SizeType>(regex_.stateCount_);
+ state1_.template Reserve<SizeType>(regex_.stateCount_);
+ }
+
+ ~GenericRegexSearch() {
+ Allocator::Free(stateSet_);
+ RAPIDJSON_DELETE(ownAllocator_);
+ }
+
+ template <typename InputStream>
+ bool Match(InputStream& is) {
+ return SearchWithAnchoring(is, true, true);
+ }
+
+ bool Match(const Ch* s) {
+ GenericStringStream<Encoding> is(s);
+ return Match(is);
+ }
+
template <typename InputStream>
- bool SearchWithAnchoring(InputStream& is, bool anchorBegin, bool anchorEnd) const {
- RAPIDJSON_ASSERT(IsValid());
- DecodedStream<InputStream> ds(is);
+ bool Search(InputStream& is) {
+ return SearchWithAnchoring(is, regex_.anchorBegin_, regex_.anchorEnd_);
+ }
+
+ bool Search(const Ch* s) {
+ GenericStringStream<Encoding> is(s);
+ return Search(is);
+ }
+
+private:
+ typedef typename RegexType::State State;
+ typedef typename RegexType::Range Range;
+
+ template <typename InputStream>
+ bool SearchWithAnchoring(InputStream& is, bool anchorBegin, bool anchorEnd) {
+ DecodedStream<InputStream, Encoding> ds(is);
state0_.Clear();
Stack<Allocator> *current = &state0_, *next = &state1_;
const size_t stateSetSize = GetStateSetSize();
std::memset(stateSet_, 0, stateSetSize);
- bool matched = AddState(*current, root_);
+ bool matched = AddState(*current, regex_.root_);
unsigned codepoint;
while (!current->Empty() && (codepoint = ds.Take()) != 0) {
std::memset(stateSet_, 0, stateSetSize);
next->Clear();
matched = false;
for (const SizeType* s = current->template Bottom<SizeType>(); s != current->template End<SizeType>(); ++s) {
- const State& sr = GetState(*s);
+ const State& sr = regex_.GetState(*s);
if (sr.codepoint == codepoint ||
- sr.codepoint == kAnyCharacterClass ||
- (sr.codepoint == kRangeCharacterClass && MatchRange(sr.rangeStart, codepoint)))
+ sr.codepoint == RegexType::kAnyCharacterClass ||
+ (sr.codepoint == RegexType::kRangeCharacterClass && MatchRange(sr.rangeStart, codepoint)))
{
matched = AddState(*next, sr.out) || matched;
if (!anchorEnd && matched)
return true;
}
if (!anchorBegin)
- AddState(*next, root_);
+ AddState(*next, regex_.root_);
}
internal::Swap(current, next);
}
@@ -637,54 +679,46 @@ private:
}
size_t GetStateSetSize() const {
- return (stateCount_ + 31) / 32 * 4;
+ return (regex_.stateCount_ + 31) / 32 * 4;
}
// Return whether the added states is a match state
- bool AddState(Stack<Allocator>& l, SizeType index) const {
- if (index == kRegexInvalidState)
- return true;
+ bool AddState(Stack<Allocator>& l, SizeType index) {
+ RAPIDJSON_ASSERT(index != kRegexInvalidState);
- const State& s = GetState(index);
+ const State& s = regex_.GetState(index);
if (s.out1 != kRegexInvalidState) { // Split
bool matched = AddState(l, s.out);
return AddState(l, s.out1) || matched;
}
- else if (!(stateSet_[index >> 5] & (1 << (index & 31)))) {
- stateSet_[index >> 5] |= (1 << (index & 31));
+ else if (!(stateSet_[index >> 5] & (1u << (index & 31)))) {
+ stateSet_[index >> 5] |= (1u << (index & 31));
*l.template PushUnsafe<SizeType>() = index;
}
return s.out == kRegexInvalidState; // by using PushUnsafe() above, we can ensure s is not validated due to reallocation.
}
bool MatchRange(SizeType rangeIndex, unsigned codepoint) const {
- bool yes = (GetRange(rangeIndex).start & kRangeNegationFlag) == 0;
+ bool yes = (regex_.GetRange(rangeIndex).start & RegexType::kRangeNegationFlag) == 0;
while (rangeIndex != kRegexInvalidRange) {
- const Range& r = GetRange(rangeIndex);
- if (codepoint >= (r.start & ~kRangeNegationFlag) && codepoint <= r.end)
+ const Range& r = regex_.GetRange(rangeIndex);
+ if (codepoint >= (r.start & ~RegexType::kRangeNegationFlag) && codepoint <= r.end)
return yes;
rangeIndex = r.next;
}
return !yes;
}
- Stack<Allocator> states_;
- Stack<Allocator> ranges_;
- SizeType root_;
- SizeType stateCount_;
- SizeType rangeCount_;
-
- static const unsigned kInfinityQuantifier = ~0u;
-
- // For SearchWithAnchoring()
- uint32_t* stateSet_; // allocated by states_.GetAllocator()
- mutable Stack<Allocator> state0_;
- mutable Stack<Allocator> state1_;
- bool anchorBegin_;
- bool anchorEnd_;
+ const RegexType& regex_;
+ Allocator* allocator_;
+ Allocator* ownAllocator_;
+ Stack<Allocator> state0_;
+ Stack<Allocator> state1_;
+ uint32_t* stateSet_;
};
typedef GenericRegex<UTF8<> > Regex;
+typedef GenericRegexSearch<Regex> RegexSearch;
} // namespace internal
RAPIDJSON_NAMESPACE_END
@@ -693,4 +727,8 @@ RAPIDJSON_NAMESPACE_END
RAPIDJSON_DIAG_POP
#endif
+#ifdef _MSC_VER
+RAPIDJSON_DIAG_POP
+#endif
+
#endif // RAPIDJSON_INTERNAL_REGEX_H_
diff --git a/include/rapidjson/internal/stack.h b/include/rapidjson/internal/stack.h
index 022c9aab..5c5398c3 100644
--- a/include/rapidjson/internal/stack.h
+++ b/include/rapidjson/internal/stack.h
@@ -126,6 +126,7 @@ public:
template<typename T>
RAPIDJSON_FORCEINLINE T* PushUnsafe(size_t count = 1) {
+ RAPIDJSON_ASSERT(stackTop_);
RAPIDJSON_ASSERT(stackTop_ + sizeof(T) * count <= stackEnd_);
T* ret = reinterpret_cast<T*>(stackTop_);
stackTop_ += sizeof(T) * count;
@@ -183,7 +184,7 @@ private:
size_t newCapacity;
if (stack_ == 0) {
if (!allocator_)
- ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator());
+ ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator)();
newCapacity = initialCapacity_;
} else {
newCapacity = GetCapacity();
diff --git a/include/rapidjson/internal/strfunc.h b/include/rapidjson/internal/strfunc.h
index 2edfae52..226439a7 100644
--- a/include/rapidjson/internal/strfunc.h
+++ b/include/rapidjson/internal/strfunc.h
@@ -16,6 +16,7 @@
#define RAPIDJSON_INTERNAL_STRFUNC_H_
#include "../stream.h"
+#include <cwchar>
RAPIDJSON_NAMESPACE_BEGIN
namespace internal {
@@ -28,14 +29,27 @@ namespace internal {
*/
template <typename Ch>
inline SizeType StrLen(const Ch* s) {
+ RAPIDJSON_ASSERT(s != 0);
const Ch* p = s;
while (*p) ++p;
return SizeType(p - s);
}
+template <>
+inline SizeType StrLen(const char* s) {
+ return SizeType(std::strlen(s));
+}
+
+template <>
+inline SizeType StrLen(const wchar_t* s) {
+ return SizeType(std::wcslen(s));
+}
+
//! Returns number of code points in a encoded string.
template<typename Encoding>
bool CountStringCodePoint(const typename Encoding::Ch* s, SizeType length, SizeType* outCount) {
+ RAPIDJSON_ASSERT(s != 0);
+ RAPIDJSON_ASSERT(outCount != 0);
GenericStringStream<Encoding> is(s);
const typename Encoding::Ch* end = s + length;
SizeType count = 0;
diff --git a/include/rapidjson/internal/strtod.h b/include/rapidjson/internal/strtod.h
index fd4b01e8..adf49e34 100644
--- a/include/rapidjson/internal/strtod.h
+++ b/include/rapidjson/internal/strtod.h
@@ -140,9 +140,9 @@ inline bool StrtodDiyFp(const char* decimals, size_t length, size_t decimalPosit
significand++;
size_t remaining = length - i;
- const unsigned kUlpShift = 3;
- const unsigned kUlp = 1 << kUlpShift;
- int error = (remaining == 0) ? 0 : kUlp / 2;
+ const int kUlpShift = 3;
+ const int kUlp = 1 << kUlpShift;
+ int64_t error = (remaining == 0) ? 0 : kUlp / 2;
DiyFp v(significand, 0);
v = v.Normalize();
@@ -177,17 +177,17 @@ inline bool StrtodDiyFp(const char* decimals, size_t length, size_t decimalPosit
v = v.Normalize();
error <<= oldExp - v.e;
- const unsigned effectiveSignificandSize = Double::EffectiveSignificandSize(64 + v.e);
- unsigned precisionSize = 64 - effectiveSignificandSize;
+ const int effectiveSignificandSize = Double::EffectiveSignificandSize(64 + v.e);
+ int precisionSize = 64 - effectiveSignificandSize;
if (precisionSize + kUlpShift >= 64) {
- unsigned scaleExp = (precisionSize + kUlpShift) - 63;
+ int scaleExp = (precisionSize + kUlpShift) - 63;
v.f >>= scaleExp;
v.e += scaleExp;
- error = (error >> scaleExp) + 1 + static_cast<int>(kUlp);
+ error = (error >> scaleExp) + 1 + kUlp;
precisionSize -= scaleExp;
}
- DiyFp rounded(v.f >> precisionSize, v.e + static_cast<int>(precisionSize));
+ DiyFp rounded(v.f >> precisionSize, v.e + precisionSize);
const uint64_t precisionBits = (v.f & ((uint64_t(1) << precisionSize) - 1)) * kUlp;
const uint64_t halfWay = (uint64_t(1) << (precisionSize - 1)) * kUlp;
if (precisionBits >= halfWay + static_cast<unsigned>(error)) {
diff --git a/include/rapidjson/istreamwrapper.h b/include/rapidjson/istreamwrapper.h
index c73586e6..8639c8c3 100644
--- a/include/rapidjson/istreamwrapper.h
+++ b/include/rapidjson/istreamwrapper.h
@@ -12,6 +12,9 @@
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.
+#ifndef RAPIDJSON_ISTREAMWRAPPER_H_
+#define RAPIDJSON_ISTREAMWRAPPER_H_
+
#include "stream.h"
#include <iosfwd>
@@ -51,7 +54,7 @@ public:
Ch Peek() const {
typename StreamType::int_type c = stream_.peek();
- return RAPIDJSON_LIKELY(c != StreamType::traits_type::eof()) ? static_cast<Ch>(c) : '\0';
+ return RAPIDJSON_LIKELY(c != StreamType::traits_type::eof()) ? static_cast<Ch>(c) : static_cast<Ch>('\0');
}
Ch Take() {
@@ -108,3 +111,5 @@ RAPIDJSON_DIAG_POP
#endif
RAPIDJSON_NAMESPACE_END
+
+#endif // RAPIDJSON_ISTREAMWRAPPER_H_
diff --git a/include/rapidjson/ostreamwrapper.h b/include/rapidjson/ostreamwrapper.h
index 8bf36dcd..6f4667c0 100644
--- a/include/rapidjson/ostreamwrapper.h
+++ b/include/rapidjson/ostreamwrapper.h
@@ -12,6 +12,9 @@
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.
+#ifndef RAPIDJSON_OSTREAMWRAPPER_H_
+#define RAPIDJSON_OSTREAMWRAPPER_H_
+
#include "stream.h"
#include <iosfwd>
@@ -74,3 +77,5 @@ RAPIDJSON_DIAG_POP
#endif
RAPIDJSON_NAMESPACE_END
+
+#endif // RAPIDJSON_OSTREAMWRAPPER_H_
diff --git a/include/rapidjson/pointer.h b/include/rapidjson/pointer.h
index eddeab42..0f377efe 100644
--- a/include/rapidjson/pointer.h
+++ b/include/rapidjson/pointer.h
@@ -23,6 +23,11 @@ RAPIDJSON_DIAG_PUSH
RAPIDJSON_DIAG_OFF(switch-enum)
#endif
+#ifdef _MSC_VER
+RAPIDJSON_DIAG_PUSH
+RAPIDJSON_DIAG_OFF(4512) // assignment operator could not be generated
+#endif
+
RAPIDJSON_NAMESPACE_BEGIN
static const SizeType kPointerInvalidIndex = ~SizeType(0); //!< Represents an invalid index in GenericPointer::Token
@@ -235,7 +240,7 @@ public:
template <typename T>
RAPIDJSON_DISABLEIF_RETURN((internal::NotExpr<internal::IsSame<typename internal::RemoveConst<T>::Type, Ch> >), (GenericPointer))
Append(T* name, Allocator* allocator = 0) const {
- return Append(name, StrLen(name), allocator);
+ return Append(name, internal::StrLen(name), allocator);
}
#if RAPIDJSON_HAS_STDSTRING
@@ -269,7 +274,7 @@ public:
else {
Ch name[21];
for (size_t i = 0; i <= length; i++)
- name[i] = buffer[i];
+ name[i] = static_cast<Ch>(buffer[i]);
Token token = { name, length, index };
return Append(token, allocator);
}
@@ -753,7 +758,7 @@ private:
*/
Ch* CopyFromRaw(const GenericPointer& rhs, size_t extraToken = 0, size_t extraNameBufferSize = 0) {
if (!allocator_) // allocator is independently owned.
- ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator());
+ ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator)();
size_t nameBufferSize = rhs.tokenCount_; // null terminators for tokens
for (Token *t = rhs.tokens_; t != rhs.tokens_ + rhs.tokenCount_; ++t)
@@ -762,8 +767,12 @@ private:
tokenCount_ = rhs.tokenCount_ + extraToken;
tokens_ = static_cast<Token *>(allocator_->Malloc(tokenCount_ * sizeof(Token) + (nameBufferSize + extraNameBufferSize) * sizeof(Ch)));
nameBuffer_ = reinterpret_cast<Ch *>(tokens_ + tokenCount_);
- std::memcpy(tokens_, rhs.tokens_, rhs.tokenCount_ * sizeof(Token));
- std::memcpy(nameBuffer_, rhs.nameBuffer_, nameBufferSize * sizeof(Ch));
+ if (rhs.tokenCount_ > 0) {
+ std::memcpy(tokens_, rhs.tokens_, rhs.tokenCount_ * sizeof(Token));
+ }
+ if (nameBufferSize > 0) {
+ std::memcpy(nameBuffer_, rhs.nameBuffer_, nameBufferSize * sizeof(Ch));
+ }
// Adjust pointers to name buffer
std::ptrdiff_t diff = nameBuffer_ - rhs.nameBuffer_;
@@ -797,7 +806,7 @@ private:
// Create own allocator if user did not supply.
if (!allocator_)
- ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator());
+ ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator)();
// Count number of '/' as tokenCount
tokenCount_ = 0;
@@ -987,11 +996,11 @@ private:
src_++;
Ch c = 0;
for (int j = 0; j < 2; j++) {
- c <<= 4;
+ c = static_cast<Ch>(c << 4);
Ch h = *src_;
- if (h >= '0' && h <= '9') c += h - '0';
- else if (h >= 'A' && h <= 'F') c += h - 'A' + 10;
- else if (h >= 'a' && h <= 'f') c += h - 'a' + 10;
+ if (h >= '0' && h <= '9') c = static_cast<Ch>(c + h - '0');
+ else if (h >= 'A' && h <= 'F') c = static_cast<Ch>(c + h - 'A' + 10);
+ else if (h >= 'a' && h <= 'f') c = static_cast<Ch>(c + h - 'a' + 10);
else {
valid_ = false;
return 0;
@@ -1020,8 +1029,8 @@ private:
unsigned char u = static_cast<unsigned char>(c);
static const char hexDigits[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
os_.Put('%');
- os_.Put(hexDigits[u >> 4]);
- os_.Put(hexDigits[u & 15]);
+ os_.Put(static_cast<typename OutputStream::Ch>(hexDigits[u >> 4]));
+ os_.Put(static_cast<typename OutputStream::Ch>(hexDigits[u & 15]));
}
private:
OutputStream& os_;
@@ -1342,4 +1351,8 @@ RAPIDJSON_NAMESPACE_END
RAPIDJSON_DIAG_POP
#endif
+#ifdef _MSC_VER
+RAPIDJSON_DIAG_POP
+#endif
+
#endif // RAPIDJSON_POINTER_H_
diff --git a/include/rapidjson/prettywriter.h b/include/rapidjson/prettywriter.h
index 5ec4ccc3..98dfb306 100644
--- a/include/rapidjson/prettywriter.h
+++ b/include/rapidjson/prettywriter.h
@@ -22,8 +22,21 @@ RAPIDJSON_DIAG_PUSH
RAPIDJSON_DIAG_OFF(effc++)
#endif
+#if defined(__clang__)
+RAPIDJSON_DIAG_PUSH
+RAPIDJSON_DIAG_OFF(c++98-compat)
+#endif
+
RAPIDJSON_NAMESPACE_BEGIN
+//! Combination of PrettyWriter format flags.
+/*! \see PrettyWriter::SetFormatOptions
+ */
+enum PrettyFormatOptions {
+ kFormatDefault = 0, //!< Default pretty formatting.
+ kFormatSingleLineArray = 1 //!< Format arrays on a single line.
+};
+
//! Writer with indentation and spacing.
/*!
\tparam OutputStream Type of ouptut os.
@@ -34,7 +47,7 @@ RAPIDJSON_NAMESPACE_BEGIN
template<typename OutputStream, typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator, unsigned writeFlags = kWriteDefaultFlags>
class PrettyWriter : public Writer<OutputStream, SourceEncoding, TargetEncoding, StackAllocator, writeFlags> {
public:
- typedef Writer<OutputStream, SourceEncoding, TargetEncoding, StackAllocator> Base;
+ typedef Writer<OutputStream, SourceEncoding, TargetEncoding, StackAllocator, writeFlags> Base;
typedef typename Base::Ch Ch;
//! Constructor
@@ -43,12 +56,17 @@ public:
\param levelDepth Initial capacity of stack.
*/
explicit PrettyWriter(OutputStream& os, StackAllocator* allocator = 0, size_t levelDepth = Base::kDefaultLevelDepth) :
- Base(os, allocator, levelDepth), indentChar_(' '), indentCharCount_(4) {}
+ Base(os, allocator, levelDepth), indentChar_(' '), indentCharCount_(4), formatOptions_(kFormatDefault) {}
explicit PrettyWriter(StackAllocator* allocator = 0, size_t levelDepth = Base::kDefaultLevelDepth) :
Base(allocator, levelDepth), indentChar_(' '), indentCharCount_(4) {}
+#if RAPIDJSON_HAS_CXX11_RVALUE_REFS
+ PrettyWriter(PrettyWriter&& rhs) :
+ Base(std::forward<PrettyWriter>(rhs)), indentChar_(rhs.indentChar_), indentCharCount_(rhs.indentCharCount_), formatOptions_(rhs.formatOptions_) {}
+#endif
+
//! Set custom indentation.
/*! \param indentChar Character for indentation. Must be whitespace character (' ', '\\t', '\\n', '\\r').
\param indentCharCount Number of indent characters for each indentation level.
@@ -61,6 +79,14 @@ public:
return *this;
}
+ //! Set pretty writer formatting options.
+ /*! \param options Formatting options.
+ */
+ PrettyWriter& SetFormatOptions(PrettyFormatOptions options) {
+ formatOptions_ = options;
+ return *this;
+ }
+
/*! @name Implementation of Handler
\see Handler
*/
@@ -75,12 +101,14 @@ public:
bool Double(double d) { PrettyPrefix(kNumberType); return Base::WriteDouble(d); }
bool RawNumber(const Ch* str, SizeType length, bool copy = false) {
+ RAPIDJSON_ASSERT(str != 0);
(void)copy;
PrettyPrefix(kNumberType);
return Base::WriteString(str, length);
}
bool String(const Ch* str, SizeType length, bool copy = false) {
+ RAPIDJSON_ASSERT(str != 0);
(void)copy;
PrettyPrefix(kStringType);
return Base::WriteString(str, length);
@@ -99,11 +127,19 @@ public:
}
bool Key(const Ch* str, SizeType length, bool copy = false) { return String(str, length, copy); }
+
+#if RAPIDJSON_HAS_STDSTRING
+ bool Key(const std::basic_string<Ch>& str) {
+ return Key(str.data(), SizeType(str.size()));
+ }
+#endif
bool EndObject(SizeType memberCount = 0) {
(void)memberCount;
- RAPIDJSON_ASSERT(Base::level_stack_.GetSize() >= sizeof(typename Base::Level));
- RAPIDJSON_ASSERT(!Base::level_stack_.template Top<typename Base::Level>()->inArray);
+ RAPIDJSON_ASSERT(Base::level_stack_.GetSize() >= sizeof(typename Base::Level)); // not inside an Object
+ RAPIDJSON_ASSERT(!Base::level_stack_.template Top<typename Base::Level>()->inArray); // currently inside an Array, not Object
+ RAPIDJSON_ASSERT(0 == Base::level_stack_.template Top<typename Base::Level>()->valueCount % 2); // Object has a Key without a Value
+
bool empty = Base::level_stack_.template Pop<typename Base::Level>(1)->valueCount == 0;
if (!empty) {
@@ -114,7 +150,7 @@ public:
(void)ret;
RAPIDJSON_ASSERT(ret == true);
if (Base::level_stack_.Empty()) // end of json text
- Base::os_->Flush();
+ Base::Flush();
return true;
}
@@ -130,7 +166,7 @@ public:
RAPIDJSON_ASSERT(Base::level_stack_.template Top<typename Base::Level>()->inArray);
bool empty = Base::level_stack_.template Pop<typename Base::Level>(1)->valueCount == 0;
- if (!empty) {
+ if (!empty && !(formatOptions_ & kFormatSingleLineArray)) {
Base::os_->Put('\n');
WriteIndent();
}
@@ -138,7 +174,7 @@ public:
(void)ret;
RAPIDJSON_ASSERT(ret == true);
if (Base::level_stack_.Empty()) // end of json text
- Base::os_->Flush();
+ Base::Flush();
return true;
}
@@ -162,7 +198,11 @@ public:
\param type Type of the root of json.
\note When using PrettyWriter::RawValue(), the result json may not be indented correctly.
*/
- bool RawValue(const Ch* json, size_t length, Type type) { PrettyPrefix(type); return Base::WriteRawValue(json, length); }
+ bool RawValue(const Ch* json, size_t length, Type type) {
+ RAPIDJSON_ASSERT(json != 0);
+ PrettyPrefix(type);
+ return Base::WriteRawValue(json, length);
+ }
protected:
void PrettyPrefix(Type type) {
@@ -173,11 +213,14 @@ protected:
if (level->inArray) {
if (level->valueCount > 0) {
Base::os_->Put(','); // add comma if it is not the first element in array
- Base::os_->Put('\n');
+ if (formatOptions_ & kFormatSingleLineArray)
+ Base::os_->Put(' ');
}
- else
+
+ if (!(formatOptions_ & kFormatSingleLineArray)) {
Base::os_->Put('\n');
- WriteIndent();
+ WriteIndent();
+ }
}
else { // in object
if (level->valueCount > 0) {
@@ -208,11 +251,12 @@ protected:
void WriteIndent() {
size_t count = (Base::level_stack_.GetSize() / sizeof(typename Base::Level)) * indentCharCount_;
- PutN(*Base::os_, static_cast<typename TargetEncoding::Ch>(indentChar_), count);
+ PutN(*Base::os_, static_cast<typename OutputStream::Ch>(indentChar_), count);
}
Ch indentChar_;
unsigned indentCharCount_;
+ PrettyFormatOptions formatOptions_;
private:
// Prohibit copy constructor & assignment operator.
@@ -222,6 +266,10 @@ private:
RAPIDJSON_NAMESPACE_END
+#if defined(__clang__)
+RAPIDJSON_DIAG_POP
+#endif
+
#ifdef __GNUC__
RAPIDJSON_DIAG_POP
#endif
diff --git a/include/rapidjson/rapidjson.h b/include/rapidjson/rapidjson.h
index c4410640..5716fdc0 100644
--- a/include/rapidjson/rapidjson.h
+++ b/include/rapidjson/rapidjson.h
@@ -49,6 +49,11 @@
// token stringification
#define RAPIDJSON_STRINGIFY(x) RAPIDJSON_DO_STRINGIFY(x)
#define RAPIDJSON_DO_STRINGIFY(x) #x
+
+// token concatenation
+#define RAPIDJSON_JOIN(X, Y) RAPIDJSON_DO_JOIN(X, Y)
+#define RAPIDJSON_DO_JOIN(X, Y) RAPIDJSON_DO_JOIN2(X, Y)
+#define RAPIDJSON_DO_JOIN2(X, Y) X##Y
//!@endcond
/*! \def RAPIDJSON_MAJOR_VERSION
@@ -68,8 +73,8 @@
\brief Version of RapidJSON in "<major>.<minor>.<patch>" string format.
*/
#define RAPIDJSON_MAJOR_VERSION 1
-#define RAPIDJSON_MINOR_VERSION 0
-#define RAPIDJSON_PATCH_VERSION 2
+#define RAPIDJSON_MINOR_VERSION 1
+#define RAPIDJSON_PATCH_VERSION 0
#define RAPIDJSON_VERSION_STRING \
RAPIDJSON_STRINGIFY(RAPIDJSON_MAJOR_VERSION.RAPIDJSON_MINOR_VERSION.RAPIDJSON_PATCH_VERSION)
@@ -236,7 +241,7 @@
# define RAPIDJSON_ENDIAN RAPIDJSON_BIGENDIAN
# elif defined(__i386__) || defined(__alpha__) || defined(__ia64) || defined(__ia64__) || defined(_M_IX86) || defined(_M_IA64) || defined(_M_ALPHA) || defined(__amd64) || defined(__amd64__) || defined(_M_AMD64) || defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || defined(__bfin__)
# define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN
-# elif defined(_MSC_VER) && defined(_M_ARM)
+# elif defined(_MSC_VER) && (defined(_M_ARM) || defined(_M_ARM64))
# define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN
# elif defined(RAPIDJSON_DOXYGEN_RUNNING)
# define RAPIDJSON_ENDIAN
@@ -250,7 +255,7 @@
//! Whether using 64-bit architecture
#ifndef RAPIDJSON_64BIT
-#if defined(__LP64__) || defined(_WIN64) || defined(__EMSCRIPTEN__)
+#if defined(__LP64__) || (defined(__x86_64__) && defined(__ILP32__)) || defined(_WIN64) || defined(__EMSCRIPTEN__)
#define RAPIDJSON_64BIT 1
#else
#define RAPIDJSON_64BIT 0
@@ -320,17 +325,17 @@
#endif
///////////////////////////////////////////////////////////////////////////////
-// RAPIDJSON_SSE2/RAPIDJSON_SSE42/RAPIDJSON_SIMD
+// RAPIDJSON_SSE2/RAPIDJSON_SSE42/RAPIDJSON_NEON/RAPIDJSON_SIMD
/*! \def RAPIDJSON_SIMD
\ingroup RAPIDJSON_CONFIG
- \brief Enable SSE2/SSE4.2 optimization.
+ \brief Enable SSE2/SSE4.2/Neon optimization.
RapidJSON supports optimized implementations for some parsing operations
- based on the SSE2 or SSE4.2 SIMD extensions on modern Intel-compatible
- processors.
+ based on the SSE2, SSE4.2 or NEon SIMD extensions on modern Intel
+ or ARM compatible processors.
- To enable these optimizations, two different symbols can be defined;
+ To enable these optimizations, three different symbols can be defined;
\code
// Enable SSE2 optimization.
#define RAPIDJSON_SSE2
@@ -339,13 +344,17 @@
#define RAPIDJSON_SSE42
\endcode
- \c RAPIDJSON_SSE42 takes precedence, if both are defined.
+ // Enable ARM Neon optimization.
+ #define RAPIDJSON_NEON
+ \endcode
+
+ \c RAPIDJSON_SSE42 takes precedence over SSE2, if both are defined.
If any of these symbols is defined, RapidJSON defines the macro
\c RAPIDJSON_SIMD to indicate the availability of the optimized code.
*/
#if defined(RAPIDJSON_SSE2) || defined(RAPIDJSON_SSE42) \
- || defined(RAPIDJSON_DOXYGEN_RUNNING)
+ || defined(RAPIDJSON_NEON) || defined(RAPIDJSON_DOXYGEN_RUNNING)
#define RAPIDJSON_SIMD
#endif
@@ -405,7 +414,15 @@ RAPIDJSON_NAMESPACE_END
///////////////////////////////////////////////////////////////////////////////
// RAPIDJSON_STATIC_ASSERT
-// Adopt from boost
+// Prefer C++11 static_assert, if available
+#ifndef RAPIDJSON_STATIC_ASSERT
+#if __cplusplus >= 201103L || ( defined(_MSC_VER) && _MSC_VER >= 1800 )
+#define RAPIDJSON_STATIC_ASSERT(x) \
+ static_assert(x, RAPIDJSON_STRINGIFY(x))
+#endif // C++11
+#endif // RAPIDJSON_STATIC_ASSERT
+
+// Adopt C++03 implementation from boost
#ifndef RAPIDJSON_STATIC_ASSERT
#ifndef __clang__
//!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN
@@ -413,13 +430,9 @@ RAPIDJSON_NAMESPACE_END
RAPIDJSON_NAMESPACE_BEGIN
template <bool x> struct STATIC_ASSERTION_FAILURE;
template <> struct STATIC_ASSERTION_FAILURE<true> { enum { value = 1 }; };
-template<int x> struct StaticAssertTest {};
+template <size_t x> struct StaticAssertTest {};
RAPIDJSON_NAMESPACE_END
-#define RAPIDJSON_JOIN(X, Y) RAPIDJSON_DO_JOIN(X, Y)
-#define RAPIDJSON_DO_JOIN(X, Y) RAPIDJSON_DO_JOIN2(X, Y)
-#define RAPIDJSON_DO_JOIN2(X, Y) X##Y
-
#if defined(__GNUC__)
#define RAPIDJSON_STATIC_ASSERT_UNUSED_ATTRIBUTE __attribute__((unused))
#else
@@ -438,7 +451,7 @@ RAPIDJSON_NAMESPACE_END
typedef ::RAPIDJSON_NAMESPACE::StaticAssertTest< \
sizeof(::RAPIDJSON_NAMESPACE::STATIC_ASSERTION_FAILURE<bool(x) >)> \
RAPIDJSON_JOIN(StaticAssertTypedef, __LINE__) RAPIDJSON_STATIC_ASSERT_UNUSED_ATTRIBUTE
-#endif
+#endif // RAPIDJSON_STATIC_ASSERT
///////////////////////////////////////////////////////////////////////////////
// RAPIDJSON_LIKELY, RAPIDJSON_UNLIKELY
@@ -529,8 +542,12 @@ RAPIDJSON_NAMESPACE_END
#ifndef RAPIDJSON_HAS_CXX11_RVALUE_REFS
#if defined(__clang__)
-#define RAPIDJSON_HAS_CXX11_RVALUE_REFS __has_feature(cxx_rvalue_references) && \
+#if __has_feature(cxx_rvalue_references) && \
(defined(_LIBCPP_VERSION) || defined(__GLIBCXX__) && __GLIBCXX__ >= 20080306)
+#define RAPIDJSON_HAS_CXX11_RVALUE_REFS 1
+#else
+#define RAPIDJSON_HAS_CXX11_RVALUE_REFS 0
+#endif
#elif (defined(RAPIDJSON_GNUC) && (RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4,3,0)) && defined(__GXX_EXPERIMENTAL_CXX0X__)) || \
(defined(_MSC_VER) && _MSC_VER >= 1600)
@@ -564,7 +581,7 @@ RAPIDJSON_NAMESPACE_END
#ifndef RAPIDJSON_HAS_CXX11_RANGE_FOR
#if defined(__clang__)
#define RAPIDJSON_HAS_CXX11_RANGE_FOR __has_feature(cxx_range_for)
-#elif (defined(RAPIDJSON_GNUC) && (RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4,3,0)) && defined(__GXX_EXPERIMENTAL_CXX0X__)) || \
+#elif (defined(RAPIDJSON_GNUC) && (RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4,6,0)) && defined(__GXX_EXPERIMENTAL_CXX0X__)) || \
(defined(_MSC_VER) && _MSC_VER >= 1700)
#define RAPIDJSON_HAS_CXX11_RANGE_FOR 1
#else
@@ -579,7 +596,7 @@ RAPIDJSON_NAMESPACE_END
#ifndef RAPIDJSON_NEW
///! customization point for global \c new
-#define RAPIDJSON_NEW(x) new x
+#define RAPIDJSON_NEW(TypeName) new TypeName
#endif
#ifndef RAPIDJSON_DELETE
///! customization point for global \c delete
diff --git a/include/rapidjson/reader.h b/include/rapidjson/reader.h
index a143c419..681dec28 100644
--- a/include/rapidjson/reader.h
+++ b/include/rapidjson/reader.h
@@ -1,5 +1,5 @@
// Tencent is pleased to support the open source community by making RapidJSON available.
-//
+//
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
//
// Licensed under the MIT License (the "License"); you may not use this file except
@@ -7,9 +7,9 @@
//
// http://opensource.org/licenses/MIT
//
-// Unless required by applicable law or agreed to in writing, software distributed
-// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
-// CONDITIONS OF ANY KIND, either express or implied. See the License for the
+// Unless required by applicable law or agreed to in writing, software distributed
+// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
+// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.
#ifndef RAPIDJSON_READER_H_
@@ -23,6 +23,7 @@
#include "internal/meta.h"
#include "internal/stack.h"
#include "internal/strtod.h"
+#include <limits>
#if defined(RAPIDJSON_SIMD) && defined(_MSC_VER)
#include <intrin.h>
@@ -32,6 +33,8 @@
#include <nmmintrin.h>
#elif defined(RAPIDJSON_SSE2)
#include <emmintrin.h>
+#elif defined(RAPIDJSON_NEON)
+#include <arm_neon.h>
#endif
#ifdef _MSC_VER
@@ -42,6 +45,7 @@ RAPIDJSON_DIAG_OFF(4702) // unreachable code
#ifdef __clang__
RAPIDJSON_DIAG_PUSH
+RAPIDJSON_DIAG_OFF(old-style-cast)
RAPIDJSON_DIAG_OFF(padded)
RAPIDJSON_DIAG_OFF(switch-enum)
#endif
@@ -127,7 +131,7 @@ RAPIDJSON_NAMESPACE_BEGIN
///////////////////////////////////////////////////////////////////////////////
// ParseFlag
-/*! \def RAPIDJSON_PARSE_DEFAULT_FLAGS
+/*! \def RAPIDJSON_PARSE_DEFAULT_FLAGS
\ingroup RAPIDJSON_CONFIG
\brief User-defined kParseDefaultFlags definition.
@@ -149,6 +153,8 @@ enum ParseFlag {
kParseFullPrecisionFlag = 16, //!< Parse number in full precision (but slower).
kParseCommentsFlag = 32, //!< Allow one-line (//) and multi-line (/**/) comments.
kParseNumbersAsStringsFlag = 64, //!< Parse all numbers (ints/doubles) as strings.
+ kParseTrailingCommasFlag = 128, //!< Allow trailing commas at the end of objects and arrays.
+ kParseNanAndInfFlag = 256, //!< Allow parsing NaN, Inf, Infinity, -Inf and -Infinity as doubles.
kParseDefaultFlags = RAPIDJSON_PARSE_DEFAULT_FLAGS //!< Default parse flags. Can be customized by defining RAPIDJSON_PARSE_DEFAULT_FLAGS
};
@@ -157,7 +163,7 @@ enum ParseFlag {
/*! \class rapidjson::Handler
\brief Concept for receiving events from GenericReader upon parsing.
- The functions return true if no error occurs. If they return false,
+ The functions return true if no error occurs. If they return false,
the event publisher should terminate the process.
\code
concept Handler {
@@ -261,7 +267,8 @@ void SkipWhitespace(InputStream& is) {
internal::StreamLocalCopy<InputStream> copy(is);
InputStream& s(copy.s);
- while (s.Peek() == ' ' || s.Peek() == '\n' || s.Peek() == '\r' || s.Peek() == '\t')
+ typename InputStream::Ch c;
+ while ((c = s.Peek()) == ' ' || c == '\n' || c == '\r' || c == '\t')
s.Take();
}
@@ -294,16 +301,9 @@ inline const char *SkipWhitespace_SIMD(const char* p) {
for (;; p += 16) {
const __m128i s = _mm_load_si128(reinterpret_cast<const __m128i *>(p));
- const int r = _mm_cvtsi128_si32(_mm_cmpistrm(w, s, _SIDD_UBYTE_OPS | _SIDD_CMP_EQUAL_ANY | _SIDD_BIT_MASK | _SIDD_NEGATIVE_POLARITY));
- if (r != 0) { // some of characters is non-whitespace
-#ifdef _MSC_VER // Find the index of first non-whitespace
- unsigned long offset;
- _BitScanForward(&offset, r);
- return p + offset;
-#else
- return p + __builtin_ffs(r) - 1;
-#endif
- }
+ const int r = _mm_cmpistri(w, s, _SIDD_UBYTE_OPS | _SIDD_CMP_EQUAL_ANY | _SIDD_LEAST_SIGNIFICANT | _SIDD_NEGATIVE_POLARITY);
+ if (r != 16) // some of characters is non-whitespace
+ return p + r;
}
}
@@ -320,16 +320,9 @@ inline const char *SkipWhitespace_SIMD(const char* p, const char* end) {
for (; p <= end - 16; p += 16) {
const __m128i s = _mm_loadu_si128(reinterpret_cast<const __m128i *>(p));
- const int r = _mm_cvtsi128_si32(_mm_cmpistrm(w, s, _SIDD_UBYTE_OPS | _SIDD_CMP_EQUAL_ANY | _SIDD_BIT_MASK | _SIDD_NEGATIVE_POLARITY));
- if (r != 0) { // some of characters is non-whitespace
-#ifdef _MSC_VER // Find the index of first non-whitespace
- unsigned long offset;
- _BitScanForward(&offset, r);
- return p + offset;
-#else
- return p + __builtin_ffs(r) - 1;
-#endif
- }
+ const int r = _mm_cmpistri(w, s, _SIDD_UBYTE_OPS | _SIDD_CMP_EQUAL_ANY | _SIDD_LEAST_SIGNIFICANT | _SIDD_NEGATIVE_POLARITY);
+ if (r != 16) // some of characters is non-whitespace
+ return p + r;
}
return SkipWhitespace(p, end);
@@ -420,11 +413,96 @@ inline const char *SkipWhitespace_SIMD(const char* p, const char* end) {
return SkipWhitespace(p, end);
}
-#endif // RAPIDJSON_SSE2
+#elif defined(RAPIDJSON_NEON)
+
+//! Skip whitespace with ARM Neon instructions, testing 16 8-byte characters at once.
+inline const char *SkipWhitespace_SIMD(const char* p) {
+ // Fast return for single non-whitespace
+ if (*p == ' ' || *p == '\n' || *p == '\r' || *p == '\t')
+ ++p;
+ else
+ return p;
+
+ // 16-byte align to the next boundary
+ const char* nextAligned = reinterpret_cast<const char*>((reinterpret_cast<size_t>(p) + 15) & static_cast<size_t>(~15));
+ while (p != nextAligned)
+ if (*p == ' ' || *p == '\n' || *p == '\r' || *p == '\t')
+ ++p;
+ else
+ return p;
+
+ const uint8x16_t w0 = vmovq_n_u8(' ');
+ const uint8x16_t w1 = vmovq_n_u8('\n');
+ const uint8x16_t w2 = vmovq_n_u8('\r');
+ const uint8x16_t w3 = vmovq_n_u8('\t');
+
+ for (;; p += 16) {
+ const uint8x16_t s = vld1q_u8(reinterpret_cast<const uint8_t *>(p));
+ uint8x16_t x = vceqq_u8(s, w0);
+ x = vorrq_u8(x, vceqq_u8(s, w1));
+ x = vorrq_u8(x, vceqq_u8(s, w2));
+ x = vorrq_u8(x, vceqq_u8(s, w3));
+
+ x = vmvnq_u8(x); // Negate
+ x = vrev64q_u8(x); // Rev in 64
+ uint64_t low = vgetq_lane_u64(reinterpret_cast<uint64x2_t>(x), 0); // extract
+ uint64_t high = vgetq_lane_u64(reinterpret_cast<uint64x2_t>(x), 1); // extract
+
+ if (low == 0) {
+ if (high != 0) {
+ int lz =__builtin_clzll(high);;
+ return p + 8 + (lz >> 3);
+ }
+ } else {
+ int lz = __builtin_clzll(low);;
+ return p + (lz >> 3);
+ }
+ }
+}
+
+inline const char *SkipWhitespace_SIMD(const char* p, const char* end) {
+ // Fast return for single non-whitespace
+ if (p != end && (*p == ' ' || *p == '\n' || *p == '\r' || *p == '\t'))
+ ++p;
+ else
+ return p;
+
+ const uint8x16_t w0 = vmovq_n_u8(' ');
+ const uint8x16_t w1 = vmovq_n_u8('\n');
+ const uint8x16_t w2 = vmovq_n_u8('\r');
+ const uint8x16_t w3 = vmovq_n_u8('\t');
+
+ for (; p <= end - 16; p += 16) {
+ const uint8x16_t s = vld1q_u8(reinterpret_cast<const uint8_t *>(p));
+ uint8x16_t x = vceqq_u8(s, w0);
+ x = vorrq_u8(x, vceqq_u8(s, w1));
+ x = vorrq_u8(x, vceqq_u8(s, w2));
+ x = vorrq_u8(x, vceqq_u8(s, w3));
+
+ x = vmvnq_u8(x); // Negate
+ x = vrev64q_u8(x); // Rev in 64
+ uint64_t low = vgetq_lane_u64(reinterpret_cast<uint64x2_t>(x), 0); // extract
+ uint64_t high = vgetq_lane_u64(reinterpret_cast<uint64x2_t>(x), 1); // extract
+
+ if (low == 0) {
+ if (high != 0) {
+ int lz = __builtin_clzll(high);
+ return p + 8 + (lz >> 3);
+ }
+ } else {
+ int lz = __builtin_clzll(low);
+ return p + (lz >> 3);
+ }
+ }
+
+ return SkipWhitespace(p, end);
+}
+
+#endif // RAPIDJSON_NEON
#ifdef RAPIDJSON_SIMD
//! Template function specialization for InsituStringStream
-template<> inline void SkipWhitespace(InsituStringStream& is) {
+template<> inline void SkipWhitespace(InsituStringStream& is) {
is.src_ = const_cast<char*>(SkipWhitespace_SIMD(is.src_));
}
@@ -442,17 +520,17 @@ template<> inline void SkipWhitespace(EncodedInputStream<UTF8<>, MemoryStream>&
// GenericReader
//! SAX-style JSON parser. Use \ref Reader for UTF8 encoding and default allocator.
-/*! GenericReader parses JSON text from a stream, and send events synchronously to an
+/*! GenericReader parses JSON text from a stream, and send events synchronously to an
object implementing Handler concept.
- It needs to allocate a stack for storing a single decoded string during
+ It needs to allocate a stack for storing a single decoded string during
non-destructive parsing.
- For in-situ parsing, the decoded string is directly written to the source
+ For in-situ parsing, the decoded string is directly written to the source
text string, no temporary buffer is required.
A GenericReader object can be reused for parsing multiple JSON text.
-
+
\tparam SourceEncoding Encoding of the input stream.
\tparam TargetEncoding Encoding of the parse output.
\tparam StackAllocator Allocator type for stack.
@@ -466,7 +544,8 @@ public:
/*! \param stackAllocator Optional allocator for allocating stack memory. (Only use for non-destructive parsing)
\param stackCapacity stack capacity in bytes for storing a single decoded string. (Only use for non-destructive parsing)
*/
- GenericReader(StackAllocator* stackAllocator = 0, size_t stackCapacity = kDefaultStackCapacity) : stack_(stackAllocator, stackCapacity), parseResult_() {}
+ GenericReader(StackAllocator* stackAllocator = 0, size_t stackCapacity = kDefaultStackCapacity) :
+ stack_(stackAllocator, stackCapacity), parseResult_(), state_(IterativeParsingStartState) {}
//! Parse JSON text.
/*! \tparam parseFlags Combination of \ref ParseFlag.
@@ -522,9 +601,86 @@ public:
return Parse<kParseDefaultFlags>(is, handler);
}
+ //! Initialize JSON text token-by-token parsing
+ /*!
+ */
+ void IterativeParseInit() {
+ parseResult_.Clear();
+ state_ = IterativeParsingStartState;
+ }
+
+ //! Parse one token from JSON text
+ /*! \tparam InputStream Type of input stream, implementing Stream concept
+ \tparam Handler Type of handler, implementing Handler concept.
+ \param is Input stream to be parsed.
+ \param handler The handler to receive events.
+ \return Whether the parsing is successful.
+ */
+ template <unsigned parseFlags, typename InputStream, typename Handler>
+ bool IterativeParseNext(InputStream& is, Handler& handler) {
+ while (RAPIDJSON_LIKELY(is.Peek() != '\0')) {
+ SkipWhitespaceAndComments<parseFlags>(is);
+
+ Token t = Tokenize(is.Peek());
+ IterativeParsingState n = Predict(state_, t);
+ IterativeParsingState d = Transit<parseFlags>(state_, t, n, is, handler);
+
+ // If we've finished or hit an error...
+ if (RAPIDJSON_UNLIKELY(IsIterativeParsingCompleteState(d))) {
+ // Report errors.
+ if (d == IterativeParsingErrorState) {
+ HandleError(state_, is);
+ return false;
+ }
+
+ // Transition to the finish state.
+ RAPIDJSON_ASSERT(d == IterativeParsingFinishState);
+ state_ = d;
+
+ // If StopWhenDone is not set...
+ if (!(parseFlags & kParseStopWhenDoneFlag)) {
+ // ... and extra non-whitespace data is found...
+ SkipWhitespaceAndComments<parseFlags>(is);
+ if (is.Peek() != '\0') {
+ // ... this is considered an error.
+ HandleError(state_, is);
+ return false;
+ }
+ }
+
+ // Success! We are done!
+ return true;
+ }
+
+ // Transition to the new state.
+ state_ = d;
+
+ // If we parsed anything other than a delimiter, we invoked the handler, so we can return true now.
+ if (!IsIterativeParsingDelimiterState(n))
+ return true;
+ }
+
+ // We reached the end of file.
+ stack_.Clear();
+
+ if (state_ != IterativeParsingFinishState) {
+ HandleError(state_, is);
+ return false;
+ }
+
+ return true;
+ }
+
+ //! Check if token-by-token parsing JSON text is complete
+ /*! \return Whether the JSON has been fully decoded.
+ */
+ RAPIDJSON_FORCEINLINE bool IterativeParseComplete() {
+ return IsIterativeParsingCompleteState(state_);
+ }
+
//! Whether a parse error has occured in the last parsing.
bool HasParseError() const { return parseResult_.IsError(); }
-
+
//! Get the \ref ParseErrorCode of last parsing.
ParseErrorCode GetParseErrorCode() const { return parseResult_.Code(); }
@@ -570,7 +726,7 @@ private:
}
}
else if (RAPIDJSON_LIKELY(Consume(is, '/')))
- while (is.Peek() != '\0' && is.Take() != '\n');
+ while (is.Peek() != '\0' && is.Take() != '\n') {}
else
RAPIDJSON_PARSE_ERROR(kParseErrorUnspecificSyntaxError, is.Tell());
@@ -584,7 +740,7 @@ private:
void ParseObject(InputStream& is, Handler& handler) {
RAPIDJSON_ASSERT(is.Peek() == '{');
is.Take(); // Skip '{'
-
+
if (RAPIDJSON_UNLIKELY(!handler.StartObject()))
RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell());
@@ -627,14 +783,22 @@ private:
SkipWhitespaceAndComments<parseFlags>(is);
RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
break;
- case '}':
+ case '}':
is.Take();
if (RAPIDJSON_UNLIKELY(!handler.EndObject(memberCount)))
RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell());
return;
- default:
- RAPIDJSON_PARSE_ERROR(kParseErrorObjectMissCommaOrCurlyBracket, is.Tell());
- break;
+ default:
+ RAPIDJSON_PARSE_ERROR(kParseErrorObjectMissCommaOrCurlyBracket, is.Tell()); break; // This useless break is only for making warning and coverage happy
+ }
+
+ if (parseFlags & kParseTrailingCommasFlag) {
+ if (is.Peek() == '}') {
+ if (RAPIDJSON_UNLIKELY(!handler.EndObject(memberCount)))
+ RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell());
+ is.Take();
+ return;
+ }
}
}
}
@@ -644,10 +808,10 @@ private:
void ParseArray(InputStream& is, Handler& handler) {
RAPIDJSON_ASSERT(is.Peek() == '[');
is.Take(); // Skip '['
-
+
if (RAPIDJSON_UNLIKELY(!handler.StartArray()))
RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell());
-
+
SkipWhitespaceAndComments<parseFlags>(is);
RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
@@ -676,6 +840,15 @@ private:
}
else
RAPIDJSON_PARSE_ERROR(kParseErrorArrayMissCommaOrSquareBracket, is.Tell());
+
+ if (parseFlags & kParseTrailingCommasFlag) {
+ if (is.Peek() == ']') {
+ if (RAPIDJSON_UNLIKELY(!handler.EndArray(elementCount)))
+ RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell());
+ is.Take();
+ return;
+ }
+ }
}
}
@@ -761,7 +934,7 @@ private:
*stack_.template Push<Ch>() = c;
++length_;
}
-
+
RAPIDJSON_FORCEINLINE void* Push(SizeType count) {
length_ += count;
return stack_.template Push<Ch>(count);
@@ -819,10 +992,10 @@ private:
//!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN
#define Z16 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
static const char escape[256] = {
- Z16, Z16, 0, 0,'\"', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,'/',
- Z16, Z16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,'\\', 0, 0, 0,
- 0, 0,'\b', 0, 0, 0,'\f', 0, 0, 0, 0, 0, 0, 0,'\n', 0,
- 0, 0,'\r', 0,'\t', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ Z16, Z16, 0, 0,'\"', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,'/',
+ Z16, Z16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,'\\', 0, 0, 0,
+ 0, 0,'\b', 0, 0, 0,'\f', 0, 0, 0, 0, 0, 0, 0,'\n', 0,
+ 0, 0,'\r', 0,'\t', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Z16, Z16, Z16, Z16, Z16, Z16, Z16, Z16
};
#undef Z16
@@ -870,12 +1043,12 @@ private:
if (c == '\0')
RAPIDJSON_PARSE_ERROR(kParseErrorStringMissQuotationMark, is.Tell());
else
- RAPIDJSON_PARSE_ERROR(kParseErrorStringEscapeInvalid, is.Tell());
+ RAPIDJSON_PARSE_ERROR(kParseErrorStringInvalidEncoding, is.Tell());
}
else {
size_t offset = is.Tell();
- if (RAPIDJSON_UNLIKELY((parseFlags & kParseValidateEncodingFlag ?
- !Transcoder<SEncoding, TEncoding>::Validate(is, os) :
+ if (RAPIDJSON_UNLIKELY((parseFlags & kParseValidateEncodingFlag ?
+ !Transcoder<SEncoding, TEncoding>::Validate(is, os) :
!Transcoder<SEncoding, TEncoding>::Transcode(is, os))))
RAPIDJSON_PARSE_ERROR(kParseErrorStringInvalidEncoding, offset);
}
@@ -905,7 +1078,7 @@ private:
// The rest of string using SIMD
static const char dquote[16] = { '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"' };
static const char bslash[16] = { '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\' };
- static const char space[16] = { 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19 };
+ static const char space[16] = { 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F };
const __m128i dq = _mm_loadu_si128(reinterpret_cast<const __m128i *>(&dquote[0]));
const __m128i bs = _mm_loadu_si128(reinterpret_cast<const __m128i *>(&bslash[0]));
const __m128i sp = _mm_loadu_si128(reinterpret_cast<const __m128i *>(&space[0]));
@@ -914,7 +1087,7 @@ private:
const __m128i s = _mm_load_si128(reinterpret_cast<const __m128i *>(p));
const __m128i t1 = _mm_cmpeq_epi8(s, dq);
const __m128i t2 = _mm_cmpeq_epi8(s, bs);
- const __m128i t3 = _mm_cmpeq_epi8(_mm_max_epu8(s, sp), sp); // s < 0x20 <=> max(s, 0x19) == 0x19
+ const __m128i t3 = _mm_cmpeq_epi8(_mm_max_epu8(s, sp), sp); // s < 0x20 <=> max(s, 0x1F) == 0x1F
const __m128i x = _mm_or_si128(_mm_or_si128(t1, t2), t3);
unsigned short r = static_cast<unsigned short>(_mm_movemask_epi8(x));
if (RAPIDJSON_UNLIKELY(r != 0)) { // some of characters is escaped
@@ -926,16 +1099,18 @@ private:
#else
length = static_cast<SizeType>(__builtin_ffs(r) - 1);
#endif
- char* q = reinterpret_cast<char*>(os.Push(length));
- for (size_t i = 0; i < length; i++)
- q[i] = p[i];
+ if (length != 0) {
+ char* q = reinterpret_cast<char*>(os.Push(length));
+ for (size_t i = 0; i < length; i++)
+ q[i] = p[i];
- p += length;
+ p += length;
+ }
break;
}
_mm_storeu_si128(reinterpret_cast<__m128i *>(os.Push(16)), s);
}
-
+
is.src_ = p;
}
@@ -958,7 +1133,7 @@ private:
if (RAPIDJSON_UNLIKELY(*p == '\"') || RAPIDJSON_UNLIKELY(*p == '\\') || RAPIDJSON_UNLIKELY(static_cast<unsigned>(*p) < 0x20)) {
is.src_ = p;
is.dst_ = q;
- return;
+ return;
}
else
*q++ = *p++;
@@ -966,7 +1141,7 @@ private:
// The rest of string using SIMD
static const char dquote[16] = { '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"' };
static const char bslash[16] = { '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\' };
- static const char space[16] = { 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19 };
+ static const char space[16] = { 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F };
const __m128i dq = _mm_loadu_si128(reinterpret_cast<const __m128i *>(&dquote[0]));
const __m128i bs = _mm_loadu_si128(reinterpret_cast<const __m128i *>(&bslash[0]));
const __m128i sp = _mm_loadu_si128(reinterpret_cast<const __m128i *>(&space[0]));
@@ -975,7 +1150,7 @@ private:
const __m128i s = _mm_load_si128(reinterpret_cast<const __m128i *>(p));
const __m128i t1 = _mm_cmpeq_epi8(s, dq);
const __m128i t2 = _mm_cmpeq_epi8(s, bs);
- const __m128i t3 = _mm_cmpeq_epi8(_mm_max_epu8(s, sp), sp); // s < 0x20 <=> max(s, 0x19) == 0x19
+ const __m128i t3 = _mm_cmpeq_epi8(_mm_max_epu8(s, sp), sp); // s < 0x20 <=> max(s, 0x1F) == 0x1F
const __m128i x = _mm_or_si128(_mm_or_si128(t1, t2), t3);
unsigned short r = static_cast<unsigned short>(_mm_movemask_epi8(x));
if (RAPIDJSON_UNLIKELY(r != 0)) { // some of characters is escaped
@@ -1014,7 +1189,7 @@ private:
// The rest of string using SIMD
static const char dquote[16] = { '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"' };
static const char bslash[16] = { '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\' };
- static const char space[16] = { 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19 };
+ static const char space[16] = { 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F };
const __m128i dq = _mm_loadu_si128(reinterpret_cast<const __m128i *>(&dquote[0]));
const __m128i bs = _mm_loadu_si128(reinterpret_cast<const __m128i *>(&bslash[0]));
const __m128i sp = _mm_loadu_si128(reinterpret_cast<const __m128i *>(&space[0]));
@@ -1023,7 +1198,7 @@ private:
const __m128i s = _mm_load_si128(reinterpret_cast<const __m128i *>(p));
const __m128i t1 = _mm_cmpeq_epi8(s, dq);
const __m128i t2 = _mm_cmpeq_epi8(s, bs);
- const __m128i t3 = _mm_cmpeq_epi8(_mm_max_epu8(s, sp), sp); // s < 0x20 <=> max(s, 0x19) == 0x19
+ const __m128i t3 = _mm_cmpeq_epi8(_mm_max_epu8(s, sp), sp); // s < 0x20 <=> max(s, 0x1F) == 0x1F
const __m128i x = _mm_or_si128(_mm_or_si128(t1, t2), t3);
unsigned short r = static_cast<unsigned short>(_mm_movemask_epi8(x));
if (RAPIDJSON_UNLIKELY(r != 0)) { // some of characters is escaped
@@ -1042,18 +1217,190 @@ private:
is.src_ = is.dst_ = p;
}
-#endif
+#elif defined(RAPIDJSON_NEON)
+ // StringStream -> StackStream<char>
+ static RAPIDJSON_FORCEINLINE void ScanCopyUnescapedString(StringStream& is, StackStream<char>& os) {
+ const char* p = is.src_;
- template<typename InputStream, bool backup>
+ // Scan one by one until alignment (unaligned load may cross page boundary and cause crash)
+ const char* nextAligned = reinterpret_cast<const char*>((reinterpret_cast<size_t>(p) + 15) & static_cast<size_t>(~15));
+ while (p != nextAligned)
+ if (RAPIDJSON_UNLIKELY(*p == '\"') || RAPIDJSON_UNLIKELY(*p == '\\') || RAPIDJSON_UNLIKELY(static_cast<unsigned>(*p) < 0x20)) {
+ is.src_ = p;
+ return;
+ }
+ else
+ os.Put(*p++);
+
+ // The rest of string using SIMD
+ const uint8x16_t s0 = vmovq_n_u8('"');
+ const uint8x16_t s1 = vmovq_n_u8('\\');
+ const uint8x16_t s2 = vmovq_n_u8('\b');
+ const uint8x16_t s3 = vmovq_n_u8(32);
+
+ for (;; p += 16) {
+ const uint8x16_t s = vld1q_u8(reinterpret_cast<const uint8_t *>(p));
+ uint8x16_t x = vceqq_u8(s, s0);
+ x = vorrq_u8(x, vceqq_u8(s, s1));
+ x = vorrq_u8(x, vceqq_u8(s, s2));
+ x = vorrq_u8(x, vcltq_u8(s, s3));
+
+ x = vrev64q_u8(x); // Rev in 64
+ uint64_t low = vgetq_lane_u64(reinterpret_cast<uint64x2_t>(x), 0); // extract
+ uint64_t high = vgetq_lane_u64(reinterpret_cast<uint64x2_t>(x), 1); // extract
+
+ SizeType length = 0;
+ bool escaped = false;
+ if (low == 0) {
+ if (high != 0) {
+ unsigned lz = (unsigned)__builtin_clzll(high);;
+ length = 8 + (lz >> 3);
+ escaped = true;
+ }
+ } else {
+ unsigned lz = (unsigned)__builtin_clzll(low);;
+ length = lz >> 3;
+ escaped = true;
+ }
+ if (RAPIDJSON_UNLIKELY(escaped)) { // some of characters is escaped
+ if (length != 0) {
+ char* q = reinterpret_cast<char*>(os.Push(length));
+ for (size_t i = 0; i < length; i++)
+ q[i] = p[i];
+
+ p += length;
+ }
+ break;
+ }
+ vst1q_u8(reinterpret_cast<uint8_t *>(os.Push(16)), s);
+ }
+
+ is.src_ = p;
+ }
+
+ // InsituStringStream -> InsituStringStream
+ static RAPIDJSON_FORCEINLINE void ScanCopyUnescapedString(InsituStringStream& is, InsituStringStream& os) {
+ RAPIDJSON_ASSERT(&is == &os);
+ (void)os;
+
+ if (is.src_ == is.dst_) {
+ SkipUnescapedString(is);
+ return;
+ }
+
+ char* p = is.src_;
+ char *q = is.dst_;
+
+ // Scan one by one until alignment (unaligned load may cross page boundary and cause crash)
+ const char* nextAligned = reinterpret_cast<const char*>((reinterpret_cast<size_t>(p) + 15) & static_cast<size_t>(~15));
+ while (p != nextAligned)
+ if (RAPIDJSON_UNLIKELY(*p == '\"') || RAPIDJSON_UNLIKELY(*p == '\\') || RAPIDJSON_UNLIKELY(static_cast<unsigned>(*p) < 0x20)) {
+ is.src_ = p;
+ is.dst_ = q;
+ return;
+ }
+ else
+ *q++ = *p++;
+
+ // The rest of string using SIMD
+ const uint8x16_t s0 = vmovq_n_u8('"');
+ const uint8x16_t s1 = vmovq_n_u8('\\');
+ const uint8x16_t s2 = vmovq_n_u8('\b');
+ const uint8x16_t s3 = vmovq_n_u8(32);
+
+ for (;; p += 16, q += 16) {
+ const uint8x16_t s = vld1q_u8(reinterpret_cast<uint8_t *>(p));
+ uint8x16_t x = vceqq_u8(s, s0);
+ x = vorrq_u8(x, vceqq_u8(s, s1));
+ x = vorrq_u8(x, vceqq_u8(s, s2));
+ x = vorrq_u8(x, vcltq_u8(s, s3));
+
+ x = vrev64q_u8(x); // Rev in 64
+ uint64_t low = vgetq_lane_u64(reinterpret_cast<uint64x2_t>(x), 0); // extract
+ uint64_t high = vgetq_lane_u64(reinterpret_cast<uint64x2_t>(x), 1); // extract
+
+ SizeType length = 0;
+ bool escaped = false;
+ if (low == 0) {
+ if (high != 0) {
+ unsigned lz = (unsigned)__builtin_clzll(high);
+ length = 8 + (lz >> 3);
+ escaped = true;
+ }
+ } else {
+ unsigned lz = (unsigned)__builtin_clzll(low);
+ length = lz >> 3;
+ escaped = true;
+ }
+ if (RAPIDJSON_UNLIKELY(escaped)) { // some of characters is escaped
+ for (const char* pend = p + length; p != pend; ) {
+ *q++ = *p++;
+ }
+ break;
+ }
+ vst1q_u8(reinterpret_cast<uint8_t *>(q), s);
+ }
+
+ is.src_ = p;
+ is.dst_ = q;
+ }
+
+ // When read/write pointers are the same for insitu stream, just skip unescaped characters
+ static RAPIDJSON_FORCEINLINE void SkipUnescapedString(InsituStringStream& is) {
+ RAPIDJSON_ASSERT(is.src_ == is.dst_);
+ char* p = is.src_;
+
+ // Scan one by one until alignment (unaligned load may cross page boundary and cause crash)
+ const char* nextAligned = reinterpret_cast<const char*>((reinterpret_cast<size_t>(p) + 15) & static_cast<size_t>(~15));
+ for (; p != nextAligned; p++)
+ if (RAPIDJSON_UNLIKELY(*p == '\"') || RAPIDJSON_UNLIKELY(*p == '\\') || RAPIDJSON_UNLIKELY(static_cast<unsigned>(*p) < 0x20)) {
+ is.src_ = is.dst_ = p;
+ return;
+ }
+
+ // The rest of string using SIMD
+ const uint8x16_t s0 = vmovq_n_u8('"');
+ const uint8x16_t s1 = vmovq_n_u8('\\');
+ const uint8x16_t s2 = vmovq_n_u8('\b');
+ const uint8x16_t s3 = vmovq_n_u8(32);
+
+ for (;; p += 16) {
+ const uint8x16_t s = vld1q_u8(reinterpret_cast<uint8_t *>(p));
+ uint8x16_t x = vceqq_u8(s, s0);
+ x = vorrq_u8(x, vceqq_u8(s, s1));
+ x = vorrq_u8(x, vceqq_u8(s, s2));
+ x = vorrq_u8(x, vcltq_u8(s, s3));
+
+ x = vrev64q_u8(x); // Rev in 64
+ uint64_t low = vgetq_lane_u64(reinterpret_cast<uint64x2_t>(x), 0); // extract
+ uint64_t high = vgetq_lane_u64(reinterpret_cast<uint64x2_t>(x), 1); // extract
+
+ if (low == 0) {
+ if (high != 0) {
+ int lz = __builtin_clzll(high);
+ p += 8 + (lz >> 3);
+ break;
+ }
+ } else {
+ int lz = __builtin_clzll(low);
+ p += lz >> 3;
+ break;
+ }
+ }
+
+ is.src_ = is.dst_ = p;
+ }
+#endif // RAPIDJSON_NEON
+
+ template<typename InputStream, bool backup, bool pushOnTake>
class NumberStream;
template<typename InputStream>
- class NumberStream<InputStream, false> {
+ class NumberStream<InputStream, false, false> {
public:
typedef typename InputStream::Ch Ch;
NumberStream(GenericReader& reader, InputStream& s) : is(s) { (void)reader; }
- ~NumberStream() {}
RAPIDJSON_FORCEINLINE Ch Peek() const { return is.Peek(); }
RAPIDJSON_FORCEINLINE Ch TakePush() { return is.Take(); }
@@ -1071,20 +1418,19 @@ private:
};
template<typename InputStream>
- class NumberStream<InputStream, true> : public NumberStream<InputStream, false> {
- typedef NumberStream<InputStream, false> Base;
+ class NumberStream<InputStream, true, false> : public NumberStream<InputStream, false, false> {
+ typedef NumberStream<InputStream, false, false> Base;
public:
- NumberStream(GenericReader& reader, InputStream& is) : NumberStream<InputStream, false>(reader, is), stackStream(reader.stack_) {}
- ~NumberStream() {}
+ NumberStream(GenericReader& reader, InputStream& is) : Base(reader, is), stackStream(reader.stack_) {}
RAPIDJSON_FORCEINLINE Ch TakePush() {
stackStream.Put(static_cast<char>(Base::is.Peek()));
return Base::is.Take();
}
- RAPIDJSON_FORCEINLINE void Push(char c) {
- stackStream.Put(c);
- }
+ RAPIDJSON_FORCEINLINE void Push(char c) {
+ stackStream.Put(c);
+ }
size_t Length() { return stackStream.Length(); }
@@ -1097,15 +1443,28 @@ private:
StackStream<char> stackStream;
};
+ template<typename InputStream>
+ class NumberStream<InputStream, true, true> : public NumberStream<InputStream, true, false> {
+ typedef NumberStream<InputStream, true, false> Base;
+ public:
+ NumberStream(GenericReader& reader, InputStream& is) : Base(reader, is) {}
+
+ RAPIDJSON_FORCEINLINE Ch Take() { return Base::TakePush(); }
+ };
+
template<unsigned parseFlags, typename InputStream, typename Handler>
void ParseNumber(InputStream& is, Handler& handler) {
internal::StreamLocalCopy<InputStream> copy(is);
NumberStream<InputStream,
((parseFlags & kParseNumbersAsStringsFlag) != 0) ?
((parseFlags & kParseInsituFlag) == 0) :
- ((parseFlags & kParseFullPrecisionFlag) != 0)> s(*this, copy.s);
+ ((parseFlags & kParseFullPrecisionFlag) != 0),
+ (parseFlags & kParseNumbersAsStringsFlag) != 0 &&
+ (parseFlags & kParseInsituFlag) == 0> s(*this, copy.s);
size_t startOffset = s.Tell();
+ double d = 0.0;
+ bool useNanOrInf = false;
// Parse minus
bool minus = Consume(s, '-');
@@ -1147,14 +1506,37 @@ private:
significandDigit++;
}
}
+ // Parse NaN or Infinity here
+ else if ((parseFlags & kParseNanAndInfFlag) && RAPIDJSON_LIKELY((s.Peek() == 'I' || s.Peek() == 'N'))) {
+ if (Consume(s, 'N')) {
+ if (Consume(s, 'a') && Consume(s, 'N')) {
+ d = std::numeric_limits<double>::quiet_NaN();
+ useNanOrInf = true;
+ }
+ }
+ else if (RAPIDJSON_LIKELY(Consume(s, 'I'))) {
+ if (Consume(s, 'n') && Consume(s, 'f')) {
+ d = (minus ? -std::numeric_limits<double>::infinity() : std::numeric_limits<double>::infinity());
+ useNanOrInf = true;
+
+ if (RAPIDJSON_UNLIKELY(s.Peek() == 'i' && !(Consume(s, 'i') && Consume(s, 'n')
+ && Consume(s, 'i') && Consume(s, 't') && Consume(s, 'y')))) {
+ RAPIDJSON_PARSE_ERROR(kParseErrorValueInvalid, s.Tell());
+ }
+ }
+ }
+
+ if (RAPIDJSON_UNLIKELY(!useNanOrInf)) {
+ RAPIDJSON_PARSE_ERROR(kParseErrorValueInvalid, s.Tell());
+ }
+ }
else
RAPIDJSON_PARSE_ERROR(kParseErrorValueInvalid, s.Tell());
// Parse 64bit int
bool useDouble = false;
- double d = 0.0;
if (use64bit) {
- if (minus)
+ if (minus)
while (RAPIDJSON_LIKELY(s.Peek() >= '0' && s.Peek() <= '9')) {
if (RAPIDJSON_UNLIKELY(i64 >= RAPIDJSON_UINT64_C2(0x0CCCCCCC, 0xCCCCCCCC))) // 2^63 = 9223372036854775808
if (RAPIDJSON_LIKELY(i64 != RAPIDJSON_UINT64_C2(0x0CCCCCCC, 0xCCCCCCCC) || s.Peek() > '8')) {
@@ -1191,9 +1573,6 @@ private:
int expFrac = 0;
size_t decimalPosition;
if (Consume(s, '.')) {
- if (((parseFlags & kParseNumbersAsStringsFlag) != 0) && ((parseFlags & kParseInsituFlag) == 0)) {
- s.Push('.');
- }
decimalPosition = s.Length();
if (RAPIDJSON_UNLIKELY(!(s.Peek() >= '0' && s.Peek() <= '9')))
@@ -1204,7 +1583,7 @@ private:
// Use i64 to store significand in 64-bit architecture
if (!use64bit)
i64 = i;
-
+
while (RAPIDJSON_LIKELY(s.Peek() >= '0' && s.Peek() <= '9')) {
if (i64 > RAPIDJSON_UINT64_C2(0x1FFFFF, 0xFFFFFFFF)) // 2^53 - 1 for fast path
break;
@@ -1241,11 +1620,7 @@ private:
// Parse exp = e [ minus / plus ] 1*DIGIT
int exp = 0;
if (Consume(s, 'e') || Consume(s, 'E')) {
- if ( ((parseFlags & kParseNumbersAsStringsFlag) != 0) && ((parseFlags & kParseInsituFlag) == 0) ) {
- s.Push( 'e' );
- }
-
- if (!useDouble) {
+ if (!useDouble) {
d = static_cast<double>(use64bit ? i64 : i);
useDouble = true;
}
@@ -1287,28 +1662,27 @@ private:
bool cont = true;
if (parseFlags & kParseNumbersAsStringsFlag) {
-
if (parseFlags & kParseInsituFlag) {
s.Pop(); // Pop stack no matter if it will be used or not.
typename InputStream::Ch* head = is.PutBegin();
const size_t length = s.Tell() - startOffset;
RAPIDJSON_ASSERT(length <= 0xFFFFFFFF);
-// *(head + length) = '\0';
+ // unable to insert the \0 character here, it will erase the comma after this number
const typename TargetEncoding::Ch* const str = reinterpret_cast<typename TargetEncoding::Ch*>(head);
cont = handler.RawNumber(str, SizeType(length), false);
}
else {
- StackStream<typename TargetEncoding::Ch> stackStream(stack_);
SizeType numCharsToCopy = static_cast<SizeType>(s.Length());
+ StringStream srcStream(s.Pop());
+ StackStream<typename TargetEncoding::Ch> dstStream(stack_);
while (numCharsToCopy--) {
- Transcoder<SourceEncoding, TargetEncoding>::Transcode(is, stackStream);
+ Transcoder<UTF8<>, TargetEncoding>::Transcode(srcStream, dstStream);
}
- stackStream.Put('\0');
- const typename TargetEncoding::Ch* str = stackStream.Pop();
- const SizeType length = static_cast<SizeType>(stackStream.Length()) - 1;
+ dstStream.Put('\0');
+ const typename TargetEncoding::Ch* str = dstStream.Pop();
+ const SizeType length = static_cast<SizeType>(dstStream.Length()) - 1;
cont = handler.RawNumber(str, SizeType(length), true);
}
-
}
else {
size_t length = s.Length();
@@ -1323,6 +1697,9 @@ private:
cont = handler.Double(minus ? -d : d);
}
+ else if (useNanOrInf) {
+ cont = handler.Double(d);
+ }
else {
if (use64bit) {
if (minus)
@@ -1352,10 +1729,10 @@ private:
case '"': ParseString<parseFlags>(is, handler); break;
case '{': ParseObject<parseFlags>(is, handler); break;
case '[': ParseArray <parseFlags>(is, handler); break;
- default :
+ default :
ParseNumber<parseFlags>(is, handler);
break;
-
+
}
}
@@ -1363,30 +1740,32 @@ private:
// States
enum IterativeParsingState {
- IterativeParsingStartState = 0,
- IterativeParsingFinishState,
- IterativeParsingErrorState,
+ IterativeParsingFinishState = 0, // sink states at top
+ IterativeParsingErrorState, // sink states at top
+ IterativeParsingStartState,
// Object states
IterativeParsingObjectInitialState,
IterativeParsingMemberKeyState,
- IterativeParsingKeyValueDelimiterState,
IterativeParsingMemberValueState,
- IterativeParsingMemberDelimiterState,
IterativeParsingObjectFinishState,
// Array states
IterativeParsingArrayInitialState,
IterativeParsingElementState,
- IterativeParsingElementDelimiterState,
IterativeParsingArrayFinishState,
// Single value state
- IterativeParsingValueState
+ IterativeParsingValueState,
+
+ // Delimiter states (at bottom)
+ IterativeParsingElementDelimiterState,
+ IterativeParsingMemberDelimiterState,
+ IterativeParsingKeyValueDelimiterState,
+
+ cIterativeParsingStateCount
};
- enum { cIterativeParsingStateCount = IterativeParsingValueState + 1 };
-
// Tokens
enum Token {
LeftBracketToken = 0,
@@ -1427,7 +1806,7 @@ private:
#undef N
#undef N16
//!@endcond
-
+
if (sizeof(Ch) == 1 || static_cast<unsigned>(c) < 256)
return static_cast<Token>(tokenMap[static_cast<unsigned char>(c)]);
else
@@ -1437,6 +1816,18 @@ private:
RAPIDJSON_FORCEINLINE IterativeParsingState Predict(IterativeParsingState state, Token token) {
// current state x one lookahead token -> new state
static const char G[cIterativeParsingStateCount][kTokenCount] = {
+ // Finish(sink state)
+ {
+ IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
+ IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
+ IterativeParsingErrorState
+ },
+ // Error(sink state)
+ {
+ IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
+ IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
+ IterativeParsingErrorState
+ },
// Start
{
IterativeParsingArrayInitialState, // Left bracket
@@ -1451,18 +1842,6 @@ private:
IterativeParsingValueState, // Null
IterativeParsingValueState // Number
},
- // Finish(sink state)
- {
- IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
- IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
- IterativeParsingErrorState
- },
- // Error(sink state)
- {
- IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
- IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
- IterativeParsingErrorState
- },
// ObjectInitial
{
IterativeParsingErrorState, // Left bracket
@@ -1491,20 +1870,6 @@ private:
IterativeParsingErrorState, // Null
IterativeParsingErrorState // Number
},
- // KeyValueDelimiter
- {
- IterativeParsingArrayInitialState, // Left bracket(push MemberValue state)
- IterativeParsingErrorState, // Right bracket
- IterativeParsingObjectInitialState, // Left curly bracket(push MemberValue state)
- IterativeParsingErrorState, // Right curly bracket
- IterativeParsingErrorState, // Comma
- IterativeParsingErrorState, // Colon
- IterativeParsingMemberValueState, // String
- IterativeParsingMemberValueState, // False
- IterativeParsingMemberValueState, // True
- IterativeParsingMemberValueState, // Null
- IterativeParsingMemberValueState // Number
- },
// MemberValue
{
IterativeParsingErrorState, // Left bracket
@@ -1519,20 +1884,6 @@ private:
IterativeParsingErrorState, // Null
IterativeParsingErrorState // Number
},
- // MemberDelimiter
- {
- IterativeParsingErrorState, // Left bracket
- IterativeParsingErrorState, // Right bracket
- IterativeParsingErrorState, // Left curly bracket
- IterativeParsingErrorState, // Right curly bracket
- IterativeParsingErrorState, // Comma
- IterativeParsingErrorState, // Colon
- IterativeParsingMemberKeyState, // String
- IterativeParsingErrorState, // False
- IterativeParsingErrorState, // True
- IterativeParsingErrorState, // Null
- IterativeParsingErrorState // Number
- },
// ObjectFinish(sink state)
{
IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
@@ -1567,10 +1918,22 @@ private:
IterativeParsingErrorState, // Null
IterativeParsingErrorState // Number
},
+ // ArrayFinish(sink state)
+ {
+ IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
+ IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
+ IterativeParsingErrorState
+ },
+ // Single Value (sink state)
+ {
+ IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
+ IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
+ IterativeParsingErrorState
+ },
// ElementDelimiter
{
IterativeParsingArrayInitialState, // Left bracket(push Element state)
- IterativeParsingErrorState, // Right bracket
+ IterativeParsingArrayFinishState, // Right bracket
IterativeParsingObjectInitialState, // Left curly bracket(push Element state)
IterativeParsingErrorState, // Right curly bracket
IterativeParsingErrorState, // Comma
@@ -1581,18 +1944,34 @@ private:
IterativeParsingElementState, // Null
IterativeParsingElementState // Number
},
- // ArrayFinish(sink state)
+ // MemberDelimiter
{
- IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
- IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
- IterativeParsingErrorState
+ IterativeParsingErrorState, // Left bracket
+ IterativeParsingErrorState, // Right bracket
+ IterativeParsingErrorState, // Left curly bracket
+ IterativeParsingObjectFinishState, // Right curly bracket
+ IterativeParsingErrorState, // Comma
+ IterativeParsingErrorState, // Colon
+ IterativeParsingMemberKeyState, // String
+ IterativeParsingErrorState, // False
+ IterativeParsingErrorState, // True
+ IterativeParsingErrorState, // Null
+ IterativeParsingErrorState // Number
},
- // Single Value (sink state)
+ // KeyValueDelimiter
{
- IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
- IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
- IterativeParsingErrorState
- }
+ IterativeParsingArrayInitialState, // Left bracket(push MemberValue state)
+ IterativeParsingErrorState, // Right bracket
+ IterativeParsingObjectInitialState, // Left curly bracket(push MemberValue state)
+ IterativeParsingErrorState, // Right curly bracket
+ IterativeParsingErrorState, // Comma
+ IterativeParsingErrorState, // Colon
+ IterativeParsingMemberValueState, // String
+ IterativeParsingMemberValueState, // False
+ IterativeParsingMemberValueState, // True
+ IterativeParsingMemberValueState, // Null
+ IterativeParsingMemberValueState // Number
+ },
}; // End of G
return static_cast<IterativeParsingState>(G[state][token]);
@@ -1672,6 +2051,11 @@ private:
case IterativeParsingObjectFinishState:
{
+ // Transit from delimiter is only allowed when trailing commas are enabled
+ if (!(parseFlags & kParseTrailingCommasFlag) && src == IterativeParsingMemberDelimiterState) {
+ RAPIDJSON_PARSE_ERROR_NORETURN(kParseErrorObjectMissName, is.Tell());
+ return IterativeParsingErrorState;
+ }
// Get member count.
SizeType c = *stack_.template Pop<SizeType>(1);
// If the object is not empty, count the last member.
@@ -1697,6 +2081,11 @@ private:
case IterativeParsingArrayFinishState:
{
+ // Transit from delimiter is only allowed when trailing commas are enabled
+ if (!(parseFlags & kParseTrailingCommasFlag) && src == IterativeParsingElementDelimiterState) {
+ RAPIDJSON_PARSE_ERROR_NORETURN(kParseErrorValueInvalid, is.Tell());
+ return IterativeParsingErrorState;
+ }
// Get element count.
SizeType c = *stack_.template Pop<SizeType>(1);
// If the array is not empty, count the last element.
@@ -1748,7 +2137,7 @@ private:
// Error flag has been set.
return;
}
-
+
switch (src) {
case IterativeParsingStartState: RAPIDJSON_PARSE_ERROR(kParseErrorDocumentEmpty, is.Tell()); return;
case IterativeParsingFinishState: RAPIDJSON_PARSE_ERROR(kParseErrorDocumentRootNotSingular, is.Tell()); return;
@@ -1756,49 +2145,60 @@ private:
case IterativeParsingMemberDelimiterState: RAPIDJSON_PARSE_ERROR(kParseErrorObjectMissName, is.Tell()); return;
case IterativeParsingMemberKeyState: RAPIDJSON_PARSE_ERROR(kParseErrorObjectMissColon, is.Tell()); return;
case IterativeParsingMemberValueState: RAPIDJSON_PARSE_ERROR(kParseErrorObjectMissCommaOrCurlyBracket, is.Tell()); return;
- case IterativeParsingElementState: RAPIDJSON_PARSE_ERROR(kParseErrorArrayMissCommaOrSquareBracket, is.Tell()); return;
- default: RAPIDJSON_PARSE_ERROR(kParseErrorUnspecificSyntaxError, is.Tell()); return;
- }
+ case IterativeParsingKeyValueDelimiterState:
+ case IterativeParsingArrayInitialState:
+ case IterativeParsingElementDelimiterState: RAPIDJSON_PARSE_ERROR(kParseErrorValueInvalid, is.Tell()); return;
+ default: RAPIDJSON_ASSERT(src == IterativeParsingElementState); RAPIDJSON_PARSE_ERROR(kParseErrorArrayMissCommaOrSquareBracket, is.Tell()); return;
+ }
}
+ RAPIDJSON_FORCEINLINE bool IsIterativeParsingDelimiterState(IterativeParsingState s) {
+ return s >= IterativeParsingElementDelimiterState;
+ }
+
+ RAPIDJSON_FORCEINLINE bool IsIterativeParsingCompleteState(IterativeParsingState s) {
+ return s <= IterativeParsingErrorState;
+ }
+
template <unsigned parseFlags, typename InputStream, typename Handler>
ParseResult IterativeParse(InputStream& is, Handler& handler) {
parseResult_.Clear();
ClearStackOnExit scope(*this);
IterativeParsingState state = IterativeParsingStartState;
-
+
SkipWhitespaceAndComments<parseFlags>(is);
RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_);
while (is.Peek() != '\0') {
Token t = Tokenize(is.Peek());
IterativeParsingState n = Predict(state, t);
IterativeParsingState d = Transit<parseFlags>(state, t, n, is, handler);
-
+
if (d == IterativeParsingErrorState) {
HandleError(state, is);
break;
}
-
+
state = d;
-
+
// Do not further consume streams if a root JSON has been parsed.
if ((parseFlags & kParseStopWhenDoneFlag) && state == IterativeParsingFinishState)
break;
-
+
SkipWhitespaceAndComments<parseFlags>(is);
RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_);
}
-
+
// Handle the end of file.
if (state != IterativeParsingFinishState)
HandleError(state, is);
-
+
return parseResult_;
}
static const size_t kDefaultStackCapacity = 256; //!< Default stack capacity in bytes for storing a single decoded string.
internal::Stack<StackAllocator> stack_; //!< A stack for storing decoded string temporarily during non-destructive parsing.
ParseResult parseResult_;
+ IterativeParsingState state_;
}; // class GenericReader
//! Reader with UTF8 encoding and default allocator.
diff --git a/include/rapidjson/schema.h b/include/rapidjson/schema.h
index 26da8a6b..2713fb26 100644
--- a/include/rapidjson/schema.h
+++ b/include/rapidjson/schema.h
@@ -19,13 +19,6 @@
#include "pointer.h"
#include <cmath> // abs, floor
-#ifdef __clang__
-RAPIDJSON_DIAG_PUSH
-RAPIDJSON_DIAG_OFF(weak-vtables)
-RAPIDJSON_DIAG_OFF(exit-time-destructors)
-RAPIDJSON_DIAG_OFF(c++98-compat-pedantic)
-#endif
-
#if !defined(RAPIDJSON_SCHEMA_USE_INTERNALREGEX)
#define RAPIDJSON_SCHEMA_USE_INTERNALREGEX 1
#else
@@ -58,16 +51,23 @@ RAPIDJSON_DIAG_OFF(c++98-compat-pedantic)
#include "stringbuffer.h"
#endif
-#if defined(__GNUC__)
RAPIDJSON_DIAG_PUSH
+
+#if defined(__GNUC__)
RAPIDJSON_DIAG_OFF(effc++)
#endif
#ifdef __clang__
-RAPIDJSON_DIAG_PUSH
+RAPIDJSON_DIAG_OFF(weak-vtables)
+RAPIDJSON_DIAG_OFF(exit-time-destructors)
+RAPIDJSON_DIAG_OFF(c++98-compat-pedantic)
RAPIDJSON_DIAG_OFF(variadic-macros)
#endif
+#ifdef _MSC_VER
+RAPIDJSON_DIAG_OFF(4512) // assignment operator could not be generated
+#endif
+
RAPIDJSON_NAMESPACE_BEGIN
///////////////////////////////////////////////////////////////////////////////
@@ -154,7 +154,6 @@ public:
virtual uint64_t GetHashCode(void* hasher) = 0;
virtual void DestroryHasher(void* hasher) = 0;
virtual void* MallocState(size_t size) = 0;
- virtual void* ReallocState(void* originalPtr, size_t originalSize, size_t newSize) = 0;
virtual void FreeState(void* p) = 0;
};
@@ -285,7 +284,7 @@ struct SchemaValidationContext {
patternPropertiesSchemas(),
patternPropertiesSchemaCount(),
valuePatternValidatorType(kPatternValidatorOnly),
- objectDependencies(),
+ propertyExist(),
inArray(false),
valueUniqueness(false),
arrayUniqueness(false)
@@ -307,8 +306,8 @@ struct SchemaValidationContext {
}
if (patternPropertiesSchemas)
factory.FreeState(patternPropertiesSchemas);
- if (objectDependencies)
- factory.FreeState(objectDependencies);
+ if (propertyExist)
+ factory.FreeState(propertyExist);
}
SchemaValidatorFactoryType& factory;
@@ -325,9 +324,8 @@ struct SchemaValidationContext {
SizeType patternPropertiesSchemaCount;
PatternValidatorType valuePatternValidatorType;
PatternValidatorType objectPatternValidatorType;
- SizeType objectRequiredCount;
SizeType arrayElementIndex;
- bool* objectDependencies;
+ bool* propertyExist;
bool inArray;
bool valueUniqueness;
bool arrayUniqueness;
@@ -351,21 +349,23 @@ public:
Schema(SchemaDocumentType* schemaDocument, const PointerType& p, const ValueType& value, const ValueType& document, AllocatorType* allocator) :
allocator_(allocator),
+ typeless_(schemaDocument->GetTypeless()),
enum_(),
enumCount_(),
not_(),
type_((1 << kTotalSchemaType) - 1), // typeless
validatorCount_(),
+ notValidatorIndex_(),
properties_(),
additionalPropertiesSchema_(),
patternProperties_(),
patternPropertyCount_(),
propertyCount_(),
- requiredCount_(),
minProperties_(),
maxProperties_(SizeType(~0)),
additionalProperties_(true),
hasDependencies_(),
+ hasRequired_(),
hasSchemaDependencies_(),
additionalItemsSchema_(),
itemsList_(),
@@ -410,9 +410,11 @@ public:
}
}
- AssignIfExist(allOf_, *schemaDocument, p, value, GetAllOfString(), document);
- AssignIfExist(anyOf_, *schemaDocument, p, value, GetAnyOfString(), document);
- AssignIfExist(oneOf_, *schemaDocument, p, value, GetOneOfString(), document);
+ if (schemaDocument) {
+ AssignIfExist(allOf_, *schemaDocument, p, value, GetAllOfString(), document);
+ AssignIfExist(anyOf_, *schemaDocument, p, value, GetAnyOfString(), document);
+ AssignIfExist(oneOf_, *schemaDocument, p, value, GetOneOfString(), document);
+ }
if (const ValueType* v = GetMember(value, GetNotString())) {
schemaDocument->CreateSchema(&not_, p.Append(GetNotString(), allocator_), *v, document);
@@ -453,7 +455,7 @@ public:
for (SizeType i = 0; i < propertyCount_; i++) {
new (&properties_[i]) Property();
properties_[i].name = allProperties[i];
- properties_[i].schema = GetTypeless();
+ properties_[i].schema = typeless_;
}
}
}
@@ -486,7 +488,7 @@ public:
SizeType index;
if (FindPropertyIndex(*itr, &index)) {
properties_[index].required = true;
- requiredCount_++;
+ hasRequired_ = true;
}
}
@@ -575,7 +577,7 @@ public:
}
~Schema() {
- allocator_->Free(enum_);
+ AllocatorType::Free(enum_);
if (properties_) {
for (SizeType i = 0; i < propertyCount_; i++)
properties_[i].~Property();
@@ -590,7 +592,7 @@ public:
#if RAPIDJSON_SCHEMA_HAS_REGEX
if (pattern_) {
pattern_->~RegexType();
- allocator_->Free(pattern_);
+ AllocatorType::Free(pattern_);
}
#endif
}
@@ -608,12 +610,12 @@ public:
else if (additionalItemsSchema_)
context.valueSchema = additionalItemsSchema_;
else if (additionalItems_)
- context.valueSchema = GetTypeless();
+ context.valueSchema = typeless_;
else
RAPIDJSON_INVALID_KEYWORD_RETURN(GetItemsString());
}
else
- context.valueSchema = GetTypeless();
+ context.valueSchema = typeless_;
context.arrayElementIndex++;
}
@@ -763,10 +765,9 @@ public:
if (!(type_ & (1 << kObjectSchemaType)))
RAPIDJSON_INVALID_KEYWORD_RETURN(GetTypeString());
- context.objectRequiredCount = 0;
- if (hasDependencies_) {
- context.objectDependencies = static_cast<bool*>(context.factory.MallocState(sizeof(bool) * propertyCount_));
- std::memset(context.objectDependencies, 0, sizeof(bool) * propertyCount_);
+ if (hasDependencies_ || hasRequired_) {
+ context.propertyExist = static_cast<bool*>(context.factory.MallocState(sizeof(bool) * propertyCount_));
+ std::memset(context.propertyExist, 0, sizeof(bool) * propertyCount_);
}
if (patternProperties_) { // pre-allocate schema array
@@ -783,25 +784,24 @@ public:
if (patternProperties_) {
context.patternPropertiesSchemaCount = 0;
for (SizeType i = 0; i < patternPropertyCount_; i++)
- if (patternProperties_[i].pattern && IsPatternMatch(patternProperties_[i].pattern, str, len))
+ if (patternProperties_[i].pattern && IsPatternMatch(patternProperties_[i].pattern, str, len)) {
context.patternPropertiesSchemas[context.patternPropertiesSchemaCount++] = patternProperties_[i].schema;
+ context.valueSchema = typeless_;
+ }
}
SizeType index;
if (FindPropertyIndex(ValueType(str, len).Move(), &index)) {
if (context.patternPropertiesSchemaCount > 0) {
context.patternPropertiesSchemas[context.patternPropertiesSchemaCount++] = properties_[index].schema;
- context.valueSchema = GetTypeless();
+ context.valueSchema = typeless_;
context.valuePatternValidatorType = Context::kPatternValidatorWithProperty;
}
else
context.valueSchema = properties_[index].schema;
- if (properties_[index].required)
- context.objectRequiredCount++;
-
- if (hasDependencies_)
- context.objectDependencies[index] = true;
+ if (context.propertyExist)
+ context.propertyExist[index] = true;
return true;
}
@@ -809,7 +809,7 @@ public:
if (additionalPropertiesSchema_) {
if (additionalPropertiesSchema_ && context.patternPropertiesSchemaCount > 0) {
context.patternPropertiesSchemas[context.patternPropertiesSchemaCount++] = additionalPropertiesSchema_;
- context.valueSchema = GetTypeless();
+ context.valueSchema = typeless_;
context.valuePatternValidatorType = Context::kPatternValidatorWithAdditionalProperty;
}
else
@@ -817,7 +817,7 @@ public:
return true;
}
else if (additionalProperties_) {
- context.valueSchema = GetTypeless();
+ context.valueSchema = typeless_;
return true;
}
@@ -828,8 +828,11 @@ public:
}
bool EndObject(Context& context, SizeType memberCount) const {
- if (context.objectRequiredCount != requiredCount_)
- RAPIDJSON_INVALID_KEYWORD_RETURN(GetRequiredString());
+ if (hasRequired_)
+ for (SizeType index = 0; index < propertyCount_; index++)
+ if (properties_[index].required)
+ if (!context.propertyExist[index])
+ RAPIDJSON_INVALID_KEYWORD_RETURN(GetRequiredString());
if (memberCount < minProperties_)
RAPIDJSON_INVALID_KEYWORD_RETURN(GetMinPropertiesString());
@@ -839,10 +842,10 @@ public:
if (hasDependencies_) {
for (SizeType sourceIndex = 0; sourceIndex < propertyCount_; sourceIndex++)
- if (context.objectDependencies[sourceIndex]) {
+ if (context.propertyExist[sourceIndex]) {
if (properties_[sourceIndex].dependencies) {
for (SizeType targetIndex = 0; targetIndex < propertyCount_; targetIndex++)
- if (properties_[sourceIndex].dependencies[targetIndex] && !context.objectDependencies[targetIndex])
+ if (properties_[sourceIndex].dependencies[targetIndex] && !context.propertyExist[targetIndex])
RAPIDJSON_INVALID_KEYWORD_RETURN(GetDependenciesString());
}
else if (properties_[sourceIndex].dependenciesSchema)
@@ -880,7 +883,7 @@ public:
#define RAPIDJSON_STRING_(name, ...) \
static const ValueType& Get##name##String() {\
static const Ch s[] = { __VA_ARGS__, '\0' };\
- static const ValueType v(s, sizeof(s) / sizeof(Ch) - 1);\
+ static const ValueType v(s, static_cast<SizeType>(sizeof(s) / sizeof(Ch) - 1));\
return v;\
}
@@ -933,7 +936,7 @@ private:
};
#if RAPIDJSON_SCHEMA_USE_INTERNALREGEX
- typedef internal::GenericRegex<EncodingType> RegexType;
+ typedef internal::GenericRegex<EncodingType, AllocatorType> RegexType;
#elif RAPIDJSON_SCHEMA_USE_STDREGEX
typedef std::basic_regex<Ch> RegexType;
#else
@@ -948,11 +951,6 @@ private:
SizeType count;
};
- static const SchemaType* GetTypeless() {
- static SchemaType typeless(0, PointerType(), ValueType(kObjectType).Move(), ValueType(kObjectType).Move(), 0);
- return &typeless;
- }
-
template <typename V1, typename V2>
void AddUniqueElement(V1& a, const V2& v) {
for (typename V1::ConstValueIterator itr = a.Begin(); itr != a.End(); ++itr)
@@ -998,9 +996,10 @@ private:
template <typename ValueType>
RegexType* CreatePattern(const ValueType& value) {
if (value.IsString()) {
- RegexType* r = new (allocator_->Malloc(sizeof(RegexType))) RegexType(value.GetString());
+ RegexType* r = new (allocator_->Malloc(sizeof(RegexType))) RegexType(value.GetString(), allocator_);
if (!r->IsValid()) {
r->~RegexType();
+ AllocatorType::Free(r);
r = 0;
}
return r;
@@ -1009,7 +1008,8 @@ private:
}
static bool IsPatternMatch(const RegexType* pattern, const Ch *str, SizeType) {
- return pattern->Search(str);
+ GenericRegexSearch<RegexType> rs(*pattern);
+ return rs.Search(str);
}
#elif RAPIDJSON_SCHEMA_USE_STDREGEX
template <typename ValueType>
@@ -1103,6 +1103,9 @@ private:
if (exclusiveMinimum_ ? i <= minimum_.GetInt64() : i < minimum_.GetInt64())
RAPIDJSON_INVALID_KEYWORD_RETURN(GetMinimumString());
}
+ else if (minimum_.IsUint64()) {
+ RAPIDJSON_INVALID_KEYWORD_RETURN(GetMinimumString()); // i <= max(int64_t) < minimum.GetUint64()
+ }
else if (!CheckDoubleMinimum(context, static_cast<double>(i)))
return false;
}
@@ -1112,6 +1115,8 @@ private:
if (exclusiveMaximum_ ? i >= maximum_.GetInt64() : i > maximum_.GetInt64())
RAPIDJSON_INVALID_KEYWORD_RETURN(GetMaximumString());
}
+ else if (maximum_.IsUint64()) { }
+ /* do nothing */ // i <= max(int64_t) < maximum_.GetUint64()
else if (!CheckDoubleMaximum(context, static_cast<double>(i)))
return false;
}
@@ -1137,6 +1142,8 @@ private:
if (exclusiveMinimum_ ? i <= minimum_.GetUint64() : i < minimum_.GetUint64())
RAPIDJSON_INVALID_KEYWORD_RETURN(GetMinimumString());
}
+ else if (minimum_.IsInt64())
+ /* do nothing */; // i >= 0 > minimum.Getint64()
else if (!CheckDoubleMinimum(context, static_cast<double>(i)))
return false;
}
@@ -1146,6 +1153,8 @@ private:
if (exclusiveMaximum_ ? i >= maximum_.GetUint64() : i > maximum_.GetUint64())
RAPIDJSON_INVALID_KEYWORD_RETURN(GetMaximumString());
}
+ else if (maximum_.IsInt64())
+ RAPIDJSON_INVALID_KEYWORD_RETURN(GetMaximumString()); // i >= 0 > maximum_
else if (!CheckDoubleMaximum(context, static_cast<double>(i)))
return false;
}
@@ -1207,6 +1216,7 @@ private:
};
AllocatorType* allocator_;
+ const SchemaType* typeless_;
uint64_t* enum_;
SizeType enumCount_;
SchemaArray allOf_;
@@ -1222,11 +1232,11 @@ private:
PatternProperty* patternProperties_;
SizeType patternPropertyCount_;
SizeType propertyCount_;
- SizeType requiredCount_;
SizeType minProperties_;
SizeType maxProperties_;
bool additionalProperties_;
bool hasDependencies_;
+ bool hasRequired_;
bool hasSchemaDependencies_;
const SchemaType* additionalItemsSchema_;
@@ -1256,7 +1266,7 @@ struct TokenHelper {
char buffer[21];
size_t length = static_cast<size_t>((sizeof(SizeType) == 4 ? u32toa(index, buffer) : u64toa(index, buffer)) - buffer);
for (size_t i = 0; i < length; i++)
- *documentStack.template Push<Ch>() = buffer[i];
+ *documentStack.template Push<Ch>() = static_cast<Ch>(buffer[i]);
}
};
@@ -1327,16 +1337,20 @@ public:
\param remoteProvider An optional remote schema document provider for resolving remote reference. Can be null.
\param allocator An optional allocator instance for allocating memory. Can be null.
*/
- GenericSchemaDocument(const ValueType& document, IRemoteSchemaDocumentProviderType* remoteProvider = 0, Allocator* allocator = 0) RAPIDJSON_NOEXCEPT :
+ explicit GenericSchemaDocument(const ValueType& document, IRemoteSchemaDocumentProviderType* remoteProvider = 0, Allocator* allocator = 0) :
remoteProvider_(remoteProvider),
allocator_(allocator),
ownAllocator_(),
root_(),
+ typeless_(),
schemaMap_(allocator, kInitialSchemaMapSize),
schemaRef_(allocator, kInitialSchemaRefSize)
{
if (!allocator_)
- ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator());
+ ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator)();
+
+ typeless_ = static_cast<SchemaType*>(allocator_->Malloc(sizeof(SchemaType)));
+ new (typeless_) SchemaType(this, PointerType(), ValueType(kObjectType).Move(), ValueType(kObjectType).Move(), 0);
// Generate root schema, it will call CreateSchema() to create sub-schemas,
// And call AddRefSchema() if there are $ref.
@@ -1354,6 +1368,9 @@ public:
new (schemaMap_.template Push<SchemaEntry>()) SchemaEntry(refEntry->source, const_cast<SchemaType*>(s), false, allocator_);
}
}
+ else if (refEntry->schema)
+ *refEntry->schema = typeless_;
+
refEntry->~SchemaRefEntry();
}
@@ -1369,12 +1386,14 @@ public:
allocator_(rhs.allocator_),
ownAllocator_(rhs.ownAllocator_),
root_(rhs.root_),
+ typeless_(rhs.typeless_),
schemaMap_(std::move(rhs.schemaMap_)),
schemaRef_(std::move(rhs.schemaRef_))
{
rhs.remoteProvider_ = 0;
rhs.allocator_ = 0;
rhs.ownAllocator_ = 0;
+ rhs.typeless_ = 0;
}
#endif
@@ -1383,6 +1402,11 @@ public:
while (!schemaMap_.Empty())
schemaMap_.template Pop<SchemaEntry>(1)->~SchemaEntry();
+ if (typeless_) {
+ typeless_->~SchemaType();
+ Allocator::Free(typeless_);
+ }
+
RAPIDJSON_DELETE(ownAllocator_);
}
@@ -1417,14 +1441,12 @@ private:
void CreateSchemaRecursive(const SchemaType** schema, const PointerType& pointer, const ValueType& v, const ValueType& document) {
if (schema)
- *schema = SchemaType::GetTypeless();
+ *schema = typeless_;
if (v.GetType() == kObjectType) {
const SchemaType* s = GetSchema(pointer);
if (!s)
CreateSchema(schema, pointer, v, document);
- else if (schema)
- *schema = s;
for (typename ValueType::ConstMemberIterator itr = v.MemberBegin(); itr != v.MemberEnd(); ++itr)
CreateSchemaRecursive(0, pointer.Append(itr->name, allocator_), itr->value, document);
@@ -1464,7 +1486,7 @@ private:
if (i > 0) { // Remote reference, resolve immediately
if (remoteProvider_) {
- if (const GenericSchemaDocument* remoteDocument = remoteProvider_->GetRemoteDocument(s, i - 1)) {
+ if (const GenericSchemaDocument* remoteDocument = remoteProvider_->GetRemoteDocument(s, i)) {
PointerType pointer(&s[i], len - i, allocator_);
if (pointer.IsValid()) {
if (const SchemaType* sc = remoteDocument->GetSchema(pointer)) {
@@ -1506,6 +1528,8 @@ private:
return PointerType();
}
+ const SchemaType* GetTypeless() const { return typeless_; }
+
static const size_t kInitialSchemaMapSize = 64;
static const size_t kInitialSchemaRefSize = 64;
@@ -1513,6 +1537,7 @@ private:
Allocator *allocator_;
Allocator *ownAllocator_;
const SchemaType* root_; //!< Root schema.
+ SchemaType* typeless_;
internal::Stack<Allocator> schemaMap_; // Stores created Pointer -> Schemas
internal::Stack<Allocator> schemaRef_; // Stores Pointer from $ref and schema which holds the $ref
};
@@ -1566,11 +1591,11 @@ public:
:
schemaDocument_(&schemaDocument),
root_(schemaDocument.GetRoot()),
- outputHandler_(GetNullHandler()),
stateAllocator_(allocator),
ownStateAllocator_(0),
schemaStack_(allocator, schemaStackCapacity),
documentStack_(allocator, documentStackCapacity),
+ outputHandler_(0),
valid_(true)
#if RAPIDJSON_SCHEMA_VERBOSE
, depth_(0)
@@ -1594,11 +1619,11 @@ public:
:
schemaDocument_(&schemaDocument),
root_(schemaDocument.GetRoot()),
- outputHandler_(outputHandler),
stateAllocator_(allocator),
ownStateAllocator_(0),
schemaStack_(allocator, schemaStackCapacity),
documentStack_(allocator, documentStackCapacity),
+ outputHandler_(&outputHandler),
valid_(true)
#if RAPIDJSON_SCHEMA_VERBOSE
, depth_(0)
@@ -1670,7 +1695,7 @@ RAPIDJSON_MULTILINEMACRO_END
}
#define RAPIDJSON_SCHEMA_HANDLE_END_(method, arg2)\
- return valid_ = EndValue() && outputHandler_.method arg2
+ return valid_ = EndValue() && (!outputHandler_ || outputHandler_->method arg2)
#define RAPIDJSON_SCHEMA_HANDLE_VALUE_(method, arg1, arg2) \
RAPIDJSON_SCHEMA_HANDLE_BEGIN_ (method, arg1);\
@@ -1692,7 +1717,7 @@ RAPIDJSON_MULTILINEMACRO_END
bool StartObject() {
RAPIDJSON_SCHEMA_HANDLE_BEGIN_(StartObject, (CurrentContext()));
RAPIDJSON_SCHEMA_HANDLE_PARALLEL_(StartObject, ());
- return valid_ = outputHandler_.StartObject();
+ return valid_ = !outputHandler_ || outputHandler_->StartObject();
}
bool Key(const Ch* str, SizeType len, bool copy) {
@@ -1700,7 +1725,7 @@ RAPIDJSON_MULTILINEMACRO_END
AppendToken(str, len);
if (!CurrentSchema().Key(CurrentContext(), str, len, copy)) return valid_ = false;
RAPIDJSON_SCHEMA_HANDLE_PARALLEL_(Key, (str, len, copy));
- return valid_ = outputHandler_.Key(str, len, copy);
+ return valid_ = !outputHandler_ || outputHandler_->Key(str, len, copy);
}
bool EndObject(SizeType memberCount) {
@@ -1713,7 +1738,7 @@ RAPIDJSON_MULTILINEMACRO_END
bool StartArray() {
RAPIDJSON_SCHEMA_HANDLE_BEGIN_(StartArray, (CurrentContext()));
RAPIDJSON_SCHEMA_HANDLE_PARALLEL_(StartArray, ());
- return valid_ = outputHandler_.StartArray();
+ return valid_ = !outputHandler_ || outputHandler_->StartArray();
}
bool EndArray(SizeType elementCount) {
@@ -1761,12 +1786,8 @@ RAPIDJSON_MULTILINEMACRO_END
return GetStateAllocator().Malloc(size);
}
- virtual void* ReallocState(void* originalPtr, size_t originalSize, size_t newSize) {
- return GetStateAllocator().Realloc(originalPtr, originalSize, newSize);
- }
-
virtual void FreeState(void* p) {
- return StateAllocator::Free(p);
+ StateAllocator::Free(p);
}
private:
@@ -1786,11 +1807,11 @@ private:
:
schemaDocument_(&schemaDocument),
root_(root),
- outputHandler_(GetNullHandler()),
stateAllocator_(allocator),
ownStateAllocator_(0),
schemaStack_(allocator, schemaStackCapacity),
documentStack_(allocator, documentStackCapacity),
+ outputHandler_(0),
valid_(true)
#if RAPIDJSON_SCHEMA_VERBOSE
, depth_(depth)
@@ -1800,7 +1821,7 @@ private:
StateAllocator& GetStateAllocator() {
if (!stateAllocator_)
- stateAllocator_ = ownStateAllocator_ = RAPIDJSON_NEW(StateAllocator());
+ stateAllocator_ = ownStateAllocator_ = RAPIDJSON_NEW(StateAllocator)();
return *stateAllocator_;
}
@@ -1818,8 +1839,8 @@ private:
const SchemaType** sa = CurrentContext().patternPropertiesSchemas;
typename Context::PatternValidatorType patternValidatorType = CurrentContext().valuePatternValidatorType;
bool valueUniqueness = CurrentContext().valueUniqueness;
- if (CurrentContext().valueSchema)
- PushSchema(*CurrentContext().valueSchema);
+ RAPIDJSON_ASSERT(CurrentContext().valueSchema);
+ PushSchema(*CurrentContext().valueSchema);
if (count > 0) {
CurrentContext().objectPatternValidatorType = patternValidatorType;
@@ -1904,20 +1925,15 @@ private:
Context& CurrentContext() { return *schemaStack_.template Top<Context>(); }
const Context& CurrentContext() const { return *schemaStack_.template Top<Context>(); }
- static OutputHandler& GetNullHandler() {
- static OutputHandler nullHandler;
- return nullHandler;
- }
-
static const size_t kDefaultSchemaStackCapacity = 1024;
static const size_t kDefaultDocumentStackCapacity = 256;
const SchemaDocumentType* schemaDocument_;
const SchemaType& root_;
- OutputHandler& outputHandler_;
StateAllocator* stateAllocator_;
StateAllocator* ownStateAllocator_;
internal::Stack<StateAllocator> schemaStack_; //!< stack to store the current path of schema (BaseSchemaType *)
internal::Stack<StateAllocator> documentStack_; //!< stack to store the current path of validating document (Ch)
+ OutputHandler* outputHandler_;
bool valid_;
#if RAPIDJSON_SCHEMA_VERBOSE
unsigned depth_;
@@ -1963,7 +1979,8 @@ public:
GenericSchemaValidator<SchemaDocumentType, Handler> validator(sd_, handler);
parseResult_ = reader.template Parse<parseFlags>(is_, validator);
- if ((isValid_ = validator.IsValid())) {
+ isValid_ = validator.IsValid();
+ if (isValid_) {
invalidSchemaPointer_ = PointerType();
invalidSchemaKeyword_ = 0;
invalidDocumentPointer_ = PointerType();
@@ -1995,13 +2012,6 @@ private:
};
RAPIDJSON_NAMESPACE_END
-
-#if defined(__GNUC__)
RAPIDJSON_DIAG_POP
-#endif
-
-#ifdef __clang__
-RAPIDJSON_DIAG_POP
-#endif
#endif // RAPIDJSON_SCHEMA_H_
diff --git a/include/rapidjson/stream.h b/include/rapidjson/stream.h
index dd2783b4..7f2643e4 100644
--- a/include/rapidjson/stream.h
+++ b/include/rapidjson/stream.h
@@ -1,5 +1,5 @@
// Tencent is pleased to support the open source community by making RapidJSON available.
-//
+//
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
//
// Licensed under the MIT License (the "License"); you may not use this file except
@@ -7,9 +7,9 @@
//
// http://opensource.org/licenses/MIT
//
-// Unless required by applicable law or agreed to in writing, software distributed
-// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
-// CONDITIONS OF ANY KIND, either express or implied. See the License for the
+// Unless required by applicable law or agreed to in writing, software distributed
+// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
+// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.
#include "rapidjson.h"
@@ -95,12 +95,56 @@ inline void PutUnsafe(Stream& stream, typename Stream::Ch c) {
//! Put N copies of a character to a stream.
template<typename Stream, typename Ch>
inline void PutN(Stream& stream, Ch c, size_t n) {
- PutReserve<Stream>(stream, n);
+ PutReserve(stream, n);
for (size_t i = 0; i < n; i++)
PutUnsafe(stream, c);
}
///////////////////////////////////////////////////////////////////////////////
+// GenericStreamWrapper
+
+//! A Stream Wrapper
+/*! \tThis string stream is a wrapper for any stream by just forwarding any
+ \treceived message to the origin stream.
+ \note implements Stream concept
+*/
+
+#if defined(_MSC_VER) && _MSC_VER <= 1800
+RAPIDJSON_DIAG_PUSH
+RAPIDJSON_DIAG_OFF(4702) // unreachable code
+RAPIDJSON_DIAG_OFF(4512) // assignment operator could not be generated
+#endif
+
+template <typename InputStream, typename Encoding = UTF8<> >
+class GenericStreamWrapper {
+public:
+ typedef typename Encoding::Ch Ch;
+ GenericStreamWrapper(InputStream& is): is_(is) {}
+
+ Ch Peek() const { return is_.Peek(); }
+ Ch Take() { return is_.Take(); }
+ size_t Tell() { return is_.Tell(); }
+ Ch* PutBegin() { return is_.PutBegin(); }
+ void Put(Ch ch) { is_.Put(ch); }
+ void Flush() { is_.Flush(); }
+ size_t PutEnd(Ch* ch) { return is_.PutEnd(ch); }
+
+ // wrapper for MemoryStream
+ const Ch* Peek4() const { return is_.Peek4(); }
+
+ // wrapper for AutoUTFInputStream
+ UTFType GetType() const { return is_.GetType(); }
+ bool HasBOM() const { return is_.HasBOM(); }
+
+protected:
+ InputStream& is_;
+};
+
+#if defined(_MSC_VER) && _MSC_VER <= 1800
+RAPIDJSON_DIAG_POP
+#endif
+
+///////////////////////////////////////////////////////////////////////////////
// StringStream
//! Read-only string stream.
diff --git a/include/rapidjson/stringbuffer.h b/include/rapidjson/stringbuffer.h
index 78f34d20..4e38b82c 100644
--- a/include/rapidjson/stringbuffer.h
+++ b/include/rapidjson/stringbuffer.h
@@ -78,8 +78,12 @@ public:
return stack_.template Bottom<Ch>();
}
+ //! Get the size of string in bytes in the string buffer.
size_t GetSize() const { return stack_.GetSize(); }
+ //! Get the length of string in Ch in the string buffer.
+ size_t GetLength() const { return stack_.GetSize() / sizeof(Ch); }
+
static const size_t kDefaultCapacity = 256;
mutable internal::Stack<Allocator> stack_;
diff --git a/include/rapidjson/writer.h b/include/rapidjson/writer.h
index 2809f705..e610ebb6 100644
--- a/include/rapidjson/writer.h
+++ b/include/rapidjson/writer.h
@@ -16,6 +16,7 @@
#define RAPIDJSON_WRITER_H_
#include "stream.h"
+#include "internal/meta.h"
#include "internal/stack.h"
#include "internal/strfunc.h"
#include "internal/dtoa.h"
@@ -31,6 +32,8 @@
#include <nmmintrin.h>
#elif defined(RAPIDJSON_SSE2)
#include <emmintrin.h>
+#elif defined(RAPIDJSON_NEON)
+#include <arm_neon.h>
#endif
#ifdef _MSC_VER
@@ -41,6 +44,8 @@ RAPIDJSON_DIAG_OFF(4127) // conditional expression is constant
#ifdef __clang__
RAPIDJSON_DIAG_PUSH
RAPIDJSON_DIAG_OFF(padded)
+RAPIDJSON_DIAG_OFF(unreachable-code)
+RAPIDJSON_DIAG_OFF(c++98-compat)
#endif
RAPIDJSON_NAMESPACE_BEGIN
@@ -62,6 +67,7 @@ RAPIDJSON_NAMESPACE_BEGIN
enum WriteFlag {
kWriteNoFlags = 0, //!< No flags are set.
kWriteValidateEncodingFlag = 1, //!< Validate encoding of JSON strings.
+ kWriteNanAndInfFlag = 2, //!< Allow writing of Infinity, -Infinity and NaN.
kWriteDefaultFlags = RAPIDJSON_WRITE_DEFAULT_FLAGS //!< Default write flags. Can be customized by defining RAPIDJSON_WRITE_DEFAULT_FLAGS
};
@@ -101,6 +107,13 @@ public:
Writer(StackAllocator* allocator = 0, size_t levelDepth = kDefaultLevelDepth) :
os_(0), level_stack_(allocator, levelDepth * sizeof(Level)), maxDecimalPlaces_(kDefaultMaxDecimalPlaces), hasRoot_(false) {}
+#if RAPIDJSON_HAS_CXX11_RVALUE_REFS
+ Writer(Writer&& rhs) :
+ os_(rhs.os_), level_stack_(std::move(rhs.level_stack_)), maxDecimalPlaces_(rhs.maxDecimalPlaces_), hasRoot_(rhs.hasRoot_) {
+ rhs.os_ = 0;
+ }
+#endif
+
//! Reset the writer with a new stream.
/*!
This function reset the writer with a new stream and default settings,
@@ -167,30 +180,32 @@ public:
*/
//@{
- bool Null() { Prefix(kNullType); return WriteNull(); }
- bool Bool(bool b) { Prefix(b ? kTrueType : kFalseType); return WriteBool(b); }
- bool Int(int i) { Prefix(kNumberType); return WriteInt(i); }
- bool Uint(unsigned u) { Prefix(kNumberType); return WriteUint(u); }
- bool Int64(int64_t i64) { Prefix(kNumberType); return WriteInt64(i64); }
- bool Uint64(uint64_t u64) { Prefix(kNumberType); return WriteUint64(u64); }
+ bool Null() { Prefix(kNullType); return EndValue(WriteNull()); }
+ bool Bool(bool b) { Prefix(b ? kTrueType : kFalseType); return EndValue(WriteBool(b)); }
+ bool Int(int i) { Prefix(kNumberType); return EndValue(WriteInt(i)); }
+ bool Uint(unsigned u) { Prefix(kNumberType); return EndValue(WriteUint(u)); }
+ bool Int64(int64_t i64) { Prefix(kNumberType); return EndValue(WriteInt64(i64)); }
+ bool Uint64(uint64_t u64) { Prefix(kNumberType); return EndValue(WriteUint64(u64)); }
//! Writes the given \c double value to the stream
/*!
\param d The value to be written.
\return Whether it is succeed.
*/
- bool Double(double d) { Prefix(kNumberType); return WriteDouble(d); }
+ bool Double(double d) { Prefix(kNumberType); return EndValue(WriteDouble(d)); }
bool RawNumber(const Ch* str, SizeType length, bool copy = false) {
+ RAPIDJSON_ASSERT(str != 0);
(void)copy;
Prefix(kNumberType);
- return WriteString(str, length);
+ return EndValue(WriteString(str, length));
}
bool String(const Ch* str, SizeType length, bool copy = false) {
+ RAPIDJSON_ASSERT(str != 0);
(void)copy;
Prefix(kStringType);
- return WriteString(str, length);
+ return EndValue(WriteString(str, length));
}
#if RAPIDJSON_HAS_STDSTRING
@@ -207,15 +222,20 @@ public:
bool Key(const Ch* str, SizeType length, bool copy = false) { return String(str, length, copy); }
+#if RAPIDJSON_HAS_STDSTRING
+ bool Key(const std::basic_string<Ch>& str)
+ {
+ return Key(str.data(), SizeType(str.size()));
+ }
+#endif
+
bool EndObject(SizeType memberCount = 0) {
(void)memberCount;
- RAPIDJSON_ASSERT(level_stack_.GetSize() >= sizeof(Level));
- RAPIDJSON_ASSERT(!level_stack_.template Top<Level>()->inArray);
+ RAPIDJSON_ASSERT(level_stack_.GetSize() >= sizeof(Level)); // not inside an Object
+ RAPIDJSON_ASSERT(!level_stack_.template Top<Level>()->inArray); // currently inside an Array, not Object
+ RAPIDJSON_ASSERT(0 == level_stack_.template Top<Level>()->valueCount % 2); // Object has a Key without a Value
level_stack_.template Pop<Level>(1);
- bool ret = WriteEndObject();
- if (RAPIDJSON_UNLIKELY(level_stack_.Empty())) // end of json text
- os_->Flush();
- return ret;
+ return EndValue(WriteEndObject());
}
bool StartArray() {
@@ -229,10 +249,7 @@ public:
RAPIDJSON_ASSERT(level_stack_.GetSize() >= sizeof(Level));
RAPIDJSON_ASSERT(level_stack_.template Top<Level>()->inArray);
level_stack_.template Pop<Level>(1);
- bool ret = WriteEndArray();
- if (RAPIDJSON_UNLIKELY(level_stack_.Empty())) // end of json text
- os_->Flush();
- return ret;
+ return EndValue(WriteEndArray());
}
//@}
@@ -240,9 +257,9 @@ public:
//@{
//! Simpler but slower overload.
- bool String(const Ch* str) { return String(str, internal::StrLen(str)); }
- bool Key(const Ch* str) { return Key(str, internal::StrLen(str)); }
-
+ bool String(const Ch* const& str) { return String(str, internal::StrLen(str)); }
+ bool Key(const Ch* const& str) { return Key(str, internal::StrLen(str)); }
+
//@}
//! Write a raw JSON value.
@@ -253,7 +270,19 @@ public:
\param length Length of the json.
\param type Type of the root of json.
*/
- bool RawValue(const Ch* json, size_t length, Type type) { Prefix(type); return WriteRawValue(json, length); }
+ bool RawValue(const Ch* json, size_t length, Type type) {
+ RAPIDJSON_ASSERT(json != 0);
+ Prefix(type);
+ return EndValue(WriteRawValue(json, length));
+ }
+
+ //! Flush the output stream.
+ /*!
+ Allows the user to flush the output stream immediately.
+ */
+ void Flush() {
+ os_->Flush();
+ }
protected:
//! Information for each nested level
@@ -287,7 +316,7 @@ protected:
const char* end = internal::i32toa(i, buffer);
PutReserve(*os_, static_cast<size_t>(end - buffer));
for (const char* p = buffer; p != end; ++p)
- PutUnsafe(*os_, static_cast<typename TargetEncoding::Ch>(*p));
+ PutUnsafe(*os_, static_cast<typename OutputStream::Ch>(*p));
return true;
}
@@ -296,7 +325,7 @@ protected:
const char* end = internal::u32toa(u, buffer);
PutReserve(*os_, static_cast<size_t>(end - buffer));
for (const char* p = buffer; p != end; ++p)
- PutUnsafe(*os_, static_cast<typename TargetEncoding::Ch>(*p));
+ PutUnsafe(*os_, static_cast<typename OutputStream::Ch>(*p));
return true;
}
@@ -305,7 +334,7 @@ protected:
const char* end = internal::i64toa(i64, buffer);
PutReserve(*os_, static_cast<size_t>(end - buffer));
for (const char* p = buffer; p != end; ++p)
- PutUnsafe(*os_, static_cast<typename TargetEncoding::Ch>(*p));
+ PutUnsafe(*os_, static_cast<typename OutputStream::Ch>(*p));
return true;
}
@@ -314,24 +343,40 @@ protected:
char* end = internal::u64toa(u64, buffer);
PutReserve(*os_, static_cast<size_t>(end - buffer));
for (char* p = buffer; p != end; ++p)
- PutUnsafe(*os_, static_cast<typename TargetEncoding::Ch>(*p));
+ PutUnsafe(*os_, static_cast<typename OutputStream::Ch>(*p));
return true;
}
bool WriteDouble(double d) {
- if (internal::Double(d).IsNanOrInf())
- return false;
-
+ if (internal::Double(d).IsNanOrInf()) {
+ if (!(writeFlags & kWriteNanAndInfFlag))
+ return false;
+ if (internal::Double(d).IsNan()) {
+ PutReserve(*os_, 3);
+ PutUnsafe(*os_, 'N'); PutUnsafe(*os_, 'a'); PutUnsafe(*os_, 'N');
+ return true;
+ }
+ if (internal::Double(d).Sign()) {
+ PutReserve(*os_, 9);
+ PutUnsafe(*os_, '-');
+ }
+ else
+ PutReserve(*os_, 8);
+ PutUnsafe(*os_, 'I'); PutUnsafe(*os_, 'n'); PutUnsafe(*os_, 'f');
+ PutUnsafe(*os_, 'i'); PutUnsafe(*os_, 'n'); PutUnsafe(*os_, 'i'); PutUnsafe(*os_, 't'); PutUnsafe(*os_, 'y');
+ return true;
+ }
+
char buffer[25];
char* end = internal::dtoa(d, buffer, maxDecimalPlaces_);
PutReserve(*os_, static_cast<size_t>(end - buffer));
for (char* p = buffer; p != end; ++p)
- PutUnsafe(*os_, static_cast<typename TargetEncoding::Ch>(*p));
+ PutUnsafe(*os_, static_cast<typename OutputStream::Ch>(*p));
return true;
}
bool WriteString(const Ch* str, SizeType length) {
- static const typename TargetEncoding::Ch hexDigits[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
+ static const typename OutputStream::Ch hexDigits[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
static const char escape[256] = {
#define Z16 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
//0 1 2 3 4 5 6 7 8 9 A B C D E F
@@ -387,7 +432,7 @@ protected:
else if ((sizeof(Ch) == 1 || static_cast<unsigned>(c) < 256) && RAPIDJSON_UNLIKELY(escape[static_cast<unsigned char>(c)])) {
is.Take();
PutUnsafe(*os_, '\\');
- PutUnsafe(*os_, static_cast<typename TargetEncoding::Ch>(escape[static_cast<unsigned char>(c)]));
+ PutUnsafe(*os_, static_cast<typename OutputStream::Ch>(escape[static_cast<unsigned char>(c)]));
if (escape[static_cast<unsigned char>(c)] == 'u') {
PutUnsafe(*os_, '0');
PutUnsafe(*os_, '0');
@@ -442,6 +487,13 @@ protected:
}
}
+ // Flush the value if it is the top level one.
+ bool EndValue(bool ret) {
+ if (RAPIDJSON_UNLIKELY(level_stack_.Empty())) // end of json text
+ Flush();
+ return ret;
+ }
+
OutputStream* os_;
internal::Stack<StackAllocator> level_stack_;
int maxDecimalPlaces_;
@@ -489,8 +541,25 @@ inline bool Writer<StringBuffer>::WriteUint64(uint64_t u) {
template<>
inline bool Writer<StringBuffer>::WriteDouble(double d) {
- if (internal::Double(d).IsNanOrInf())
- return false;
+ if (internal::Double(d).IsNanOrInf()) {
+ // Note: This code path can only be reached if (RAPIDJSON_WRITE_DEFAULT_FLAGS & kWriteNanAndInfFlag).
+ if (!(kWriteDefaultFlags & kWriteNanAndInfFlag))
+ return false;
+ if (internal::Double(d).IsNan()) {
+ PutReserve(*os_, 3);
+ PutUnsafe(*os_, 'N'); PutUnsafe(*os_, 'a'); PutUnsafe(*os_, 'N');
+ return true;
+ }
+ if (internal::Double(d).Sign()) {
+ PutReserve(*os_, 9);
+ PutUnsafe(*os_, '-');
+ }
+ else
+ PutReserve(*os_, 8);
+ PutUnsafe(*os_, 'I'); PutUnsafe(*os_, 'n'); PutUnsafe(*os_, 'f');
+ PutUnsafe(*os_, 'i'); PutUnsafe(*os_, 'n'); PutUnsafe(*os_, 'i'); PutUnsafe(*os_, 't'); PutUnsafe(*os_, 'y');
+ return true;
+ }
char *buffer = os_->Push(25);
char* end = internal::dtoa(d, buffer, maxDecimalPlaces_);
@@ -525,7 +594,7 @@ inline bool Writer<StringBuffer>::ScanWriteUnescapedString(StringStream& is, siz
// The rest of string using SIMD
static const char dquote[16] = { '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"' };
static const char bslash[16] = { '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\' };
- static const char space[16] = { 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19 };
+ static const char space[16] = { 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F };
const __m128i dq = _mm_loadu_si128(reinterpret_cast<const __m128i *>(&dquote[0]));
const __m128i bs = _mm_loadu_si128(reinterpret_cast<const __m128i *>(&bslash[0]));
const __m128i sp = _mm_loadu_si128(reinterpret_cast<const __m128i *>(&space[0]));
@@ -534,7 +603,7 @@ inline bool Writer<StringBuffer>::ScanWriteUnescapedString(StringStream& is, siz
const __m128i s = _mm_load_si128(reinterpret_cast<const __m128i *>(p));
const __m128i t1 = _mm_cmpeq_epi8(s, dq);
const __m128i t2 = _mm_cmpeq_epi8(s, bs);
- const __m128i t3 = _mm_cmpeq_epi8(_mm_max_epu8(s, sp), sp); // s < 0x20 <=> max(s, 0x19) == 0x19
+ const __m128i t3 = _mm_cmpeq_epi8(_mm_max_epu8(s, sp), sp); // s < 0x20 <=> max(s, 0x1F) == 0x1F
const __m128i x = _mm_or_si128(_mm_or_si128(t1, t2), t3);
unsigned short r = static_cast<unsigned short>(_mm_movemask_epi8(x));
if (RAPIDJSON_UNLIKELY(r != 0)) { // some of characters is escaped
@@ -559,7 +628,75 @@ inline bool Writer<StringBuffer>::ScanWriteUnescapedString(StringStream& is, siz
is.src_ = p;
return RAPIDJSON_LIKELY(is.Tell() < length);
}
-#endif // defined(RAPIDJSON_SSE2) || defined(RAPIDJSON_SSE42)
+#elif defined(RAPIDJSON_NEON)
+template<>
+inline bool Writer<StringBuffer>::ScanWriteUnescapedString(StringStream& is, size_t length) {
+ if (length < 16)
+ return RAPIDJSON_LIKELY(is.Tell() < length);
+
+ if (!RAPIDJSON_LIKELY(is.Tell() < length))
+ return false;
+
+ const char* p = is.src_;
+ const char* end = is.head_ + length;
+ const char* nextAligned = reinterpret_cast<const char*>((reinterpret_cast<size_t>(p) + 15) & static_cast<size_t>(~15));
+ const char* endAligned = reinterpret_cast<const char*>(reinterpret_cast<size_t>(end) & static_cast<size_t>(~15));
+ if (nextAligned > end)
+ return true;
+
+ while (p != nextAligned)
+ if (*p < 0x20 || *p == '\"' || *p == '\\') {
+ is.src_ = p;
+ return RAPIDJSON_LIKELY(is.Tell() < length);
+ }
+ else
+ os_->PutUnsafe(*p++);
+
+ // The rest of string using SIMD
+ const uint8x16_t s0 = vmovq_n_u8('"');
+ const uint8x16_t s1 = vmovq_n_u8('\\');
+ const uint8x16_t s2 = vmovq_n_u8('\b');
+ const uint8x16_t s3 = vmovq_n_u8(32);
+
+ for (; p != endAligned; p += 16) {
+ const uint8x16_t s = vld1q_u8(reinterpret_cast<const uint8_t *>(p));
+ uint8x16_t x = vceqq_u8(s, s0);
+ x = vorrq_u8(x, vceqq_u8(s, s1));
+ x = vorrq_u8(x, vceqq_u8(s, s2));
+ x = vorrq_u8(x, vcltq_u8(s, s3));
+
+ x = vrev64q_u8(x); // Rev in 64
+ uint64_t low = vgetq_lane_u64(reinterpret_cast<uint64x2_t>(x), 0); // extract
+ uint64_t high = vgetq_lane_u64(reinterpret_cast<uint64x2_t>(x), 1); // extract
+
+ SizeType len = 0;
+ bool escaped = false;
+ if (low == 0) {
+ if (high != 0) {
+ unsigned lz = (unsigned)__builtin_clzll(high);
+ len = 8 + (lz >> 3);
+ escaped = true;
+ }
+ } else {
+ unsigned lz = (unsigned)__builtin_clzll(low);
+ len = lz >> 3;
+ escaped = true;
+ }
+ if (RAPIDJSON_UNLIKELY(escaped)) { // some of characters is escaped
+ char* q = reinterpret_cast<char*>(os_->PushUnsafe(len));
+ for (size_t i = 0; i < len; i++)
+ q[i] = p[i];
+
+ p += len;
+ break;
+ }
+ vst1q_u8(reinterpret_cast<uint8_t *>(os_->PushUnsafe(16)), s);
+ }
+
+ is.src_ = p;
+ return RAPIDJSON_LIKELY(is.Tell() < length);
+}
+#endif // RAPIDJSON_NEON
RAPIDJSON_NAMESPACE_END
diff --git a/include_dirs.js b/include_dirs.js
new file mode 100644
index 00000000..b373e85f
--- /dev/null
+++ b/include_dirs.js
@@ -0,0 +1,2 @@
+var path = require('path');
+console.log(path.join(path.relative('.', __dirname), 'include'));
diff --git a/library.json b/library.json
index 47fd352a..2210fcd6 100644
--- a/library.json
+++ b/library.json
@@ -1,12 +1,15 @@
{
"name": "RapidJSON",
+ "version": "1.1.0",
"keywords": "json, sax, dom, parser, generator",
"description": "A fast JSON parser/generator for C++ with both SAX/DOM style API",
- "include": "include",
+ "export": {
+ "include": "include"
+ },
"examples": "example/*/*.cpp",
"repository":
{
"type": "git",
- "url": "https://github.com/miloyip/rapidjson"
+ "url": "https://github.com/Tencent/rapidjson"
}
}
diff --git a/package.json b/package.json
new file mode 100644
index 00000000..129581a6
--- /dev/null
+++ b/package.json
@@ -0,0 +1,24 @@
+{
+ "name": "rapidjson",
+ "version": "1.0.4",
+ "description": "![](doc/logo/rapidjson.png)",
+ "main": "include_dirs.js",
+ "directories": {
+ "doc": "doc",
+ "example": "example",
+ "test": "test"
+ },
+ "scripts": {
+ "test": "echo \"Error: no test specified\" && exit 1"
+ },
+ "repository": {
+ "type": "git",
+ "url": "git+https://github.com/Tencent/rapidjson.git"
+ },
+ "author": "",
+ "license": "ISC",
+ "bugs": {
+ "url": "https://github.com/Tencent/rapidjson/issues"
+ },
+ "homepage": "https://github.com/Tencent/rapidjson#readme"
+}
diff --git a/rapidjson.autopkg b/rapidjson.autopkg
index d91aaeff..cbe52586 100644
--- a/rapidjson.autopkg
+++ b/rapidjson.autopkg
@@ -1,14 +1,14 @@
nuget {
- //Usage: Write-NuGetPackage rapidjson.autopkg -defines:MYVERSION=1.0.2
+ //Usage: Write-NuGetPackage rapidjson.autopkg -defines:MYVERSION=1.1.0
//Be sure you are running Powershell 3.0 and have the CoApp powershell extensions installed properly.
nuspec {
id = rapidjson;
version : ${MYVERSION};
title: "rapidjson";
- authors: {"https://github.com/miloyip/rapidjson/releases/tag/v1.0.2"};
+ authors: {"https://github.com/Tencent/rapidjson/releases/tag/v1.1.0"};
owners: {"@lsantos (github)"};
- licenseUrl: "https://github.com/miloyip/rapidjson/blob/master/license.txt";
- projectUrl: "https://github.com/miloyip/rapidjson/";
+ licenseUrl: "https://github.com/Tencent/rapidjson/blob/master/license.txt";
+ projectUrl: "https://github.com/Tencent/rapidjson/";
iconUrl: "https://cdn1.iconfinder.com/data/icons/fatcow/32x32/json.png";
requireLicenseAcceptance:false;
summary: @"A fast JSON parser/generator for C++ with both SAX/DOM style API";
@@ -71,5 +71,7 @@ Changed
targets {
// We're trying to be standard about these sorts of thing. (Will help with config.h later :D)
//Defines += HAS_EQCORE;
+ // Fix creating the package with Raggles' fork of CoApp
+ Includes += "$(MSBuildThisFileDirectory)../..${d_include}";
};
} \ No newline at end of file
diff --git a/readme.md b/readme.md
index 9a3d6a7b..78c9540d 100644
--- a/readme.md
+++ b/readme.md
@@ -1,14 +1,14 @@
-![](doc/logo/rapidjson.png)
+![RapidJSON logo](doc/logo/rapidjson.png)
-![](https://img.shields.io/badge/release-v1.0.2-blue.png)
+![Release version](https://img.shields.io/badge/release-v1.1.0-blue.svg)
-## A fast JSON parser/generator for C++ with both SAX/DOM style API
+## A fast JSON parser/generator for C++ with both SAX/DOM style API
Tencent is pleased to support the open source community by making RapidJSON available.
Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
-* [RapidJSON GitHub](https://github.com/miloyip/rapidjson/)
+* [RapidJSON GitHub](https://github.com/Tencent/rapidjson/)
* RapidJSON Documentation
* [English](http://rapidjson.org/)
* [简体中文](http://rapidjson.org/zh-cn/)
@@ -20,12 +20,12 @@ Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights
| :---------------: | :-----------------: | :-------------------: |
| ![lin-badge] | ![win-badge] | ![cov-badge] |
-[lin-badge]: https://travis-ci.org/miloyip/rapidjson.png?branch=master "Travis build status"
-[lin-link]: https://travis-ci.org/miloyip/rapidjson "Travis build status"
-[win-badge]: https://ci.appveyor.com/api/projects/status/u658dcuwxo14a8m9/branch/master "AppVeyor build status"
-[win-link]: https://ci.appveyor.com/project/miloyip/rapidjson/branch/master "AppVeyor build status"
-[cov-badge]: https://coveralls.io/repos/miloyip/rapidjson/badge.png?branch=master
-[cov-link]: https://coveralls.io/r/miloyip/rapidjson?branch=master
+[lin-badge]: https://travis-ci.org/Tencent/rapidjson.svg?branch=master "Travis build status"
+[lin-link]: https://travis-ci.org/Tencent/rapidjson "Travis build status"
+[win-badge]: https://ci.appveyor.com/api/projects/status/l6qulgqahcayidrf/branch/master?svg=true "AppVeyor build status"
+[win-link]: https://ci.appveyor.com/project/miloyip/rapidjson-0fdqj/branch/master "AppVeyor build status"
+[cov-badge]: https://coveralls.io/repos/Tencent/rapidjson/badge.svg?branch=master "Coveralls coverage"
+[cov-link]: https://coveralls.io/r/Tencent/rapidjson?branch=master "Coveralls coverage"
## Introduction
@@ -37,16 +37,26 @@ RapidJSON is a JSON parser and generator for C++. It was inspired by [RapidXml](
* RapidJSON is **self-contained** and **header-only**. It does not depend on external libraries such as BOOST. It even does not depend on STL.
-* RapidJSON is **memory-friendly**. Each JSON value occupies exactly 16/20 bytes for most 32/64-bit machines (excluding text string). By default it uses a fast memory allocator, and the parser allocates memory compactly during parsing.
+* RapidJSON is **memory-friendly**. Each JSON value occupies exactly 16 bytes for most 32/64-bit machines (excluding text string). By default it uses a fast memory allocator, and the parser allocates memory compactly during parsing.
* RapidJSON is **Unicode-friendly**. It supports UTF-8, UTF-16, UTF-32 (LE & BE), and their detection, validation and transcoding internally. For example, you can read a UTF-8 file and let RapidJSON transcode the JSON strings into UTF-16 in the DOM. It also supports surrogates and "\u0000" (null character).
More features can be read [here](doc/features.md).
-JSON(JavaScript Object Notation) is a light-weight data exchange format. RapidJSON should be in fully compliance with RFC7159/ECMA-404. More information about JSON can be obtained at
+JSON(JavaScript Object Notation) is a light-weight data exchange format. RapidJSON should be in full compliance with RFC7159/ECMA-404, with optional support of relaxed syntax. More information about JSON can be obtained at
* [Introducing JSON](http://json.org/)
-* [RFC7159: The JavaScript Object Notation (JSON) Data Interchange Format](http://www.ietf.org/rfc/rfc7159.txt)
-* [Standard ECMA-404: The JSON Data Interchange Format](http://www.ecma-international.org/publications/standards/Ecma-404.htm)
+* [RFC7159: The JavaScript Object Notation (JSON) Data Interchange Format](https://tools.ietf.org/html/rfc7159)
+* [Standard ECMA-404: The JSON Data Interchange Format](https://www.ecma-international.org/publications/standards/Ecma-404.htm)
+
+## Highlights in v1.1 (2016-8-25)
+
+* Added [JSON Pointer](doc/pointer.md)
+* Added [JSON Schema](doc/schema.md)
+* Added [relaxed JSON syntax](doc/dom.md) (comment, trailing comma, NaN/Infinity)
+* Iterating array/object with [C++11 Range-based for loop](doc/tutorial.md)
+* Reduce memory overhead of each `Value` from 24 bytes to 16 bytes in x86-64 architecture.
+
+For other changes please refer to [change log](CHANGELOG.md).
## Compatibility
@@ -64,8 +74,8 @@ RapidJSON is a header-only C++ library. Just copy the `include/rapidjson` folder
RapidJSON uses following software as its dependencies:
* [CMake](https://cmake.org/) as a general build tool
-* (optional)[Doxygen](http://www.doxygen.org) to build documentation
-* (optional)[googletest](https://github.com/google/googletest) for unit and performance testing
+* (optional) [Doxygen](http://www.doxygen.org) to build documentation
+* (optional) [googletest](https://github.com/google/googletest) for unit and performance testing
To generate user documentation and run tests please proceed with the steps below:
@@ -74,7 +84,7 @@ To generate user documentation and run tests please proceed with the steps below
3. Change to `build` directory and run `cmake ..` command to configure your build. Windows users can do the same with cmake-gui application.
4. On Windows, build the solution found in the build directory. On Linux, run `make` from the build directory.
-On successfull build you will find compiled test and example binaries in `bin`
+On successful build you will find compiled test and example binaries in `bin`
directory. The generated documentation will be available in `doc/html`
directory of the build tree. To run tests after finished build please run `make
test` or `ctest` from your build tree. You can get detailed output using `ctest
@@ -126,4 +136,25 @@ The following diagram shows the process.
![simpledom](doc/diagram/simpledom.png)
-More [examples](https://github.com/miloyip/rapidjson/tree/master/example) are available.
+More [examples](https://github.com/Tencent/rapidjson/tree/master/example) are available:
+
+* DOM API
+ * [tutorial](https://github.com/Tencent/rapidjson/blob/master/example/tutorial/tutorial.cpp): Basic usage of DOM API.
+
+* SAX API
+ * [simplereader](https://github.com/Tencent/rapidjson/blob/master/example/simplereader/simplereader.cpp): Dumps all SAX events while parsing a JSON by `Reader`.
+ * [condense](https://github.com/Tencent/rapidjson/blob/master/example/condense/condense.cpp): A command line tool to rewrite a JSON, with all whitespaces removed.
+ * [pretty](https://github.com/Tencent/rapidjson/blob/master/example/pretty/pretty.cpp): A command line tool to rewrite a JSON with indents and newlines by `PrettyWriter`.
+ * [capitalize](https://github.com/Tencent/rapidjson/blob/master/example/capitalize/capitalize.cpp): A command line tool to capitalize strings in JSON.
+ * [messagereader](https://github.com/Tencent/rapidjson/blob/master/example/messagereader/messagereader.cpp): Parse a JSON message with SAX API.
+ * [serialize](https://github.com/Tencent/rapidjson/blob/master/example/serialize/serialize.cpp): Serialize a C++ object into JSON with SAX API.
+ * [jsonx](https://github.com/Tencent/rapidjson/blob/master/example/jsonx/jsonx.cpp): Implements a `JsonxWriter` which stringify SAX events into [JSONx](https://www-01.ibm.com/support/knowledgecenter/SS9H2Y_7.1.0/com.ibm.dp.doc/json_jsonx.html) (a kind of XML) format. The example is a command line tool which converts input JSON into JSONx format.
+
+* Schema
+ * [schemavalidator](https://github.com/Tencent/rapidjson/blob/master/example/schemavalidator/schemavalidator.cpp) : A command line tool to validate a JSON with a JSON schema.
+
+* Advanced
+ * [prettyauto](https://github.com/Tencent/rapidjson/blob/master/example/prettyauto/prettyauto.cpp): A modified version of [pretty](https://github.com/Tencent/rapidjson/blob/master/example/pretty/pretty.cpp) to automatically handle JSON with any UTF encodings.
+ * [parsebyparts](https://github.com/Tencent/rapidjson/blob/master/example/parsebyparts/parsebyparts.cpp): Implements an `AsyncDocumentParser` which can parse JSON in parts, using C++11 thread.
+ * [filterkey](https://github.com/Tencent/rapidjson/blob/master/example/filterkey/filterkey.cpp): A command line tool to remove all values with user-specified key.
+ * [filterkeydom](https://github.com/Tencent/rapidjson/blob/master/example/filterkeydom/filterkeydom.cpp): Same tool as above, but it demonstrates how to use a generator to populate a `Document`.
diff --git a/readme.zh-cn.md b/readme.zh-cn.md
index 5124f8ee..ccf16699 100644
--- a/readme.zh-cn.md
+++ b/readme.zh-cn.md
@@ -1,18 +1,18 @@
-![](doc/logo/rapidjson.png)
+![RapidJSON logo](doc/logo/rapidjson.png)
-![](https://img.shields.io/badge/release-v1.0.2-blue.png)
+![Release version](https://img.shields.io/badge/release-v1.1.0-blue.svg)
-## 高效的C++ JSON解æžï¼ç”Ÿæˆå™¨ï¼Œæä¾›SAXåŠDOM风格API
+## 高效的 C++ JSON 解æžï¼ç”Ÿæˆå™¨ï¼Œæä¾› SAX åŠ DOM 风格 API
Tencent is pleased to support the open source community by making RapidJSON available.
Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
-* [RapidJSON GitHub](https://github.com/miloyip/rapidjson/)
+* [RapidJSON GitHub](https://github.com/Tencent/rapidjson/)
* RapidJSON 文档
* [English](http://rapidjson.org/)
* [简体中文](http://rapidjson.org/zh-cn/)
- * [GitBook](https://www.gitbook.com/book/miloyip/rapidjson/)å¯ä¸‹è½½PDF/EPUB/MOBI,但ä¸å«APIå‚考手册。
+ * [GitBook](https://www.gitbook.com/book/miloyip/rapidjson/details/zh-cn) å¯ä¸‹è½½ PDF/EPUB/MOBI,但ä¸å« API å‚考手册。
## Build 状æ€
@@ -20,37 +20,47 @@ Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights
| :---------------: | :-----------------: | :-------------------: |
| ![lin-badge] | ![win-badge] | ![cov-badge] |
-[lin-badge]: https://travis-ci.org/miloyip/rapidjson.png?branch=master "Travis build status"
-[lin-link]: https://travis-ci.org/miloyip/rapidjson "Travis build status"
-[win-badge]: https://ci.appveyor.com/api/projects/status/u658dcuwxo14a8m9/branch/master "AppVeyor build status"
-[win-link]: https://ci.appveyor.com/project/miloyip/rapidjson/branch/master "AppVeyor build status"
-[cov-badge]: https://coveralls.io/repos/miloyip/rapidjson/badge.png?branch=master
-[cov-link]: https://coveralls.io/r/miloyip/rapidjson?branch=master
+[lin-badge]: https://travis-ci.org/Tencent/rapidjson.svg?branch=master "Travis build status"
+[lin-link]: https://travis-ci.org/Tencent/rapidjson "Travis build status"
+[win-badge]: https://ci.appveyor.com/api/projects/status/l6qulgqahcayidrf/branch/master?svg=true "AppVeyor build status"
+[win-link]: https://ci.appveyor.com/project/miloyip/rapidjson-0fdqj/branch/master "AppVeyor build status"
+[cov-badge]: https://coveralls.io/repos/Tencent/rapidjson/badge.svg?branch=master "Coveralls coverage"
+[cov-link]: https://coveralls.io/r/Tencent/rapidjson?branch=master "Coveralls coverage"
## 简介
-RapidJSON是一个C++çš„JSON解æžå™¨åŠç”Ÿæˆå™¨ã€‚它的çµæ„Ÿæ¥è‡ª[RapidXml](http://rapidxml.sourceforge.net/)。
+RapidJSON 是一个 C++ çš„ JSON 解æžå™¨åŠç”Ÿæˆå™¨ã€‚它的çµæ„Ÿæ¥è‡ª [RapidXml](http://rapidxml.sourceforge.net/)。
-* RapidJSONå°è€Œå…¨ã€‚它åŒæ—¶æ”¯æŒSAXå’ŒDOM风格的API。SAX解æžå™¨åªæœ‰çº¦500行代ç ã€‚
+* RapidJSON å°è€Œå…¨ã€‚它åŒæ—¶æ”¯æŒ SAX å’Œ DOM 风格的 API。SAX 解æžå™¨åªæœ‰çº¦ 500 行代ç ã€‚
-* RapidJSON快。它的性能å¯ä¸Ž`strlen()`相比。å¯æ”¯æŒSSE2/SSE4.2加速。
+* RapidJSON 快。它的性能å¯ä¸Ž `strlen()` 相比。å¯æ”¯æŒ SSE2/SSE4.2 加速。
-* RapidJSON独立。它ä¸ä¾èµ–于BOOST等外部库。它甚至ä¸ä¾èµ–于STL。
+* RapidJSON 独立。它ä¸ä¾èµ–于 BOOST 等外部库。它甚至ä¸ä¾èµ–于 STL。
-* RapidJSON对内存å‹å¥½ã€‚在大部分32/64ä½æœºå™¨ä¸Šï¼Œæ¯ä¸ªJSON值åªå 16或20字节(除字符串外)。它预设使用一个快速的内存分é…器,令分æžå™¨å¯ä»¥ç´§å‡‘地分é…内存。
+* RapidJSON 对内存å‹å¥½ã€‚在大部分 32/64 ä½æœºå™¨ä¸Šï¼Œæ¯ä¸ª JSON 值åªå  16 字节(除字符串外)。它预设使用一个快速的内存分é…器,令分æžå™¨å¯ä»¥ç´§å‡‘地分é…内存。
-* RapidJSON对Unicodeå‹å¥½ã€‚它支æŒUTF-8ã€UTF-16ã€UTF-32 (大端åºï¼å°ç«¯åº),并内部支æŒè¿™äº›ç¼–ç çš„检测ã€æ ¡éªŒåŠè½¬ç ã€‚例如,RapidJSONå¯ä»¥åœ¨åˆ†æžä¸€ä¸ªUTF-8文件至DOM时,把当中的JSON字符串转ç è‡³UTF-16。它也支æŒä»£ç†å¯¹ï¼ˆsurrogate pair)åŠ`"\u0000"`(空字符)。
+* RapidJSON 对 Unicode å‹å¥½ã€‚å®ƒæ”¯æŒ UTF-8ã€UTF-16ã€UTF-32 (大端åºï¼å°ç«¯åº),并内部支æŒè¿™äº›ç¼–ç çš„检测ã€æ ¡éªŒåŠè½¬ç ã€‚例如,RapidJSON å¯ä»¥åœ¨åˆ†æžä¸€ä¸ª UTF-8 文件至 DOM 时,把当中的 JSON 字符串转ç è‡³ UTF-16。它也支æŒä»£ç†å¯¹ï¼ˆsurrogate pairï¼‰åŠ `"\u0000"`(空字符)。
-在[这里](doc/features.md)å¯è¯»å–更多特点。
+在 [这里](doc/features.zh-cn.md) å¯è¯»å–更多特点。
-JSON(JavaScript Object Notation)是一个轻é‡çš„æ•°æ®äº¤æ¢æ ¼å¼ã€‚RapidJSON应该完全éµä»ŽRFC7159/ECMA-404。 关于JSON的更多信æ¯å¯å‚考:
+JSON(JavaScript Object Notation)是一个轻é‡çš„æ•°æ®äº¤æ¢æ ¼å¼ã€‚RapidJSON 应该完全éµä»Ž RFC7159/ECMA-404,并支æŒå¯é€‰çš„放宽语法。 关于 JSON 的更多信æ¯å¯å‚考:
* [Introducing JSON](http://json.org/)
-* [RFC7159: The JavaScript Object Notation (JSON) Data Interchange Format](http://www.ietf.org/rfc/rfc7159.txt)
-* [Standard ECMA-404: The JSON Data Interchange Format](http://www.ecma-international.org/publications/standards/Ecma-404.htm)
+* [RFC7159: The JavaScript Object Notation (JSON) Data Interchange Format](https://tools.ietf.org/html/rfc7159)
+* [Standard ECMA-404: The JSON Data Interchange Format](https://www.ecma-international.org/publications/standards/Ecma-404.htm)
+
+## v1.1 中的亮点 (2016-8-25)
+
+* 加入 [JSON Pointer](doc/pointer.zh-cn.md) 功能,å¯æ›´ç®€å•åœ°è®¿é—®åŠæ›´æ”¹ DOM。
+* 加入 [JSON Schema](doc/schema.zh-cn.md) 功能,å¯åœ¨è§£æžæˆ–ç”Ÿæˆ JSON 时进行校验。
+* 加入 [放宽的 JSON 语法](doc/dom.zh-cn.md) (注释ã€å°¾éšé€—å·ã€NaN/Infinity)
+* 使用 [C++11 范围 for 循环](doc/tutorial.zh-cn.md) 去é历 array å’Œ object。
+* 在 x86-64 架构下,缩å‡æ¯ä¸ª `Value` 的内存开销从 24 字节至 16 字节。
+
+其他改动请å‚考 [change log](CHANGELOG.md).
## 兼容性
-RapidJSON是跨平å°çš„。以下是一些曾测试的平å°ï¼ç¼–译器组åˆï¼š
+RapidJSON 是跨平å°çš„。以下是一些曾测试的平å°ï¼ç¼–译器组åˆï¼š
* Visual C++ 2008/2010/2013 在 Windows (32/64-bit)
* GNU C++ 3.8.x 在 Cygwin
* Clang 3.4 在 Mac OS X (32/64-bit) åŠ iOS
@@ -60,27 +70,27 @@ RapidJSON是跨平å°çš„。以下是一些曾测试的平å°ï¼ç¼–译器组åˆï¼
## 安装
-RapidJSON是åªæœ‰å¤´æ–‡ä»¶çš„C++库。åªéœ€æŠŠ`include/rapidjson`目录å¤åˆ¶è‡³ç³»ç»Ÿæˆ–项目的include目录中。
+RapidJSON 是åªæœ‰å¤´æ–‡ä»¶çš„ C++ 库。åªéœ€æŠŠ `include/rapidjson` 目录å¤åˆ¶è‡³ç³»ç»Ÿæˆ–项目的 include 目录中。
-RapidJSONä¾èµ–于以下软件:
-* [CMake](http://www.cmake.org) 作为通用生æˆå·¥å…·
-* (optional)[Doxygen](http://www.doxygen.org)用于生æˆæ–‡æ¡£
-* (optional)[googletest](https://code.google.com/p/googletest/)用于å•å…ƒåŠæ€§èƒ½æµ‹è¯•
+RapidJSON ä¾èµ–于以下软件:
+* [CMake](https://cmake.org/) 作为通用生æˆå·¥å…·
+* (optional) [Doxygen](http://www.doxygen.org) 用于生æˆæ–‡æ¡£
+* (optional) [googletest](https://github.com/google/googletest) 用于å•å…ƒåŠæ€§èƒ½æµ‹è¯•
生æˆæµ‹è¯•åŠä¾‹å­çš„步骤:
1. 执行 `git submodule update --init` åŽ»èŽ·å– thirdparty submodules (google test)。
-2. 在rapidjson目渌下,建立一个`build`目录。
-3. 在`build`目录下执行`cmake ..`命令以设置生æˆã€‚Windows用户å¯ä½¿ç”¨cmake-gui应用程åºã€‚
-4. 在Windows下,编译生æˆåœ¨build目录中的solution。在Linux下,于build目录è¿è¡Œ`make`。
+2. 在 rapidjson 目录下,建立一个 `build` 目录。
+3. 在 `build` 目录下执行 `cmake ..` 命令以设置生æˆã€‚Windows 用户å¯ä½¿ç”¨ cmake-gui 应用程åºã€‚
+4. 在 Windows 下,编译生æˆåœ¨ build 目录中的 solution。在 Linux 下,于 build 目录è¿è¡Œ `make`。
-æˆåŠŸç”ŸæˆåŽï¼Œä½ ä¼šåœ¨`bin`的目录下找到编译åŽçš„测试åŠä¾‹å­å¯æ‰§è¡Œæ–‡ä»¶ã€‚而生æˆçš„文档将ä½äºŽbuild下的`doc/html`目录。è¦æ‰§è¡Œæµ‹è¯•ï¼Œè¯·åœ¨build下执行`make test`或`ctest`。使用`ctest -V`命令å¯èŽ·å–详细的输出。
+æˆåŠŸç”ŸæˆåŽï¼Œä½ ä¼šåœ¨ `bin` 的目录下找到编译åŽçš„测试åŠä¾‹å­å¯æ‰§è¡Œæ–‡ä»¶ã€‚而生æˆçš„文档将ä½äºŽ build 下的 `doc/html` 目录。è¦æ‰§è¡Œæµ‹è¯•ï¼Œè¯·åœ¨ build 下执行 `make test` 或 `ctest`。使用 `ctest -V` 命令å¯èŽ·å–详细的输出。
-我们也å¯ä»¥æŠŠç¨‹åºåº“安装至全系统中,åªè¦åœ¨å…·ç®¡ç†æ¬Šé™ä¸‹ä»Žbuild目录执行`make install`命令。这样会按系统的å好设置安装所有文件。当安装RapidJSONåŽï¼Œå…¶ä»–çš„CMake项目需è¦ä½¿ç”¨å®ƒæ—¶ï¼Œå¯ä»¥é€šè¿‡åœ¨`CMakeLists.txt`加入一å¥`find_package(RapidJSON)`。
+我们也å¯ä»¥æŠŠç¨‹åºåº“安装至全系统中,åªè¦åœ¨å…·ç®¡ç†æƒé™ä¸‹ä»Ž build 目录执行 `make install` 命令。这样会按系统的å好设置安装所有文件。当安装 RapidJSON åŽï¼Œå…¶ä»–çš„ CMake 项目需è¦ä½¿ç”¨å®ƒæ—¶ï¼Œå¯ä»¥é€šè¿‡åœ¨ `CMakeLists.txt` åŠ å…¥ä¸€å¥ `find_package(RapidJSON)`。
## 用法一览
-此简å•ä¾‹å­è§£æžä¸€ä¸ªJSON字符串至一个document (DOM),对DOM作出简å•ä¿®æ”¹ï¼Œæœ€ç»ˆæŠŠDOM转æ¢ï¼ˆstringify)至JSON字符串。
+此简å•ä¾‹å­è§£æžä¸€ä¸ª JSON 字符串至一个 document (DOM),对 DOM 作出简å•ä¿®æ”¹ï¼Œæœ€ç»ˆæŠŠ DOM 转æ¢ï¼ˆstringify)至 JSON 字符串。
~~~~~~~~~~cpp
// rapidjson/example/simpledom/simpledom.cpp`
@@ -92,16 +102,16 @@ RapidJSONä¾èµ–于以下软件:
using namespace rapidjson;
int main() {
- // 1. 把JSON解æžè‡³DOM。
+ // 1. 把 JSON 解æžè‡³ DOM。
const char* json = "{\"project\":\"rapidjson\",\"stars\":10}";
Document d;
d.Parse(json);
- // 2. 利用DOM作出修改。
+ // 2. 利用 DOM 作出修改。
Value& s = d["stars"];
s.SetInt(s.GetInt() + 1);
- // 3. 把DOM转æ¢ï¼ˆstringify)æˆJSON。
+ // 3. 把 DOM 转æ¢ï¼ˆstringifyï¼‰æˆ JSON。
StringBuffer buffer;
Writer<StringBuffer> writer(buffer);
d.Accept(writer);
@@ -118,4 +128,25 @@ int main() {
![simpledom](doc/diagram/simpledom.png)
-还有许多[例å­](https://github.com/miloyip/rapidjson/tree/master/example)å¯ä¾›å‚考。
+还有许多 [例å­](https://github.com/Tencent/rapidjson/tree/master/example) å¯ä¾›å‚考:
+
+* DOM API
+ * [tutorial](https://github.com/Tencent/rapidjson/blob/master/example/tutorial/tutorial.cpp): DOM API 的基本使用方法。
+
+* SAX API
+ * [simplereader](https://github.com/Tencent/rapidjson/blob/master/example/simplereader/simplereader.cpp): 使用 `Reader` è§£æž JSON 时,打å°æ‰€æœ‰ SAX 事件。
+ * [condense](https://github.com/Tencent/rapidjson/blob/master/example/condense/condense.cpp): 移除 JSON 中所有空白符的命令行工具。
+ * [pretty](https://github.com/Tencent/rapidjson/blob/master/example/pretty/pretty.cpp): 为 JSON 加入缩进与æ¢è¡Œçš„命令行工具,当中使用了 `PrettyWriter`。
+ * [capitalize](https://github.com/Tencent/rapidjson/blob/master/example/capitalize/capitalize.cpp): 把 JSON 中所有字符串改为大写的命令行工具。
+ * [messagereader](https://github.com/Tencent/rapidjson/blob/master/example/messagereader/messagereader.cpp): 使用 SAX API 去解æžä¸€ä¸ª JSON 报文。
+ * [serialize](https://github.com/Tencent/rapidjson/blob/master/example/serialize/serialize.cpp): 使用 SAX API 去åºåˆ—化 C++ å¯¹è±¡ï¼Œç”Ÿæˆ JSON。
+ * [jsonx](https://github.com/Tencent/rapidjson/blob/master/example/jsonx/jsonx.cpp): 实现了一个 `JsonxWriter`,它能把 SAX äº‹ä»¶å†™æˆ [JSONx](https://www-01.ibm.com/support/knowledgecenter/SS9H2Y_7.1.0/com.ibm.dp.doc/json_jsonx.html)ï¼ˆä¸€ç§ XML)格å¼ã€‚这个例å­æ˜¯æŠŠ JSON 输入转æ¢æˆ JSONx æ ¼å¼çš„命令行工具。
+
+* Schema API
+ * [schemavalidator](https://github.com/Tencent/rapidjson/blob/master/example/schemavalidator/schemavalidator.cpp): 使用 JSON Schema 去校验 JSON 的命令行工具。
+
+* 进阶
+ * [prettyauto](https://github.com/Tencent/rapidjson/blob/master/example/prettyauto/prettyauto.cpp): [pretty](https://github.com/Tencent/rapidjson/blob/master/example/pretty/pretty.cpp) 的修改版本,å¯è‡ªåŠ¨å¤„ç†ä»»ä½• UTF ç¼–ç çš„ JSON。
+ * [parsebyparts](https://github.com/Tencent/rapidjson/blob/master/example/parsebyparts/parsebyparts.cpp): 这例å­ä¸­çš„ `AsyncDocumentParser` 类使用 C++ 线程æ¥é€æ®µè§£æž JSON。
+ * [filterkey](https://github.com/Tencent/rapidjson/blob/master/example/filterkey/filterkey.cpp): 移å–使用者指定的键值的命令行工具。
+ * [filterkeydom](https://github.com/Tencent/rapidjson/blob/master/example/filterkey/filterkey.cpp): 如上的工具,但展示如何使用生æˆå™¨ï¼ˆgenerator)去填充一个 `Document`。 \ No newline at end of file
diff --git a/test/perftest/CMakeLists.txt b/test/perftest/CMakeLists.txt
index c33aae46..035e544d 100644
--- a/test/perftest/CMakeLists.txt
+++ b/test/perftest/CMakeLists.txt
@@ -19,6 +19,8 @@ if(CCACHE_FOUND)
endif()
endif(CCACHE_FOUND)
+set_property(DIRECTORY PROPERTY COMPILE_OPTIONS ${EXTRA_CXX_FLAGS})
+
IF(NOT (CMAKE_BUILD_TYPE STREQUAL "Debug"))
add_test(NAME perftest
COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/perftest
diff --git a/test/perftest/perftest.h b/test/perftest/perftest.h
index b098e414..953f95de 100644
--- a/test/perftest/perftest.h
+++ b/test/perftest/perftest.h
@@ -24,10 +24,13 @@
// __SSE2__ and __SSE4_2__ are recognized by gcc, clang, and the Intel compiler.
// We use -march=native with gmake to enable -msse2 and -msse4.2, if supported.
+// Likewise, __ARM_NEON is used to detect Neon.
#if defined(__SSE4_2__)
# define RAPIDJSON_SSE42
#elif defined(__SSE2__)
# define RAPIDJSON_SSE2
+#elif defined(__ARM_NEON)
+# define RAPIDJSON_NEON
#endif
#define RAPIDJSON_HAS_STDSTRING 1
diff --git a/test/perftest/rapidjsontest.cpp b/test/perftest/rapidjsontest.cpp
index 675db318..a11a557d 100644
--- a/test/perftest/rapidjsontest.cpp
+++ b/test/perftest/rapidjsontest.cpp
@@ -28,6 +28,8 @@
#define SIMD_SUFFIX(name) name##_SSE2
#elif defined(RAPIDJSON_SSE42)
#define SIMD_SUFFIX(name) name##_SSE42
+#elif defined(RAPIDJSON_NEON)
+#define SIMD_SUFFIX(name) name##_NEON
#else
#define SIMD_SUFFIX(name) name
#endif
@@ -152,6 +154,35 @@ TEST_F(RapidJson, SIMD_SUFFIX(ReaderParseIterativeInsitu_DummyHandler)) {
}
}
+TEST_F(RapidJson, SIMD_SUFFIX(ReaderParseIterativePull_DummyHandler)) {
+ for (size_t i = 0; i < kTrialCount; i++) {
+ StringStream s(json_);
+ BaseReaderHandler<> h;
+ Reader reader;
+ reader.IterativeParseInit();
+ while (!reader.IterativeParseComplete()) {
+ if (!reader.IterativeParseNext<kParseDefaultFlags>(s, h))
+ break;
+ }
+ EXPECT_FALSE(reader.HasParseError());
+ }
+}
+
+TEST_F(RapidJson, SIMD_SUFFIX(ReaderParseIterativePullInsitu_DummyHandler)) {
+ for (size_t i = 0; i < kTrialCount; i++) {
+ memcpy(temp_, json_, length_ + 1);
+ InsituStringStream s(temp_);
+ BaseReaderHandler<> h;
+ Reader reader;
+ reader.IterativeParseInit();
+ while (!reader.IterativeParseComplete()) {
+ if (!reader.IterativeParseNext<kParseDefaultFlags|kParseInsituFlag>(s, h))
+ break;
+ }
+ EXPECT_FALSE(reader.HasParseError());
+ }
+}
+
TEST_F(RapidJson, SIMD_SUFFIX(ReaderParse_DummyHandler_ValidateEncoding)) {
for (size_t i = 0; i < kTrialCount; i++) {
StringStream s(json_);
diff --git a/test/unittest/CMakeLists.txt b/test/unittest/CMakeLists.txt
index 02c15327..072b7b15 100644
--- a/test/unittest/CMakeLists.txt
+++ b/test/unittest/CMakeLists.txt
@@ -1,6 +1,9 @@
+include(CheckCXXCompilerFlag)
+
set(UNITTEST_SOURCES
allocatorstest.cpp
bigintegertest.cpp
+ cursorstreamwrappertest.cpp
documenttest.cpp
dtoatest.cpp
encodedstreamtest.cpp
@@ -34,12 +37,31 @@ if(CCACHE_FOUND)
endif()
endif(CCACHE_FOUND)
-if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
- set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror -Wall -Wextra -Weffc++ -Wswitch-default -Wfloat-equal")
-elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
- set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror -Wall -Wextra -Weffc++ -Wswitch-default -Wfloat-equal -Wimplicit-fallthrough -Weverything")
+set_property(DIRECTORY PROPERTY COMPILE_OPTIONS ${EXTRA_CXX_FLAGS})
+
+if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
+ # If the user is running a newer version of Clang that includes the
+ # -Wdouble-promotion, we will ignore that warning.
+ if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 3.7)
+ CHECK_CXX_COMPILER_FLAG("-Wno-double-promotion" HAS_NO_DOUBLE_PROMOTION)
+ if (HAS_NO_DOUBLE_PROMOTION)
+ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-double-promotion")
+ endif()
+ endif()
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
- add_definitions(-D_CRT_SECURE_NO_WARNINGS=1)
+ # Force to always compile with /W4
+ if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
+ string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
+ else()
+ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
+ endif()
+
+ # Force to always compile with /WX
+ if(CMAKE_CXX_FLAGS MATCHES "/WX-")
+ string(REGEX REPLACE "/WX-" "/WX" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
+ else()
+ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /WX")
+ endif()
endif()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DRAPIDJSON_HAS_STDSTRING=1")
@@ -58,7 +80,7 @@ add_test(NAME unittest
if(NOT MSVC)
# Not running SIMD.* unit test cases for Valgrind
add_test(NAME valgrind_unittest
- COMMAND valgrind --leak-check=full --error-exitcode=1 ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/unittest --gtest_filter=-SIMD.*
+ COMMAND valgrind --suppressions=${CMAKE_SOURCE_DIR}/test/valgrind.supp --leak-check=full --error-exitcode=1 ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/unittest --gtest_filter=-SIMD.*
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
diff --git a/test/unittest/cursorstreamwrappertest.cpp b/test/unittest/cursorstreamwrappertest.cpp
new file mode 100644
index 00000000..a1162481
--- /dev/null
+++ b/test/unittest/cursorstreamwrappertest.cpp
@@ -0,0 +1,115 @@
+// Tencent is pleased to support the open source community by making RapidJSON available.
+//
+// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
+//
+// Licensed under the MIT License (the "License"); you may not use this file except
+// in compliance with the License. You may obtain a copy of the License at
+//
+// http://opensource.org/licenses/MIT
+//
+// Unless required by applicable law or agreed to in writing, software distributed
+// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
+// CONDITIONS OF ANY KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations under the License.
+
+#include "unittest.h"
+#include "rapidjson/document.h"
+#include "rapidjson/cursorstreamwrapper.h"
+
+using namespace rapidjson;
+
+// static const char json[] = "{\"string\"\n\n:\"my string\",\"array\"\n:[\"1\", \"2\", \"3\"]}";
+
+bool testJson(const char *json, size_t &line, size_t &col) {
+ StringStream ss(json);
+ CursorStreamWrapper<StringStream> csw(ss);
+ Document document;
+ document.ParseStream(csw);
+ bool ret = document.HasParseError();
+ if (ret) {
+ col = csw.GetColumn();
+ line = csw.GetLine();
+ }
+ return ret;
+}
+
+TEST(CursorStreamWrapper, MissingFirstBracket) {
+ const char json[] = "\"string\"\n\n:\"my string\",\"array\"\n:[\"1\", \"2\", \"3\"]}";
+ size_t col, line;
+ bool ret = testJson(json, line, col);
+ EXPECT_TRUE(ret);
+ EXPECT_EQ(line, 3);
+ EXPECT_EQ(col, 0);
+}
+
+TEST(CursorStreamWrapper, MissingQuotes) {
+ const char json[] = "{\"string\n\n:\"my string\",\"array\"\n:[\"1\", \"2\", \"3\"]}";
+ size_t col, line;
+ bool ret = testJson(json, line, col);
+ EXPECT_TRUE(ret);
+ EXPECT_EQ(line, 1);
+ EXPECT_EQ(col, 8);
+}
+
+TEST(CursorStreamWrapper, MissingColon) {
+ const char json[] = "{\"string\"\n\n\"my string\",\"array\"\n:[\"1\", \"2\", \"3\"]}";
+ size_t col, line;
+ bool ret = testJson(json, line, col);
+ EXPECT_TRUE(ret);
+ EXPECT_EQ(line, 3);
+ EXPECT_EQ(col, 0);
+}
+
+TEST(CursorStreamWrapper, MissingSecondQuotes) {
+ const char json[] = "{\"string\"\n\n:my string\",\"array\"\n:[\"1\", \"2\", \"3\"]}";
+ size_t col, line;
+ bool ret = testJson(json, line, col);
+ EXPECT_TRUE(ret);
+ EXPECT_EQ(line, 3);
+ EXPECT_EQ(col, 1);
+}
+
+TEST(CursorStreamWrapper, MissingComma) {
+ const char json[] = "{\"string\"\n\n:\"my string\"\"array\"\n:[\"1\", \"2\", \"3\"]}";
+ size_t col, line;
+ bool ret = testJson(json, line, col);
+ EXPECT_TRUE(ret);
+ EXPECT_EQ(line, 3);
+ EXPECT_EQ(col, 12);
+}
+
+TEST(CursorStreamWrapper, MissingArrayBracket) {
+ const char json[] = "{\"string\"\n\n:\"my string\",\"array\"\n:\"1\", \"2\", \"3\"]}";
+ size_t col, line;
+ bool ret = testJson(json, line, col);
+ EXPECT_TRUE(ret);
+ EXPECT_EQ(line, 4);
+ EXPECT_EQ(col, 9);
+}
+
+TEST(CursorStreamWrapper, MissingArrayComma) {
+ const char json[] = "{\"string\"\n\n:\"my string\",\"array\"\n:[\"1\" \"2\", \"3\"]}";
+ size_t col, line;
+ bool ret = testJson(json, line, col);
+ EXPECT_TRUE(ret);
+ EXPECT_EQ(line, 4);
+ EXPECT_EQ(col, 6);
+}
+
+TEST(CursorStreamWrapper, MissingLastArrayBracket) {
+ const char json8[] = "{\"string\"\n\n:\"my string\",\"array\"\n:[\"1\", \"2\", \"3\"}";
+ size_t col, line;
+ bool ret = testJson(json8, line, col);
+ EXPECT_TRUE(ret);
+ EXPECT_EQ(line, 4);
+ EXPECT_EQ(col, 15);
+}
+
+TEST(CursorStreamWrapper, MissingLastBracket) {
+ const char json9[] = "{\"string\"\n\n:\"my string\",\"array\"\n:[\"1\", \"2\", \"3\"]";
+ size_t col, line;
+ bool ret = testJson(json9, line, col);
+ EXPECT_TRUE(ret);
+ EXPECT_EQ(line, 4);
+ EXPECT_EQ(col, 16);
+}
diff --git a/test/unittest/documenttest.cpp b/test/unittest/documenttest.cpp
index 0c9ffaba..54298027 100644
--- a/test/unittest/documenttest.cpp
+++ b/test/unittest/documenttest.cpp
@@ -128,8 +128,14 @@ TEST(Document, UnchangedOnParseError) {
Document doc;
doc.SetArray().PushBack(0, doc.GetAllocator());
+ ParseResult noError;
+ EXPECT_TRUE(noError);
+
ParseResult err = doc.Parse("{]");
EXPECT_TRUE(doc.HasParseError());
+ EXPECT_NE(err, noError);
+ EXPECT_NE(err.Code(), noError);
+ EXPECT_NE(noError, doc.GetParseError());
EXPECT_EQ(err.Code(), doc.GetParseError());
EXPECT_EQ(err.Offset(), doc.GetErrorOffset());
EXPECT_TRUE(doc.IsArray());
@@ -138,6 +144,9 @@ TEST(Document, UnchangedOnParseError) {
err = doc.Parse("{}");
EXPECT_FALSE(doc.HasParseError());
EXPECT_FALSE(err.IsError());
+ EXPECT_TRUE(err);
+ EXPECT_EQ(err, noError);
+ EXPECT_EQ(err.Code(), noError);
EXPECT_EQ(err.Code(), doc.GetParseError());
EXPECT_EQ(err.Offset(), doc.GetErrorOffset());
EXPECT_TRUE(doc.IsObject());
@@ -291,7 +300,14 @@ TEST(Document, Swap) {
o.SetObject().AddMember("a", 1, a);
// Swap between Document and Value
- // d1.Swap(o); // doesn't compile
+ d1.Swap(o);
+ EXPECT_TRUE(d1.IsObject());
+ EXPECT_TRUE(o.IsArray());
+
+ d1.Swap(o);
+ EXPECT_TRUE(d1.IsArray());
+ EXPECT_TRUE(o.IsObject());
+
o.Swap(d1);
EXPECT_TRUE(d1.IsObject());
EXPECT_TRUE(o.IsArray());
@@ -447,10 +463,10 @@ TYPED_TEST_CASE(DocumentMove, MoveAllocatorTypes);
TYPED_TEST(DocumentMove, MoveConstructor) {
typedef TypeParam Allocator;
- typedef GenericDocument<UTF8<>, Allocator> Document;
+ typedef GenericDocument<UTF8<>, Allocator> D;
Allocator allocator;
- Document a(&allocator);
+ D a(&allocator);
a.Parse("[\"one\", \"two\", \"three\"]");
EXPECT_FALSE(a.HasParseError());
EXPECT_TRUE(a.IsArray());
@@ -458,7 +474,7 @@ TYPED_TEST(DocumentMove, MoveConstructor) {
EXPECT_EQ(&a.GetAllocator(), &allocator);
// Document b(a); // does not compile (!is_copy_constructible)
- Document b(std::move(a));
+ D b(std::move(a));
EXPECT_TRUE(a.IsNull());
EXPECT_TRUE(b.IsArray());
EXPECT_EQ(3u, b.Size());
@@ -471,7 +487,7 @@ TYPED_TEST(DocumentMove, MoveConstructor) {
EXPECT_EQ(2u, b.MemberCount());
// Document c = a; // does not compile (!is_copy_constructible)
- Document c = std::move(b);
+ D c = std::move(b);
EXPECT_TRUE(b.IsNull());
EXPECT_TRUE(c.IsObject());
EXPECT_EQ(2u, c.MemberCount());
@@ -481,25 +497,29 @@ TYPED_TEST(DocumentMove, MoveConstructor) {
TYPED_TEST(DocumentMove, MoveConstructorParseError) {
typedef TypeParam Allocator;
- typedef GenericDocument<UTF8<>, Allocator> Document;
+ typedef GenericDocument<UTF8<>, Allocator> D;
ParseResult noError;
- Document a;
+ D a;
a.Parse("{ 4 = 4]");
ParseResult error(a.GetParseError(), a.GetErrorOffset());
EXPECT_TRUE(a.HasParseError());
+ EXPECT_NE(error, noError);
+ EXPECT_NE(error.Code(), noError);
EXPECT_NE(error.Code(), noError.Code());
EXPECT_NE(error.Offset(), noError.Offset());
- Document b(std::move(a));
+ D b(std::move(a));
EXPECT_FALSE(a.HasParseError());
EXPECT_TRUE(b.HasParseError());
+ EXPECT_EQ(a.GetParseError(), noError);
EXPECT_EQ(a.GetParseError(), noError.Code());
- EXPECT_EQ(b.GetParseError(), error.Code());
EXPECT_EQ(a.GetErrorOffset(), noError.Offset());
+ EXPECT_EQ(b.GetParseError(), error);
+ EXPECT_EQ(b.GetParseError(), error.Code());
EXPECT_EQ(b.GetErrorOffset(), error.Offset());
- Document c(std::move(b));
+ D c(std::move(b));
EXPECT_FALSE(b.HasParseError());
EXPECT_TRUE(c.HasParseError());
EXPECT_EQ(b.GetParseError(), noError.Code());
@@ -540,10 +560,10 @@ TYPED_TEST(DocumentMove, MoveConstructorStack) {
TYPED_TEST(DocumentMove, MoveAssignment) {
typedef TypeParam Allocator;
- typedef GenericDocument<UTF8<>, Allocator> Document;
+ typedef GenericDocument<UTF8<>, Allocator> D;
Allocator allocator;
- Document a(&allocator);
+ D a(&allocator);
a.Parse("[\"one\", \"two\", \"three\"]");
EXPECT_FALSE(a.HasParseError());
EXPECT_TRUE(a.IsArray());
@@ -551,7 +571,7 @@ TYPED_TEST(DocumentMove, MoveAssignment) {
EXPECT_EQ(&a.GetAllocator(), &allocator);
// Document b; b = a; // does not compile (!is_copy_assignable)
- Document b;
+ D b;
b = std::move(a);
EXPECT_TRUE(a.IsNull());
EXPECT_TRUE(b.IsArray());
@@ -565,7 +585,7 @@ TYPED_TEST(DocumentMove, MoveAssignment) {
EXPECT_EQ(2u, b.MemberCount());
// Document c; c = a; // does not compile (see static_assert)
- Document c;
+ D c;
c = std::move(b);
EXPECT_TRUE(b.IsNull());
EXPECT_TRUE(c.IsObject());
@@ -576,17 +596,17 @@ TYPED_TEST(DocumentMove, MoveAssignment) {
TYPED_TEST(DocumentMove, MoveAssignmentParseError) {
typedef TypeParam Allocator;
- typedef GenericDocument<UTF8<>, Allocator> Document;
+ typedef GenericDocument<UTF8<>, Allocator> D;
ParseResult noError;
- Document a;
+ D a;
a.Parse("{ 4 = 4]");
ParseResult error(a.GetParseError(), a.GetErrorOffset());
EXPECT_TRUE(a.HasParseError());
EXPECT_NE(error.Code(), noError.Code());
EXPECT_NE(error.Offset(), noError.Offset());
- Document b;
+ D b;
b = std::move(a);
EXPECT_FALSE(a.HasParseError());
EXPECT_TRUE(b.HasParseError());
@@ -595,7 +615,7 @@ TYPED_TEST(DocumentMove, MoveAssignmentParseError) {
EXPECT_EQ(a.GetErrorOffset(), noError.Offset());
EXPECT_EQ(b.GetErrorOffset(), error.Offset());
- Document c;
+ D c;
c = std::move(b);
EXPECT_FALSE(b.HasParseError());
EXPECT_TRUE(c.HasParseError());
@@ -612,9 +632,9 @@ TYPED_TEST(DocumentMove, MoveAssignmentParseError) {
TYPED_TEST(DocumentMove, MoveAssignmentStack) {
typedef TypeParam Allocator;
typedef UTF8<> Encoding;
- typedef GenericDocument<Encoding, Allocator> Document;
+ typedef GenericDocument<Encoding, Allocator> D;
- Document a;
+ D a;
size_t defaultCapacity = a.GetStackCapacity();
// Trick Document into getting GetStackCapacity() to return non-zero
@@ -625,12 +645,12 @@ TYPED_TEST(DocumentMove, MoveAssignmentStack) {
size_t capacity = a.GetStackCapacity();
EXPECT_GT(capacity, 0u);
- Document b;
+ D b;
b = std::move(a);
EXPECT_EQ(a.GetStackCapacity(), defaultCapacity);
EXPECT_EQ(b.GetStackCapacity(), capacity);
- Document c;
+ D c;
c = std::move(b);
EXPECT_EQ(b.GetStackCapacity(), defaultCapacity);
EXPECT_EQ(c.GetStackCapacity(), capacity);
diff --git a/test/unittest/dtoatest.cpp b/test/unittest/dtoatest.cpp
index da020957..afd76eb0 100644
--- a/test/unittest/dtoatest.cpp
+++ b/test/unittest/dtoatest.cpp
@@ -37,6 +37,7 @@ TEST(dtoa, normal) {
TEST_DTOA(1.2345678, "1.2345678");
TEST_DTOA(0.123456789012, "0.123456789012");
TEST_DTOA(1234567.8, "1234567.8");
+ TEST_DTOA(-79.39773355813419, "-79.39773355813419");
TEST_DTOA(0.000001, "0.000001");
TEST_DTOA(0.0000001, "1e-7");
TEST_DTOA(1e30, "1e30");
@@ -81,6 +82,12 @@ TEST(dtoa, maxDecimalPlaces) {
TEST_DTOA(3, 2.225073858507201e-308, "0.0"); // Max subnormal positive double
TEST_DTOA(3, 2.2250738585072014e-308, "0.0"); // Min normal positive double
TEST_DTOA(3, 1.7976931348623157e308, "1.7976931348623157e308"); // Max double
+ TEST_DTOA(5, -0.14000000000000001, "-0.14");
+ TEST_DTOA(4, -0.14000000000000001, "-0.14");
+ TEST_DTOA(3, -0.14000000000000001, "-0.14");
+ TEST_DTOA(3, -0.10000000000000001, "-0.1");
+ TEST_DTOA(2, -0.10000000000000001, "-0.1");
+ TEST_DTOA(1, -0.10000000000000001, "-0.1");
#undef TEST_DTOA
}
diff --git a/test/unittest/encodingstest.cpp b/test/unittest/encodingstest.cpp
index b3cbb766..82cf7776 100644
--- a/test/unittest/encodingstest.cpp
+++ b/test/unittest/encodingstest.cpp
@@ -267,7 +267,7 @@ static unsigned inline decode(unsigned* state, unsigned* codep, unsigned byte) {
*codep = (*state != UTF8_ACCEPT) ?
(byte & 0x3fu) | (*codep << 6) :
- (0xff >> type) & (byte);
+ (0xffu >> type) & (byte);
*state = utf8d[256 + *state + type];
return *state;
@@ -302,8 +302,9 @@ TEST(EncodingsTest, UTF8) {
decodedCount++;
}
- if (*encodedStr) // This decoder cannot handle U+0000
+ if (*encodedStr) { // This decoder cannot handle U+0000
EXPECT_EQ(1u, decodedCount); // Should only contain one code point
+ }
EXPECT_EQ(UTF8_ACCEPT, state);
if (UTF8_ACCEPT != state)
@@ -423,3 +424,28 @@ TEST(EncodingsTest, UTF32) {
}
}
}
+
+TEST(EncodingsTest, ASCII) {
+ StringBuffer os, os2;
+ for (unsigned codepoint = 0; codepoint < 128; codepoint++) {
+ os.Clear();
+ ASCII<>::Encode(os, codepoint);
+ const ASCII<>::Ch* encodedStr = os.GetString();
+ {
+ StringStream is(encodedStr);
+ unsigned decodedCodepoint;
+ bool result = ASCII<>::Decode(is, &decodedCodepoint);
+ if (!result || codepoint != decodedCodepoint)
+ std::cout << std::hex << codepoint << " " << decodedCodepoint << std::endl;
+ }
+
+ // Validate
+ {
+ StringStream is(encodedStr);
+ os2.Clear();
+ bool result = ASCII<>::Validate(is, os2);
+ EXPECT_TRUE(result);
+ EXPECT_EQ(0, StrCmp(encodedStr, os2.GetString()));
+ }
+ }
+}
diff --git a/test/unittest/fwdtest.cpp b/test/unittest/fwdtest.cpp
index 4f326846..1936d977 100644
--- a/test/unittest/fwdtest.cpp
+++ b/test/unittest/fwdtest.cpp
@@ -100,6 +100,9 @@ struct Foo {
#include "rapidjson/prettywriter.h"
#include "rapidjson/schema.h" // -> pointer.h
+typedef Transcoder<UTF8<>, UTF8<> > TranscoderUtf8ToUtf8;
+typedef BaseReaderHandler<UTF8<>, void> BaseReaderHandlerUtf8Void;
+
Foo::Foo() :
// encodings.h
utf8(RAPIDJSON_NEW(UTF8<>)),
@@ -111,40 +114,40 @@ Foo::Foo() :
utf32le(RAPIDJSON_NEW(UTF32LE<>)),
ascii(RAPIDJSON_NEW(ASCII<>)),
autoutf(RAPIDJSON_NEW(AutoUTF<unsigned>)),
- transcoder(RAPIDJSON_NEW((Transcoder<UTF8<>, UTF8<> >))),
+ transcoder(RAPIDJSON_NEW(TranscoderUtf8ToUtf8)),
// allocators.h
crtallocator(RAPIDJSON_NEW(CrtAllocator)),
memorypoolallocator(RAPIDJSON_NEW(MemoryPoolAllocator<>)),
// stream.h
- stringstream(RAPIDJSON_NEW(StringStream(0))),
- insitustringstream(RAPIDJSON_NEW(InsituStringStream(0))),
+ stringstream(RAPIDJSON_NEW(StringStream)(NULL)),
+ insitustringstream(RAPIDJSON_NEW(InsituStringStream)(NULL)),
// stringbuffer.h
stringbuffer(RAPIDJSON_NEW(StringBuffer)),
// // filereadstream.h
- // filereadstream(RAPIDJSON_NEW(FileReadStream(stdout, buffer, sizeof(buffer)))),
+ // filereadstream(RAPIDJSON_NEW(FileReadStream)(stdout, buffer, sizeof(buffer))),
// // filewritestream.h
- // filewritestream(RAPIDJSON_NEW(FileWriteStream(stdout, buffer, sizeof(buffer)))),
+ // filewritestream(RAPIDJSON_NEW(FileWriteStream)(stdout, buffer, sizeof(buffer))),
// memorybuffer.h
memorybuffer(RAPIDJSON_NEW(MemoryBuffer)),
// memorystream.h
- memorystream(RAPIDJSON_NEW(MemoryStream(0, 0))),
+ memorystream(RAPIDJSON_NEW(MemoryStream)(NULL, 0)),
// reader.h
- basereaderhandler(RAPIDJSON_NEW((BaseReaderHandler<UTF8<>, void>))),
+ basereaderhandler(RAPIDJSON_NEW(BaseReaderHandlerUtf8Void)),
reader(RAPIDJSON_NEW(Reader)),
// writer.h
- writer(RAPIDJSON_NEW((Writer<StringBuffer>))),
+ writer(RAPIDJSON_NEW(Writer<StringBuffer>)),
// prettywriter.h
- prettywriter(RAPIDJSON_NEW((PrettyWriter<StringBuffer>))),
+ prettywriter(RAPIDJSON_NEW(PrettyWriter<StringBuffer>)),
// document.h
value(RAPIDJSON_NEW(Value)),
@@ -154,8 +157,8 @@ Foo::Foo() :
pointer(RAPIDJSON_NEW(Pointer)),
// schema.h
- schemadocument(RAPIDJSON_NEW(SchemaDocument(*document))),
- schemavalidator(RAPIDJSON_NEW(SchemaValidator(*schemadocument)))
+ schemadocument(RAPIDJSON_NEW(SchemaDocument)(*document)),
+ schemavalidator(RAPIDJSON_NEW(SchemaValidator)(*schemadocument))
{
}
diff --git a/test/unittest/istreamwrappertest.cpp b/test/unittest/istreamwrappertest.cpp
index f6b0fa91..9d6fbcff 100644
--- a/test/unittest/istreamwrappertest.cpp
+++ b/test/unittest/istreamwrappertest.cpp
@@ -20,6 +20,11 @@
#include <sstream>
#include <fstream>
+#ifdef _MSC_VER
+RAPIDJSON_DIAG_PUSH
+RAPIDJSON_DIAG_OFF(4702) // unreachable code
+#endif
+
using namespace rapidjson;
using namespace std;
@@ -45,8 +50,9 @@ static void TestStringStream() {
StringStreamType iss(s);
BasicIStreamWrapper<StringStreamType> is(iss);
EXPECT_EQ(0, is.Tell());
- if (sizeof(Ch) == 1)
+ if (sizeof(Ch) == 1) {
EXPECT_EQ(0, is.Peek4()); // less than 4 bytes
+ }
for (int i = 0; i < 3; i++) {
EXPECT_EQ(static_cast<size_t>(i), is.Tell());
EXPECT_EQ('A' + i, is.Peek());
@@ -168,4 +174,8 @@ TEST(IStreamWrapper, wfstream) {
EXPECT_EQ(5, d.MemberCount());
}
-#endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS
+#endif
+
+#ifdef _MSC_VER
+RAPIDJSON_DIAG_POP
+#endif
diff --git a/test/unittest/itoatest.cpp b/test/unittest/itoatest.cpp
index 9c3107d4..f41edeb7 100644
--- a/test/unittest/itoatest.cpp
+++ b/test/unittest/itoatest.cpp
@@ -70,11 +70,11 @@ template <typename T>
static void Verify(void(*f)(T, char*), char* (*g)(T, char*)) {
// Boundary cases
VerifyValue<T>(0, f, g);
- VerifyValue<T>(std::numeric_limits<T>::min(), f, g);
- VerifyValue<T>(std::numeric_limits<T>::max(), f, g);
+ VerifyValue<T>((std::numeric_limits<T>::min)(), f, g);
+ VerifyValue<T>((std::numeric_limits<T>::max)(), f, g);
// 2^n - 1, 2^n, 10^n - 1, 10^n until overflow
- for (uint32_t power = 2; power <= 10; power += 8) {
+ for (int power = 2; power <= 10; power += 8) {
T i = 1, last;
do {
VerifyValue<T>(i - 1, f, g);
@@ -84,7 +84,9 @@ static void Verify(void(*f)(T, char*), char* (*g)(T, char*)) {
VerifyValue<T>(Traits<T>::Negate(i + 1), f, g);
}
last = i;
- i *= power;
+ if (i > static_cast<T>(std::numeric_limits<T>::max() / static_cast<T>(power)))
+ break;
+ i *= static_cast<T>(power);
} while (last < i);
}
}
@@ -93,7 +95,7 @@ static void u32toa_naive(uint32_t value, char* buffer) {
char temp[10];
char *p = temp;
do {
- *p++ = char(value % 10) + '0';
+ *p++ = static_cast<char>(char(value % 10) + '0');
value /= 10;
} while (value > 0);
@@ -117,7 +119,7 @@ static void u64toa_naive(uint64_t value, char* buffer) {
char temp[20];
char *p = temp;
do {
- *p++ = char(value % 10) + '0';
+ *p++ = static_cast<char>(char(value % 10) + '0');
value /= 10;
} while (value > 0);
diff --git a/test/unittest/jsoncheckertest.cpp b/test/unittest/jsoncheckertest.cpp
index bea788d2..47c2b567 100644
--- a/test/unittest/jsoncheckertest.cpp
+++ b/test/unittest/jsoncheckertest.cpp
@@ -48,6 +48,24 @@ static char* ReadFile(const char* filename, size_t& length) {
return json;
}
+struct NoOpHandler {
+ bool Null() { return true; }
+ bool Bool(bool) { return true; }
+ bool Int(int) { return true; }
+ bool Uint(unsigned) { return true; }
+ bool Int64(int64_t) { return true; }
+ bool Uint64(uint64_t) { return true; }
+ bool Double(double) { return true; }
+ bool RawNumber(const char*, SizeType, bool) { return true; }
+ bool String(const char*, SizeType, bool) { return true; }
+ bool StartObject() { return true; }
+ bool Key(const char*, SizeType, bool) { return true; }
+ bool EndObject(SizeType) { return true; }
+ bool StartArray() { return true; }
+ bool EndArray(SizeType) { return true; }
+};
+
+
TEST(JsonChecker, Reader) {
char filename[256];
@@ -67,13 +85,26 @@ TEST(JsonChecker, Reader) {
continue;
}
+ // Test stack-based parsing.
GenericDocument<UTF8<>, CrtAllocator> document; // Use Crt allocator to check exception-safety (no memory leak)
document.Parse(json);
- EXPECT_TRUE(document.HasParseError());
+ EXPECT_TRUE(document.HasParseError()) << filename;
+ // Test iterative parsing.
document.Parse<kParseIterativeFlag>(json);
- EXPECT_TRUE(document.HasParseError());
-
+ EXPECT_TRUE(document.HasParseError()) << filename;
+
+ // Test iterative pull-parsing.
+ Reader reader;
+ StringStream ss(json);
+ NoOpHandler h;
+ reader.IterativeParseInit();
+ while (!reader.IterativeParseComplete()) {
+ if (!reader.IterativeParseNext<kParseDefaultFlags>(ss, h))
+ break;
+ }
+ EXPECT_TRUE(reader.HasParseError()) << filename;
+
free(json);
}
@@ -87,12 +118,25 @@ TEST(JsonChecker, Reader) {
continue;
}
+ // Test stack-based parsing.
GenericDocument<UTF8<>, CrtAllocator> document; // Use Crt allocator to check exception-safety (no memory leak)
document.Parse(json);
- EXPECT_FALSE(document.HasParseError());
+ EXPECT_FALSE(document.HasParseError()) << filename;
+ // Test iterative parsing.
document.Parse<kParseIterativeFlag>(json);
- EXPECT_FALSE(document.HasParseError());
+ EXPECT_FALSE(document.HasParseError()) << filename;
+
+ // Test iterative pull-parsing.
+ Reader reader;
+ StringStream ss(json);
+ NoOpHandler h;
+ reader.IterativeParseInit();
+ while (!reader.IterativeParseComplete()) {
+ if (!reader.IterativeParseNext<kParseDefaultFlags>(ss, h))
+ break;
+ }
+ EXPECT_FALSE(reader.HasParseError()) << filename;
free(json);
}
diff --git a/test/unittest/namespacetest.cpp b/test/unittest/namespacetest.cpp
index 1814724a..9f5c9afb 100644
--- a/test/unittest/namespacetest.cpp
+++ b/test/unittest/namespacetest.cpp
@@ -12,6 +12,10 @@
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.
+// Not throwing exception for this test
+#include <cassert>
+#define RAPIDJSON_ASSERT(x) assert(x)
+
#include "unittest.h"
// test another instantiation of RapidJSON in a different namespace
diff --git a/test/unittest/ostreamwrappertest.cpp b/test/unittest/ostreamwrappertest.cpp
index b1d1cd82..50f8da63 100644
--- a/test/unittest/ostreamwrappertest.cpp
+++ b/test/unittest/ostreamwrappertest.cpp
@@ -69,14 +69,15 @@ static void TestFileStream() {
const char* s = "Hello World!\n";
{
- ofstream ofs(filename, ios::out | ios::binary);
- BasicOStreamWrapper<ofstream> osw(ofs);
+ FileStreamType ofs(filename, ios::out | ios::binary);
+ BasicOStreamWrapper<FileStreamType> osw(ofs);
for (const char* p = s; *p; p++)
osw.Put(*p);
osw.Flush();
}
fp = fopen(filename, "r");
+ ASSERT_TRUE( fp != NULL );
for (const char* p = s; *p; p++)
EXPECT_EQ(*p, static_cast<char>(fgetc(fp)));
fclose(fp);
diff --git a/test/unittest/pointertest.cpp b/test/unittest/pointertest.cpp
index dbddbede..d5a688db 100644
--- a/test/unittest/pointertest.cpp
+++ b/test/unittest/pointertest.cpp
@@ -441,8 +441,8 @@ TEST(Pointer, Stringify) {
}
// Construct a Pointer with static tokens, no dynamic allocation involved.
-#define NAME(s) { s, sizeof(s) / sizeof(s[0]) - 1, kPointerInvalidIndex }
-#define INDEX(i) { #i, sizeof(#i) - 1, i }
+#define NAME(s) { s, static_cast<SizeType>(sizeof(s) / sizeof(s[0]) - 1), kPointerInvalidIndex }
+#define INDEX(i) { #i, static_cast<SizeType>(sizeof(#i) - 1), i }
static const Pointer::Token kTokens[] = { NAME("foo"), INDEX(0) }; // equivalent to "/foo/0"
@@ -1488,7 +1488,7 @@ TEST(Pointer, Ambiguity) {
}
}
-// https://github.com/miloyip/rapidjson/issues/483
+// https://github.com/Tencent/rapidjson/issues/483
namespace myjson {
class MyAllocator
diff --git a/test/unittest/prettywritertest.cpp b/test/unittest/prettywritertest.cpp
index e05d710f..43617a2f 100644
--- a/test/unittest/prettywritertest.cpp
+++ b/test/unittest/prettywritertest.cpp
@@ -18,6 +18,11 @@
#include "rapidjson/stringbuffer.h"
#include "rapidjson/filewritestream.h"
+#ifdef __clang__
+RAPIDJSON_DIAG_PUSH
+RAPIDJSON_DIAG_OFF(c++98-compat)
+#endif
+
using namespace rapidjson;
static const char kJson[] = "{\"hello\":\"world\",\"t\":true,\"f\":false,\"n\":null,\"i\":123,\"pi\":3.1416,\"a\":[1,2,3,-1],\"u64\":1234567890123456789,\"i64\":-1234567890123456789}";
@@ -39,6 +44,19 @@ static const char kPrettyJson[] =
" \"i64\": -1234567890123456789\n"
"}";
+static const char kPrettyJson_FormatOptions_SLA[] =
+"{\n"
+" \"hello\": \"world\",\n"
+" \"t\": true,\n"
+" \"f\": false,\n"
+" \"n\": null,\n"
+" \"i\": 123,\n"
+" \"pi\": 3.1416,\n"
+" \"a\": [1, 2, 3, -1],\n"
+" \"u64\": 1234567890123456789,\n"
+" \"i64\": -1234567890123456789\n"
+"}";
+
TEST(PrettyWriter, Basic) {
StringBuffer buffer;
PrettyWriter<StringBuffer> writer(buffer);
@@ -48,6 +66,16 @@ TEST(PrettyWriter, Basic) {
EXPECT_STREQ(kPrettyJson, buffer.GetString());
}
+TEST(PrettyWriter, FormatOptions) {
+ StringBuffer buffer;
+ PrettyWriter<StringBuffer> writer(buffer);
+ writer.SetFormatOptions(kFormatSingleLineArray);
+ Reader reader;
+ StringStream s(kJson);
+ reader.Parse(s, writer);
+ EXPECT_STREQ(kPrettyJson_FormatOptions_SLA, buffer.GetString());
+}
+
TEST(PrettyWriter, SetIndent) {
StringBuffer buffer;
PrettyWriter<StringBuffer> writer(buffer);
@@ -139,6 +167,7 @@ TEST(PrettyWriter, OStreamWrapper) {
TEST(PrettyWriter, FileWriteStream) {
char filename[L_tmpnam];
FILE* fp = TempFile(filename);
+ ASSERT_TRUE(fp!=NULL);
char buffer[16];
FileWriteStream os(fp, buffer, sizeof(buffer));
PrettyWriter<FileWriteStream> writer(os);
@@ -178,3 +207,138 @@ TEST(PrettyWriter, RawValue) {
"}",
buffer.GetString());
}
+
+TEST(PrettyWriter, InvalidEventSequence) {
+ // {]
+ {
+ StringBuffer buffer;
+ PrettyWriter<StringBuffer> writer(buffer);
+ writer.StartObject();
+ EXPECT_THROW(writer.EndArray(), AssertException);
+ EXPECT_FALSE(writer.IsComplete());
+ }
+
+ // [}
+ {
+ StringBuffer buffer;
+ PrettyWriter<StringBuffer> writer(buffer);
+ writer.StartArray();
+ EXPECT_THROW(writer.EndObject(), AssertException);
+ EXPECT_FALSE(writer.IsComplete());
+ }
+
+ // { 1:
+ {
+ StringBuffer buffer;
+ PrettyWriter<StringBuffer> writer(buffer);
+ writer.StartObject();
+ EXPECT_THROW(writer.Int(1), AssertException);
+ EXPECT_FALSE(writer.IsComplete());
+ }
+
+ // { 'a' }
+ {
+ StringBuffer buffer;
+ PrettyWriter<StringBuffer> writer(buffer);
+ writer.StartObject();
+ writer.Key("a");
+ EXPECT_THROW(writer.EndObject(), AssertException);
+ EXPECT_FALSE(writer.IsComplete());
+ }
+
+ // { 'a':'b','c' }
+ {
+ StringBuffer buffer;
+ PrettyWriter<StringBuffer> writer(buffer);
+ writer.StartObject();
+ writer.Key("a");
+ writer.String("b");
+ writer.Key("c");
+ EXPECT_THROW(writer.EndObject(), AssertException);
+ EXPECT_FALSE(writer.IsComplete());
+ }
+}
+
+TEST(PrettyWriter, NaN) {
+ double nan = std::numeric_limits<double>::quiet_NaN();
+
+ EXPECT_TRUE(internal::Double(nan).IsNan());
+ StringBuffer buffer;
+ {
+ PrettyWriter<StringBuffer> writer(buffer);
+ EXPECT_FALSE(writer.Double(nan));
+ }
+ {
+ PrettyWriter<StringBuffer, UTF8<>, UTF8<>, CrtAllocator, kWriteNanAndInfFlag> writer(buffer);
+ EXPECT_TRUE(writer.Double(nan));
+ EXPECT_STREQ("NaN", buffer.GetString());
+ }
+ GenericStringBuffer<UTF16<> > buffer2;
+ PrettyWriter<GenericStringBuffer<UTF16<> > > writer2(buffer2);
+ EXPECT_FALSE(writer2.Double(nan));
+}
+
+TEST(PrettyWriter, Inf) {
+ double inf = std::numeric_limits<double>::infinity();
+
+ EXPECT_TRUE(internal::Double(inf).IsInf());
+ StringBuffer buffer;
+ {
+ PrettyWriter<StringBuffer> writer(buffer);
+ EXPECT_FALSE(writer.Double(inf));
+ }
+ {
+ PrettyWriter<StringBuffer> writer(buffer);
+ EXPECT_FALSE(writer.Double(-inf));
+ }
+ {
+ PrettyWriter<StringBuffer, UTF8<>, UTF8<>, CrtAllocator, kWriteNanAndInfFlag> writer(buffer);
+ EXPECT_TRUE(writer.Double(inf));
+ }
+ {
+ PrettyWriter<StringBuffer, UTF8<>, UTF8<>, CrtAllocator, kWriteNanAndInfFlag> writer(buffer);
+ EXPECT_TRUE(writer.Double(-inf));
+ }
+ EXPECT_STREQ("Infinity-Infinity", buffer.GetString());
+}
+
+TEST(PrettyWriter, Issue_889) {
+ char buf[100] = "Hello";
+
+ StringBuffer buffer;
+ PrettyWriter<StringBuffer> writer(buffer);
+ writer.StartArray();
+ writer.String(buf);
+ writer.EndArray();
+
+ EXPECT_STREQ("[\n \"Hello\"\n]", buffer.GetString());
+ EXPECT_TRUE(writer.IsComplete()); \
+}
+
+
+#if RAPIDJSON_HAS_CXX11_RVALUE_REFS
+
+static PrettyWriter<StringBuffer> WriterGen(StringBuffer &target) {
+ PrettyWriter<StringBuffer> writer(target);
+ writer.StartObject();
+ writer.Key("a");
+ writer.Int(1);
+ return writer;
+}
+
+TEST(PrettyWriter, MoveCtor) {
+ StringBuffer buffer;
+ PrettyWriter<StringBuffer> writer(WriterGen(buffer));
+ writer.EndObject();
+ EXPECT_TRUE(writer.IsComplete());
+ EXPECT_STREQ(
+ "{\n"
+ " \"a\": 1\n"
+ "}",
+ buffer.GetString());
+}
+#endif
+
+#ifdef __clang__
+RAPIDJSON_DIAG_POP
+#endif
diff --git a/test/unittest/readertest.cpp b/test/unittest/readertest.cpp
index 32af8a86..e5308019 100644
--- a/test/unittest/readertest.cpp
+++ b/test/unittest/readertest.cpp
@@ -1,5 +1,5 @@
// Tencent is pleased to support the open source community by making RapidJSON available.
-//
+//
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
//
// Licensed under the MIT License (the "License"); you may not use this file except
@@ -7,9 +7,9 @@
//
// http://opensource.org/licenses/MIT
//
-// Unless required by applicable law or agreed to in writing, software distributed
-// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
-// CONDITIONS OF ANY KIND, either express or implied. See the License for the
+// Unless required by applicable law or agreed to in writing, software distributed
+// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
+// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.
#include "unittest.h"
@@ -19,17 +19,21 @@
#include "rapidjson/internal/itoa.h"
#include "rapidjson/memorystream.h"
+#include <limits>
+
using namespace rapidjson;
-#ifdef __GNUC__
RAPIDJSON_DIAG_PUSH
+#ifdef __GNUC__
RAPIDJSON_DIAG_OFF(effc++)
RAPIDJSON_DIAG_OFF(float-equal)
RAPIDJSON_DIAG_OFF(missing-noreturn)
+#if __GNUC__ >= 7
+RAPIDJSON_DIAG_OFF(dangling-else)
#endif
+#endif // __GNUC__
#ifdef __clang__
-RAPIDJSON_DIAG_PUSH
RAPIDJSON_DIAG_OFF(variadic-macros)
RAPIDJSON_DIAG_OFF(c++98-compat-pedantic)
#endif
@@ -229,7 +233,7 @@ static void TestParseDouble() {
TEST_DOUBLE(fullPrecision, "1e-10000", 0.0); // must underflow
TEST_DOUBLE(fullPrecision, "18446744073709551616", 18446744073709551616.0); // 2^64 (max of uint64_t + 1, force to use double)
TEST_DOUBLE(fullPrecision, "-9223372036854775809", -9223372036854775809.0); // -2^63 - 1(min of int64_t + 1, force to use double)
- TEST_DOUBLE(fullPrecision, "0.9868011474609375", 0.9868011474609375); // https://github.com/miloyip/rapidjson/issues/120
+ TEST_DOUBLE(fullPrecision, "0.9868011474609375", 0.9868011474609375); // https://github.com/Tencent/rapidjson/issues/120
TEST_DOUBLE(fullPrecision, "123e34", 123e34); // Fast Path Cases In Disguise
TEST_DOUBLE(fullPrecision, "45913141877270640000.0", 45913141877270640000.0);
TEST_DOUBLE(fullPrecision, "2.2250738585072011e-308", 2.2250738585072011e-308); // http://www.exploringbinary.com/php-hangs-on-numeric-value-2-2250738585072011e-308/
@@ -241,13 +245,13 @@ static void TestParseDouble() {
TEST_DOUBLE(fullPrecision, "0.017976931348623157e+310", 1.7976931348623157e+308); // Max double in another form
// Since
- // abs((2^-1022 - 2^-1074) - 2.2250738585072012e-308) = 3.109754131239141401123495768877590405345064751974375599... ¡Á 10^-324
- // abs((2^-1022) - 2.2250738585072012e-308) = 1.830902327173324040642192159804623318305533274168872044... ¡Á 10 ^ -324
+ // abs((2^-1022 - 2^-1074) - 2.2250738585072012e-308) = 3.109754131239141401123495768877590405345064751974375599... x 10^-324
+ // abs((2^-1022) - 2.2250738585072012e-308) = 1.830902327173324040642192159804623318305533274168872044... x 10 ^ -324
// So 2.2250738585072012e-308 should round to 2^-1022 = 2.2250738585072014e-308
TEST_DOUBLE(fullPrecision, "2.2250738585072012e-308", 2.2250738585072014e-308); // http://www.exploringbinary.com/java-hangs-when-converting-2-2250738585072012e-308/
// More closer to normal/subnormal boundary
- // boundary = 2^-1022 - 2^-1075 = 2.225073858507201136057409796709131975934819546351645648... ¡Á 10^-308
+ // boundary = 2^-1022 - 2^-1075 = 2.225073858507201136057409796709131975934819546351645648... x 10^-308
TEST_DOUBLE(fullPrecision, "2.22507385850720113605740979670913197593481954635164564e-308", 2.2250738585072009e-308);
TEST_DOUBLE(fullPrecision, "2.22507385850720113605740979670913197593481954635164565e-308", 2.2250738585072014e-308);
@@ -297,7 +301,7 @@ static void TestParseDouble() {
}
// Cover trimming
- TEST_DOUBLE(fullPrecision,
+ TEST_DOUBLE(fullPrecision,
"2.22507385850720113605740979670913197593481954635164564802342610972482222202107694551652952390813508"
"7914149158913039621106870086438694594645527657207407820621743379988141063267329253552286881372149012"
"9811224514518898490572223072852551331557550159143974763979834118019993239625482890171070818506906306"
@@ -306,7 +310,7 @@ static void TestParseDouble() {
"5722898802581825451803257070188608721131280795122334262883686223215037756666225039825343359745688844"
"2390026549819838548794829220689472168983109969836584681402285424333066033985088644580400103493397042"
"7567186443383770486037861622771738545623065874679014086723327636718751234567890123456789012345678901"
-"e-308",
+"e-308",
2.2250738585072014e-308);
{
@@ -411,7 +415,7 @@ TEST(Reader, ParseNumber_NormalPrecisionError) {
uint64_t bias1 = e.ToBias();
uint64_t bias2 = a.ToBias();
double ulp = static_cast<double>(bias1 >= bias2 ? bias1 - bias2 : bias2 - bias1);
- ulpMax = std::max(ulpMax, ulp);
+ ulpMax = (std::max)(ulpMax, ulp);
ulpSum += ulp;
}
printf("ULP Average = %g, Max = %g \n", ulpSum / count, ulpMax);
@@ -457,12 +461,12 @@ template <typename Encoding>
struct ParseStringHandler : BaseReaderHandler<Encoding, ParseStringHandler<Encoding> > {
ParseStringHandler() : str_(0), length_(0), copy_() {}
~ParseStringHandler() { EXPECT_TRUE(str_ != 0); if (copy_) free(const_cast<typename Encoding::Ch*>(str_)); }
-
+
ParseStringHandler(const ParseStringHandler&);
ParseStringHandler& operator=(const ParseStringHandler&);
bool Default() { ADD_FAILURE(); return false; }
- bool String(const typename Encoding::Ch* str, size_t length, bool copy) {
+ bool String(const typename Encoding::Ch* str, size_t length, bool copy) {
EXPECT_EQ(0, str_);
if (copy) {
str_ = static_cast<typename Encoding::Ch*>(malloc((length + 1) * sizeof(typename Encoding::Ch)));
@@ -470,7 +474,7 @@ struct ParseStringHandler : BaseReaderHandler<Encoding, ParseStringHandler<Encod
}
else
str_ = str;
- length_ = length;
+ length_ = length;
copy_ = copy;
return true;
}
@@ -499,7 +503,7 @@ TEST(Reader, ParseString) {
EXPECT_EQ(StrLen(e), h2.length_); \
}
- // String constant L"\xXX" can only specify character code in bytes, which is not endianness-neutral.
+ // String constant L"\xXX" can only specify character code in bytes, which is not endianness-neutral.
// And old compiler does not support u"" and U"" string literal. So here specify string literal by array of Ch.
// In addition, GCC 4.8 generates -Wnarrowing warnings when character code >= 128 are assigned to signed integer types.
// Therefore, utype is added for declaring unsigned array, and then cast it to Encoding::Ch.
@@ -650,7 +654,7 @@ TEST(Reader, ParseString_Error) {
// http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt
- // 3 Malformed sequences
+ // 3 Malformed sequences
// 3.1 Unexpected continuation bytes
{
@@ -684,19 +688,19 @@ TEST(Reader, ParseString_Error) {
}
}
- // 4 Overlong sequences
+ // 4 Overlong sequences
// 4.1 Examples of an overlong ASCII character
TEST_STRINGENCODING_ERROR(UTF8<>, UTF16<>, unsigned char, ARRAY('[', '\"', 0xC0u, 0xAFu, '\"', ']', '\0'));
TEST_STRINGENCODING_ERROR(UTF8<>, UTF16<>, unsigned char, ARRAY('[', '\"', 0xE0u, 0x80u, 0xAFu, '\"', ']', '\0'));
TEST_STRINGENCODING_ERROR(UTF8<>, UTF16<>, unsigned char, ARRAY('[', '\"', 0xF0u, 0x80u, 0x80u, 0xAFu, '\"', ']', '\0'));
- // 4.2 Maximum overlong sequences
+ // 4.2 Maximum overlong sequences
TEST_STRINGENCODING_ERROR(UTF8<>, UTF16<>, unsigned char, ARRAY('[', '\"', 0xC1u, 0xBFu, '\"', ']', '\0'));
TEST_STRINGENCODING_ERROR(UTF8<>, UTF16<>, unsigned char, ARRAY('[', '\"', 0xE0u, 0x9Fu, 0xBFu, '\"', ']', '\0'));
TEST_STRINGENCODING_ERROR(UTF8<>, UTF16<>, unsigned char, ARRAY('[', '\"', 0xF0u, 0x8Fu, 0xBFu, 0xBFu, '\"', ']', '\0'));
- // 4.3 Overlong representation of the NUL character
+ // 4.3 Overlong representation of the NUL character
TEST_STRINGENCODING_ERROR(UTF8<>, UTF16<>, unsigned char, ARRAY('[', '\"', 0xC0u, 0x80u, '\"', ']', '\0'));
TEST_STRINGENCODING_ERROR(UTF8<>, UTF16<>, unsigned char, ARRAY('[', '\"', 0xE0u, 0x80u, 0x80u, '\"', ']', '\0'));
TEST_STRINGENCODING_ERROR(UTF8<>, UTF16<>, unsigned char, ARRAY('[', '\"', 0xF0u, 0x80u, 0x80u, 0x80u, '\"', ']', '\0'));
@@ -720,7 +724,9 @@ TEST(Reader, ParseString_Error) {
TEST_STRINGENCODING_ERROR(UTF32<>, UTF8<>, unsigned, ARRAY('[', '\"', 0x110000, '\"', ']', '\0'));
// Malform ASCII sequence
- TEST_STRINGENCODING_ERROR(ASCII<>, UTF8<>, char, ARRAY('[', '\"', char(0x80), '\"', ']', '\0'));
+ TEST_STRINGENCODING_ERROR(ASCII<>, UTF8<>, char, ARRAY('[', '\"', char(0x80u), '\"', ']', '\0'));
+ TEST_STRINGENCODING_ERROR(ASCII<>, UTF8<>, char, ARRAY('[', '\"', char(0x01u), '\"', ']', '\0'));
+ TEST_STRINGENCODING_ERROR(ASCII<>, UTF8<>, char, ARRAY('[', '\"', char(0x1Cu), '\"', ']', '\0'));
#undef ARRAY
#undef TEST_STRINGARRAY_ERROR
@@ -778,6 +784,10 @@ TEST(Reader, ParseArray_Error) {
TEST_ARRAY_ERROR(kParseErrorArrayMissCommaOrSquareBracket, "[1}", 2);
TEST_ARRAY_ERROR(kParseErrorArrayMissCommaOrSquareBracket, "[1 2]", 3);
+ // Array cannot have a trailing comma (without kParseTrailingCommasFlag);
+ // a value must follow a comma
+ TEST_ARRAY_ERROR(kParseErrorValueInvalid, "[1,]", 3);
+
#undef TEST_ARRAY_ERROR
}
@@ -786,14 +796,14 @@ struct ParseObjectHandler : BaseReaderHandler<UTF8<>, ParseObjectHandler> {
bool Default() { ADD_FAILURE(); return false; }
bool Null() { EXPECT_EQ(8u, step_); step_++; return true; }
- bool Bool(bool b) {
+ bool Bool(bool b) {
switch(step_) {
case 4: EXPECT_TRUE(b); step_++; return true;
case 6: EXPECT_FALSE(b); step_++; return true;
default: ADD_FAILURE(); return false;
}
}
- bool Int(int i) {
+ bool Int(int i) {
switch(step_) {
case 10: EXPECT_EQ(123, i); step_++; return true;
case 15: EXPECT_EQ(1, i); step_++; return true;
@@ -804,7 +814,7 @@ struct ParseObjectHandler : BaseReaderHandler<UTF8<>, ParseObjectHandler> {
}
bool Uint(unsigned i) { return Int(static_cast<int>(i)); }
bool Double(double d) { EXPECT_EQ(12u, step_); EXPECT_DOUBLE_EQ(3.1416, d); step_++; return true; }
- bool String(const char* str, size_t, bool) {
+ bool String(const char* str, size_t, bool) {
switch(step_) {
case 1: EXPECT_STREQ("hello", str); step_++; return true;
case 2: EXPECT_STREQ("world", str); step_++; return true;
@@ -978,6 +988,10 @@ TEST(Reader, ParseObject_Error) {
// Must be a comma or '}' after an object member
TEST_ERROR(kParseErrorObjectMissCommaOrCurlyBracket, "{\"a\":1]", 6);
+ // Object cannot have a trailing comma (without kParseTrailingCommasFlag);
+ // an object member name must follow a comma
+ TEST_ERROR(kParseErrorObjectMissName, "{\"a\":1,}", 7);
+
// This tests that MemoryStream is checking the length in Peek().
{
MemoryStream ms("{\"a\"", 1);
@@ -1037,7 +1051,7 @@ struct StreamTraits<CustomStringStream<Encoding> > {
};
} // namespace rapidjson
-#endif
+#endif
TEST(Reader, CustomStringStream) {
const char* json = "{ \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3] } ";
@@ -1061,7 +1075,7 @@ public:
return c == std::char_traits<char>::eof() ? '\0' : static_cast<Ch>(c);
}
- Ch Take() {
+ Ch Take() {
int c = is_.get();
return c == std::char_traits<char>::eof() ? '\0' : static_cast<Ch>(c);
}
@@ -1089,7 +1103,7 @@ TEST(Reader, Parse_IStreamWrapper_StringStream) {
Reader reader;
ParseArrayHandler<4> h;
reader.Parse(is, h);
- EXPECT_FALSE(reader.HasParseError());
+ EXPECT_FALSE(reader.HasParseError());
}
// Test iterative parsing.
@@ -1119,6 +1133,16 @@ TEST(Reader, IterativeParsing_ErrorHandling) {
TESTERRORHANDLING("{\"a\": 1", kParseErrorObjectMissCommaOrCurlyBracket, 7u);
TESTERRORHANDLING("[1 2 3]", kParseErrorArrayMissCommaOrSquareBracket, 3u);
TESTERRORHANDLING("{\"a: 1", kParseErrorStringMissQuotationMark, 6u);
+ TESTERRORHANDLING("{\"a\":}", kParseErrorValueInvalid, 5u);
+ TESTERRORHANDLING("{\"a\":]", kParseErrorValueInvalid, 5u);
+ TESTERRORHANDLING("[1,2,}", kParseErrorValueInvalid, 5u);
+ TESTERRORHANDLING("[}]", kParseErrorValueInvalid, 1u);
+ TESTERRORHANDLING("[,]", kParseErrorValueInvalid, 1u);
+ TESTERRORHANDLING("[1,,]", kParseErrorValueInvalid, 3u);
+
+ // Trailing commas are not allowed without kParseTrailingCommasFlag
+ TESTERRORHANDLING("{\"a\": 1,}", kParseErrorObjectMissName, 8u);
+ TESTERRORHANDLING("[1,2,3,]", kParseErrorValueInvalid, 7u);
// Any JSON value can be a valid root element in RFC7159.
TESTERRORHANDLING("\"ab", kParseErrorStringMissQuotationMark, 3u);
@@ -1135,22 +1159,22 @@ template<typename Encoding = UTF8<> >
struct IterativeParsingReaderHandler {
typedef typename Encoding::Ch Ch;
- const static int LOG_NULL = -1;
- const static int LOG_BOOL = -2;
- const static int LOG_INT = -3;
- const static int LOG_UINT = -4;
- const static int LOG_INT64 = -5;
- const static int LOG_UINT64 = -6;
- const static int LOG_DOUBLE = -7;
- const static int LOG_STRING = -8;
- const static int LOG_STARTOBJECT = -9;
- const static int LOG_KEY = -10;
- const static int LOG_ENDOBJECT = -11;
- const static int LOG_STARTARRAY = -12;
- const static int LOG_ENDARRAY = -13;
+ const static uint32_t LOG_NULL = 0x10000000;
+ const static uint32_t LOG_BOOL = 0x20000000;
+ const static uint32_t LOG_INT = 0x30000000;
+ const static uint32_t LOG_UINT = 0x40000000;
+ const static uint32_t LOG_INT64 = 0x50000000;
+ const static uint32_t LOG_UINT64 = 0x60000000;
+ const static uint32_t LOG_DOUBLE = 0x70000000;
+ const static uint32_t LOG_STRING = 0x80000000;
+ const static uint32_t LOG_STARTOBJECT = 0x90000000;
+ const static uint32_t LOG_KEY = 0xA0000000;
+ const static uint32_t LOG_ENDOBJECT = 0xB0000000;
+ const static uint32_t LOG_STARTARRAY = 0xC0000000;
+ const static uint32_t LOG_ENDARRAY = 0xD0000000;
const static size_t LogCapacity = 256;
- int Logs[LogCapacity];
+ uint32_t Logs[LogCapacity];
size_t LogCount;
IterativeParsingReaderHandler() : LogCount(0) {
@@ -1177,11 +1201,11 @@ struct IterativeParsingReaderHandler {
bool StartObject() { RAPIDJSON_ASSERT(LogCount < LogCapacity); Logs[LogCount++] = LOG_STARTOBJECT; return true; }
bool Key (const Ch*, SizeType, bool) { RAPIDJSON_ASSERT(LogCount < LogCapacity); Logs[LogCount++] = LOG_KEY; return true; }
-
+
bool EndObject(SizeType c) {
RAPIDJSON_ASSERT(LogCount < LogCapacity);
- Logs[LogCount++] = LOG_ENDOBJECT;
- Logs[LogCount++] = static_cast<int>(c);
+ RAPIDJSON_ASSERT((static_cast<uint32_t>(c) & 0xF0000000) == 0);
+ Logs[LogCount++] = LOG_ENDOBJECT | static_cast<uint32_t>(c);
return true;
}
@@ -1189,8 +1213,8 @@ struct IterativeParsingReaderHandler {
bool EndArray(SizeType c) {
RAPIDJSON_ASSERT(LogCount < LogCapacity);
- Logs[LogCount++] = LOG_ENDARRAY;
- Logs[LogCount++] = static_cast<int>(c);
+ RAPIDJSON_ASSERT((static_cast<uint32_t>(c) & 0xF0000000) == 0);
+ Logs[LogCount++] = LOG_ENDARRAY | static_cast<uint32_t>(c);
return true;
}
};
@@ -1206,7 +1230,7 @@ TEST(Reader, IterativeParsing_General) {
EXPECT_FALSE(r.IsError());
EXPECT_FALSE(reader.HasParseError());
- int e[] = {
+ uint32_t e[] = {
handler.LOG_STARTARRAY,
handler.LOG_INT,
handler.LOG_STARTOBJECT,
@@ -1214,14 +1238,14 @@ TEST(Reader, IterativeParsing_General) {
handler.LOG_STARTARRAY,
handler.LOG_INT,
handler.LOG_INT,
- handler.LOG_ENDARRAY, 2,
- handler.LOG_ENDOBJECT, 1,
+ handler.LOG_ENDARRAY | 2,
+ handler.LOG_ENDOBJECT | 1,
handler.LOG_NULL,
handler.LOG_BOOL,
handler.LOG_BOOL,
handler.LOG_STRING,
handler.LOG_DOUBLE,
- handler.LOG_ENDARRAY, 7
+ handler.LOG_ENDARRAY | 7
};
EXPECT_EQ(sizeof(e) / sizeof(int), handler.LogCount);
@@ -1243,20 +1267,20 @@ TEST(Reader, IterativeParsing_Count) {
EXPECT_FALSE(r.IsError());
EXPECT_FALSE(reader.HasParseError());
- int e[] = {
+ uint32_t e[] = {
handler.LOG_STARTARRAY,
handler.LOG_STARTOBJECT,
- handler.LOG_ENDOBJECT, 0,
+ handler.LOG_ENDOBJECT | 0,
handler.LOG_STARTOBJECT,
handler.LOG_KEY,
handler.LOG_INT,
- handler.LOG_ENDOBJECT, 1,
+ handler.LOG_ENDOBJECT | 1,
handler.LOG_STARTARRAY,
handler.LOG_INT,
- handler.LOG_ENDARRAY, 1,
+ handler.LOG_ENDARRAY | 1,
handler.LOG_STARTARRAY,
- handler.LOG_ENDARRAY, 0,
- handler.LOG_ENDARRAY, 4
+ handler.LOG_ENDARRAY | 0,
+ handler.LOG_ENDARRAY | 4
};
EXPECT_EQ(sizeof(e) / sizeof(int), handler.LogCount);
@@ -1267,6 +1291,51 @@ TEST(Reader, IterativeParsing_Count) {
}
}
+TEST(Reader, IterativePullParsing_General) {
+ {
+ IterativeParsingReaderHandler<> handler;
+ uint32_t e[] = {
+ handler.LOG_STARTARRAY,
+ handler.LOG_INT,
+ handler.LOG_STARTOBJECT,
+ handler.LOG_KEY,
+ handler.LOG_STARTARRAY,
+ handler.LOG_INT,
+ handler.LOG_INT,
+ handler.LOG_ENDARRAY | 2,
+ handler.LOG_ENDOBJECT | 1,
+ handler.LOG_NULL,
+ handler.LOG_BOOL,
+ handler.LOG_BOOL,
+ handler.LOG_STRING,
+ handler.LOG_DOUBLE,
+ handler.LOG_ENDARRAY | 7
+ };
+
+ StringStream is("[1, {\"k\": [1, 2]}, null, false, true, \"string\", 1.2]");
+ Reader reader;
+
+ reader.IterativeParseInit();
+ while (!reader.IterativeParseComplete()) {
+ size_t oldLogCount = handler.LogCount;
+ EXPECT_TRUE(oldLogCount < sizeof(e) / sizeof(int)) << "overrun";
+
+ EXPECT_TRUE(reader.IterativeParseNext<kParseDefaultFlags>(is, handler)) << "parse fail";
+ EXPECT_EQ(handler.LogCount, oldLogCount + 1) << "handler should be invoked exactly once each time";
+ EXPECT_EQ(e[oldLogCount], handler.Logs[oldLogCount]) << "wrong event returned";
+ }
+
+ EXPECT_FALSE(reader.HasParseError());
+ EXPECT_EQ(sizeof(e) / sizeof(int), handler.LogCount) << "handler invoked wrong number of times";
+
+ // The handler should not be invoked when the JSON has been fully read, but it should not fail
+ size_t oldLogCount = handler.LogCount;
+ EXPECT_TRUE(reader.IterativeParseNext<kParseDefaultFlags>(is, handler)) << "parse-next past complete is allowed";
+ EXPECT_EQ(handler.LogCount, oldLogCount) << "parse-next past complete should not invoke handler";
+ EXPECT_FALSE(reader.HasParseError()) << "parse-next past complete should not generate parse error";
+ }
+}
+
// Test iterative parsing on kParseErrorTermination.
struct HandlerTerminateAtStartObject : public IterativeParsingReaderHandler<> {
bool StartObject() { return false; }
@@ -1428,7 +1497,7 @@ TEST(Reader, ParseEmptyOnelineComment) {
}
TEST(Reader, ParseMultipleCommentsInARow) {
- const char* json =
+ const char* json =
"{/* first comment *//* second */\n"
"/* third */ /*fourth*/// last one\n"
"\"hello\" : \"world\", \"t\" : true, \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3] }";
@@ -1523,7 +1592,8 @@ struct NumbersAsStringsHandler {
// 'str' is not null-terminated
bool RawNumber(const char* str, SizeType length, bool) {
EXPECT_TRUE(str != 0);
- EXPECT_TRUE(strncmp(str, "3.1416", length) == 0);
+ EXPECT_TRUE(expected_len_ == length);
+ EXPECT_TRUE(strncmp(str, expected_, length) == 0);
return true;
}
bool String(const char*, SizeType, bool) { return true; }
@@ -1532,30 +1602,294 @@ struct NumbersAsStringsHandler {
bool EndObject(SizeType) { return true; }
bool StartArray() { return true; }
bool EndArray(SizeType) { return true; }
+
+ NumbersAsStringsHandler(const char* expected)
+ : expected_(expected)
+ , expected_len_(strlen(expected)) {}
+
+ const char* expected_;
+ size_t expected_len_;
};
TEST(Reader, NumbersAsStrings) {
- {
- const char* json = "{ \"pi\": 3.1416 } ";
- StringStream s(json);
- NumbersAsStringsHandler h;
- Reader reader;
- EXPECT_TRUE(reader.Parse<kParseNumbersAsStringsFlag>(s, h));
- }
- {
- char* json = StrDup("{ \"pi\": 3.1416 } ");
- InsituStringStream s(json);
- NumbersAsStringsHandler h;
- Reader reader;
- EXPECT_TRUE(reader.Parse<kParseInsituFlag|kParseNumbersAsStringsFlag>(s, h));
- free(json);
- }
+ {
+ const char* json = "{ \"pi\": 3.1416 } ";
+ StringStream s(json);
+ NumbersAsStringsHandler h("3.1416");
+ Reader reader;
+ EXPECT_TRUE(reader.Parse<kParseNumbersAsStringsFlag>(s, h));
+ }
+ {
+ char* json = StrDup("{ \"pi\": 3.1416 } ");
+ InsituStringStream s(json);
+ NumbersAsStringsHandler h("3.1416");
+ Reader reader;
+ EXPECT_TRUE(reader.Parse<kParseInsituFlag|kParseNumbersAsStringsFlag>(s, h));
+ free(json);
+ }
+ {
+ const char* json = "{ \"gigabyte\": 1.0e9 } ";
+ StringStream s(json);
+ NumbersAsStringsHandler h("1.0e9");
+ Reader reader;
+ EXPECT_TRUE(reader.Parse<kParseNumbersAsStringsFlag>(s, h));
+ }
+ {
+ char* json = StrDup("{ \"gigabyte\": 1.0e9 } ");
+ InsituStringStream s(json);
+ NumbersAsStringsHandler h("1.0e9");
+ Reader reader;
+ EXPECT_TRUE(reader.Parse<kParseInsituFlag|kParseNumbersAsStringsFlag>(s, h));
+ free(json);
+ }
+ {
+ const char* json = "{ \"pi\": 314.159e-2 } ";
+ StringStream s(json);
+ NumbersAsStringsHandler h("314.159e-2");
+ Reader reader;
+ EXPECT_TRUE(reader.Parse<kParseNumbersAsStringsFlag>(s, h));
+ }
+ {
+ char* json = StrDup("{ \"gigabyte\": 314.159e-2 } ");
+ InsituStringStream s(json);
+ NumbersAsStringsHandler h("314.159e-2");
+ Reader reader;
+ EXPECT_TRUE(reader.Parse<kParseInsituFlag|kParseNumbersAsStringsFlag>(s, h));
+ free(json);
+ }
+ {
+ const char* json = "{ \"negative\": -1.54321 } ";
+ StringStream s(json);
+ NumbersAsStringsHandler h("-1.54321");
+ Reader reader;
+ EXPECT_TRUE(reader.Parse<kParseNumbersAsStringsFlag>(s, h));
+ }
+ {
+ char* json = StrDup("{ \"negative\": -1.54321 } ");
+ InsituStringStream s(json);
+ NumbersAsStringsHandler h("-1.54321");
+ Reader reader;
+ EXPECT_TRUE(reader.Parse<kParseInsituFlag|kParseNumbersAsStringsFlag>(s, h));
+ free(json);
+ }
+ {
+ const char* json = "{ \"pi\": 314.159e-2 } ";
+ std::stringstream ss(json);
+ IStreamWrapper s(ss);
+ NumbersAsStringsHandler h("314.159e-2");
+ Reader reader;
+ EXPECT_TRUE(reader.Parse<kParseNumbersAsStringsFlag>(s, h));
+ }
}
-#ifdef __GNUC__
-RAPIDJSON_DIAG_POP
-#endif
+template <unsigned extraFlags>
+void TestTrailingCommas() {
+ {
+ StringStream s("[1,2,3,]");
+ ParseArrayHandler<3> h;
+ Reader reader;
+ EXPECT_TRUE(reader.Parse<extraFlags|kParseTrailingCommasFlag>(s, h));
+ EXPECT_EQ(5u, h.step_);
+ }
+ {
+ const char* json = "{ \"hello\" : \"world\", \"t\" : true , \"f\" : false,"
+ "\"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3],}";
+ StringStream s(json);
+ ParseObjectHandler h;
+ Reader reader;
+ EXPECT_TRUE(reader.Parse<extraFlags|kParseTrailingCommasFlag>(s, h));
+ EXPECT_EQ(20u, h.step_);
+ }
+ {
+ // whitespace around trailing commas
+ const char* json = "{ \"hello\" : \"world\", \"t\" : true , \"f\" : false,"
+ "\"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3\n,\n]\n,\n} ";
+ StringStream s(json);
+ ParseObjectHandler h;
+ Reader reader;
+ EXPECT_TRUE(reader.Parse<extraFlags|kParseTrailingCommasFlag>(s, h));
+ EXPECT_EQ(20u, h.step_);
+ }
+ {
+ // comments around trailing commas
+ const char* json = "{ \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null,"
+ "\"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3/*test*/,/*test*/]/*test*/,/*test*/}";
+ StringStream s(json);
+ ParseObjectHandler h;
+ Reader reader;
+ EXPECT_TRUE(reader.Parse<extraFlags|kParseTrailingCommasFlag|kParseCommentsFlag>(s, h));
+ EXPECT_EQ(20u, h.step_);
+ }
+}
+
+TEST(Reader, TrailingCommas) {
+ TestTrailingCommas<kParseNoFlags>();
+}
+
+TEST(Reader, TrailingCommasIterative) {
+ TestTrailingCommas<kParseIterativeFlag>();
+}
+
+template <unsigned extraFlags>
+void TestMultipleTrailingCommaErrors() {
+ // only a single trailing comma is allowed.
+ {
+ StringStream s("[1,2,3,,]");
+ ParseArrayHandler<3> h;
+ Reader reader;
+ ParseResult r = reader.Parse<extraFlags|kParseTrailingCommasFlag>(s, h);
+ EXPECT_TRUE(reader.HasParseError());
+ EXPECT_EQ(kParseErrorValueInvalid, r.Code());
+ EXPECT_EQ(7u, r.Offset());
+ }
+ {
+ const char* json = "{ \"hello\" : \"world\", \"t\" : true , \"f\" : false,"
+ "\"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3,],,}";
+ StringStream s(json);
+ ParseObjectHandler h;
+ Reader reader;
+ ParseResult r = reader.Parse<extraFlags|kParseTrailingCommasFlag>(s, h);
+ EXPECT_TRUE(reader.HasParseError());
+ EXPECT_EQ(kParseErrorObjectMissName, r.Code());
+ EXPECT_EQ(95u, r.Offset());
+ }
+}
+
+TEST(Reader, MultipleTrailingCommaErrors) {
+ TestMultipleTrailingCommaErrors<kParseNoFlags>();
+}
+
+TEST(Reader, MultipleTrailingCommaErrorsIterative) {
+ TestMultipleTrailingCommaErrors<kParseIterativeFlag>();
+}
+
+template <unsigned extraFlags>
+void TestEmptyExceptForCommaErrors() {
+ // not allowed even with trailing commas enabled; the
+ // trailing comma must follow a value.
+ {
+ StringStream s("[,]");
+ ParseArrayHandler<3> h;
+ Reader reader;
+ ParseResult r = reader.Parse<extraFlags|kParseTrailingCommasFlag>(s, h);
+ EXPECT_TRUE(reader.HasParseError());
+ EXPECT_EQ(kParseErrorValueInvalid, r.Code());
+ EXPECT_EQ(1u, r.Offset());
+ }
+ {
+ StringStream s("{,}");
+ ParseObjectHandler h;
+ Reader reader;
+ ParseResult r = reader.Parse<extraFlags|kParseTrailingCommasFlag>(s, h);
+ EXPECT_TRUE(reader.HasParseError());
+ EXPECT_EQ(kParseErrorObjectMissName, r.Code());
+ EXPECT_EQ(1u, r.Offset());
+ }
+}
+
+TEST(Reader, EmptyExceptForCommaErrors) {
+ TestEmptyExceptForCommaErrors<kParseNoFlags>();
+}
+
+TEST(Reader, EmptyExceptForCommaErrorsIterative) {
+ TestEmptyExceptForCommaErrors<kParseIterativeFlag>();
+}
+
+template <unsigned extraFlags>
+void TestTrailingCommaHandlerTermination() {
+ {
+ HandlerTerminateAtEndArray h;
+ Reader reader;
+ StringStream s("[1,2,3,]");
+ ParseResult r = reader.Parse<extraFlags|kParseTrailingCommasFlag>(s, h);
+ EXPECT_TRUE(reader.HasParseError());
+ EXPECT_EQ(kParseErrorTermination, r.Code());
+ EXPECT_EQ(7u, r.Offset());
+ }
+ {
+ HandlerTerminateAtEndObject h;
+ Reader reader;
+ StringStream s("{\"t\": true, \"f\": false,}");
+ ParseResult r = reader.Parse<extraFlags|kParseTrailingCommasFlag>(s, h);
+ EXPECT_TRUE(reader.HasParseError());
+ EXPECT_EQ(kParseErrorTermination, r.Code());
+ EXPECT_EQ(23u, r.Offset());
+ }
+}
+
+TEST(Reader, TrailingCommaHandlerTermination) {
+ TestTrailingCommaHandlerTermination<kParseNoFlags>();
+}
+
+TEST(Reader, TrailingCommaHandlerTerminationIterative) {
+ TestTrailingCommaHandlerTermination<kParseIterativeFlag>();
+}
+
+TEST(Reader, ParseNanAndInfinity) {
+#define TEST_NAN_INF(str, x) \
+ { \
+ { \
+ StringStream s(str); \
+ ParseDoubleHandler h; \
+ Reader reader; \
+ ASSERT_EQ(kParseErrorNone, reader.Parse<kParseNanAndInfFlag>(s, h).Code()); \
+ EXPECT_EQ(1u, h.step_); \
+ internal::Double e(x), a(h.actual_); \
+ EXPECT_EQ(e.IsNan(), a.IsNan()); \
+ EXPECT_EQ(e.IsInf(), a.IsInf()); \
+ if (!e.IsNan()) \
+ EXPECT_EQ(e.Sign(), a.Sign()); \
+ } \
+ { \
+ const char* json = "{ \"naninfdouble\": " str " } "; \
+ StringStream s(json); \
+ NumbersAsStringsHandler h(str); \
+ Reader reader; \
+ EXPECT_TRUE(reader.Parse<kParseNumbersAsStringsFlag|kParseNanAndInfFlag>(s, h)); \
+ } \
+ { \
+ char* json = StrDup("{ \"naninfdouble\": " str " } "); \
+ InsituStringStream s(json); \
+ NumbersAsStringsHandler h(str); \
+ Reader reader; \
+ EXPECT_TRUE(reader.Parse<kParseInsituFlag|kParseNumbersAsStringsFlag|kParseNanAndInfFlag>(s, h)); \
+ free(json); \
+ } \
+ }
+#define TEST_NAN_INF_ERROR(errorCode, str, errorOffset) \
+ { \
+ int streamPos = errorOffset; \
+ char buffer[1001]; \
+ strncpy(buffer, str, 1000); \
+ InsituStringStream s(buffer); \
+ BaseReaderHandler<> h; \
+ Reader reader; \
+ EXPECT_FALSE(reader.Parse<kParseNanAndInfFlag>(s, h)); \
+ EXPECT_EQ(errorCode, reader.GetParseErrorCode());\
+ EXPECT_EQ(errorOffset, reader.GetErrorOffset());\
+ EXPECT_EQ(streamPos, s.Tell());\
+ }
+
+ double nan = std::numeric_limits<double>::quiet_NaN();
+ double inf = std::numeric_limits<double>::infinity();
+
+ TEST_NAN_INF("NaN", nan);
+ TEST_NAN_INF("-NaN", nan);
+ TEST_NAN_INF("Inf", inf);
+ TEST_NAN_INF("Infinity", inf);
+ TEST_NAN_INF("-Inf", -inf);
+ TEST_NAN_INF("-Infinity", -inf);
+ TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "NInf", 1);
+ TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "NaInf", 2);
+ TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "INan", 1);
+ TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "InNan", 2);
+ TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "nan", 1);
+ TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "-nan", 1);
+ TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "NAN", 1);
+ TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "-Infinty", 6);
+
+#undef TEST_NAN_INF_ERROR
+#undef TEST_NAN_INF
+}
-#ifdef __clang__
RAPIDJSON_DIAG_POP
-#endif
diff --git a/test/unittest/regextest.cpp b/test/unittest/regextest.cpp
index e3371d16..cdd36301 100644
--- a/test/unittest/regextest.cpp
+++ b/test/unittest/regextest.cpp
@@ -17,518 +17,572 @@
using namespace rapidjson::internal;
+TEST(Regex, Single) {
+ Regex re("a");
+ ASSERT_TRUE(re.IsValid());
+ RegexSearch rs(re);
+ EXPECT_TRUE(rs.Match("a"));
+ EXPECT_FALSE(rs.Match(""));
+ EXPECT_FALSE(rs.Match("b"));
+}
+
TEST(Regex, Concatenation) {
Regex re("abc");
ASSERT_TRUE(re.IsValid());
- EXPECT_TRUE(re.Match("abc"));
- EXPECT_FALSE(re.Match(""));
- EXPECT_FALSE(re.Match("a"));
- EXPECT_FALSE(re.Match("b"));
- EXPECT_FALSE(re.Match("ab"));
- EXPECT_FALSE(re.Match("abcd"));
+ RegexSearch rs(re);
+ EXPECT_TRUE(rs.Match("abc"));
+ EXPECT_FALSE(rs.Match(""));
+ EXPECT_FALSE(rs.Match("a"));
+ EXPECT_FALSE(rs.Match("b"));
+ EXPECT_FALSE(rs.Match("ab"));
+ EXPECT_FALSE(rs.Match("abcd"));
}
TEST(Regex, Alternation1) {
Regex re("abab|abbb");
ASSERT_TRUE(re.IsValid());
- EXPECT_TRUE(re.Match("abab"));
- EXPECT_TRUE(re.Match("abbb"));
- EXPECT_FALSE(re.Match(""));
- EXPECT_FALSE(re.Match("ab"));
- EXPECT_FALSE(re.Match("ababa"));
- EXPECT_FALSE(re.Match("abb"));
- EXPECT_FALSE(re.Match("abbbb"));
+ RegexSearch rs(re);
+ EXPECT_TRUE(rs.Match("abab"));
+ EXPECT_TRUE(rs.Match("abbb"));
+ EXPECT_FALSE(rs.Match(""));
+ EXPECT_FALSE(rs.Match("ab"));
+ EXPECT_FALSE(rs.Match("ababa"));
+ EXPECT_FALSE(rs.Match("abb"));
+ EXPECT_FALSE(rs.Match("abbbb"));
}
TEST(Regex, Alternation2) {
Regex re("a|b|c");
ASSERT_TRUE(re.IsValid());
- EXPECT_TRUE(re.Match("a"));
- EXPECT_TRUE(re.Match("b"));
- EXPECT_TRUE(re.Match("c"));
- EXPECT_FALSE(re.Match(""));
- EXPECT_FALSE(re.Match("aa"));
- EXPECT_FALSE(re.Match("ab"));
+ RegexSearch rs(re);
+ EXPECT_TRUE(rs.Match("a"));
+ EXPECT_TRUE(rs.Match("b"));
+ EXPECT_TRUE(rs.Match("c"));
+ EXPECT_FALSE(rs.Match(""));
+ EXPECT_FALSE(rs.Match("aa"));
+ EXPECT_FALSE(rs.Match("ab"));
}
TEST(Regex, Parenthesis1) {
Regex re("(ab)c");
ASSERT_TRUE(re.IsValid());
- EXPECT_TRUE(re.Match("abc"));
- EXPECT_FALSE(re.Match(""));
- EXPECT_FALSE(re.Match("a"));
- EXPECT_FALSE(re.Match("b"));
- EXPECT_FALSE(re.Match("ab"));
- EXPECT_FALSE(re.Match("abcd"));
+ RegexSearch rs(re);
+ EXPECT_TRUE(rs.Match("abc"));
+ EXPECT_FALSE(rs.Match(""));
+ EXPECT_FALSE(rs.Match("a"));
+ EXPECT_FALSE(rs.Match("b"));
+ EXPECT_FALSE(rs.Match("ab"));
+ EXPECT_FALSE(rs.Match("abcd"));
}
TEST(Regex, Parenthesis2) {
Regex re("a(bc)");
ASSERT_TRUE(re.IsValid());
- EXPECT_TRUE(re.Match("abc"));
- EXPECT_FALSE(re.Match(""));
- EXPECT_FALSE(re.Match("a"));
- EXPECT_FALSE(re.Match("b"));
- EXPECT_FALSE(re.Match("ab"));
- EXPECT_FALSE(re.Match("abcd"));
+ RegexSearch rs(re);
+ EXPECT_TRUE(rs.Match("abc"));
+ EXPECT_FALSE(rs.Match(""));
+ EXPECT_FALSE(rs.Match("a"));
+ EXPECT_FALSE(rs.Match("b"));
+ EXPECT_FALSE(rs.Match("ab"));
+ EXPECT_FALSE(rs.Match("abcd"));
}
TEST(Regex, Parenthesis3) {
Regex re("(a|b)(c|d)");
ASSERT_TRUE(re.IsValid());
- EXPECT_TRUE(re.Match("ac"));
- EXPECT_TRUE(re.Match("ad"));
- EXPECT_TRUE(re.Match("bc"));
- EXPECT_TRUE(re.Match("bd"));
- EXPECT_FALSE(re.Match(""));
- EXPECT_FALSE(re.Match("ab"));
- EXPECT_FALSE(re.Match("cd"));
+ RegexSearch rs(re);
+ EXPECT_TRUE(rs.Match("ac"));
+ EXPECT_TRUE(rs.Match("ad"));
+ EXPECT_TRUE(rs.Match("bc"));
+ EXPECT_TRUE(rs.Match("bd"));
+ EXPECT_FALSE(rs.Match(""));
+ EXPECT_FALSE(rs.Match("ab"));
+ EXPECT_FALSE(rs.Match("cd"));
}
TEST(Regex, ZeroOrOne1) {
Regex re("a?");
ASSERT_TRUE(re.IsValid());
- EXPECT_TRUE(re.Match(""));
- EXPECT_TRUE(re.Match("a"));
- EXPECT_FALSE(re.Match("aa"));
+ RegexSearch rs(re);
+ EXPECT_TRUE(rs.Match(""));
+ EXPECT_TRUE(rs.Match("a"));
+ EXPECT_FALSE(rs.Match("aa"));
}
TEST(Regex, ZeroOrOne2) {
Regex re("a?b");
ASSERT_TRUE(re.IsValid());
- EXPECT_TRUE(re.Match("b"));
- EXPECT_TRUE(re.Match("ab"));
- EXPECT_FALSE(re.Match("a"));
- EXPECT_FALSE(re.Match("aa"));
- EXPECT_FALSE(re.Match("bb"));
- EXPECT_FALSE(re.Match("ba"));
+ RegexSearch rs(re);
+ EXPECT_TRUE(rs.Match("b"));
+ EXPECT_TRUE(rs.Match("ab"));
+ EXPECT_FALSE(rs.Match("a"));
+ EXPECT_FALSE(rs.Match("aa"));
+ EXPECT_FALSE(rs.Match("bb"));
+ EXPECT_FALSE(rs.Match("ba"));
}
TEST(Regex, ZeroOrOne3) {
Regex re("ab?");
ASSERT_TRUE(re.IsValid());
- EXPECT_TRUE(re.Match("a"));
- EXPECT_TRUE(re.Match("ab"));
- EXPECT_FALSE(re.Match("b"));
- EXPECT_FALSE(re.Match("aa"));
- EXPECT_FALSE(re.Match("bb"));
- EXPECT_FALSE(re.Match("ba"));
+ RegexSearch rs(re);
+ EXPECT_TRUE(rs.Match("a"));
+ EXPECT_TRUE(rs.Match("ab"));
+ EXPECT_FALSE(rs.Match("b"));
+ EXPECT_FALSE(rs.Match("aa"));
+ EXPECT_FALSE(rs.Match("bb"));
+ EXPECT_FALSE(rs.Match("ba"));
}
TEST(Regex, ZeroOrOne4) {
Regex re("a?b?");
ASSERT_TRUE(re.IsValid());
- EXPECT_TRUE(re.Match(""));
- EXPECT_TRUE(re.Match("a"));
- EXPECT_TRUE(re.Match("b"));
- EXPECT_TRUE(re.Match("ab"));
- EXPECT_FALSE(re.Match("aa"));
- EXPECT_FALSE(re.Match("bb"));
- EXPECT_FALSE(re.Match("ba"));
- EXPECT_FALSE(re.Match("abc"));
+ RegexSearch rs(re);
+ EXPECT_TRUE(rs.Match(""));
+ EXPECT_TRUE(rs.Match("a"));
+ EXPECT_TRUE(rs.Match("b"));
+ EXPECT_TRUE(rs.Match("ab"));
+ EXPECT_FALSE(rs.Match("aa"));
+ EXPECT_FALSE(rs.Match("bb"));
+ EXPECT_FALSE(rs.Match("ba"));
+ EXPECT_FALSE(rs.Match("abc"));
}
TEST(Regex, ZeroOrOne5) {
Regex re("a(ab)?b");
ASSERT_TRUE(re.IsValid());
- EXPECT_TRUE(re.Match("ab"));
- EXPECT_TRUE(re.Match("aabb"));
- EXPECT_FALSE(re.Match("aab"));
- EXPECT_FALSE(re.Match("abb"));
+ RegexSearch rs(re);
+ EXPECT_TRUE(rs.Match("ab"));
+ EXPECT_TRUE(rs.Match("aabb"));
+ EXPECT_FALSE(rs.Match("aab"));
+ EXPECT_FALSE(rs.Match("abb"));
}
TEST(Regex, ZeroOrMore1) {
Regex re("a*");
ASSERT_TRUE(re.IsValid());
- EXPECT_TRUE(re.Match(""));
- EXPECT_TRUE(re.Match("a"));
- EXPECT_TRUE(re.Match("aa"));
- EXPECT_FALSE(re.Match("b"));
- EXPECT_FALSE(re.Match("ab"));
+ RegexSearch rs(re);
+ EXPECT_TRUE(rs.Match(""));
+ EXPECT_TRUE(rs.Match("a"));
+ EXPECT_TRUE(rs.Match("aa"));
+ EXPECT_FALSE(rs.Match("b"));
+ EXPECT_FALSE(rs.Match("ab"));
}
TEST(Regex, ZeroOrMore2) {
Regex re("a*b");
ASSERT_TRUE(re.IsValid());
- EXPECT_TRUE(re.Match("b"));
- EXPECT_TRUE(re.Match("ab"));
- EXPECT_TRUE(re.Match("aab"));
- EXPECT_FALSE(re.Match(""));
- EXPECT_FALSE(re.Match("bb"));
+ RegexSearch rs(re);
+ EXPECT_TRUE(rs.Match("b"));
+ EXPECT_TRUE(rs.Match("ab"));
+ EXPECT_TRUE(rs.Match("aab"));
+ EXPECT_FALSE(rs.Match(""));
+ EXPECT_FALSE(rs.Match("bb"));
}
TEST(Regex, ZeroOrMore3) {
Regex re("a*b*");
ASSERT_TRUE(re.IsValid());
- EXPECT_TRUE(re.Match(""));
- EXPECT_TRUE(re.Match("a"));
- EXPECT_TRUE(re.Match("aa"));
- EXPECT_TRUE(re.Match("b"));
- EXPECT_TRUE(re.Match("bb"));
- EXPECT_TRUE(re.Match("ab"));
- EXPECT_TRUE(re.Match("aabb"));
- EXPECT_FALSE(re.Match("ba"));
+ RegexSearch rs(re);
+ EXPECT_TRUE(rs.Match(""));
+ EXPECT_TRUE(rs.Match("a"));
+ EXPECT_TRUE(rs.Match("aa"));
+ EXPECT_TRUE(rs.Match("b"));
+ EXPECT_TRUE(rs.Match("bb"));
+ EXPECT_TRUE(rs.Match("ab"));
+ EXPECT_TRUE(rs.Match("aabb"));
+ EXPECT_FALSE(rs.Match("ba"));
}
TEST(Regex, ZeroOrMore4) {
Regex re("a(ab)*b");
ASSERT_TRUE(re.IsValid());
- EXPECT_TRUE(re.Match("ab"));
- EXPECT_TRUE(re.Match("aabb"));
- EXPECT_TRUE(re.Match("aababb"));
- EXPECT_FALSE(re.Match(""));
- EXPECT_FALSE(re.Match("aa"));
+ RegexSearch rs(re);
+ EXPECT_TRUE(rs.Match("ab"));
+ EXPECT_TRUE(rs.Match("aabb"));
+ EXPECT_TRUE(rs.Match("aababb"));
+ EXPECT_FALSE(rs.Match(""));
+ EXPECT_FALSE(rs.Match("aa"));
}
TEST(Regex, OneOrMore1) {
Regex re("a+");
ASSERT_TRUE(re.IsValid());
- EXPECT_TRUE(re.Match("a"));
- EXPECT_TRUE(re.Match("aa"));
- EXPECT_FALSE(re.Match(""));
- EXPECT_FALSE(re.Match("b"));
- EXPECT_FALSE(re.Match("ab"));
+ RegexSearch rs(re);
+ EXPECT_TRUE(rs.Match("a"));
+ EXPECT_TRUE(rs.Match("aa"));
+ EXPECT_FALSE(rs.Match(""));
+ EXPECT_FALSE(rs.Match("b"));
+ EXPECT_FALSE(rs.Match("ab"));
}
TEST(Regex, OneOrMore2) {
Regex re("a+b");
ASSERT_TRUE(re.IsValid());
- EXPECT_TRUE(re.Match("ab"));
- EXPECT_TRUE(re.Match("aab"));
- EXPECT_FALSE(re.Match(""));
- EXPECT_FALSE(re.Match("b"));
+ RegexSearch rs(re);
+ EXPECT_TRUE(rs.Match("ab"));
+ EXPECT_TRUE(rs.Match("aab"));
+ EXPECT_FALSE(rs.Match(""));
+ EXPECT_FALSE(rs.Match("b"));
}
TEST(Regex, OneOrMore3) {
Regex re("a+b+");
ASSERT_TRUE(re.IsValid());
- EXPECT_TRUE(re.Match("ab"));
- EXPECT_TRUE(re.Match("aab"));
- EXPECT_TRUE(re.Match("abb"));
- EXPECT_TRUE(re.Match("aabb"));
- EXPECT_FALSE(re.Match(""));
- EXPECT_FALSE(re.Match("b"));
- EXPECT_FALSE(re.Match("ba"));
+ RegexSearch rs(re);
+ EXPECT_TRUE(rs.Match("ab"));
+ EXPECT_TRUE(rs.Match("aab"));
+ EXPECT_TRUE(rs.Match("abb"));
+ EXPECT_TRUE(rs.Match("aabb"));
+ EXPECT_FALSE(rs.Match(""));
+ EXPECT_FALSE(rs.Match("b"));
+ EXPECT_FALSE(rs.Match("ba"));
}
TEST(Regex, OneOrMore4) {
Regex re("a(ab)+b");
ASSERT_TRUE(re.IsValid());
- EXPECT_TRUE(re.Match("aabb"));
- EXPECT_TRUE(re.Match("aababb"));
- EXPECT_FALSE(re.Match(""));
- EXPECT_FALSE(re.Match("ab"));
+ RegexSearch rs(re);
+ EXPECT_TRUE(rs.Match("aabb"));
+ EXPECT_TRUE(rs.Match("aababb"));
+ EXPECT_FALSE(rs.Match(""));
+ EXPECT_FALSE(rs.Match("ab"));
}
TEST(Regex, QuantifierExact1) {
Regex re("ab{3}c");
ASSERT_TRUE(re.IsValid());
- EXPECT_TRUE(re.Match("abbbc"));
- EXPECT_FALSE(re.Match("ac"));
- EXPECT_FALSE(re.Match("abc"));
- EXPECT_FALSE(re.Match("abbc"));
- EXPECT_FALSE(re.Match("abbbbc"));
+ RegexSearch rs(re);
+ EXPECT_TRUE(rs.Match("abbbc"));
+ EXPECT_FALSE(rs.Match("ac"));
+ EXPECT_FALSE(rs.Match("abc"));
+ EXPECT_FALSE(rs.Match("abbc"));
+ EXPECT_FALSE(rs.Match("abbbbc"));
}
TEST(Regex, QuantifierExact2) {
Regex re("a(bc){3}d");
ASSERT_TRUE(re.IsValid());
- EXPECT_TRUE(re.Match("abcbcbcd"));
- EXPECT_FALSE(re.Match("ad"));
- EXPECT_FALSE(re.Match("abcd"));
- EXPECT_FALSE(re.Match("abcbcd"));
- EXPECT_FALSE(re.Match("abcbcbcbcd"));
+ RegexSearch rs(re);
+ EXPECT_TRUE(rs.Match("abcbcbcd"));
+ EXPECT_FALSE(rs.Match("ad"));
+ EXPECT_FALSE(rs.Match("abcd"));
+ EXPECT_FALSE(rs.Match("abcbcd"));
+ EXPECT_FALSE(rs.Match("abcbcbcbcd"));
}
TEST(Regex, QuantifierExact3) {
Regex re("a(b|c){3}d");
ASSERT_TRUE(re.IsValid());
- EXPECT_TRUE(re.Match("abbbd"));
- EXPECT_TRUE(re.Match("acccd"));
- EXPECT_TRUE(re.Match("abcbd"));
- EXPECT_FALSE(re.Match("ad"));
- EXPECT_FALSE(re.Match("abbd"));
- EXPECT_FALSE(re.Match("accccd"));
- EXPECT_FALSE(re.Match("abbbbd"));
+ RegexSearch rs(re);
+ EXPECT_TRUE(rs.Match("abbbd"));
+ EXPECT_TRUE(rs.Match("acccd"));
+ EXPECT_TRUE(rs.Match("abcbd"));
+ EXPECT_FALSE(rs.Match("ad"));
+ EXPECT_FALSE(rs.Match("abbd"));
+ EXPECT_FALSE(rs.Match("accccd"));
+ EXPECT_FALSE(rs.Match("abbbbd"));
}
TEST(Regex, QuantifierMin1) {
Regex re("ab{3,}c");
ASSERT_TRUE(re.IsValid());
- EXPECT_TRUE(re.Match("abbbc"));
- EXPECT_TRUE(re.Match("abbbbc"));
- EXPECT_TRUE(re.Match("abbbbbc"));
- EXPECT_FALSE(re.Match("ac"));
- EXPECT_FALSE(re.Match("abc"));
- EXPECT_FALSE(re.Match("abbc"));
+ RegexSearch rs(re);
+ EXPECT_TRUE(rs.Match("abbbc"));
+ EXPECT_TRUE(rs.Match("abbbbc"));
+ EXPECT_TRUE(rs.Match("abbbbbc"));
+ EXPECT_FALSE(rs.Match("ac"));
+ EXPECT_FALSE(rs.Match("abc"));
+ EXPECT_FALSE(rs.Match("abbc"));
}
TEST(Regex, QuantifierMin2) {
Regex re("a(bc){3,}d");
ASSERT_TRUE(re.IsValid());
- EXPECT_TRUE(re.Match("abcbcbcd"));
- EXPECT_TRUE(re.Match("abcbcbcbcd"));
- EXPECT_FALSE(re.Match("ad"));
- EXPECT_FALSE(re.Match("abcd"));
- EXPECT_FALSE(re.Match("abcbcd"));
+ RegexSearch rs(re);
+ EXPECT_TRUE(rs.Match("abcbcbcd"));
+ EXPECT_TRUE(rs.Match("abcbcbcbcd"));
+ EXPECT_FALSE(rs.Match("ad"));
+ EXPECT_FALSE(rs.Match("abcd"));
+ EXPECT_FALSE(rs.Match("abcbcd"));
}
TEST(Regex, QuantifierMin3) {
Regex re("a(b|c){3,}d");
ASSERT_TRUE(re.IsValid());
- EXPECT_TRUE(re.Match("abbbd"));
- EXPECT_TRUE(re.Match("acccd"));
- EXPECT_TRUE(re.Match("abcbd"));
- EXPECT_TRUE(re.Match("accccd"));
- EXPECT_TRUE(re.Match("abbbbd"));
- EXPECT_FALSE(re.Match("ad"));
- EXPECT_FALSE(re.Match("abbd"));
+ RegexSearch rs(re);
+ EXPECT_TRUE(rs.Match("abbbd"));
+ EXPECT_TRUE(rs.Match("acccd"));
+ EXPECT_TRUE(rs.Match("abcbd"));
+ EXPECT_TRUE(rs.Match("accccd"));
+ EXPECT_TRUE(rs.Match("abbbbd"));
+ EXPECT_FALSE(rs.Match("ad"));
+ EXPECT_FALSE(rs.Match("abbd"));
}
TEST(Regex, QuantifierMinMax1) {
Regex re("ab{3,5}c");
ASSERT_TRUE(re.IsValid());
- EXPECT_TRUE(re.Match("abbbc"));
- EXPECT_TRUE(re.Match("abbbbc"));
- EXPECT_TRUE(re.Match("abbbbbc"));
- EXPECT_FALSE(re.Match("ac"));
- EXPECT_FALSE(re.Match("abc"));
- EXPECT_FALSE(re.Match("abbc"));
- EXPECT_FALSE(re.Match("abbbbbbc"));
+ RegexSearch rs(re);
+ EXPECT_TRUE(rs.Match("abbbc"));
+ EXPECT_TRUE(rs.Match("abbbbc"));
+ EXPECT_TRUE(rs.Match("abbbbbc"));
+ EXPECT_FALSE(rs.Match("ac"));
+ EXPECT_FALSE(rs.Match("abc"));
+ EXPECT_FALSE(rs.Match("abbc"));
+ EXPECT_FALSE(rs.Match("abbbbbbc"));
}
TEST(Regex, QuantifierMinMax2) {
Regex re("a(bc){3,5}d");
ASSERT_TRUE(re.IsValid());
- EXPECT_TRUE(re.Match("abcbcbcd"));
- EXPECT_TRUE(re.Match("abcbcbcbcd"));
- EXPECT_TRUE(re.Match("abcbcbcbcbcd"));
- EXPECT_FALSE(re.Match("ad"));
- EXPECT_FALSE(re.Match("abcd"));
- EXPECT_FALSE(re.Match("abcbcd"));
- EXPECT_FALSE(re.Match("abcbcbcbcbcbcd"));
+ RegexSearch rs(re);
+ EXPECT_TRUE(rs.Match("abcbcbcd"));
+ EXPECT_TRUE(rs.Match("abcbcbcbcd"));
+ EXPECT_TRUE(rs.Match("abcbcbcbcbcd"));
+ EXPECT_FALSE(rs.Match("ad"));
+ EXPECT_FALSE(rs.Match("abcd"));
+ EXPECT_FALSE(rs.Match("abcbcd"));
+ EXPECT_FALSE(rs.Match("abcbcbcbcbcbcd"));
}
TEST(Regex, QuantifierMinMax3) {
Regex re("a(b|c){3,5}d");
ASSERT_TRUE(re.IsValid());
- EXPECT_TRUE(re.Match("abbbd"));
- EXPECT_TRUE(re.Match("acccd"));
- EXPECT_TRUE(re.Match("abcbd"));
- EXPECT_TRUE(re.Match("accccd"));
- EXPECT_TRUE(re.Match("abbbbd"));
- EXPECT_TRUE(re.Match("acccccd"));
- EXPECT_TRUE(re.Match("abbbbbd"));
- EXPECT_FALSE(re.Match("ad"));
- EXPECT_FALSE(re.Match("abbd"));
- EXPECT_FALSE(re.Match("accccccd"));
- EXPECT_FALSE(re.Match("abbbbbbd"));
+ RegexSearch rs(re);
+ EXPECT_TRUE(rs.Match("abbbd"));
+ EXPECT_TRUE(rs.Match("acccd"));
+ EXPECT_TRUE(rs.Match("abcbd"));
+ EXPECT_TRUE(rs.Match("accccd"));
+ EXPECT_TRUE(rs.Match("abbbbd"));
+ EXPECT_TRUE(rs.Match("acccccd"));
+ EXPECT_TRUE(rs.Match("abbbbbd"));
+ EXPECT_FALSE(rs.Match("ad"));
+ EXPECT_FALSE(rs.Match("abbd"));
+ EXPECT_FALSE(rs.Match("accccccd"));
+ EXPECT_FALSE(rs.Match("abbbbbbd"));
}
// Issue538
TEST(Regex, QuantifierMinMax4) {
Regex re("a(b|c){0,3}d");
ASSERT_TRUE(re.IsValid());
- EXPECT_TRUE(re.Match("ad"));
- EXPECT_TRUE(re.Match("abd"));
- EXPECT_TRUE(re.Match("acd"));
- EXPECT_TRUE(re.Match("abbd"));
- EXPECT_TRUE(re.Match("accd"));
- EXPECT_TRUE(re.Match("abcd"));
- EXPECT_TRUE(re.Match("abbbd"));
- EXPECT_TRUE(re.Match("acccd"));
- EXPECT_FALSE(re.Match("abbbbd"));
- EXPECT_FALSE(re.Match("add"));
- EXPECT_FALSE(re.Match("accccd"));
- EXPECT_FALSE(re.Match("abcbcd"));
+ RegexSearch rs(re);
+ EXPECT_TRUE(rs.Match("ad"));
+ EXPECT_TRUE(rs.Match("abd"));
+ EXPECT_TRUE(rs.Match("acd"));
+ EXPECT_TRUE(rs.Match("abbd"));
+ EXPECT_TRUE(rs.Match("accd"));
+ EXPECT_TRUE(rs.Match("abcd"));
+ EXPECT_TRUE(rs.Match("abbbd"));
+ EXPECT_TRUE(rs.Match("acccd"));
+ EXPECT_FALSE(rs.Match("abbbbd"));
+ EXPECT_FALSE(rs.Match("add"));
+ EXPECT_FALSE(rs.Match("accccd"));
+ EXPECT_FALSE(rs.Match("abcbcd"));
}
// Issue538
TEST(Regex, QuantifierMinMax5) {
Regex re("a(b|c){0,}d");
ASSERT_TRUE(re.IsValid());
- EXPECT_TRUE(re.Match("ad"));
- EXPECT_TRUE(re.Match("abd"));
- EXPECT_TRUE(re.Match("acd"));
- EXPECT_TRUE(re.Match("abbd"));
- EXPECT_TRUE(re.Match("accd"));
- EXPECT_TRUE(re.Match("abcd"));
- EXPECT_TRUE(re.Match("abbbd"));
- EXPECT_TRUE(re.Match("acccd"));
- EXPECT_TRUE(re.Match("abbbbd"));
- EXPECT_TRUE(re.Match("accccd"));
- EXPECT_TRUE(re.Match("abcbcd"));
- EXPECT_FALSE(re.Match("add"));
- EXPECT_FALSE(re.Match("aad"));
+ RegexSearch rs(re);
+ EXPECT_TRUE(rs.Match("ad"));
+ EXPECT_TRUE(rs.Match("abd"));
+ EXPECT_TRUE(rs.Match("acd"));
+ EXPECT_TRUE(rs.Match("abbd"));
+ EXPECT_TRUE(rs.Match("accd"));
+ EXPECT_TRUE(rs.Match("abcd"));
+ EXPECT_TRUE(rs.Match("abbbd"));
+ EXPECT_TRUE(rs.Match("acccd"));
+ EXPECT_TRUE(rs.Match("abbbbd"));
+ EXPECT_TRUE(rs.Match("accccd"));
+ EXPECT_TRUE(rs.Match("abcbcd"));
+ EXPECT_FALSE(rs.Match("add"));
+ EXPECT_FALSE(rs.Match("aad"));
}
-#define EURO "\xE2\x82\xAC" // "\xE2\x82\xAC" is UTF-8 sequence of Euro sign U+20AC
+#define EURO "\xE2\x82\xAC" // "\xE2\x82\xAC" is UTF-8 rsquence of Euro sign U+20AC
TEST(Regex, Unicode) {
Regex re("a" EURO "+b");
ASSERT_TRUE(re.IsValid());
- EXPECT_TRUE(re.Match("a" EURO "b"));
- EXPECT_TRUE(re.Match("a" EURO EURO "b"));
- EXPECT_FALSE(re.Match("a?b"));
- EXPECT_FALSE(re.Match("a" EURO "\xAC" "b")); // unaware of UTF-8 will match
+ RegexSearch rs(re);
+ EXPECT_TRUE(rs.Match("a" EURO "b"));
+ EXPECT_TRUE(rs.Match("a" EURO EURO "b"));
+ EXPECT_FALSE(rs.Match("a?b"));
+ EXPECT_FALSE(rs.Match("a" EURO "\xAC" "b")); // unaware of UTF-8 will match
}
TEST(Regex, AnyCharacter) {
Regex re(".");
ASSERT_TRUE(re.IsValid());
- EXPECT_TRUE(re.Match("a"));
- EXPECT_TRUE(re.Match("b"));
- EXPECT_TRUE(re.Match(EURO));
- EXPECT_FALSE(re.Match(""));
- EXPECT_FALSE(re.Match("aa"));
+ RegexSearch rs(re);
+ EXPECT_TRUE(rs.Match("a"));
+ EXPECT_TRUE(rs.Match("b"));
+ EXPECT_TRUE(rs.Match(EURO));
+ EXPECT_FALSE(rs.Match(""));
+ EXPECT_FALSE(rs.Match("aa"));
}
TEST(Regex, CharacterRange1) {
Regex re("[abc]");
ASSERT_TRUE(re.IsValid());
- EXPECT_TRUE(re.Match("a"));
- EXPECT_TRUE(re.Match("b"));
- EXPECT_TRUE(re.Match("c"));
- EXPECT_FALSE(re.Match(""));
- EXPECT_FALSE(re.Match("`"));
- EXPECT_FALSE(re.Match("d"));
- EXPECT_FALSE(re.Match("aa"));
+ RegexSearch rs(re);
+ EXPECT_TRUE(rs.Match("a"));
+ EXPECT_TRUE(rs.Match("b"));
+ EXPECT_TRUE(rs.Match("c"));
+ EXPECT_FALSE(rs.Match(""));
+ EXPECT_FALSE(rs.Match("`"));
+ EXPECT_FALSE(rs.Match("d"));
+ EXPECT_FALSE(rs.Match("aa"));
}
TEST(Regex, CharacterRange2) {
Regex re("[^abc]");
ASSERT_TRUE(re.IsValid());
- EXPECT_TRUE(re.Match("`"));
- EXPECT_TRUE(re.Match("d"));
- EXPECT_FALSE(re.Match("a"));
- EXPECT_FALSE(re.Match("b"));
- EXPECT_FALSE(re.Match("c"));
- EXPECT_FALSE(re.Match(""));
- EXPECT_FALSE(re.Match("aa"));
+ RegexSearch rs(re);
+ EXPECT_TRUE(rs.Match("`"));
+ EXPECT_TRUE(rs.Match("d"));
+ EXPECT_FALSE(rs.Match("a"));
+ EXPECT_FALSE(rs.Match("b"));
+ EXPECT_FALSE(rs.Match("c"));
+ EXPECT_FALSE(rs.Match(""));
+ EXPECT_FALSE(rs.Match("aa"));
}
TEST(Regex, CharacterRange3) {
Regex re("[a-c]");
ASSERT_TRUE(re.IsValid());
- EXPECT_TRUE(re.Match("a"));
- EXPECT_TRUE(re.Match("b"));
- EXPECT_TRUE(re.Match("c"));
- EXPECT_FALSE(re.Match(""));
- EXPECT_FALSE(re.Match("`"));
- EXPECT_FALSE(re.Match("d"));
- EXPECT_FALSE(re.Match("aa"));
+ RegexSearch rs(re);
+ EXPECT_TRUE(rs.Match("a"));
+ EXPECT_TRUE(rs.Match("b"));
+ EXPECT_TRUE(rs.Match("c"));
+ EXPECT_FALSE(rs.Match(""));
+ EXPECT_FALSE(rs.Match("`"));
+ EXPECT_FALSE(rs.Match("d"));
+ EXPECT_FALSE(rs.Match("aa"));
}
TEST(Regex, CharacterRange4) {
Regex re("[^a-c]");
ASSERT_TRUE(re.IsValid());
- EXPECT_TRUE(re.Match("`"));
- EXPECT_TRUE(re.Match("d"));
- EXPECT_FALSE(re.Match("a"));
- EXPECT_FALSE(re.Match("b"));
- EXPECT_FALSE(re.Match("c"));
- EXPECT_FALSE(re.Match(""));
- EXPECT_FALSE(re.Match("aa"));
+ RegexSearch rs(re);
+ EXPECT_TRUE(rs.Match("`"));
+ EXPECT_TRUE(rs.Match("d"));
+ EXPECT_FALSE(rs.Match("a"));
+ EXPECT_FALSE(rs.Match("b"));
+ EXPECT_FALSE(rs.Match("c"));
+ EXPECT_FALSE(rs.Match(""));
+ EXPECT_FALSE(rs.Match("aa"));
}
TEST(Regex, CharacterRange5) {
Regex re("[-]");
ASSERT_TRUE(re.IsValid());
- EXPECT_TRUE(re.Match("-"));
- EXPECT_FALSE(re.Match(""));
- EXPECT_FALSE(re.Match("a"));
+ RegexSearch rs(re);
+ EXPECT_TRUE(rs.Match("-"));
+ EXPECT_FALSE(rs.Match(""));
+ EXPECT_FALSE(rs.Match("a"));
}
TEST(Regex, CharacterRange6) {
Regex re("[a-]");
ASSERT_TRUE(re.IsValid());
- EXPECT_TRUE(re.Match("a"));
- EXPECT_TRUE(re.Match("-"));
- EXPECT_FALSE(re.Match(""));
- EXPECT_FALSE(re.Match("`"));
- EXPECT_FALSE(re.Match("b"));
+ RegexSearch rs(re);
+ EXPECT_TRUE(rs.Match("a"));
+ EXPECT_TRUE(rs.Match("-"));
+ EXPECT_FALSE(rs.Match(""));
+ EXPECT_FALSE(rs.Match("`"));
+ EXPECT_FALSE(rs.Match("b"));
}
TEST(Regex, CharacterRange7) {
Regex re("[-a]");
ASSERT_TRUE(re.IsValid());
- EXPECT_TRUE(re.Match("a"));
- EXPECT_TRUE(re.Match("-"));
- EXPECT_FALSE(re.Match(""));
- EXPECT_FALSE(re.Match("`"));
- EXPECT_FALSE(re.Match("b"));
+ RegexSearch rs(re);
+ EXPECT_TRUE(rs.Match("a"));
+ EXPECT_TRUE(rs.Match("-"));
+ EXPECT_FALSE(rs.Match(""));
+ EXPECT_FALSE(rs.Match("`"));
+ EXPECT_FALSE(rs.Match("b"));
}
TEST(Regex, CharacterRange8) {
Regex re("[a-zA-Z0-9]*");
ASSERT_TRUE(re.IsValid());
- EXPECT_TRUE(re.Match("Milo"));
- EXPECT_TRUE(re.Match("MT19937"));
- EXPECT_TRUE(re.Match("43"));
- EXPECT_FALSE(re.Match("a_b"));
- EXPECT_FALSE(re.Match("!"));
+ RegexSearch rs(re);
+ EXPECT_TRUE(rs.Match("Milo"));
+ EXPECT_TRUE(rs.Match("MT19937"));
+ EXPECT_TRUE(rs.Match("43"));
+ EXPECT_FALSE(rs.Match("a_b"));
+ EXPECT_FALSE(rs.Match("!"));
}
TEST(Regex, Search) {
Regex re("abc");
ASSERT_TRUE(re.IsValid());
- EXPECT_TRUE(re.Search("abc"));
- EXPECT_TRUE(re.Search("_abc"));
- EXPECT_TRUE(re.Search("abc_"));
- EXPECT_TRUE(re.Search("_abc_"));
- EXPECT_TRUE(re.Search("__abc__"));
- EXPECT_TRUE(re.Search("abcabc"));
- EXPECT_FALSE(re.Search("a"));
- EXPECT_FALSE(re.Search("ab"));
- EXPECT_FALSE(re.Search("bc"));
- EXPECT_FALSE(re.Search("cba"));
+ RegexSearch rs(re);
+ EXPECT_TRUE(rs.Search("abc"));
+ EXPECT_TRUE(rs.Search("_abc"));
+ EXPECT_TRUE(rs.Search("abc_"));
+ EXPECT_TRUE(rs.Search("_abc_"));
+ EXPECT_TRUE(rs.Search("__abc__"));
+ EXPECT_TRUE(rs.Search("abcabc"));
+ EXPECT_FALSE(rs.Search("a"));
+ EXPECT_FALSE(rs.Search("ab"));
+ EXPECT_FALSE(rs.Search("bc"));
+ EXPECT_FALSE(rs.Search("cba"));
}
TEST(Regex, Search_BeginAnchor) {
Regex re("^abc");
ASSERT_TRUE(re.IsValid());
- EXPECT_TRUE(re.Search("abc"));
- EXPECT_TRUE(re.Search("abc_"));
- EXPECT_TRUE(re.Search("abcabc"));
- EXPECT_FALSE(re.Search("_abc"));
- EXPECT_FALSE(re.Search("_abc_"));
- EXPECT_FALSE(re.Search("a"));
- EXPECT_FALSE(re.Search("ab"));
- EXPECT_FALSE(re.Search("bc"));
- EXPECT_FALSE(re.Search("cba"));
+ RegexSearch rs(re);
+ EXPECT_TRUE(rs.Search("abc"));
+ EXPECT_TRUE(rs.Search("abc_"));
+ EXPECT_TRUE(rs.Search("abcabc"));
+ EXPECT_FALSE(rs.Search("_abc"));
+ EXPECT_FALSE(rs.Search("_abc_"));
+ EXPECT_FALSE(rs.Search("a"));
+ EXPECT_FALSE(rs.Search("ab"));
+ EXPECT_FALSE(rs.Search("bc"));
+ EXPECT_FALSE(rs.Search("cba"));
}
TEST(Regex, Search_EndAnchor) {
Regex re("abc$");
ASSERT_TRUE(re.IsValid());
- EXPECT_TRUE(re.Search("abc"));
- EXPECT_TRUE(re.Search("_abc"));
- EXPECT_TRUE(re.Search("abcabc"));
- EXPECT_FALSE(re.Search("abc_"));
- EXPECT_FALSE(re.Search("_abc_"));
- EXPECT_FALSE(re.Search("a"));
- EXPECT_FALSE(re.Search("ab"));
- EXPECT_FALSE(re.Search("bc"));
- EXPECT_FALSE(re.Search("cba"));
+ RegexSearch rs(re);
+ EXPECT_TRUE(rs.Search("abc"));
+ EXPECT_TRUE(rs.Search("_abc"));
+ EXPECT_TRUE(rs.Search("abcabc"));
+ EXPECT_FALSE(rs.Search("abc_"));
+ EXPECT_FALSE(rs.Search("_abc_"));
+ EXPECT_FALSE(rs.Search("a"));
+ EXPECT_FALSE(rs.Search("ab"));
+ EXPECT_FALSE(rs.Search("bc"));
+ EXPECT_FALSE(rs.Search("cba"));
}
TEST(Regex, Search_BothAnchor) {
Regex re("^abc$");
ASSERT_TRUE(re.IsValid());
- EXPECT_TRUE(re.Search("abc"));
- EXPECT_FALSE(re.Search(""));
- EXPECT_FALSE(re.Search("a"));
- EXPECT_FALSE(re.Search("b"));
- EXPECT_FALSE(re.Search("ab"));
- EXPECT_FALSE(re.Search("abcd"));
+ RegexSearch rs(re);
+ EXPECT_TRUE(rs.Search("abc"));
+ EXPECT_FALSE(rs.Search(""));
+ EXPECT_FALSE(rs.Search("a"));
+ EXPECT_FALSE(rs.Search("b"));
+ EXPECT_FALSE(rs.Search("ab"));
+ EXPECT_FALSE(rs.Search("abcd"));
}
TEST(Regex, Escape) {
const char* s = "\\^\\$\\|\\(\\)\\?\\*\\+\\.\\[\\]\\{\\}\\\\\\f\\n\\r\\t\\v[\\b][\\[][\\]]";
Regex re(s);
ASSERT_TRUE(re.IsValid());
- EXPECT_TRUE(re.Match("^$|()?*+.[]{}\\\x0C\n\r\t\x0B\b[]"));
- EXPECT_FALSE(re.Match(s)); // Not escaping
+ RegexSearch rs(re);
+ EXPECT_TRUE(rs.Match("^$|()?*+.[]{}\\\x0C\n\r\t\x0B\b[]"));
+ EXPECT_FALSE(rs.Match(s)); // Not escaping
}
TEST(Regex, Invalid) {
@@ -560,6 +614,9 @@ TEST(Regex, Invalid) {
TEST_INVALID("a{1,0}");
TEST_INVALID("a{-1,0}");
TEST_INVALID("a{-1,1}");
+ TEST_INVALID("a{4294967296}"); // overflow of unsigned
+ TEST_INVALID("a{1a}");
+ TEST_INVALID("[");
TEST_INVALID("[]");
TEST_INVALID("[^]");
TEST_INVALID("[\\a]");
@@ -573,4 +630,9 @@ TEST(Regex, Issue538) {
EXPECT_TRUE(re.IsValid());
}
+TEST(Regex, Issue583) {
+ Regex re("[0-9]{99999}");
+ ASSERT_TRUE(re.IsValid());
+}
+
#undef EURO
diff --git a/test/unittest/schematest.cpp b/test/unittest/schematest.cpp
index 7182ad29..9b99ba89 100644
--- a/test/unittest/schematest.cpp
+++ b/test/unittest/schematest.cpp
@@ -51,6 +51,11 @@ TEST(SchemaValidator, Hasher) {
TEST_HASHER("false", "null", false);
TEST_HASHER("1", "1", true);
+ TEST_HASHER("2147483648", "2147483648", true); // 2^31 can only be fit in unsigned
+ TEST_HASHER("-2147483649", "-2147483649", true); // -2^31 - 1 can only be fit in int64_t
+ TEST_HASHER("2147483648", "2147483648", true); // 2^31 can only be fit in unsigned
+ TEST_HASHER("4294967296", "4294967296", true); // 2^32 can only be fit in int64_t
+ TEST_HASHER("9223372036854775808", "9223372036854775808", true); // 2^63 can only be fit in uint64_t
TEST_HASHER("1.5", "1.5", true);
TEST_HASHER("1", "1.0", true);
TEST_HASHER("1", "-1", false);
@@ -106,7 +111,7 @@ TEST(SchemaValidator, Hasher) {
EXPECT_FALSE(d.HasParseError());\
EXPECT_TRUE(expected == d.Accept(validator));\
EXPECT_TRUE(expected == validator.IsValid());\
- if (expected && !validator.IsValid()) {\
+ if ((expected) && !validator.IsValid()) {\
StringBuffer sb;\
validator.GetInvalidSchemaPointer().StringifyUriFragment(sb);\
printf("Invalid schema: %s\n", sb.GetString());\
@@ -316,6 +321,10 @@ TEST(SchemaValidator, String) {
VALIDATE(s, "\"I'm a string\"", true);
INVALIDATE(s, "42", "", "type", "");
+ INVALIDATE(s, "2147483648", "", "type", ""); // 2^31 can only be fit in unsigned
+ INVALIDATE(s, "-2147483649", "", "type", ""); // -2^31 - 1 can only be fit in int64_t
+ INVALIDATE(s, "4294967296", "", "type", ""); // 2^32 can only be fit in int64_t
+ INVALIDATE(s, "3.1415926", "", "type", "");
}
TEST(SchemaValidator, String_LengthRange) {
@@ -340,6 +349,16 @@ TEST(SchemaValidator, String_Pattern) {
INVALIDATE(s, "\"(888)555-1212 ext. 532\"", "", "pattern", "");
INVALIDATE(s, "\"(800)FLOWERS\"", "", "pattern", "");
}
+
+TEST(SchemaValidator, String_Pattern_Invalid) {
+ Document sd;
+ sd.Parse("{\"type\":\"string\",\"pattern\":\"a{0}\"}"); // TODO: report regex is invalid somehow
+ SchemaDocument s(sd);
+
+ VALIDATE(s, "\"\"", true);
+ VALIDATE(s, "\"a\"", true);
+ VALIDATE(s, "\"aa\"", true);
+}
#endif
TEST(SchemaValidator, Integer) {
@@ -349,6 +368,10 @@ TEST(SchemaValidator, Integer) {
VALIDATE(s, "42", true);
VALIDATE(s, "-1", true);
+ VALIDATE(s, "2147483648", true); // 2^31 can only be fit in unsigned
+ VALIDATE(s, "-2147483649", true); // -2^31 - 1 can only be fit in int64_t
+ VALIDATE(s, "2147483648", true); // 2^31 can only be fit in unsigned
+ VALIDATE(s, "4294967296", true); // 2^32 can only be fit in int64_t
INVALIDATE(s, "3.1415926", "", "type", "");
INVALIDATE(s, "\"42\"", "", "type", "");
}
@@ -368,11 +391,34 @@ TEST(SchemaValidator, Integer_Range) {
TEST(SchemaValidator, Integer_Range64Boundary) {
Document sd;
- sd.Parse("{\"type\":\"integer\",\"minimum\":-9223372036854775807,\"maximum\":18446744073709551614}");
+ sd.Parse("{\"type\":\"integer\",\"minimum\":-9223372036854775807,\"maximum\":9223372036854775806}");
SchemaDocument s(sd);
INVALIDATE(s, "-9223372036854775808", "", "minimum", "");
VALIDATE(s, "-9223372036854775807", true);
+ VALIDATE(s, "-2147483648", true); // int min
+ VALIDATE(s, "0", true);
+ VALIDATE(s, "2147483647", true); // int max
+ VALIDATE(s, "2147483648", true); // unsigned first
+ VALIDATE(s, "4294967295", true); // unsigned max
+ VALIDATE(s, "9223372036854775806", true);
+ INVALIDATE(s, "9223372036854775807", "", "maximum", "");
+ INVALIDATE(s, "18446744073709551615", "", "maximum", ""); // uint64_t max
+}
+
+TEST(SchemaValidator, Integer_RangeU64Boundary) {
+ Document sd;
+ sd.Parse("{\"type\":\"integer\",\"minimum\":9223372036854775808,\"maximum\":18446744073709551614}");
+ SchemaDocument s(sd);
+
+ INVALIDATE(s, "-9223372036854775808", "", "minimum", "");
+ INVALIDATE(s, "9223372036854775807", "", "minimum", "");
+ INVALIDATE(s, "-2147483648", "", "minimum", ""); // int min
+ INVALIDATE(s, "0", "", "minimum", "");
+ INVALIDATE(s, "2147483647", "", "minimum", ""); // int max
+ INVALIDATE(s, "2147483648", "", "minimum", ""); // unsigned first
+ INVALIDATE(s, "4294967295", "", "minimum", ""); // unsigned max
+ VALIDATE(s, "9223372036854775808", true);
VALIDATE(s, "18446744073709551614", true);
INVALIDATE(s, "18446744073709551615", "", "maximum", "");
}
@@ -418,10 +464,73 @@ TEST(SchemaValidator, Number_Range) {
INVALIDATE(s, "-1", "", "minimum", "");
VALIDATE(s, "0", true);
+ VALIDATE(s, "0.1", true);
VALIDATE(s, "10", true);
VALIDATE(s, "99", true);
+ VALIDATE(s, "99.9", true);
INVALIDATE(s, "100", "", "maximum", "");
+ INVALIDATE(s, "100.0", "", "maximum", "");
+ INVALIDATE(s, "101.5", "", "maximum", "");
+}
+
+TEST(SchemaValidator, Number_RangeInt) {
+ Document sd;
+ sd.Parse("{\"type\":\"number\",\"minimum\":-100,\"maximum\":-1,\"exclusiveMaximum\":true}");
+ SchemaDocument s(sd);
+
+ INVALIDATE(s, "-101", "", "minimum", "");
+ INVALIDATE(s, "-100.1", "", "minimum", "");
+ VALIDATE(s, "-100", true);
+ VALIDATE(s, "-2", true);
+ INVALIDATE(s, "-1", "", "maximum", "");
+ INVALIDATE(s, "-0.9", "", "maximum", "");
+ INVALIDATE(s, "0", "", "maximum", "");
+ INVALIDATE(s, "2147483647", "", "maximum", ""); // int max
+ INVALIDATE(s, "2147483648", "", "maximum", ""); // unsigned first
+ INVALIDATE(s, "4294967295", "", "maximum", ""); // unsigned max
+ INVALIDATE(s, "9223372036854775808", "", "maximum", "");
+ INVALIDATE(s, "18446744073709551614", "", "maximum", "");
+ INVALIDATE(s, "18446744073709551615", "", "maximum", "");
+}
+
+TEST(SchemaValidator, Number_RangeDouble) {
+ Document sd;
+ sd.Parse("{\"type\":\"number\",\"minimum\":0.1,\"maximum\":100.1,\"exclusiveMaximum\":true}");
+ SchemaDocument s(sd);
+
+ INVALIDATE(s, "-9223372036854775808", "", "minimum", "");
+ INVALIDATE(s, "-2147483648", "", "minimum", ""); // int min
+ INVALIDATE(s, "-1", "", "minimum", "");
+ VALIDATE(s, "0.1", true);
+ VALIDATE(s, "10", true);
+ VALIDATE(s, "99", true);
+ VALIDATE(s, "100", true);
INVALIDATE(s, "101", "", "maximum", "");
+ INVALIDATE(s, "101.5", "", "maximum", "");
+ INVALIDATE(s, "18446744073709551614", "", "maximum", "");
+ INVALIDATE(s, "18446744073709551615", "", "maximum", "");
+ INVALIDATE(s, "2147483647", "", "maximum", ""); // int max
+ INVALIDATE(s, "2147483648", "", "maximum", ""); // unsigned first
+ INVALIDATE(s, "4294967295", "", "maximum", ""); // unsigned max
+ INVALIDATE(s, "9223372036854775808", "", "maximum", "");
+ INVALIDATE(s, "18446744073709551614", "", "maximum", "");
+ INVALIDATE(s, "18446744073709551615", "", "maximum", "");
+}
+
+TEST(SchemaValidator, Number_RangeDoubleU64Boundary) {
+ Document sd;
+ sd.Parse("{\"type\":\"number\",\"minimum\":9223372036854775808.0,\"maximum\":18446744073709550000.0}");
+ SchemaDocument s(sd);
+
+ INVALIDATE(s, "-9223372036854775808", "", "minimum", "");
+ INVALIDATE(s, "-2147483648", "", "minimum", ""); // int min
+ INVALIDATE(s, "0", "", "minimum", "");
+ INVALIDATE(s, "2147483647", "", "minimum", ""); // int max
+ INVALIDATE(s, "2147483648", "", "minimum", ""); // unsigned first
+ INVALIDATE(s, "4294967295", "", "minimum", ""); // unsigned max
+ VALIDATE(s, "9223372036854775808", true);
+ VALIDATE(s, "18446744073709540000", true);
+ INVALIDATE(s, "18446744073709551615", "", "maximum", "");
}
TEST(SchemaValidator, Number_MultipleOf) {
@@ -434,6 +543,13 @@ TEST(SchemaValidator, Number_MultipleOf) {
VALIDATE(s, "-10", true);
VALIDATE(s, "20", true);
INVALIDATE(s, "23", "", "multipleOf", "");
+ INVALIDATE(s, "-2147483648", "", "multipleOf", ""); // int min
+ VALIDATE(s, "-2147483640", true);
+ INVALIDATE(s, "2147483647", "", "multipleOf", ""); // int max
+ INVALIDATE(s, "2147483648", "", "multipleOf", ""); // unsigned first
+ VALIDATE(s, "2147483650", true);
+ INVALIDATE(s, "4294967295", "", "multipleOf", ""); // unsigned max
+ VALIDATE(s, "4294967300", true);
}
TEST(SchemaValidator, Number_MultipleOfOne) {
@@ -844,6 +960,19 @@ TEST(SchemaValidator, AllOf_Nested) {
INVALIDATE(s, "123", "", "allOf", "");
}
+TEST(SchemaValidator, EscapedPointer) {
+ Document sd;
+ sd.Parse(
+ "{"
+ " \"type\": \"object\","
+ " \"properties\": {"
+ " \"~/\": { \"type\": \"number\" }"
+ " }"
+ "}");
+ SchemaDocument s(sd);
+ INVALIDATE(s, "{\"~/\":true}", "/properties/~0~1", "type", "/~0~1");
+}
+
template <typename Allocator>
static char* ReadFile(const char* filename, Allocator& allocator) {
const char *paths[] = {
@@ -972,7 +1101,7 @@ public:
};
for (size_t i = 0; i < kCount; i++)
- if (strncmp(uri, uris[i], length) == 0)
+ if (strncmp(uri, uris[i], length) == 0 && strlen(uris[i]) == length)
return sd_[i];
return 0;
}
@@ -1152,6 +1281,12 @@ TEST(SchemaValidatingWriter, Simple) {
EXPECT_TRUE(validator.GetInvalidDocumentPointer() == SchemaDocument::PointerType(""));
}
+TEST(Schema, Issue848) {
+ rapidjson::Document d;
+ rapidjson::SchemaDocument s(d);
+ rapidjson::GenericSchemaValidator<rapidjson::SchemaDocument, rapidjson::Document> v(s);
+}
+
#if RAPIDJSON_HAS_CXX11_RVALUE_REFS
static SchemaDocument ReturnSchemaDocument() {
@@ -1170,6 +1305,49 @@ TEST(Schema, Issue552) {
#endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS
+TEST(SchemaValidator, Issue608) {
+ Document sd;
+ sd.Parse("{\"required\": [\"a\", \"b\"] }");
+ SchemaDocument s(sd);
+
+ VALIDATE(s, "{\"a\" : null, \"b\": null}", true);
+ INVALIDATE(s, "{\"a\" : null, \"a\" : null}", "", "required", "");
+}
+
+// Fail to resolve $ref in allOf causes crash in SchemaValidator::StartObject()
+TEST(SchemaValidator, Issue728_AllOfRef) {
+ Document sd;
+ sd.Parse("{\"allOf\": [{\"$ref\": \"#/abc\"}]}");
+ SchemaDocument s(sd);
+ VALIDATE(s, "{\"key1\": \"abc\", \"key2\": \"def\"}", true);
+}
+
+TEST(SchemaValidator, Issue825) {
+ Document sd;
+ sd.Parse("{\"type\": \"object\", \"additionalProperties\": false, \"patternProperties\": {\"^i\": { \"type\": \"string\" } } }");
+ SchemaDocument s(sd);
+ VALIDATE(s, "{ \"item\": \"hello\" }", true);
+}
+
+TEST(SchemaValidator, Issue1017_allOfHandler) {
+ Document sd;
+ sd.Parse("{\"allOf\": [{\"type\": \"object\",\"properties\": {\"cyanArray2\": {\"type\": \"array\",\"items\": { \"type\": \"string\" }}}},{\"type\": \"object\",\"properties\": {\"blackArray\": {\"type\": \"array\",\"items\": { \"type\": \"string\" }}},\"required\": [ \"blackArray\" ]}]}");
+ SchemaDocument s(sd);
+ StringBuffer sb;
+ Writer<StringBuffer> writer(sb);
+ GenericSchemaValidator<SchemaDocument, Writer<StringBuffer> > validator(s, writer);
+ EXPECT_TRUE(validator.StartObject());
+ EXPECT_TRUE(validator.Key("cyanArray2", 10, false));
+ EXPECT_TRUE(validator.StartArray());
+ EXPECT_TRUE(validator.EndArray(0));
+ EXPECT_TRUE(validator.Key("blackArray", 10, false));
+ EXPECT_TRUE(validator.StartArray());
+ EXPECT_TRUE(validator.EndArray(0));
+ EXPECT_TRUE(validator.EndObject(0));
+ EXPECT_TRUE(validator.IsValid());
+ EXPECT_STREQ("{\"cyanArray2\":[],\"blackArray\":[]}", sb.GetString());
+}
+
#ifdef __clang__
RAPIDJSON_DIAG_POP
#endif
diff --git a/test/unittest/simdtest.cpp b/test/unittest/simdtest.cpp
index 1b6fcef3..7b58cd05 100644
--- a/test/unittest/simdtest.cpp
+++ b/test/unittest/simdtest.cpp
@@ -21,6 +21,8 @@
# define RAPIDJSON_SSE42
#elif defined(__SSE2__)
# define RAPIDJSON_SSE2
+#elif defined(__ARM_NEON)
+# define RAPIDJSON_NEON
#endif
#define RAPIDJSON_NAMESPACE rapidjson_simd
@@ -41,6 +43,8 @@ using namespace rapidjson_simd;
#define SIMD_SUFFIX(name) name##_SSE2
#elif defined(RAPIDJSON_SSE42)
#define SIMD_SUFFIX(name) name##_SSE42
+#elif defined(RAPIDJSON_NEON)
+#define SIMD_SUFFIX(name) name##_NEON
#else
#define SIMD_SUFFIX(name) name
#endif
@@ -100,29 +104,60 @@ struct ScanCopyUnescapedStringHandler : BaseReaderHandler<UTF8<>, ScanCopyUnesca
memcpy(buffer, str, length + 1);
return true;
}
- char buffer[1024 + 5];
+ char buffer[1024 + 5 + 32];
};
template <unsigned parseFlags, typename StreamType>
void TestScanCopyUnescapedString() {
- for (size_t step = 0; step < 1024; step++) {
- char json[1024 + 5];
- char *p = json;
- *p ++= '\"';
- for (size_t i = 0; i < step; i++)
- *p++= "ABCD"[i % 4];
- *p++ = '\\';
- *p++ = '\\';
- *p++ = '\"';
- *p++ = '\0';
-
- StreamType s(json);
- Reader reader;
- ScanCopyUnescapedStringHandler h;
- reader.Parse<parseFlags>(s, h);
- EXPECT_TRUE(memcmp(h.buffer, json + 1, step) == 0);
- EXPECT_EQ('\\', h.buffer[step]); // escaped
- EXPECT_EQ('\0', h.buffer[step + 1]);
+ char buffer[1024 + 5 + 32];
+ char backup[1024 + 5 + 32];
+
+ // Test "ABCDABCD...\\"
+ for (size_t offset = 0; offset < 32; offset++) {
+ for (size_t step = 0; step < 1024; step++) {
+ char* json = buffer + offset;
+ char *p = json;
+ *p++ = '\"';
+ for (size_t i = 0; i < step; i++)
+ *p++ = "ABCD"[i % 4];
+ *p++ = '\\';
+ *p++ = '\\';
+ *p++ = '\"';
+ *p++ = '\0';
+ strcpy(backup, json); // insitu parsing will overwrite buffer, so need to backup first
+
+ StreamType s(json);
+ Reader reader;
+ ScanCopyUnescapedStringHandler h;
+ reader.Parse<parseFlags>(s, h);
+ EXPECT_TRUE(memcmp(h.buffer, backup + 1, step) == 0);
+ EXPECT_EQ('\\', h.buffer[step]); // escaped
+ EXPECT_EQ('\0', h.buffer[step + 1]);
+ }
+ }
+
+ // Test "\\ABCDABCD..."
+ for (size_t offset = 0; offset < 32; offset++) {
+ for (size_t step = 0; step < 1024; step++) {
+ char* json = buffer + offset;
+ char *p = json;
+ *p++ = '\"';
+ *p++ = '\\';
+ *p++ = '\\';
+ for (size_t i = 0; i < step; i++)
+ *p++ = "ABCD"[i % 4];
+ *p++ = '\"';
+ *p++ = '\0';
+ strcpy(backup, json); // insitu parsing will overwrite buffer, so need to backup first
+
+ StreamType s(json);
+ Reader reader;
+ ScanCopyUnescapedStringHandler h;
+ reader.Parse<parseFlags>(s, h);
+ EXPECT_TRUE(memcmp(h.buffer + 1, backup + 3, step) == 0);
+ EXPECT_EQ('\\', h.buffer[0]); // escaped
+ EXPECT_EQ('\0', h.buffer[step + 1]);
+ }
}
}
@@ -132,47 +167,50 @@ TEST(SIMD, SIMD_SUFFIX(ScanCopyUnescapedString)) {
}
TEST(SIMD, SIMD_SUFFIX(ScanWriteUnescapedString)) {
- for (size_t step = 0; step < 1024; step++) {
- char s[2048 + 1];
- char *p = s;
- for (size_t i = 0; i < step; i++)
- *p++= "ABCD"[i % 4];
- char escape = "\0\n\\\""[step % 4];
- *p++ = escape;
- for (size_t i = 0; i < step; i++)
- *p++= "ABCD"[i % 4];
-
- StringBuffer sb;
- Writer<StringBuffer> writer(sb);
- writer.String(s, SizeType(step * 2 + 1));
- const char* q = sb.GetString();
- EXPECT_EQ('\"', *q++);
- for (size_t i = 0; i < step; i++)
- EXPECT_EQ("ABCD"[i % 4], *q++);
- if (escape == '\0') {
- EXPECT_EQ('\\', *q++);
- EXPECT_EQ('u', *q++);
- EXPECT_EQ('0', *q++);
- EXPECT_EQ('0', *q++);
- EXPECT_EQ('0', *q++);
- EXPECT_EQ('0', *q++);
- }
- else if (escape == '\n') {
- EXPECT_EQ('\\', *q++);
- EXPECT_EQ('n', *q++);
- }
- else if (escape == '\\') {
- EXPECT_EQ('\\', *q++);
- EXPECT_EQ('\\', *q++);
- }
- else if (escape == '\"') {
- EXPECT_EQ('\\', *q++);
+ char buffer[2048 + 1 + 32];
+ for (size_t offset = 0; offset < 32; offset++) {
+ for (size_t step = 0; step < 1024; step++) {
+ char* s = buffer + offset;
+ char* p = s;
+ for (size_t i = 0; i < step; i++)
+ *p++ = "ABCD"[i % 4];
+ char escape = "\0\n\\\""[step % 4];
+ *p++ = escape;
+ for (size_t i = 0; i < step; i++)
+ *p++ = "ABCD"[i % 4];
+
+ StringBuffer sb;
+ Writer<StringBuffer> writer(sb);
+ writer.String(s, SizeType(step * 2 + 1));
+ const char* q = sb.GetString();
+ EXPECT_EQ('\"', *q++);
+ for (size_t i = 0; i < step; i++)
+ EXPECT_EQ("ABCD"[i % 4], *q++);
+ if (escape == '\0') {
+ EXPECT_EQ('\\', *q++);
+ EXPECT_EQ('u', *q++);
+ EXPECT_EQ('0', *q++);
+ EXPECT_EQ('0', *q++);
+ EXPECT_EQ('0', *q++);
+ EXPECT_EQ('0', *q++);
+ }
+ else if (escape == '\n') {
+ EXPECT_EQ('\\', *q++);
+ EXPECT_EQ('n', *q++);
+ }
+ else if (escape == '\\') {
+ EXPECT_EQ('\\', *q++);
+ EXPECT_EQ('\\', *q++);
+ }
+ else if (escape == '\"') {
+ EXPECT_EQ('\\', *q++);
+ EXPECT_EQ('\"', *q++);
+ }
+ for (size_t i = 0; i < step; i++)
+ EXPECT_EQ("ABCD"[i % 4], *q++);
EXPECT_EQ('\"', *q++);
+ EXPECT_EQ('\0', *q++);
}
- for (size_t i = 0; i < step; i++)
- EXPECT_EQ("ABCD"[i % 4], *q++);
- EXPECT_EQ('\"', *q++);
- EXPECT_EQ('\0', *q++);
}
}
diff --git a/test/unittest/stringbuffertest.cpp b/test/unittest/stringbuffertest.cpp
index 9be98fce..8a36102f 100644
--- a/test/unittest/stringbuffertest.cpp
+++ b/test/unittest/stringbuffertest.cpp
@@ -26,6 +26,7 @@ using namespace rapidjson;
TEST(StringBuffer, InitialSize) {
StringBuffer buffer;
EXPECT_EQ(0u, buffer.GetSize());
+ EXPECT_EQ(0u, buffer.GetLength());
EXPECT_STREQ("", buffer.GetString());
}
@@ -34,9 +35,19 @@ TEST(StringBuffer, Put) {
buffer.Put('A');
EXPECT_EQ(1u, buffer.GetSize());
+ EXPECT_EQ(1u, buffer.GetLength());
EXPECT_STREQ("A", buffer.GetString());
}
+TEST(StringBuffer, PutN_Issue672) {
+ GenericStringBuffer<UTF8<>, MemoryPoolAllocator<> > buffer;
+ EXPECT_EQ(0, buffer.GetSize());
+ EXPECT_EQ(0, buffer.GetLength());
+ rapidjson::PutN(buffer, ' ', 1);
+ EXPECT_EQ(1, buffer.GetSize());
+ EXPECT_EQ(1, buffer.GetLength());
+}
+
TEST(StringBuffer, Clear) {
StringBuffer buffer;
buffer.Put('A');
@@ -45,6 +56,7 @@ TEST(StringBuffer, Clear) {
buffer.Clear();
EXPECT_EQ(0u, buffer.GetSize());
+ EXPECT_EQ(0u, buffer.GetLength());
EXPECT_STREQ("", buffer.GetString());
}
@@ -53,6 +65,7 @@ TEST(StringBuffer, Push) {
buffer.Push(5);
EXPECT_EQ(5u, buffer.GetSize());
+ EXPECT_EQ(5u, buffer.GetLength());
// Causes sudden expansion to make the stack's capacity equal to size
buffer.Push(65536u);
@@ -69,9 +82,19 @@ TEST(StringBuffer, Pop) {
buffer.Pop(3);
EXPECT_EQ(2u, buffer.GetSize());
+ EXPECT_EQ(2u, buffer.GetLength());
EXPECT_STREQ("AB", buffer.GetString());
}
+TEST(StringBuffer, GetLength_Issue744) {
+ GenericStringBuffer<UTF16<wchar_t> > buffer;
+ buffer.Put('A');
+ buffer.Put('B');
+ buffer.Put('C');
+ EXPECT_EQ(3u * sizeof(wchar_t), buffer.GetSize());
+ EXPECT_EQ(3u, buffer.GetLength());
+}
+
#if RAPIDJSON_HAS_CXX11_RVALUE_REFS
#if 0 // Many old compiler does not support these. Turn it off temporaily.
@@ -123,18 +146,23 @@ TEST(StringBuffer, MoveConstructor) {
x.Put('D');
EXPECT_EQ(4u, x.GetSize());
+ EXPECT_EQ(4u, x.GetLength());
EXPECT_STREQ("ABCD", x.GetString());
// StringBuffer y(x); // does not compile (!is_copy_constructible)
StringBuffer y(std::move(x));
EXPECT_EQ(0u, x.GetSize());
+ EXPECT_EQ(0u, x.GetLength());
EXPECT_EQ(4u, y.GetSize());
+ EXPECT_EQ(4u, y.GetLength());
EXPECT_STREQ("ABCD", y.GetString());
// StringBuffer z = y; // does not compile (!is_copy_assignable)
StringBuffer z = std::move(y);
EXPECT_EQ(0u, y.GetSize());
+ EXPECT_EQ(0u, y.GetLength());
EXPECT_EQ(4u, z.GetSize());
+ EXPECT_EQ(4u, z.GetLength());
EXPECT_STREQ("ABCD", z.GetString());
}
@@ -146,13 +174,14 @@ TEST(StringBuffer, MoveAssignment) {
x.Put('D');
EXPECT_EQ(4u, x.GetSize());
+ EXPECT_EQ(4u, x.GetLength());
EXPECT_STREQ("ABCD", x.GetString());
StringBuffer y;
// y = x; // does not compile (!is_copy_assignable)
y = std::move(x);
EXPECT_EQ(0u, x.GetSize());
- EXPECT_EQ(4u, y.GetSize());
+ EXPECT_EQ(4u, y.GetLength());
EXPECT_STREQ("ABCD", y.GetString());
}
diff --git a/test/unittest/strtodtest.cpp b/test/unittest/strtodtest.cpp
index a42d96e4..807f8872 100644
--- a/test/unittest/strtodtest.cpp
+++ b/test/unittest/strtodtest.cpp
@@ -42,7 +42,7 @@ TEST(Strtod, CheckApproximationCase) {
u.u = 0x465a72e467d88 | ((static_cast<uint64_t>(-149 + kExponentBias)) << kSignificandSize);
const double b = u.d;
const uint64_t bInt = (u.u & kSignificandMask) | kHiddenBit;
- const int bExp = ((u.u & kExponentMask) >> kSignificandSize) - kExponentBias - kSignificandSize;
+ const int bExp = static_cast<int>(((u.u & kExponentMask) >> kSignificandSize) - kExponentBias - kSignificandSize);
EXPECT_DOUBLE_EQ(1.7864e-45, b);
EXPECT_EQ(RAPIDJSON_UINT64_C2(0x001465a7, 0x2e467d88), bInt);
EXPECT_EQ(-201, bExp);
@@ -91,7 +91,7 @@ TEST(Strtod, CheckApproximationCase) {
}
// Remove common power of two factor from all three scaled values
- int common_Exp2 = std::min(dS_Exp2, std::min(bS_Exp2, hS_Exp2));
+ int common_Exp2 = (std::min)(dS_Exp2, (std::min)(bS_Exp2, hS_Exp2));
dS_Exp2 -= common_Exp2;
bS_Exp2 -= common_Exp2;
hS_Exp2 -= common_Exp2;
diff --git a/test/unittest/unittest.cpp b/test/unittest/unittest.cpp
index e0e8576e..b754563e 100644
--- a/test/unittest/unittest.cpp
+++ b/test/unittest/unittest.cpp
@@ -35,6 +35,7 @@ int main(int argc, char **argv) {
#ifdef _MSC_VER
_CrtMemState memoryState = { 0 };
+ (void)memoryState;
_CrtMemCheckpoint(&memoryState);
//_CrtSetBreakAlloc(X);
//void *testWhetherMemoryLeakDetectionWorks = malloc(1);
diff --git a/test/unittest/unittest.h b/test/unittest/unittest.h
index e125bf88..4b1c293c 100644
--- a/test/unittest/unittest.h
+++ b/test/unittest/unittest.h
@@ -78,7 +78,7 @@ inline Ch* StrDup(const Ch* str) {
}
inline FILE* TempFile(char *filename) {
-#ifdef _MSC_VER
+#if defined(__WIN32__) || defined(_MSC_VER)
filename = tmpnam(filename);
// For Visual Studio, tmpnam() adds a backslash in front. Remove it.
@@ -117,7 +117,9 @@ public:
#pragma GCC diagnostic pop
#endif
-#define RAPIDJSON_ASSERT(x) if (!(x)) throw AssertException(RAPIDJSON_STRINGIFY(x))
+#ifndef RAPIDJSON_ASSERT
+#define RAPIDJSON_ASSERT(x) (!(x) ? throw AssertException(RAPIDJSON_STRINGIFY(x)) : (void)0u)
+#endif
class Random {
public:
diff --git a/test/unittest/valuetest.cpp b/test/unittest/valuetest.cpp
index 9d3609d7..307e1b06 100644
--- a/test/unittest/valuetest.cpp
+++ b/test/unittest/valuetest.cpp
@@ -94,23 +94,23 @@ TEST(Value, Traits) {
#endif
TEST(Value, MoveConstructor) {
- typedef GenericValue<UTF8<>, CrtAllocator> Value;
- Value::AllocatorType allocator;
+ typedef GenericValue<UTF8<>, CrtAllocator> V;
+ V::AllocatorType allocator;
- Value x((Value(kArrayType)));
+ V x((V(kArrayType)));
x.Reserve(4u, allocator);
x.PushBack(1, allocator).PushBack(2, allocator).PushBack(3, allocator).PushBack(4, allocator);
EXPECT_TRUE(x.IsArray());
EXPECT_EQ(4u, x.Size());
// Value y(x); // does not compile (!is_copy_constructible)
- Value y(std::move(x));
+ V y(std::move(x));
EXPECT_TRUE(x.IsNull());
EXPECT_TRUE(y.IsArray());
EXPECT_EQ(4u, y.Size());
// Value z = y; // does not compile (!is_copy_assignable)
- Value z = std::move(y);
+ V z = std::move(y);
EXPECT_TRUE(y.IsNull());
EXPECT_TRUE(z.IsArray());
EXPECT_EQ(4u, z.Size());
@@ -402,6 +402,7 @@ TEST(Value, Int) {
EXPECT_TRUE(x.IsUint64());
EXPECT_FALSE(x.IsDouble());
+ EXPECT_FALSE(x.IsFloat());
EXPECT_FALSE(x.IsNull());
EXPECT_FALSE(x.IsBool());
EXPECT_FALSE(x.IsFalse());
@@ -456,6 +457,7 @@ TEST(Value, Uint) {
EXPECT_NEAR(1234.0, x.GetDouble(), 0.0); // Number can always be cast as double but !IsDouble().
EXPECT_FALSE(x.IsDouble());
+ EXPECT_FALSE(x.IsFloat());
EXPECT_FALSE(x.IsNull());
EXPECT_FALSE(x.IsBool());
EXPECT_FALSE(x.IsFalse());
@@ -500,6 +502,7 @@ TEST(Value, Int64) {
EXPECT_TRUE(x.IsUint64());
EXPECT_FALSE(x.IsDouble());
+ EXPECT_FALSE(x.IsFloat());
EXPECT_FALSE(x.IsNull());
EXPECT_FALSE(x.IsBool());
EXPECT_FALSE(x.IsFalse());
@@ -542,8 +545,10 @@ TEST(Value, Int64) {
// Templated functions
EXPECT_TRUE(z.Is<int64_t>());
EXPECT_EQ(i, z.Get<int64_t>());
+#if 0 // signed integer underflow is undefined behaviour
EXPECT_EQ(i - 1, z.Set(i - 1).Get<int64_t>());
EXPECT_EQ(i - 2, z.Set<int64_t>(i - 2).Get<int64_t>());
+#endif
}
TEST(Value, Uint64) {
@@ -561,6 +566,7 @@ TEST(Value, Uint64) {
EXPECT_TRUE(x.IsUint64());
EXPECT_FALSE(x.IsDouble());
+ EXPECT_FALSE(x.IsFloat());
EXPECT_FALSE(x.IsNull());
EXPECT_FALSE(x.IsBool());
EXPECT_FALSE(x.IsFalse());
@@ -652,6 +658,10 @@ TEST(Value, Float) {
z.SetFloat(12.34f);
EXPECT_NEAR(12.34f, z.GetFloat(), 0.0f);
+ // Issue 573
+ z.SetInt(0);
+ EXPECT_EQ(0.0f, z.GetFloat());
+
z = 56.78f;
EXPECT_NEAR(56.78f, z.GetFloat(), 0.0f);
@@ -663,6 +673,7 @@ TEST(Value, Float) {
}
TEST(Value, IsLosslessDouble) {
+ EXPECT_TRUE(Value(0.0).IsLosslessDouble());
EXPECT_TRUE(Value(12.34).IsLosslessDouble());
EXPECT_TRUE(Value(-123).IsLosslessDouble());
EXPECT_TRUE(Value(2147483648u).IsLosslessDouble());
@@ -671,8 +682,19 @@ TEST(Value, IsLosslessDouble) {
EXPECT_TRUE(Value(RAPIDJSON_UINT64_C2(0xA0000000, 0x00000000)).IsLosslessDouble());
#endif
- EXPECT_FALSE(Value(-static_cast<int64_t>(RAPIDJSON_UINT64_C2(0x7FFFFFFF, 0xFFFFFFFF))).IsLosslessDouble());
- EXPECT_FALSE(Value(RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0xFFFFFFFF)).IsLosslessDouble());
+ EXPECT_FALSE(Value(static_cast<int64_t>(RAPIDJSON_UINT64_C2(0x7FFFFFFF, 0xFFFFFFFF))).IsLosslessDouble()); // INT64_MAX
+ EXPECT_FALSE(Value(-static_cast<int64_t>(RAPIDJSON_UINT64_C2(0x7FFFFFFF, 0xFFFFFFFF))).IsLosslessDouble()); // -INT64_MAX
+ EXPECT_TRUE(Value(-static_cast<int64_t>(RAPIDJSON_UINT64_C2(0x7FFFFFFF, 0xFFFFFFFF)) - 1).IsLosslessDouble()); // INT64_MIN
+ EXPECT_FALSE(Value(RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0xFFFFFFFF)).IsLosslessDouble()); // UINT64_MAX
+
+ EXPECT_TRUE(Value(3.4028234e38f).IsLosslessDouble()); // FLT_MAX
+ EXPECT_TRUE(Value(-3.4028234e38f).IsLosslessDouble()); // -FLT_MAX
+ EXPECT_TRUE(Value(1.17549435e-38f).IsLosslessDouble()); // FLT_MIN
+ EXPECT_TRUE(Value(-1.17549435e-38f).IsLosslessDouble()); // -FLT_MIN
+ EXPECT_TRUE(Value(1.7976931348623157e+308).IsLosslessDouble()); // DBL_MAX
+ EXPECT_TRUE(Value(-1.7976931348623157e+308).IsLosslessDouble()); // -DBL_MAX
+ EXPECT_TRUE(Value(2.2250738585072014e-308).IsLosslessDouble()); // DBL_MIN
+ EXPECT_TRUE(Value(-2.2250738585072014e-308).IsLosslessDouble()); // -DBL_MIN
}
TEST(Value, IsLosslessFloat) {
@@ -835,9 +857,46 @@ TEST(Value, String) {
}
// Issue 226: Value of string type should not point to NULL
-TEST(Value, SetStringNullException) {
- Value v;
- EXPECT_THROW(v.SetString(0, 0), AssertException);
+TEST(Value, SetStringNull) {
+
+ MemoryPoolAllocator<> allocator;
+ const char* nullPtr = 0;
+ {
+ // Construction with string type creates empty string
+ Value v(kStringType);
+ EXPECT_NE(v.GetString(), nullPtr); // non-null string returned
+ EXPECT_EQ(v.GetStringLength(), 0u);
+
+ // Construction from/setting to null without length not allowed
+ EXPECT_THROW(Value(StringRef(nullPtr)), AssertException);
+ EXPECT_THROW(Value(StringRef(nullPtr), allocator), AssertException);
+ EXPECT_THROW(v.SetString(nullPtr, allocator), AssertException);
+
+ // Non-empty length with null string is not allowed
+ EXPECT_THROW(v.SetString(nullPtr, 17u), AssertException);
+ EXPECT_THROW(v.SetString(nullPtr, 42u, allocator), AssertException);
+
+ // Setting to null string with empty length is allowed
+ v.SetString(nullPtr, 0u);
+ EXPECT_NE(v.GetString(), nullPtr); // non-null string returned
+ EXPECT_EQ(v.GetStringLength(), 0u);
+
+ v.SetNull();
+ v.SetString(nullPtr, 0u, allocator);
+ EXPECT_NE(v.GetString(), nullPtr); // non-null string returned
+ EXPECT_EQ(v.GetStringLength(), 0u);
+ }
+ // Construction with null string and empty length is allowed
+ {
+ Value v(nullPtr,0u);
+ EXPECT_NE(v.GetString(), nullPtr); // non-null string returned
+ EXPECT_EQ(v.GetStringLength(), 0u);
+ }
+ {
+ Value v(nullPtr, 0u, allocator);
+ EXPECT_NE(v.GetString(), nullPtr); // non-null string returned
+ EXPECT_EQ(v.GetStringLength(), 0u);
+ }
}
template <typename T, typename Allocator>
@@ -1111,14 +1170,18 @@ TEST(Value, ArrayHelperRangeFor) {
{
int i = 0;
- for (auto& v : x.GetArray())
- EXPECT_EQ(i++, v.GetInt());
+ for (auto& v : x.GetArray()) {
+ EXPECT_EQ(i, v.GetInt());
+ i++;
+ }
EXPECT_EQ(i, 10);
}
{
int i = 0;
- for (const auto& v : const_cast<const Value&>(x).GetArray())
- EXPECT_EQ(i++, v.GetInt());
+ for (const auto& v : const_cast<const Value&>(x).GetArray()) {
+ EXPECT_EQ(i, v.GetInt());
+ i++;
+ }
EXPECT_EQ(i, 10);
}
diff --git a/test/unittest/writertest.cpp b/test/unittest/writertest.cpp
index 238aa79e..b190c6c2 100644
--- a/test/unittest/writertest.cpp
+++ b/test/unittest/writertest.cpp
@@ -18,6 +18,12 @@
#include "rapidjson/reader.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
+#include "rapidjson/memorybuffer.h"
+
+#ifdef __clang__
+RAPIDJSON_DIAG_PUSH
+RAPIDJSON_DIAG_OFF(c++98-compat)
+#endif
using namespace rapidjson;
@@ -94,6 +100,32 @@ TEST(Writer, String) {
#endif
}
+TEST(Writer, Issue_889) {
+ char buf[100] = "Hello";
+
+ StringBuffer buffer;
+ Writer<StringBuffer> writer(buffer);
+ writer.StartArray();
+ writer.String(buf);
+ writer.EndArray();
+
+ EXPECT_STREQ("[\"Hello\"]", buffer.GetString());
+ EXPECT_TRUE(writer.IsComplete()); \
+}
+
+TEST(Writer, ScanWriteUnescapedString) {
+ const char json[] = "[\" \\\"0123456789ABCDEF\"]";
+ // ^ scanning stops here.
+ char buffer2[sizeof(json) + 32];
+
+ // Use different offset to test different alignments
+ for (int i = 0; i < 32; i++) {
+ char* p = buffer2 + i;
+ memcpy(p, json, sizeof(json));
+ TEST_ROUNDTRIP(p);
+ }
+}
+
TEST(Writer, Double) {
TEST_ROUNDTRIP("[1.2345,1.2345678,0.123456789012,1234567.8]");
TEST_ROUNDTRIP("0.0");
@@ -107,35 +139,59 @@ TEST(Writer, Double) {
}
+// UTF8 -> TargetEncoding -> UTF8
+template <typename TargetEncoding>
+void TestTranscode(const char* json) {
+ StringStream s(json);
+ GenericStringBuffer<TargetEncoding> buffer;
+ Writer<GenericStringBuffer<TargetEncoding>, UTF8<>, TargetEncoding> writer(buffer);
+ Reader reader;
+ reader.Parse(s, writer);
+
+ StringBuffer buffer2;
+ Writer<StringBuffer> writer2(buffer2);
+ GenericReader<TargetEncoding, UTF8<> > reader2;
+ GenericStringStream<TargetEncoding> s2(buffer.GetString());
+ reader2.Parse(s2, writer2);
+
+ EXPECT_STREQ(json, buffer2.GetString());
+}
+
TEST(Writer, Transcode) {
const char json[] = "{\"hello\":\"world\",\"t\":true,\"f\":false,\"n\":null,\"i\":123,\"pi\":3.1416,\"a\":[1,2,3],\"dollar\":\"\x24\",\"cents\":\"\xC2\xA2\",\"euro\":\"\xE2\x82\xAC\",\"gclef\":\"\xF0\x9D\x84\x9E\"}";
// UTF8 -> UTF16 -> UTF8
- {
- StringStream s(json);
- StringBuffer buffer;
- Writer<StringBuffer, UTF16<>, UTF8<> > writer(buffer);
- GenericReader<UTF8<>, UTF16<> > reader;
- reader.Parse(s, writer);
- EXPECT_STREQ(json, buffer.GetString());
- }
+ TestTranscode<UTF8<> >(json);
- // UTF8 -> UTF8 -> ASCII -> UTF8 -> UTF8
- {
+ // UTF8 -> ASCII -> UTF8
+ TestTranscode<ASCII<> >(json);
+
+ // UTF8 -> UTF16 -> UTF8
+ TestTranscode<UTF16<> >(json);
+
+ // UTF8 -> UTF32 -> UTF8
+ TestTranscode<UTF32<> >(json);
+
+ // UTF8 -> AutoUTF -> UTF8
+ UTFType types[] = { kUTF8, kUTF16LE , kUTF16BE, kUTF32LE , kUTF32BE };
+ for (size_t i = 0; i < 5; i++) {
StringStream s(json);
- StringBuffer buffer;
- Writer<StringBuffer, UTF8<>, ASCII<> > writer(buffer);
+ MemoryBuffer buffer;
+ AutoUTFOutputStream<unsigned, MemoryBuffer> os(buffer, types[i], true);
+ Writer<AutoUTFOutputStream<unsigned, MemoryBuffer>, UTF8<>, AutoUTF<unsigned> > writer(os);
Reader reader;
reader.Parse(s, writer);
StringBuffer buffer2;
Writer<StringBuffer> writer2(buffer2);
- GenericReader<ASCII<>, UTF8<> > reader2;
- StringStream s2(buffer.GetString());
- reader2.Parse(s2, writer2);
+ GenericReader<AutoUTF<unsigned>, UTF8<> > reader2;
+ MemoryStream s2(buffer.GetBuffer(), buffer.GetSize());
+ AutoUTFInputStream<unsigned, MemoryStream> is(s2);
+ reader2.Parse(is, writer2);
EXPECT_STREQ(json, buffer2.GetString());
}
+
}
#include <sstream>
@@ -356,8 +412,10 @@ TEST(Writer, ValidateEncoding) {
EXPECT_TRUE(writer.String("\xC2\xA2")); // Cents sign U+00A2
EXPECT_TRUE(writer.String("\xE2\x82\xAC")); // Euro sign U+20AC
EXPECT_TRUE(writer.String("\xF0\x9D\x84\x9E")); // G clef sign U+1D11E
+ EXPECT_TRUE(writer.String("\x01")); // SOH control U+0001
+ EXPECT_TRUE(writer.String("\x1B")); // Escape control U+001B
writer.EndArray();
- EXPECT_STREQ("[\"\x24\",\"\xC2\xA2\",\"\xE2\x82\xAC\",\"\xF0\x9D\x84\x9E\"]", buffer.GetString());
+ EXPECT_STREQ("[\"\x24\",\"\xC2\xA2\",\"\xE2\x82\xAC\",\"\xF0\x9D\x84\x9E\",\"\\u0001\",\"\\u001B\"]", buffer.GetString());
}
// Fail in decoding invalid UTF-8 sequence http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt
@@ -399,21 +457,52 @@ TEST(Writer, InvalidEventSequence) {
EXPECT_THROW(writer.Int(1), AssertException);
EXPECT_FALSE(writer.IsComplete());
}
-}
-extern double zero; // clang -Wmissing-variable-declarations
-double zero = 0.0; // Use global variable to prevent compiler warning
+ // { 'a' }
+ {
+ StringBuffer buffer;
+ Writer<StringBuffer> writer(buffer);
+ writer.StartObject();
+ writer.Key("a");
+ EXPECT_THROW(writer.EndObject(), AssertException);
+ EXPECT_FALSE(writer.IsComplete());
+ }
+
+ // { 'a':'b','c' }
+ {
+ StringBuffer buffer;
+ Writer<StringBuffer> writer(buffer);
+ writer.StartObject();
+ writer.Key("a");
+ writer.String("b");
+ writer.Key("c");
+ EXPECT_THROW(writer.EndObject(), AssertException);
+ EXPECT_FALSE(writer.IsComplete());
+ }
+}
TEST(Writer, NaN) {
- double nan = zero / zero;
+ double nan = std::numeric_limits<double>::quiet_NaN();
+
EXPECT_TRUE(internal::Double(nan).IsNan());
StringBuffer buffer;
- Writer<StringBuffer> writer(buffer);
- EXPECT_FALSE(writer.Double(nan));
+ {
+ Writer<StringBuffer> writer(buffer);
+ EXPECT_FALSE(writer.Double(nan));
+ }
+ {
+ Writer<StringBuffer, UTF8<>, UTF8<>, CrtAllocator, kWriteNanAndInfFlag> writer(buffer);
+ EXPECT_TRUE(writer.Double(nan));
+ EXPECT_STREQ("NaN", buffer.GetString());
+ }
+ GenericStringBuffer<UTF16<> > buffer2;
+ Writer<GenericStringBuffer<UTF16<> > > writer2(buffer2);
+ EXPECT_FALSE(writer2.Double(nan));
}
TEST(Writer, Inf) {
- double inf = 1.0 / zero;
+ double inf = std::numeric_limits<double>::infinity();
+
EXPECT_TRUE(internal::Double(inf).IsInf());
StringBuffer buffer;
{
@@ -424,6 +513,15 @@ TEST(Writer, Inf) {
Writer<StringBuffer> writer(buffer);
EXPECT_FALSE(writer.Double(-inf));
}
+ {
+ Writer<StringBuffer, UTF8<>, UTF8<>, CrtAllocator, kWriteNanAndInfFlag> writer(buffer);
+ EXPECT_TRUE(writer.Double(inf));
+ }
+ {
+ Writer<StringBuffer, UTF8<>, UTF8<>, CrtAllocator, kWriteNanAndInfFlag> writer(buffer);
+ EXPECT_TRUE(writer.Double(-inf));
+ }
+ EXPECT_STREQ("Infinity-Infinity", buffer.GetString());
}
TEST(Writer, RawValue) {
@@ -439,3 +537,25 @@ TEST(Writer, RawValue) {
EXPECT_TRUE(writer.IsComplete());
EXPECT_STREQ("{\"a\":1,\"raw\":[\"Hello\\nWorld\", 123.456]}", buffer.GetString());
}
+
+#if RAPIDJSON_HAS_CXX11_RVALUE_REFS
+static Writer<StringBuffer> WriterGen(StringBuffer &target) {
+ Writer<StringBuffer> writer(target);
+ writer.StartObject();
+ writer.Key("a");
+ writer.Int(1);
+ return writer;
+}
+
+TEST(Writer, MoveCtor) {
+ StringBuffer buffer;
+ Writer<StringBuffer> writer(WriterGen(buffer));
+ writer.EndObject();
+ EXPECT_TRUE(writer.IsComplete());
+ EXPECT_STREQ("{\"a\":1}", buffer.GetString());
+}
+#endif
+
+#ifdef __clang__
+RAPIDJSON_DIAG_POP
+#endif
diff --git a/test/valgrind.supp b/test/valgrind.supp
new file mode 100644
index 00000000..1fed18be
--- /dev/null
+++ b/test/valgrind.supp
@@ -0,0 +1,17 @@
+{
+ Suppress wcslen valgrind report 1
+ Memcheck:Cond
+ fun:__wcslen_sse2
+}
+
+{
+ Suppress wcslen valgrind report 2
+ Memcheck:Addr8
+ fun:__wcslen_sse2
+}
+
+{
+ Suppress wcslen valgrind report 3
+ Memcheck:Value8
+ fun:__wcslen_sse2
+}
diff --git a/thirdparty/gtest b/thirdparty/gtest
-Subproject 0476e154db5fab1721c2a0f32abf4aa773679b5
+Subproject 0a439623f75c029912728d80cb7f1b8b48739ca
diff --git a/travis-doxygen.sh b/travis-doxygen.sh
index e9eb6b9c..33ec6ab3 100755
--- a/travis-doxygen.sh
+++ b/travis-doxygen.sh
@@ -4,12 +4,11 @@
set -e
-DOXYGEN_VER=doxygen-1.8.7
+DOXYGEN_VER=doxygen-1.8.13
DOXYGEN_TAR=${DOXYGEN_VER}.linux.bin.tar.gz
DOXYGEN_URL="http://ftp.stack.nl/pub/users/dimitri/${DOXYGEN_TAR}"
-DOXYGEN_BIN="/usr/local/bin/doxygen"
-: ${GITHUB_REPO:="miloyip/rapidjson"}
+: ${GITHUB_REPO:="Tencent/rapidjson"}
GITHUB_HOST="github.com"
GITHUB_CLONE="git://${GITHUB_HOST}/${GITHUB_REPO}"
GITHUB_URL="https://${GITHUB_HOST}/${GITHUB_PUSH-${GITHUB_REPO}}"
@@ -42,8 +41,8 @@ abort() {
skip "Running Doxygen only for updates on 'master' branch (current: ${TRAVIS_BRANCH})."
# check for job number
-[ "${TRAVIS_JOB_NUMBER}" = "${TRAVIS_BUILD_NUMBER}.1" ] || \
- skip "Running Doxygen only on first job of build ${TRAVIS_BUILD_NUMBER} (current: ${TRAVIS_JOB_NUMBER})."
+# [ "${TRAVIS_JOB_NUMBER}" = "${TRAVIS_BUILD_NUMBER}.1" ] || \
+# skip "Running Doxygen only on first job of build ${TRAVIS_BUILD_NUMBER} (current: ${TRAVIS_JOB_NUMBER})."
# install doxygen binary distribution
doxygen_install()
@@ -66,7 +65,7 @@ gh_pages_prepare()
[ ! -d "html" ] || \
abort "Doxygen target directory already exists."
git --version
- git clone -b gh-pages "${GITHUB_CLONE}" html
+ git clone --single-branch -b gh-pages "${GITHUB_CLONE}" html
cd html
# setup git config (with defaults)
git config user.name "${GIT_NAME-travis}"