Compare commits

..

16 Commits

Author SHA1 Message Date
Jordan Bayles
9059f5cad0 Roll version numbers for 1.9.4 release (#1223) 2020-09-25 19:19:16 -07:00
Daniel Engberg
45733df96c meson: Don't specifically look for python3
Not all distributions provide Python as python3 and as Meson already depends on 3.5+ just use what Meson uses.
References: https://mesonbuild.com/Getting-meson.html
https://mesonbuild.com/Python-module.html#find_installation

Signed-off-by: Daniel Engberg <daniel.engberg.lists@pyret.net>
2020-09-01 23:02:57 -05:00
Ben Wolsieffer
5be07bdc5e Fix generation of pkg-config file with absolute includedir/libdir. (#1199) 2020-07-20 20:36:30 +08:00
Chen
bf0cfa5b46 hot fix for building static lib (#1203)
Fix #1197
2020-07-14 16:37:22 +08:00
Chen
cfc1ad72ad Enhance cmake script (#1197)
* BUILD_TYPE corresponds to Release/Debug
   but LIB_TYPE corresponds to shared/static.

* Add support to build shared, static and object lib at the same time.
2020-07-13 20:33:58 +08:00
nathanruiz
c8453d39d1 Delete nullptr Json::Value constructor (#1194)
This patch adds an explicit ctor with a std::nullptr_t argument, that is `delete`-d. This keeps Json::Value from exposing a coding error when automatically promoted to a const char* type.
2020-06-23 14:52:28 -07:00
Billy Donahue
632044ad95 Billy donahue avoid isprint (#1191)
* avoid isprint

`std::isprint` is locale-specific and the JSON-spec is not.
In particular, isprint('\t') is true in Windows CP1252.

Has bitten others, e.g. https://github.com/laurikari/tre/issues/64

Fixes #1187

* semicolon (rookie mistake!)

* Windows tab escape testing with custom locale (#1190)

Co-authored-by: Nikolay Baklicharov <thestorm.nik@gmail.com>
2020-06-11 18:14:03 -04:00
Billy Donahue
b3189a0800 avoid isprint, because it is locale specific (#1189)
* avoid isprint

`std::isprint` is locale-specific and the JSON-spec is not.
In particular, isprint('\t') is true in Windows CP1252.

Has bitten others, e.g. https://github.com/laurikari/tre/issues/64

Fixes #1187

* semicolon (rookie mistake!)
2020-06-11 17:43:44 -04:00
Jordan Bayles
9be5895985 Issue 1182: Fix fuzzing bug (#1183)
This patch fixes a fuzzing bug by resolving a bad fallthrough in the
setComment logic.

The result is that we get a proper error instead of an assert, making
the library friendlier to use and less likely to cause issue for
consumers.

See related Chromium project bug:
https://bugs.chromium.org/p/chromium/issues/detail?id=989851

Issue: 1182
2020-05-30 20:20:20 -07:00
kabeer27
6aba23f4a8 Fixes Oss-Fuzz issue: 21916 (#1180)
* Fix heap-buffer-overflow in json_reader
2020-05-29 21:50:26 +08:00
Billy Donahue
c161f4ac69 Escape control chars even if emitting UTF8 (#1178)
* Escape control chars even if emitting UTF8

See #1176
Fixes #1175

* review comments

* fix test by stopping early enough to punt on utf8-input.
2020-05-21 11:30:59 -04:00
Billy Donahue
75b360af4a spot fix #1171: isprint argument must be representable as unsigned char (#1173) 2020-05-13 18:37:02 -04:00
Rosen Penev
e36cff19f0 clang-tidy + any_of usage (#1171)
* [clang-tidy] change functions to static

Found with readability-convert-member-functions-to-static

Signed-off-by: Rosen Penev <rosenp@gmail.com>

* optimize JsonWriter::validate #1171

* do the same for json_reader

Signed-off-by: Rosen Penev <rosenp@gmail.com>

* use std::any_of

Also simplified two loops.

Signed-off-by: Rosen Penev <rosenp@gmail.com>

Co-authored-by: Billy Donahue <billy.donahue@gmail.com>
2020-05-12 19:19:36 -04:00
Edward Brey
b8cb8889aa Added current dir specifier for PowerShell (#1169)
The `./` is needed before `vcpkg install jsoncpp` when installing with PowerShell.
2020-05-08 09:00:12 +08:00
Chen
d2d4c74a03 Update README.md and add dota17 to AUTHORS list. (#1168)
* update README

* add dota17 to AUTHORS list
2020-04-30 18:05:17 +08:00
Chen
8b7ea09b80 Bump soversion to 24 (#1167) 2020-04-30 17:58:07 +08:00
36 changed files with 893 additions and 1063 deletions

1
.gitignore vendored
View File

@@ -28,7 +28,6 @@
# CMake-generated files: # CMake-generated files:
CMakeFiles/ CMakeFiles/
*.cmake
/pkg-config/jsoncpp.pc /pkg-config/jsoncpp.pc
jsoncpp_lib_static.dir/ jsoncpp_lib_static.dir/

View File

@@ -9,18 +9,18 @@ sudo: false
addons: addons:
homebrew: homebrew:
packages: packages:
- clang-format - clang-format
- meson - meson
- ninja - ninja
update: false # do not update homebrew by default update: false # do not update homebrew by default
apt: apt:
sources: sources:
- ubuntu-toolchain-r-test - ubuntu-toolchain-r-test
- llvm-toolchain-xenial-8 - llvm-toolchain-xenial-8
packages: packages:
- clang-format-8 - clang-format-8
- clang-8 - clang-8
- valgrind - valgrind
matrix: matrix:
allow_failures: allow_failures:
- os: osx - os: osx
@@ -30,74 +30,27 @@ matrix:
osx_image: xcode11 osx_image: xcode11
compiler: clang compiler: clang
env: env:
CXX="clang++" CXX="clang++"
CC="clang" CC="clang"
LIB_TYPE=static LIB_TYPE=static
BUILD_TYPE=release BUILD_TYPE=release
LANGUAGE_STANDARD="11"
script: ./.travis_scripts/meson_builder.sh script: ./.travis_scripts/meson_builder.sh
- name: Linux xenial clang meson static release testing - name: Linux xenial clang meson static release testing
os: linux os: linux
dist: xenial dist: xenial
compiler: clang compiler: clang
env: env:
CXX="clang++" CXX="clang++"
CC="clang" CC="clang"
LIB_TYPE=static LIB_TYPE=static
BUILD_TYPE=release BUILD_TYPE=release
LANGUAGE_STANDARD="11"
# before_install and install steps only needed for linux meson builds # before_install and install steps only needed for linux meson builds
before_install: before_install:
- source ./.travis_scripts/travis.before_install.${TRAVIS_OS_NAME}.sh - source ./.travis_scripts/travis.before_install.${TRAVIS_OS_NAME}.sh
install: install:
- source ./.travis_scripts/travis.install.${TRAVIS_OS_NAME}.sh - source ./.travis_scripts/travis.install.${TRAVIS_OS_NAME}.sh
script: ./.travis_scripts/meson_builder.sh script: ./.travis_scripts/meson_builder.sh
- name: Linux xenial gcc-4.6 meson static release with C++03 testing - name: Linux xenial gcc cmake coverage
os: linux
dist: xenial
compiler: gcc
addons:
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- g++-4.6
env:
CC=gcc-4.6
CXX=g++-4.6
LIB_TYPE=static
BUILD_TYPE=release
LANGUAGE_STANDARD="03"
# before_install and install steps only needed for linux meson builds
before_install:
- source ./.travis_scripts/travis.before_install.${TRAVIS_OS_NAME}.sh
install:
- source ./.travis_scripts/travis.install.${TRAVIS_OS_NAME}.sh
script: ./.travis_scripts/meson_builder.sh
- name: Linux xenial gcc-4.6 meson static release with C++98 testing
os: linux
dist: xenial
compiler: gcc
addons:
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- g++-4.6
env:
CC=gcc-4.6
CXX=g++-4.6
LIB_TYPE=static
BUILD_TYPE=release
LANGUAGE_STANDARD="98"
# before_install and install steps only needed for linux meson builds
before_install:
- source ./.travis_scripts/travis.before_install.${TRAVIS_OS_NAME}.sh
install:
- source ./.travis_scripts/travis.install.${TRAVIS_OS_NAME}.sh
script: ./.travis_scripts/meson_builder.sh
- name: Linux xenial gcc-5.4 cmake-3.12 coverage
os: linux os: linux
dist: xenial dist: xenial
compiler: gcc compiler: gcc
@@ -109,77 +62,10 @@ matrix:
BUILD_TYPE=Debug BUILD_TYPE=Debug
LIB_TYPE=shared LIB_TYPE=shared
DESTDIR=/tmp/cmake_json_cpp DESTDIR=/tmp/cmake_json_cpp
LANGUAGE_STANDARD="11"
before_install: before_install:
- pip install --user cpp-coveralls - pip install --user cpp-coveralls
script: ./.travis_scripts/cmake_builder.sh script: ./.travis_scripts/cmake_builder.sh
after_success: after_success:
- coveralls --include src/lib_json --include include - coveralls --include src/lib_json --include include
- name: Linux xenial gcc-4.6 cmake-3.12 with C++98 testing
os: linux
dist: xenial
compiler: gcc
addons:
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- g++-4.6
- valgrind
env:
CC=gcc-4.6
CXX=g++-4.6
DO_MemCheck=ON
BUILD_TOOL="Unix Makefiles"
LIB_TYPE=static
BUILD_TYPE=release
DESTDIR=/tmp/cmake_json_cpp
LANGUAGE_STANDARD="98"
before_install:
- sudo apt-get update
- sudo apt-get install python3
script: ./.travis_scripts/cmake_builder.sh
- name: Linux xenial gcc-5.4 cmake-3.12 with C++98 testing
os: linux
dist: xenial
compiler: gcc
env:
CC=gcc
CXX=g++
DO_MemCheck=ON
BUILD_TOOL="Unix Makefiles"
LIB_TYPE=static
BUILD_TYPE=release
DESTDIR=/tmp/cmake_json_cpp
LANGUAGE_STANDARD="98"
script: ./.travis_scripts/cmake_builder.sh
- name: Linux xenial clang cmake-3.12 with C++11 testing
os: linux
dist: xenial
compiler: clang
env:
CC=clang
CXX=clang++
DO_MemCheck=ON
BUILD_TOOL="Unix Makefiles"
LIB_TYPE=static
BUILD_TYPE=release
DESTDIR=/tmp/cmake_json_cpp
LANGUAGE_STANDARD="11"
script: ./.travis_scripts/cmake_builder.sh
- name: Linux xenial clang cmake-3.12 with C++98 testing
os: linux
dist: xenial
compiler: gcc
env:
CC=clang
CXX=clang++
DO_MemCheck=ON
BUILD_TOOL="Unix Makefiles"
LIB_TYPE=static
BUILD_TYPE=release
DESTDIR=/tmp/cmake_json_cpp
LANGUAGE_STANDARD="98"
script: ./.travis_scripts/cmake_builder.sh
notifications: notifications:
email: false email: false

View File

@@ -66,7 +66,7 @@ cmake --version
echo ${CXX} echo ${CXX}
${CXX} --version ${CXX} --version
_COMPILER_NAME=`basename ${CXX}` _COMPILER_NAME=`basename ${CXX}`
if [ "${BUILD_TYPE}" = "shared" ]; then if [ "${LIB_TYPE}" = "shared" ]; then
_CMAKE_BUILD_SHARED_LIBS=ON _CMAKE_BUILD_SHARED_LIBS=ON
else else
_CMAKE_BUILD_SHARED_LIBS=OFF _CMAKE_BUILD_SHARED_LIBS=OFF
@@ -98,14 +98,6 @@ else
export _BUILD_EXE=make export _BUILD_EXE=make
fi fi
# Language standard
# Set default to ON
if [ "${LANGUAGE_STANDARD}" = "98" ]; then
_BUILD_WITH_CXX_11=OFF
else
_BUILD_WITH_CXX_11=ON
fi
_BUILD_DIR_NAME="build-cmake_${BUILD_TYPE}_${LIB_TYPE}_${_COMPILER_NAME}_${_BUILD_EXE}" _BUILD_DIR_NAME="build-cmake_${BUILD_TYPE}_${LIB_TYPE}_${_COMPILER_NAME}_${_BUILD_EXE}"
mkdir -p ${_BUILD_DIR_NAME} mkdir -p ${_BUILD_DIR_NAME}
cd "${_BUILD_DIR_NAME}" cd "${_BUILD_DIR_NAME}"
@@ -120,7 +112,6 @@ cd "${_BUILD_DIR_NAME}"
-DCMAKE_BUILD_TYPE:STRING=${BUILD_TYPE} \ -DCMAKE_BUILD_TYPE:STRING=${BUILD_TYPE} \
-DBUILD_SHARED_LIBS:BOOL=${_CMAKE_BUILD_SHARED_LIBS} \ -DBUILD_SHARED_LIBS:BOOL=${_CMAKE_BUILD_SHARED_LIBS} \
-DCMAKE_INSTALL_PREFIX:PATH=${DESTDIR} \ -DCMAKE_INSTALL_PREFIX:PATH=${DESTDIR} \
-DBUILD_WITH_CXX_11=${_BUILD_WITH_CXX_11} \
../ ../
ctest -C ${BUILD_TYPE} -D ExperimentalStart -D ExperimentalConfigure -D ExperimentalBuild ${CTEST_TESTING_OPTION} -D ExperimentalSubmit ctest -C ${BUILD_TYPE} -D ExperimentalStart -D ExperimentalConfigure -D ExperimentalBuild ${CTEST_TESTING_OPTION} -D ExperimentalSubmit

View File

@@ -64,15 +64,11 @@ ninja --version
_COMPILER_NAME=`basename ${CXX}` _COMPILER_NAME=`basename ${CXX}`
_BUILD_DIR_NAME="build-${BUILD_TYPE}_${LIB_TYPE}_${_COMPILER_NAME}" _BUILD_DIR_NAME="build-${BUILD_TYPE}_${LIB_TYPE}_${_COMPILER_NAME}"
# if LANGUAGE_STANDARD not set or null, set it to 11
_CPP_STD=${LANGUAGE_STANDARD:="11"}
./.travis_scripts/run-clang-format.sh ./.travis_scripts/run-clang-format.sh
meson --fatal-meson-warnings --werror --buildtype ${BUILD_TYPE} --default-library ${LIB_TYPE} . "${_BUILD_DIR_NAME}" meson --fatal-meson-warnings --werror --buildtype ${BUILD_TYPE} --default-library ${LIB_TYPE} . "${_BUILD_DIR_NAME}"
ninja -v -j 2 -C "${_BUILD_DIR_NAME}"
cd "${_BUILD_DIR_NAME}" cd "${_BUILD_DIR_NAME}"
meson configure -Dcpp_std="c++${_CPP_STD}"
ninja -v -j 2 -C ./
meson test --no-rebuild --print-errorlogs meson test --no-rebuild --print-errorlogs
if [ "${DESTDIR}" != "/usr/local" ]; then if [ "${DESTDIR}" != "/usr/local" ]; then

View File

@@ -21,7 +21,7 @@ Braden McDorman <bmcdorman@gmail.com>
Brandon Myers <bmyers1788@gmail.com> Brandon Myers <bmyers1788@gmail.com>
Brendan Drew <brendan.drew@daqri.com> Brendan Drew <brendan.drew@daqri.com>
chason <cxchao802@gmail.com> chason <cxchao802@gmail.com>
Chen Guoping chenguopingdota@163.com chenguoping <chenguopingdota@163.com>
Chris Gilling <cgilling@iparadigms.com> Chris Gilling <cgilling@iparadigms.com>
Christopher Dawes <christopher.dawes.1981@googlemail.com> Christopher Dawes <christopher.dawes.1981@googlemail.com>
Christopher Dunn <cdunn2001@gmail.com> Christopher Dunn <cdunn2001@gmail.com>

View File

@@ -37,12 +37,20 @@ foreach(pold "") # Currently Empty
endif() endif()
endforeach() endforeach()
# Build the library with C++11 standard support, independent from other including
# software which may use a different CXX_STANDARD or CMAKE_CXX_STANDARD.
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# Ensure that CMAKE_BUILD_TYPE has a value specified for single configuration generators. # Ensure that CMAKE_BUILD_TYPE has a value specified for single configuration generators.
if(NOT DEFINED CMAKE_BUILD_TYPE AND NOT DEFINED CMAKE_CONFIGURATION_TYPES) if(NOT DEFINED CMAKE_BUILD_TYPE AND NOT DEFINED CMAKE_CONFIGURATION_TYPES)
set(CMAKE_BUILD_TYPE Release CACHE STRING set(CMAKE_BUILD_TYPE Release CACHE STRING
"Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel Coverage.") "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel Coverage.")
endif() endif()
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# use ccache if found, has to be done before project() # use ccache if found, has to be done before project()
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
@@ -53,47 +61,19 @@ if(CCACHE_EXECUTABLE)
set(CMAKE_C_COMPILER_LAUNCHER "${CCACHE_EXECUTABLE}" CACHE PATH "ccache" FORCE) set(CMAKE_C_COMPILER_LAUNCHER "${CCACHE_EXECUTABLE}" CACHE PATH "ccache" FORCE)
endif() endif()
# Note: project(VERSION XX) - the VERSION here is number, but VERSION in meson is string. project(jsoncpp
# Thus, it is better to be consistent. # Note: version must be updated in three places when doing a release. This
project(JSONCPP # annoying process ensures that amalgamate, CMake, and meson all report the
# correct version.
# 1. ./meson.build
# 2. ./include/json/version.h
# 3. ./CMakeLists.txt
# IMPORTANT: also update the PROJECT_SOVERSION!!
VERSION 1.9.4 # <major>[.<minor>[.<patch>[.<tweak>]]]
LANGUAGES CXX) LANGUAGES CXX)
# Set variable named ${VAR_NAME} to value ${VALUE} message(STATUS "JsonCpp Version: ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}")
function(set_using_dynamic_name VAR_NAME VALUE) set(PROJECT_SOVERSION 24)
set( "${VAR_NAME}" "${VALUE}" PARENT_SCOPE)
endfunction()
# Extract major, minor, patch from version text
# Parse a version string "X.Y.Z" and outputs
# version parts in ${OUPUT_PREFIX}_MAJOR, _MINOR, _PATCH.
# If parse succeeds then ${OUPUT_PREFIX}_FOUND is TRUE.
macro(jsoncpp_parse_version VERSION_TEXT OUPUT_PREFIX)
set(VERSION_REGEX "[0-9]+\\.[0-9]+\\.[0-9]+(-[a-zA-Z0-9_]+)?")
if( ${VERSION_TEXT} MATCHES ${VERSION_REGEX} )
string(REGEX MATCHALL "[0-9]+|-([A-Za-z0-9_]+)" VERSION_PARTS ${VERSION_TEXT})
list(GET VERSION_PARTS 0 ${OUPUT_PREFIX}_MAJOR)
list(GET VERSION_PARTS 1 ${OUPUT_PREFIX}_MINOR)
list(GET VERSION_PARTS 2 ${OUPUT_PREFIX}_PATCH)
set_using_dynamic_name( "${OUPUT_PREFIX}_FOUND" TRUE )
else( ${VERSION_TEXT} MATCHES ${VERSION_REGEX} )
set_using_dynamic_name( "${OUPUT_PREFIX}_FOUND" FALSE )
endif()
endmacro()
# Note: version must be updated in three places when doing a release. This
# annoying process ensures that amalgamate, CMake, and meson all report the
# correct version.
# 1. ./meson.build
# 2. ./include/json/version.h
# 3. ./CMakeLists.txt
# IMPORTANT: also update the JSONCPP_SOVERSION!!
set( JSONCPP_VERSION 00.11.0 )
set( JSONCPP_SOVERSION 23 )
jsoncpp_parse_version( ${JSONCPP_VERSION} JSONCPP_VERSION )
message(STATUS "JsonCpp Version: ${JSONCPP_VERSION_MAJOR}.${JSONCPP_VERSION_MINOR}.${JSONCPP_VERSION_PATCH}")
#if(NOT JSONCPP_VERSION_FOUND)
# message(FATAL_ERROR "Failed to parse version string properly. Expect X.Y.Z")
#endif(NOT JSONCPP_VERSION_FOUND)
option(JSONCPP_WITH_TESTS "Compile and (for jsoncpp_check) run JsonCpp test executables" ON) option(JSONCPP_WITH_TESTS "Compile and (for jsoncpp_check) run JsonCpp test executables" ON)
option(JSONCPP_WITH_POST_BUILD_UNITTEST "Automatically run unit-tests as a post build step" ON) option(JSONCPP_WITH_POST_BUILD_UNITTEST "Automatically run unit-tests as a post build step" ON)
@@ -102,37 +82,9 @@ option(JSONCPP_WITH_STRICT_ISO "Issue all the warnings demanded by strict ISO C
option(JSONCPP_WITH_PKGCONFIG_SUPPORT "Generate and install .pc files" ON) option(JSONCPP_WITH_PKGCONFIG_SUPPORT "Generate and install .pc files" ON)
option(JSONCPP_WITH_CMAKE_PACKAGE "Generate and install cmake package files" ON) option(JSONCPP_WITH_CMAKE_PACKAGE "Generate and install cmake package files" ON)
option(JSONCPP_WITH_EXAMPLE "Compile JsonCpp example" OFF) option(JSONCPP_WITH_EXAMPLE "Compile JsonCpp example" OFF)
option(BUILD_SHARED_LIBS "Build jsoncpp_lib as a shared library." OFF) option(BUILD_SHARED_LIBS "Build jsoncpp_lib as a shared library." ON)
option(BUILD_WITH_CXX_11 "Build jsoncpp_lib with C++11 standard." ON) option(BUILD_STATIC_LIBS "Build jsoncpp_lib as a static library." ON)
option(BUILD_OBJECT_LIBS "Build jsoncpp_lib as a object library." ON)
## To compatible with C++0x and C++1x
set(CMAKE_MINIMUN_CXX_STANDARD 98)
if(CMAKE_COMPILER_IS_GNUCXX)
set(CMAKE_CXX_COMPILER "/usr/bin/g++")
execute_process(COMMAND ${CMAKE_CXX_COMPILER} -dumpversion OUTPUT_VARIABLE CXX_VERSION)
if(CXX_VERSION VERSION_GREATER 4.8.0)
if(BUILD_WITH_CXX_11)
set(CMAKE_CXX_STANDARD 11)
message(STATUS "Compiled with C++11(or newer) standard!")
else()
set(CMAKE_CXX_STANDARD 98)
message(STATUS "Compiled with C++0x standard!")
endif()
else()
set(CMAKE_CXX_STANDARD 98)
message(STATUS "Compiled with C++0x standard!")
endif()
endif()
if (NOT CMAKE_CXX_STANDARD)
if (BUILD_WITH_CXX_11)
set(CMAKE_CXX_STANDARD 11)
message(STATUS "Compiled with C++1x standard!")
else()
set(CMAKE_CXX_STANDARD 98)
message(STATUS "Compiled with C++0x standard!")
endif()
endif()
# Adhere to GNU filesystem layout conventions # Adhere to GNU filesystem layout conventions
include(GNUInstallDirs) include(GNUInstallDirs)
@@ -179,7 +131,7 @@ elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
# not yet ready for -Wsign-conversion # not yet ready for -Wsign-conversion
if(JSONCPP_WITH_STRICT_ISO) if(JSONCPP_WITH_STRICT_ISO)
add_compile_options(-Wall) add_compile_options(-Wpedantic)
endif() endif()
if(JSONCPP_WITH_WARNING_AS_ERROR) if(JSONCPP_WITH_WARNING_AS_ERROR)
add_compile_options(-Werror=conversion) add_compile_options(-Werror=conversion)
@@ -198,6 +150,11 @@ if(JSONCPP_WITH_WARNING_AS_ERROR)
endif() endif()
if(JSONCPP_WITH_PKGCONFIG_SUPPORT) if(JSONCPP_WITH_PKGCONFIG_SUPPORT)
include(JoinPaths)
join_paths(libdir_for_pc_file "\${exec_prefix}" "${CMAKE_INSTALL_LIBDIR}")
join_paths(includedir_for_pc_file "\${prefix}" "${CMAKE_INSTALL_INCLUDEDIR}")
configure_file( configure_file(
"pkg-config/jsoncpp.pc.in" "pkg-config/jsoncpp.pc.in"
"pkg-config/jsoncpp.pc" "pkg-config/jsoncpp.pc"
@@ -212,7 +169,7 @@ if(JSONCPP_WITH_CMAKE_PACKAGE)
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/jsoncpp DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/jsoncpp
FILE jsoncppConfig.cmake) FILE jsoncppConfig.cmake)
write_basic_package_version_file("${CMAKE_CURRENT_BINARY_DIR}/jsoncppConfigVersion.cmake" write_basic_package_version_file("${CMAKE_CURRENT_BINARY_DIR}/jsoncppConfigVersion.cmake"
VERSION ${JSONCPP_VERSION} VERSION ${PROJECT_VERSION}
COMPATIBILITY SameMajorVersion) COMPATIBILITY SameMajorVersion)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/jsoncppConfigVersion.cmake install(FILES ${CMAKE_CURRENT_BINARY_DIR}/jsoncppConfigVersion.cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/jsoncpp) DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/jsoncpp)

View File

@@ -1,5 +1,11 @@
# JsonCpp # JsonCpp
[![badge](https://img.shields.io/badge/conan.io-jsoncpp%2F1.8.0-green.svg?logo=data:image/png;base64%2CiVBORw0KGgoAAAANSUhEUgAAAA4AAAAOCAMAAAAolt3jAAAA1VBMVEUAAABhlctjlstkl8tlmMtlmMxlmcxmmcxnmsxpnMxpnM1qnc1sn85voM91oM11oc1xotB2oc56pNF6pNJ2ptJ8ptJ8ptN9ptN8p9N5qNJ9p9N9p9R8qtOBqdSAqtOAqtR%2BrNSCrNJ/rdWDrNWCsNWCsNaJs9eLs9iRvNuVvdyVv9yXwd2Zwt6axN6dxt%2Bfx%2BChyeGiyuGjyuCjyuGly%2BGlzOKmzOGozuKoz%2BKqz%2BOq0OOv1OWw1OWw1eWx1eWy1uay1%2Baz1%2Baz1%2Bez2Oe02Oe12ee22ujUGwH3AAAAAXRSTlMAQObYZgAAAAFiS0dEAIgFHUgAAAAJcEhZcwAACxMAAAsTAQCanBgAAAAHdElNRQfgBQkREyOxFIh/AAAAiklEQVQI12NgAAMbOwY4sLZ2NtQ1coVKWNvoc/Eq8XDr2wB5Ig62ekza9vaOqpK2TpoMzOxaFtwqZua2Bm4makIM7OzMAjoaCqYuxooSUqJALjs7o4yVpbowvzSUy87KqSwmxQfnsrPISyFzWeWAXCkpMaBVIC4bmCsOdgiUKwh3JojLgAQ4ZCE0AMm2D29tZwe6AAAAAElFTkSuQmCC)](https://bintray.com/theirix/conan-repo/jsoncpp%3Atheirix)
[![badge](https://img.shields.io/badge/license-MIT-blue)](https://github.com/open-source-parsers/jsoncpp/blob/master/LICENSE)
[![badge](https://img.shields.io/badge/document-doxygen-brightgreen)](http://open-source-parsers.github.io/jsoncpp-docs/doxygen/index.html)
[![Coverage Status](https://coveralls.io/repos/github/open-source-parsers/jsoncpp/badge.svg?branch=master)](https://coveralls.io/github/open-source-parsers/jsoncpp?branch=master)
[JSON][json-org] is a lightweight data-interchange format. It can represent [JSON][json-org] is a lightweight data-interchange format. It can represent
numbers, strings, ordered sequences of values, and collections of name/value numbers, strings, ordered sequences of values, and collections of name/value
pairs. pairs.
@@ -11,13 +17,6 @@ serialization and deserialization to and from strings. It can also preserve
existing comment in unserialization/serialization steps, making it a convenient existing comment in unserialization/serialization steps, making it a convenient
format to store user input files. format to store user input files.
## Release notes
The 00.11.z branch is a new version, its major version number `00` is to shows that it is
different from branch 0.y.z and 1.y.z. The main purpose of this branch is to give users a
third choice, that is, users can only have a copy of the code, but can build in different environments,
so it can be used with old or newer compilers.
The benefit is that users can used some new features in this new branch that introduced in 1.y.z,
but can hardly applied into 0.y.z.
## Documentation ## Documentation
@@ -31,9 +30,14 @@ but can hardly applied into 0.y.z.
* `1.y.z` is built with C++11. * `1.y.z` is built with C++11.
* `0.y.z` can be used with older compilers. * `0.y.z` can be used with older compilers.
* `00.11.z` can be used with older compilers , with new features from `1.y.z` * `00.11.z` can be used both in old and new compilers.
* Major versions maintain binary-compatibility. * Major versions maintain binary-compatibility.
### Special note
The branch `00.11.z`is a new branch, its major version number `00` is to show that it is
different from `0.y.z` and `1.y.z`, the main purpose of this branch is to make a balance
between the other two branches. Thus, users can use some new features in this new branch
that introduced in 1.y.z, but can hardly applied into 0.y.z.
## Using JsonCpp in your project ## Using JsonCpp in your project
@@ -44,7 +48,7 @@ You can download and install JsonCpp using the [vcpkg](https://github.com/Micros
cd vcpkg cd vcpkg
./bootstrap-vcpkg.sh ./bootstrap-vcpkg.sh
./vcpkg integrate install ./vcpkg integrate install
vcpkg install jsoncpp ./vcpkg install jsoncpp
The JsonCpp port in vcpkg is kept up to date by Microsoft team members and community contributors. If the version is out of date, please [create an issue or pull request](https://github.com/Microsoft/vcpkg) on the vcpkg repository. The JsonCpp port in vcpkg is kept up to date by Microsoft team members and community contributors. If the version is out of date, please [create an issue or pull request](https://github.com/Microsoft/vcpkg) on the vcpkg repository.

23
cmake/JoinPaths.cmake Normal file
View File

@@ -0,0 +1,23 @@
# This module provides a function for joining paths
# known from most languages
#
# SPDX-License-Identifier: (MIT OR CC0-1.0)
# Copyright 2020 Jan Tojnar
# https://github.com/jtojnar/cmake-snips
#
# Modelled after Pythons os.path.join
# https://docs.python.org/3.7/library/os.path.html#os.path.join
# Windows not supported
function(join_paths joined_path first_path_segment)
set(temp_path "${first_path_segment}")
foreach(current_segment IN LISTS ARGN)
if(NOT ("${current_segment}" STREQUAL ""))
if(IS_ABSOLUTE "${current_segment}")
set(temp_path "${current_segment}")
else()
set(temp_path "${temp_path}/${current_segment}")
endif()
endif()
endforeach()
set(${joined_path} "${temp_path}" PARENT_SCOPE)
endfunction()

View File

@@ -1,5 +1,4 @@
#include "json/json.h" #include "json/json.h"
#include <cstdlib>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
/** \brief Parse from stream, collect comments and capture error info. /** \brief Parse from stream, collect comments and capture error info.

View File

@@ -1,5 +1,4 @@
#include "json/json.h" #include "json/json.h"
#include <cstdlib>
#include <iostream> #include <iostream>
/** /**
* \brief Parse a raw string into Value object using the CharReaderBuilder * \brief Parse a raw string into Value object using the CharReaderBuilder
@@ -11,9 +10,9 @@
* 20 * 20
*/ */
int main() { int main() {
const std::string rawJson = "{\"Age\": 20, \"Name\": \"colin\"}"; const std::string rawJson = R"({"Age": 20, "Name": "colin"})";
const int rawJsonLength = static_cast<int>(rawJson.length()); const auto rawJsonLength = static_cast<int>(rawJson.length());
JSONCPP_CONST bool shouldUseOldWay = false; constexpr bool shouldUseOldWay = false;
JSONCPP_STRING err; JSONCPP_STRING err;
Json::Value root; Json::Value root;
@@ -22,13 +21,12 @@ int main() {
reader.parse(rawJson, root); reader.parse(rawJson, root);
} else { } else {
Json::CharReaderBuilder builder; Json::CharReaderBuilder builder;
Json::CharReader* reader(builder.newCharReader()); const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
if (!reader->parse(rawJson.c_str(), rawJson.c_str() + rawJsonLength, &root, if (!reader->parse(rawJson.c_str(), rawJson.c_str() + rawJsonLength, &root,
&err)) { &err)) {
std::cout << "error" << std::endl; std::cout << "error" << std::endl;
return EXIT_FAILURE; return EXIT_FAILURE;
} }
delete reader;
} }
const std::string name = root["Name"].asString(); const std::string name = root["Name"].asString();
const int age = root["Age"].asInt(); const int age = root["Age"].asInt();

View File

@@ -12,11 +12,11 @@
int main() { int main() {
Json::Value root; Json::Value root;
Json::StreamWriterBuilder builder; Json::StreamWriterBuilder builder;
Json::StreamWriter* writer(builder.newStreamWriter()); const std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
root["Name"] = "robin"; root["Name"] = "robin";
root["Age"] = 20; root["Age"] = 20;
writer->write(root, &std::cout); writer->write(root, &std::cout);
delete writer;
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@@ -1,5 +1,4 @@
#include "json/json.h" #include "json/json.h"
#include <cstdlib>
#include <iostream> #include <iostream>
/** \brief Write a Value object to a string. /** \brief Write a Value object to a string.
* Example Usage: * Example Usage:
@@ -16,7 +15,7 @@
int main() { int main() {
Json::Value root; Json::Value root;
Json::Value data; Json::Value data;
JSONCPP_CONST bool shouldUseOldWay = false; constexpr bool shouldUseOldWay = false;
root["action"] = "run"; root["action"] = "run";
data["number"] = 1; data["number"] = 1;
root["data"] = data; root["data"] = data;

View File

@@ -58,10 +58,4 @@
} \ } \
} while (0) } while (0)
#if JSONCPP_CXX_STD_11
#define JSONCPP_STATIC_ASSERT static_assert
#else
#define JSONCPP_STATIC_ASSERT JSON_ASSERT_MESSAGE
#endif
#endif // JSON_ASSERTIONS_H_INCLUDED #endif // JSON_ASSERTIONS_H_INCLUDED

View File

@@ -5,20 +5,14 @@
#ifndef JSON_CONFIG_H_INCLUDED #ifndef JSON_CONFIG_H_INCLUDED
#define JSON_CONFIG_H_INCLUDED #define JSON_CONFIG_H_INCLUDED
#include <cstddef>
#include <cstdint>
#include <istream> #include <istream>
#include <memory> #include <memory>
#include <ostream> #include <ostream>
#include <sstream> #include <sstream>
#include <string> #include <string>
#include <type_traits>
#if JSONCPP_CXX_STD_11
#include <cstddef> // typedef ptrdiff_t
#include <cstdint> // typedef int64_t, uint64_t
#else
#include <stddef.h>
#include <stdint.h>
#endif
// If non-zero, the library uses exceptions to report bad input instead of C // If non-zero, the library uses exceptions to report bad input instead of C
// assertion macros. The default is to use exceptions. // assertion macros. The default is to use exceptions.
@@ -56,6 +50,11 @@
#define JSON_API #define JSON_API
#endif #endif
#if defined(_MSC_VER) && _MSC_VER < 1800
#error \
"ERROR: Visual Studio 12 (2013) with _MSC_VER=1800 is the oldest supported compiler with sufficient C++11 capabilities"
#endif
#if defined(_MSC_VER) && _MSC_VER < 1900 #if defined(_MSC_VER) && _MSC_VER < 1900
// As recommended at // As recommended at
// https://stackoverflow.com/questions/2915672/snprintf-and-visual-studio-2010 // https://stackoverflow.com/questions/2915672/snprintf-and-visual-studio-2010
@@ -71,41 +70,10 @@ extern JSON_API int msvc_pre1900_c99_snprintf(char* outBuf, size_t size,
// Storages, and 64 bits integer support is disabled. // Storages, and 64 bits integer support is disabled.
// #define JSON_NO_INT64 1 // #define JSON_NO_INT64 1
#if __cplusplus >= 201103L || defined(_MSC_VER) // JSONCPP_OVERRIDE is maintained for backwards compatibility of external tools.
#define JSONCPP_OP_EXPLICIT explicit // C++11 should be used directly in JSONCPP.
#else
#define JSONCPP_OP_EXPLICIT
#endif
// These Macros are maintained for backwards compatibility of external tools.
#if (defined(_MSC_VER) && _MSC_VER >= 1900) || \
(defined(__GNUC__) && __cplusplus >= 201103L) || \
(defined(__clang__) && __clang_major__ == 3 && __clang_minor__ >= 3)
#define JSONCPP_CXX_STD_11 1
#else
#define JSONCPP_CXX_STD_11 0
#endif
#if JSONCPP_CXX_STD_11
#define JSONCPP_NULL nullptr
#define JSONCPP_CONST constexpr
#define JSONCPP_CTOR_DELETE = delete
#define JSONCPP_NOEXCEPT noexcept
#define JSONCPP_OVERRIDE override #define JSONCPP_OVERRIDE override
#define JSONCPP_MOVE(value) std::move(value)
#else
#define JSONCPP_NULL NULL
#define JSONCPP_CONST const
#define JSONCPP_CTOR_DELETE
#define JSONCPP_NOEXCEPT throw()
#define JSONCPP_OVERRIDE
#define JSONCPP_MOVE(value) value
#endif
// Define *deprecated* attribute
// [[deprecated]] is in C++14 or in Visual Studio 2015 and later
// For compatibility, [[deprecated]] is not used
#ifdef __clang__ #ifdef __clang__
#if __has_extension(attribute_deprecated_with_message) #if __has_extension(attribute_deprecated_with_message)
#define JSONCPP_DEPRECATED(message) __attribute__((deprecated(message))) #define JSONCPP_DEPRECATED(message) __attribute__((deprecated(message)))
@@ -130,36 +98,33 @@ extern JSON_API int msvc_pre1900_c99_snprintf(char* outBuf, size_t size,
#endif #endif
#if !defined(JSON_IS_AMALGAMATION) #if !defined(JSON_IS_AMALGAMATION)
#if JSONCPP_CXX_STD_11
#include "allocator.h" #include "allocator.h"
#endif
#include "version.h" #include "version.h"
#endif // if !defined(JSON_IS_AMALGAMATION) #endif // if !defined(JSON_IS_AMALGAMATION)
namespace Json { namespace Json {
using Int = int;
typedef int Int; using UInt = unsigned int;
typedef unsigned int UInt;
#if defined(JSON_NO_INT64) #if defined(JSON_NO_INT64)
typedef int LargestInt; using LargestInt = int;
typedef unsigned int LargestUInt; using LargestUInt = unsigned int;
#undef JSON_HAS_INT64 #undef JSON_HAS_INT64
#else // if defined(JSON_NO_INT64) #else // if defined(JSON_NO_INT64)
// For Microsoft Visual use specific types as long long is not supported // For Microsoft Visual use specific types as long long is not supported
#if defined(_MSC_VER) // Microsoft Visual Studio #if defined(_MSC_VER) // Microsoft Visual Studio
typedef __int64 Int64; using Int64 = __int64;
typedef unsigned __int64 UInt64; using UInt64 = unsigned __int64;
#else // if defined(_MSC_VER) // Other platforms, use long long #else // if defined(_MSC_VER) // Other platforms, use long long
typedef int64_t Int64; using Int64 = int64_t;
typedef uint64_t UInt64; using UInt64 = uint64_t;
#endif // if defined(_MSC_VER) #endif // if defined(_MSC_VER)
typedef Int64 LargestInt; using LargestInt = Int64;
typedef UInt64 LargestUInt; using LargestUInt = UInt64;
#define JSON_HAS_INT64 #define JSON_HAS_INT64
#endif // if defined(JSON_NO_INT64) #endif // if defined(JSON_NO_INT64)
#if JSONCPP_CXX_STD_11
template <typename T> template <typename T>
using Allocator = using Allocator =
typename std::conditional<JSONCPP_USING_SECURE_MEMORY, SecureAllocator<T>, typename std::conditional<JSONCPP_USING_SECURE_MEMORY, SecureAllocator<T>,
@@ -173,20 +138,13 @@ using OStringStream =
String::allocator_type>; String::allocator_type>;
using IStream = std::istream; using IStream = std::istream;
using OStream = std::ostream; using OStream = std::ostream;
#else
typedef std::string String;
typedef std::istringstream IStringStream;
typedef std::ostringstream OStringStream;
typedef std::istream IStream;
typedef std::ostream OStream;
#endif // JSONCPP_CXX_STD_11
} // namespace Json } // namespace Json
// Legacy names (formerly macros). // Legacy names (formerly macros).
typedef Json::String JSONCPP_STRING; using JSONCPP_STRING = Json::String;
typedef Json::IStringStream JSONCPP_ISTRINGSTREAM; using JSONCPP_ISTRINGSTREAM = Json::IStringStream;
typedef Json::OStringStream JSONCPP_OSTRINGSTREAM; using JSONCPP_OSTRINGSTREAM = Json::OStringStream;
typedef Json::IStream JSONCPP_ISTREAM; using JSONCPP_ISTREAM = Json::IStream;
typedef Json::OStream JSONCPP_OSTREAM; using JSONCPP_OSTREAM = Json::OStream;
#endif // JSON_CONFIG_H_INCLUDED #endif // JSON_CONFIG_H_INCLUDED

View File

@@ -29,7 +29,7 @@ class CharReaderBuilder;
class Features; class Features;
// value.h // value.h
typedef unsigned int ArrayIndex; using ArrayIndex = unsigned int;
class StaticString; class StaticString;
class Path; class Path;
class PathArgument; class PathArgument;

View File

@@ -41,17 +41,17 @@ public:
Features(); Features();
/// \c true if comments are allowed. Default: \c true. /// \c true if comments are allowed. Default: \c true.
bool allowComments_; bool allowComments_{true};
/// \c true if root must be either an array or an object value. Default: \c /// \c true if root must be either an array or an object value. Default: \c
/// false. /// false.
bool strictRoot_; bool strictRoot_{false};
/// \c true if dropped null placeholders are allowed. Default: \c false. /// \c true if dropped null placeholders are allowed. Default: \c false.
bool allowDroppedNullPlaceholders_; bool allowDroppedNullPlaceholders_{false};
/// \c true if numeric object key are allowed. Default: \c false. /// \c true if numeric object key are allowed. Default: \c false.
bool allowNumericKeys_; bool allowNumericKeys_{false};
}; };
} // namespace Json } // namespace Json

View File

@@ -36,8 +36,8 @@ namespace Json {
class JSONCPP_DEPRECATED( class JSONCPP_DEPRECATED(
"Use CharReader and CharReaderBuilder instead.") JSON_API Reader { "Use CharReader and CharReaderBuilder instead.") JSON_API Reader {
public: public:
typedef char Char; using Char = char;
typedef const Char* Location; using Location = const Char*;
/** \brief An error tagged with where in the JSON text it was encountered. /** \brief An error tagged with where in the JSON text it was encountered.
* *
@@ -187,7 +187,7 @@ private:
Location extra_; Location extra_;
}; };
typedef std::deque<ErrorInfo> Errors; using Errors = std::deque<ErrorInfo>;
bool readToken(Token& token); bool readToken(Token& token);
void skipSpaces(); void skipSpaces();
@@ -210,8 +210,7 @@ private:
unsigned int& unicode); unsigned int& unicode);
bool decodeUnicodeEscapeSequence(Token& token, Location& current, bool decodeUnicodeEscapeSequence(Token& token, Location& current,
Location end, unsigned int& unicode); Location end, unsigned int& unicode);
bool addError(const String& message, Token& token, bool addError(const String& message, Token& token, Location extra = nullptr);
Location extra = JSONCPP_NULL);
bool recoverFromError(TokenType skipUntilToken); bool recoverFromError(TokenType skipUntilToken);
bool addErrorAndRecover(const String& message, Token& token, bool addErrorAndRecover(const String& message, Token& token,
TokenType skipUntilToken); TokenType skipUntilToken);
@@ -227,25 +226,25 @@ private:
static bool containsNewLine(Location begin, Location end); static bool containsNewLine(Location begin, Location end);
static String normalizeEOL(Location begin, Location end); static String normalizeEOL(Location begin, Location end);
typedef std::stack<Value*> Nodes; using Nodes = std::stack<Value*>;
Nodes nodes_; Nodes nodes_;
Errors errors_; Errors errors_;
String document_; String document_;
Location begin_; Location begin_{};
Location end_; Location end_{};
Location current_; Location current_{};
Location lastValueEnd_; Location lastValueEnd_{};
Value* lastValue_; Value* lastValue_{};
String commentsBefore_; String commentsBefore_;
Features features_; Features features_;
bool collectComments_; bool collectComments_{};
}; // Reader }; // Reader
/** Interface for reading JSON from a char array. /** Interface for reading JSON from a char array.
*/ */
class JSON_API CharReader { class JSON_API CharReader {
public: public:
virtual ~CharReader() {} virtual ~CharReader() = default;
/** \brief Read a Value from a <a HREF="http://www.json.org">JSON</a> /** \brief Read a Value from a <a HREF="http://www.json.org">JSON</a>
* document. The document must be a UTF-8 encoded string containing the * document. The document must be a UTF-8 encoded string containing the
* document to read. * document to read.
@@ -267,7 +266,7 @@ public:
class JSON_API Factory { class JSON_API Factory {
public: public:
virtual ~Factory() {} virtual ~Factory() = default;
/** \brief Allocate a CharReader via operator new(). /** \brief Allocate a CharReader via operator new().
* \throw std::exception if something goes wrong (e.g. invalid settings) * \throw std::exception if something goes wrong (e.g. invalid settings)
*/ */
@@ -333,9 +332,9 @@ public:
Json::Value settings_; Json::Value settings_;
CharReaderBuilder(); CharReaderBuilder();
~CharReaderBuilder() JSONCPP_OVERRIDE; ~CharReaderBuilder() override;
CharReader* newCharReader() const JSONCPP_OVERRIDE; CharReader* newCharReader() const override;
/** \return true if 'settings' are legal and consistent; /** \return true if 'settings' are legal and consistent;
* otherwise, indicate bad settings via 'invalid'. * otherwise, indicate bad settings via 'invalid'.

View File

@@ -13,16 +13,13 @@
// Conditional NORETURN attribute on the throw functions would: // Conditional NORETURN attribute on the throw functions would:
// a) suppress false positives from static code analysis // a) suppress false positives from static code analysis
// b) possibly improve optimization opportunities. // b) possibly improve optimization opportunities.
// For compatibility, [[noreturn]] is not used
#if !defined(JSONCPP_NORETURN) #if !defined(JSONCPP_NORETURN)
#if defined(_MSC_VER) #if defined(_MSC_VER) && _MSC_VER == 1800
#define JSONCPP_NORETURN __declspec(noreturn) #define JSONCPP_NORETURN __declspec(noreturn)
#elif defined(__GNUC__) || defined(__clang__)
#define JSONCPP_NORETURN __attribute__((noreturn))
#else #else
#define JSONCPP_NORETURN #define JSONCPP_NORETURN [[noreturn]]
#endif
#endif #endif
#endif // if !defined(JSONCPP_NORETURN)
// Support for '= delete' with template declarations was a late addition // Support for '= delete' with template declarations was a late addition
// to the c++11 standard and is rejected by clang 3.8 and Apple clang 8.2 // to the c++11 standard and is rejected by clang 3.8 and Apple clang 8.2
@@ -42,15 +39,10 @@
#endif #endif
#endif #endif
#if JSONCPP_CXX_STD_11 #include <array>
#else
#undef JSONCPP_TEMPLATE_DELETE
#define JSONCPP_TEMPLATE_DELETE
#include <string.h>
#endif
#include <exception> #include <exception>
#include <map> #include <map>
#include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
@@ -75,8 +67,8 @@ namespace Json {
class JSON_API Exception : public std::exception { class JSON_API Exception : public std::exception {
public: public:
Exception(String msg); Exception(String msg);
~Exception() JSONCPP_NOEXCEPT JSONCPP_OVERRIDE; ~Exception() noexcept override;
char const* what() const JSONCPP_NOEXCEPT JSONCPP_OVERRIDE; char const* what() const noexcept override;
protected: protected:
String msg_; String msg_;
@@ -154,7 +146,7 @@ enum PrecisionType {
*/ */
class JSON_API StaticString { class JSON_API StaticString {
public: public:
JSONCPP_OP_EXPLICIT StaticString(const char* czstring) : c_str_(czstring) {} explicit StaticString(const char* czstring) : c_str_(czstring) {}
operator const char*() const { return c_str_; } operator const char*() const { return c_str_; }
@@ -202,21 +194,21 @@ class JSON_API Value {
friend class ValueIteratorBase; friend class ValueIteratorBase;
public: public:
typedef std::vector<String> Members; using Members = std::vector<String>;
typedef ValueIterator iterator; using iterator = ValueIterator;
typedef ValueConstIterator const_iterator; using const_iterator = ValueConstIterator;
typedef Json::UInt UInt; using UInt = Json::UInt;
typedef Json::Int Int; using Int = Json::Int;
#if defined(JSON_HAS_INT64) #if defined(JSON_HAS_INT64)
typedef Json::UInt64 UInt64; using UInt64 = Json::UInt64;
typedef Json::Int64 Int64; using Int64 = Json::Int64;
#endif // defined(JSON_HAS_INT64) #endif // defined(JSON_HAS_INT64)
typedef Json::LargestInt LargestInt; using LargestInt = Json::LargestInt;
typedef Json::LargestUInt LargestUInt; using LargestUInt = Json::LargestUInt;
typedef Json::ArrayIndex ArrayIndex; using ArrayIndex = Json::ArrayIndex;
// Required for boost integration, e. g. BOOST_TEST // Required for boost integration, e. g. BOOST_TEST
typedef std::string value_type; using value_type = std::string;
#if JSON_USE_NULLREF #if JSON_USE_NULLREF
// Binary compatibility kludges, do not use. // Binary compatibility kludges, do not use.
@@ -228,35 +220,34 @@ public:
static Value const& nullSingleton(); static Value const& nullSingleton();
/// Minimum signed integer value that can be stored in a Json::Value. /// Minimum signed integer value that can be stored in a Json::Value.
static JSONCPP_CONST LargestInt minLargestInt = static constexpr LargestInt minLargestInt =
LargestInt(~(LargestUInt(-1) / 2)); LargestInt(~(LargestUInt(-1) / 2));
/// Maximum signed integer value that can be stored in a Json::Value. /// Maximum signed integer value that can be stored in a Json::Value.
static JSONCPP_CONST LargestInt maxLargestInt = static constexpr LargestInt maxLargestInt = LargestInt(LargestUInt(-1) / 2);
LargestInt(LargestUInt(-1) / 2);
/// Maximum unsigned integer value that can be stored in a Json::Value. /// Maximum unsigned integer value that can be stored in a Json::Value.
static JSONCPP_CONST LargestUInt maxLargestUInt = LargestUInt(-1); static constexpr LargestUInt maxLargestUInt = LargestUInt(-1);
/// Minimum signed int value that can be stored in a Json::Value. /// Minimum signed int value that can be stored in a Json::Value.
static JSONCPP_CONST Int minInt = Int(~(UInt(-1) / 2)); static constexpr Int minInt = Int(~(UInt(-1) / 2));
/// Maximum signed int value that can be stored in a Json::Value. /// Maximum signed int value that can be stored in a Json::Value.
static JSONCPP_CONST Int maxInt = Int(UInt(-1) / 2); static constexpr Int maxInt = Int(UInt(-1) / 2);
/// Maximum unsigned int value that can be stored in a Json::Value. /// Maximum unsigned int value that can be stored in a Json::Value.
static JSONCPP_CONST UInt maxUInt = UInt(-1); static constexpr UInt maxUInt = UInt(-1);
#if defined(JSON_HAS_INT64) #if defined(JSON_HAS_INT64)
/// Minimum signed 64 bits int value that can be stored in a Json::Value. /// Minimum signed 64 bits int value that can be stored in a Json::Value.
static JSONCPP_CONST Int64 minInt64 = Int64(~(UInt64(-1) / 2)); static constexpr Int64 minInt64 = Int64(~(UInt64(-1) / 2));
/// Maximum signed 64 bits int value that can be stored in a Json::Value. /// Maximum signed 64 bits int value that can be stored in a Json::Value.
static JSONCPP_CONST Int64 maxInt64 = Int64(UInt64(-1) / 2); static constexpr Int64 maxInt64 = Int64(UInt64(-1) / 2);
/// Maximum unsigned 64 bits int value that can be stored in a Json::Value. /// Maximum unsigned 64 bits int value that can be stored in a Json::Value.
static JSONCPP_CONST UInt64 maxUInt64 = UInt64(-1); static constexpr UInt64 maxUInt64 = UInt64(-1);
#endif // defined(JSON_HAS_INT64) #endif // defined(JSON_HAS_INT64)
/// Default precision for real value for string representation. /// Default precision for real value for string representation.
static JSONCPP_CONST UInt defaultRealPrecision = 17; static constexpr UInt defaultRealPrecision = 17;
// The constant is hard-coded because some compiler have trouble // The constant is hard-coded because some compiler have trouble
// converting Value::maxUInt64 to a double correctly (AIX/xlC). // converting Value::maxUInt64 to a double correctly (AIX/xlC).
// Assumes that UInt64 is a 64 bits integer. // Assumes that UInt64 is a 64 bits integer.
static JSONCPP_CONST double maxUInt64AsDouble = 18446744073709551615.0; static constexpr double maxUInt64AsDouble = 18446744073709551615.0;
// Workaround for bug in the NVIDIAs CUDA 9.1 nvcc compiler // Workaround for bug in the NVIDIAs CUDA 9.1 nvcc compiler
// when using gcc and clang backend compilers. CZString // when using gcc and clang backend compilers. CZString
// cannot be defined as private. See issue #486 // cannot be defined as private. See issue #486
@@ -272,14 +263,11 @@ private:
CZString(ArrayIndex index); CZString(ArrayIndex index);
CZString(char const* str, unsigned length, DuplicationPolicy allocate); CZString(char const* str, unsigned length, DuplicationPolicy allocate);
CZString(CZString const& other); CZString(CZString const& other);
#if JSONCPP_CXX_STD_11
CZString(CZString&& other); CZString(CZString&& other);
#endif
~CZString(); ~CZString();
CZString& operator=(const CZString& other); CZString& operator=(const CZString& other);
#if JSONCPP_CXX_STD_11
CZString& operator=(CZString&& other); CZString& operator=(CZString&& other);
#endif
bool operator<(CZString const& other) const; bool operator<(CZString const& other) const;
bool operator==(CZString const& other) const; bool operator==(CZString const& other) const;
ArrayIndex index() const; ArrayIndex index() const;
@@ -354,18 +342,15 @@ public:
Value(const StaticString& value); Value(const StaticString& value);
Value(const String& value); Value(const String& value);
Value(bool value); Value(bool value);
Value(std::nullptr_t ptr) = delete;
Value(const Value& other); Value(const Value& other);
#if JSONCPP_CXX_STD_11
Value(Value&& other); Value(Value&& other);
#endif
~Value(); ~Value();
/// \note Overwrite existing comments. To preserve comments, use /// \note Overwrite existing comments. To preserve comments, use
/// #swapPayload(). /// #swapPayload().
Value& operator=(const Value& other); Value& operator=(const Value& other);
#if JSONCPP_CXX_STD_11
Value& operator=(Value&& other); Value& operator=(Value&& other);
#endif
/// Swap everything. /// Swap everything.
void swap(Value& other); void swap(Value& other);
@@ -437,7 +422,7 @@ public:
bool empty() const; bool empty() const;
/// Return !isNull() /// Return !isNull()
JSONCPP_OP_EXPLICIT operator bool() const; explicit operator bool() const;
/// Remove all object members and array elements. /// Remove all object members and array elements.
/// \pre type() is arrayValue, objectValue, or nullValue /// \pre type() is arrayValue, objectValue, or nullValue
@@ -478,15 +463,11 @@ public:
/// ///
/// Equivalent to jsonvalue[jsonvalue.size()] = value; /// Equivalent to jsonvalue[jsonvalue.size()] = value;
Value& append(const Value& value); Value& append(const Value& value);
#if JSONCPP_CXX_STD_11
Value& append(Value&& value); Value& append(Value&& value);
#endif
/// \brief Insert value in array at specific index /// \brief Insert value in array at specific index
bool insert(ArrayIndex index, const Value& newValue); bool insert(ArrayIndex index, const Value& newValue);
#if JSONCPP_CXX_STD_11
bool insert(ArrayIndex index, Value&& newValue); bool insert(ArrayIndex index, Value&& newValue);
#endif
/// Access an object value by name, create a null member if it does not exist. /// Access an object value by name, create a null member if it does not exist.
/// \note Because of our implementation, keys are limited to 2^30 -1 chars. /// \note Because of our implementation, keys are limited to 2^30 -1 chars.
@@ -582,11 +563,15 @@ public:
/// \deprecated Always pass len. /// \deprecated Always pass len.
JSONCPP_DEPRECATED("Use setComment(String const&) instead.") JSONCPP_DEPRECATED("Use setComment(String const&) instead.")
void setComment(const char* comment, CommentPlacement placement); void setComment(const char* comment, CommentPlacement placement) {
setComment(String(comment, strlen(comment)), placement);
}
/// Comments must be //... or /* ... */ /// Comments must be //... or /* ... */
void setComment(const char* comment, size_t len, CommentPlacement placement); void setComment(const char* comment, size_t len, CommentPlacement placement) {
setComment(String(comment, len), placement);
}
/// Comments must be //... or /* ... */ /// Comments must be //... or /* ... */
void setComment(const String& comment, CommentPlacement placement); void setComment(String comment, CommentPlacement placement);
bool hasComment(CommentPlacement placement) const; bool hasComment(CommentPlacement placement) const;
/// Include delimiters and embedded newlines. /// Include delimiters and embedded newlines.
String getComment(CommentPlacement placement) const; String getComment(CommentPlacement placement) const;
@@ -648,15 +633,18 @@ private:
class Comments { class Comments {
public: public:
Comments() {} Comments() = default;
Comments(const Comments& that); Comments(const Comments& that);
Comments(Comments&& that);
Comments& operator=(const Comments& that); Comments& operator=(const Comments& that);
Comments& operator=(Comments&& that);
bool has(CommentPlacement slot) const; bool has(CommentPlacement slot) const;
String get(CommentPlacement slot) const; String get(CommentPlacement slot) const;
void set(CommentPlacement slot, String s); void set(CommentPlacement slot, String comment);
private: private:
String ptr_[numberOfCommentPlacement]; using Array = std::array<String, numberOfCommentPlacement>;
std::unique_ptr<Array> ptr_;
}; };
Comments comments_; Comments comments_;
@@ -711,8 +699,8 @@ public:
private: private:
enum Kind { kindNone = 0, kindIndex, kindKey }; enum Kind { kindNone = 0, kindIndex, kindKey };
String key_; String key_;
ArrayIndex index_; ArrayIndex index_{};
Kind kind_; Kind kind_{kindNone};
}; };
/** \brief Experimental and untested: represents a "path" to access a node. /** \brief Experimental and untested: represents a "path" to access a node.
@@ -741,8 +729,8 @@ public:
Value& make(Value& root) const; Value& make(Value& root) const;
private: private:
typedef std::vector<const PathArgument*> InArgs; using InArgs = std::vector<const PathArgument*>;
typedef std::vector<PathArgument> Args; using Args = std::vector<PathArgument>;
void makePath(const String& path, const InArgs& in); void makePath(const String& path, const InArgs& in);
void addPathInArg(const String& path, const InArgs& in, void addPathInArg(const String& path, const InArgs& in,
@@ -757,10 +745,10 @@ private:
*/ */
class JSON_API ValueIteratorBase { class JSON_API ValueIteratorBase {
public: public:
typedef std::bidirectional_iterator_tag iterator_category; using iterator_category = std::bidirectional_iterator_tag;
typedef unsigned int size_t; using size_t = unsigned int;
typedef int difference_type; using difference_type = int;
typedef ValueIteratorBase SelfType; using SelfType = ValueIteratorBase;
bool operator==(const SelfType& other) const { return isEqual(other); } bool operator==(const SelfType& other) const { return isEqual(other); }
@@ -817,14 +805,13 @@ protected:
private: private:
Value::ObjectValues::iterator current_; Value::ObjectValues::iterator current_;
// Indicates that iterator is for a null value. // Indicates that iterator is for a null value.
bool isNull_; bool isNull_{true};
public: public:
// For some reason, BORLAND needs these at the end, rather // For some reason, BORLAND needs these at the end, rather
// than earlier. No idea why. // than earlier. No idea why.
ValueIteratorBase(); ValueIteratorBase();
JSONCPP_OP_EXPLICIT explicit ValueIteratorBase(const Value::ObjectValues::iterator& current);
ValueIteratorBase(const Value::ObjectValues::iterator& current);
}; };
/** \brief const iterator for object and array value. /** \brief const iterator for object and array value.
@@ -834,12 +821,12 @@ class JSON_API ValueConstIterator : public ValueIteratorBase {
friend class Value; friend class Value;
public: public:
typedef const Value value_type; using value_type = const Value;
// typedef unsigned int size_t; // typedef unsigned int size_t;
// typedef int difference_type; // typedef int difference_type;
typedef const Value& reference; using reference = const Value&;
typedef const Value* pointer; using pointer = const Value*;
typedef ValueConstIterator SelfType; using SelfType = ValueConstIterator;
ValueConstIterator(); ValueConstIterator();
ValueConstIterator(ValueIterator const& other); ValueConstIterator(ValueIterator const& other);
@@ -847,8 +834,7 @@ public:
private: private:
/*! \internal Use by Value to create an iterator. /*! \internal Use by Value to create an iterator.
*/ */
JSONCPP_OP_EXPLICIT explicit ValueConstIterator(const Value::ObjectValues::iterator& current);
ValueConstIterator(const Value::ObjectValues::iterator& current);
public: public:
SelfType& operator=(const ValueIteratorBase& other); SelfType& operator=(const ValueIteratorBase& other);
@@ -886,22 +872,21 @@ class JSON_API ValueIterator : public ValueIteratorBase {
friend class Value; friend class Value;
public: public:
typedef Value value_type; using value_type = Value;
typedef unsigned int size_t; using size_t = unsigned int;
typedef int difference_type; using difference_type = int;
typedef Value& reference; using reference = Value&;
typedef Value* pointer; using pointer = Value*;
typedef ValueIterator SelfType; using SelfType = ValueIterator;
ValueIterator(); ValueIterator();
JSONCPP_OP_EXPLICIT ValueIterator(const ValueConstIterator& other); explicit ValueIterator(const ValueConstIterator& other);
ValueIterator(const ValueIterator& other); ValueIterator(const ValueIterator& other);
private: private:
/*! \internal Use by Value to create an iterator. /*! \internal Use by Value to create an iterator.
*/ */
JSONCPP_OP_EXPLICIT explicit ValueIterator(const Value::ObjectValues::iterator& current);
ValueIterator(const Value::ObjectValues::iterator& current);
public: public:
SelfType& operator=(const SelfType& other); SelfType& operator=(const SelfType& other);

View File

@@ -9,10 +9,10 @@
// 3. /CMakeLists.txt // 3. /CMakeLists.txt
// IMPORTANT: also update the SOVERSION!! // IMPORTANT: also update the SOVERSION!!
#define JSONCPP_VERSION_STRING "00.11.0" #define JSONCPP_VERSION_STRING "1.9.4"
#define JSONCPP_VERSION_MAJOR 00 #define JSONCPP_VERSION_MAJOR 1
#define JSONCPP_VERSION_MINOR 11 #define JSONCPP_VERSION_MINOR 9
#define JSONCPP_VERSION_PATCH 0 #define JSONCPP_VERSION_PATCH 3
#define JSONCPP_VERSION_QUALIFIER #define JSONCPP_VERSION_QUALIFIER
#define JSONCPP_VERSION_HEXA \ #define JSONCPP_VERSION_HEXA \
((JSONCPP_VERSION_MAJOR << 24) | (JSONCPP_VERSION_MINOR << 16) | \ ((JSONCPP_VERSION_MAJOR << 24) | (JSONCPP_VERSION_MINOR << 16) | \

View File

@@ -119,12 +119,12 @@ public:
Json::Value settings_; Json::Value settings_;
StreamWriterBuilder(); StreamWriterBuilder();
~StreamWriterBuilder() JSONCPP_OVERRIDE; ~StreamWriterBuilder() override;
/** /**
* \throw std::exception if something goes wrong (e.g. invalid settings) * \throw std::exception if something goes wrong (e.g. invalid settings)
*/ */
StreamWriter* newStreamWriter() const JSONCPP_OVERRIDE; StreamWriter* newStreamWriter() const override;
/** \return true if 'settings' are legal and consistent; /** \return true if 'settings' are legal and consistent;
* otherwise, indicate bad settings via 'invalid'. * otherwise, indicate bad settings via 'invalid'.
@@ -169,7 +169,7 @@ class JSONCPP_DEPRECATED("Use StreamWriterBuilder instead") JSON_API FastWriter
: public Writer { : public Writer {
public: public:
FastWriter(); FastWriter();
~FastWriter() JSONCPP_OVERRIDE {} ~FastWriter() override = default;
void enableYAMLCompatibility(); void enableYAMLCompatibility();
@@ -183,15 +183,15 @@ public:
void omitEndingLineFeed(); void omitEndingLineFeed();
public: // overridden from Writer public: // overridden from Writer
String write(const Value& root) JSONCPP_OVERRIDE; String write(const Value& root) override;
private: private:
void writeValue(const Value& value); void writeValue(const Value& value);
String document_; String document_;
bool yamlCompatibilityEnabled_; bool yamlCompatibilityEnabled_{false};
bool dropNullPlaceholders_; bool dropNullPlaceholders_{false};
bool omitEndingLineFeed_; bool omitEndingLineFeed_{false};
}; };
#if defined(_MSC_VER) #if defined(_MSC_VER)
#pragma warning(pop) #pragma warning(pop)
@@ -229,14 +229,14 @@ class JSONCPP_DEPRECATED("Use StreamWriterBuilder instead") JSON_API
StyledWriter : public Writer { StyledWriter : public Writer {
public: public:
StyledWriter(); StyledWriter();
~StyledWriter() JSONCPP_OVERRIDE {} ~StyledWriter() override = default;
public: // overridden from Writer public: // overridden from Writer
/** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format. /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
* \param root Value to serialize. * \param root Value to serialize.
* \return String containing the JSON document that represents the root value. * \return String containing the JSON document that represents the root value.
*/ */
String write(const Value& root) JSONCPP_OVERRIDE; String write(const Value& root) override;
private: private:
void writeValue(const Value& value); void writeValue(const Value& value);
@@ -252,14 +252,14 @@ private:
static bool hasCommentForValue(const Value& value); static bool hasCommentForValue(const Value& value);
static String normalizeEOL(const String& text); static String normalizeEOL(const String& text);
typedef std::vector<String> ChildValues; using ChildValues = std::vector<String>;
ChildValues childValues_; ChildValues childValues_;
String document_; String document_;
String indentString_; String indentString_;
unsigned int rightMargin_; unsigned int rightMargin_{74};
unsigned int indentSize_; unsigned int indentSize_{3};
bool addChildValues_; bool addChildValues_{false};
}; };
#if defined(_MSC_VER) #if defined(_MSC_VER)
#pragma warning(pop) #pragma warning(pop)
@@ -301,7 +301,7 @@ public:
* \param indentation Each level will be indented by this amount extra. * \param indentation Each level will be indented by this amount extra.
*/ */
StyledStreamWriter(String indentation = "\t"); StyledStreamWriter(String indentation = "\t");
~StyledStreamWriter() {} ~StyledStreamWriter() = default;
public: public:
/** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format. /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
@@ -326,12 +326,12 @@ private:
static bool hasCommentForValue(const Value& value); static bool hasCommentForValue(const Value& value);
static String normalizeEOL(const String& text); static String normalizeEOL(const String& text);
typedef std::vector<String> ChildValues; using ChildValues = std::vector<String>;
ChildValues childValues_; ChildValues childValues_;
OStream* document_; OStream* document_;
String indentString_; String indentString_;
unsigned int rightMargin_; unsigned int rightMargin_{74};
String indentation_; String indentation_;
bool addChildValues_ : 1; bool addChildValues_ : 1;
bool indented_ : 1; bool indented_ : 1;
@@ -348,7 +348,7 @@ String JSON_API valueToString(LargestInt value);
String JSON_API valueToString(LargestUInt value); String JSON_API valueToString(LargestUInt value);
String JSON_API valueToString( String JSON_API valueToString(
double value, unsigned int precision = Value::defaultRealPrecision, double value, unsigned int precision = Value::defaultRealPrecision,
PrecisionType precisionType = significantDigits); PrecisionType precisionType = PrecisionType::significantDigits);
String JSON_API valueToString(bool value); String JSON_API valueToString(bool value);
String JSON_API valueToQuotedString(const char* value); String JSON_API valueToQuotedString(const char* value);

View File

@@ -9,7 +9,7 @@ project(
# 2. /include/json/version.h # 2. /include/json/version.h
# 3. /CMakeLists.txt # 3. /CMakeLists.txt
# IMPORTANT: also update the SOVERSION!! # IMPORTANT: also update the SOVERSION!!
version : '00.11.0', version : '1.9.4',
default_options : [ default_options : [
'buildtype=release', 'buildtype=release',
'cpp_std=c++11', 'cpp_std=c++11',
@@ -50,7 +50,7 @@ jsoncpp_lib = library(
'src/lib_json/json_value.cpp', 'src/lib_json/json_value.cpp',
'src/lib_json/json_writer.cpp', 'src/lib_json/json_writer.cpp',
]), ]),
soversion : 23, soversion : 24,
install : true, install : true,
include_directories : jsoncpp_include_directories, include_directories : jsoncpp_include_directories,
cpp_args: dll_export_flag) cpp_args: dll_export_flag)
@@ -73,7 +73,7 @@ if meson.is_subproject() or not get_option('tests')
subdir_done() subdir_done()
endif endif
python = import('python').find_installation('python3') python = import('python').find_installation()
jsoncpp_test = executable( jsoncpp_test = executable(
'jsoncpp_test', files([ 'jsoncpp_test', files([

View File

@@ -1,7 +1,7 @@
prefix=@CMAKE_INSTALL_PREFIX@ prefix=@CMAKE_INSTALL_PREFIX@
exec_prefix=@CMAKE_INSTALL_PREFIX@ exec_prefix=@CMAKE_INSTALL_PREFIX@
libdir=${exec_prefix}/@CMAKE_INSTALL_LIBDIR@ libdir=@libdir_for_pc_file@
includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@ includedir=@includedir_for_pc_file@
Name: jsoncpp Name: jsoncpp
Description: A C++ library for interacting with JSON Description: A C++ library for interacting with JSON

View File

@@ -19,8 +19,10 @@ if(BUILD_SHARED_LIBS)
else() else()
add_definitions(-DJSON_DLL) add_definitions(-DJSON_DLL)
endif() endif()
target_link_libraries(jsontestrunner_exe jsoncpp_lib)
else()
target_link_libraries(jsontestrunner_exe jsoncpp_static)
endif() endif()
target_link_libraries(jsontestrunner_exe jsoncpp_lib)
set_target_properties(jsontestrunner_exe PROPERTIES OUTPUT_NAME jsontestrunner_exe) set_target_properties(jsontestrunner_exe PROPERTIES OUTPUT_NAME jsontestrunner_exe)

View File

@@ -24,9 +24,7 @@ struct Options {
Json::String path; Json::String path;
Json::Features features; Json::Features features;
bool parseOnly; bool parseOnly;
using writeFuncType = Json::String (*)(Json::Value const&);
typedef Json::String (*writeFuncType)(Json::Value const&);
writeFuncType write; writeFuncType write;
}; };
@@ -59,11 +57,11 @@ static Json::String readInputTestFile(const char* path) {
if (!file) if (!file)
return ""; return "";
fseek(file, 0, SEEK_END); fseek(file, 0, SEEK_END);
long const size = ftell(file); auto const size = ftell(file);
size_t const usize = static_cast<size_t>(size); auto const usize = static_cast<size_t>(size);
fseek(file, 0, SEEK_SET); fseek(file, 0, SEEK_SET);
char* buffer = new char[usize + 1]; auto buffer = new char[size + 1];
buffer[usize] = 0; buffer[size] = 0;
Json::String text; Json::String text;
if (fread(buffer, 1, usize, file) == usize) if (fread(buffer, 1, usize, file) == usize)
text = buffer; text = buffer;
@@ -113,9 +111,7 @@ static void printValueTree(FILE* fout, Json::Value& value,
Json::Value::Members members(value.getMemberNames()); Json::Value::Members members(value.getMemberNames());
std::sort(members.begin(), members.end()); std::sort(members.begin(), members.end());
Json::String suffix = *(path.end() - 1) == '.' ? "" : "."; Json::String suffix = *(path.end() - 1) == '.' ? "" : ".";
for (Json::Value::Members::const_iterator it = members.begin(); for (const auto& name : members) {
it != members.end(); it++) {
const Json::String& name = *it;
printValueTree(fout, value[name], path + suffix + name); printValueTree(fout, value[name], path + suffix + name);
} }
} break; } break;
@@ -142,7 +138,7 @@ static int parseAndSaveValueTree(const Json::String& input,
features.allowDroppedNullPlaceholders_; features.allowDroppedNullPlaceholders_;
builder.settings_["allowNumericKeys"] = features.allowNumericKeys_; builder.settings_["allowNumericKeys"] = features.allowNumericKeys_;
Json::CharReader* reader(builder.newCharReader()); std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
Json::String errors; Json::String errors;
const bool parsingSuccessful = const bool parsingSuccessful =
reader->parse(input.data(), input.data() + input.size(), root, &errors); reader->parse(input.data(), input.data() + input.size(), root, &errors);
@@ -152,7 +148,7 @@ static int parseAndSaveValueTree(const Json::String& input,
<< errors << std::endl; << errors << std::endl;
return 1; return 1;
} }
delete reader;
// We may instead check the legacy implementation (to ensure it doesn't // We may instead check the legacy implementation (to ensure it doesn't
// randomly get broken). // randomly get broken).
} else { } else {

View File

@@ -50,7 +50,7 @@ set(PUBLIC_HEADERS
source_group("Public API" FILES ${PUBLIC_HEADERS}) source_group("Public API" FILES ${PUBLIC_HEADERS})
set(jsoncpp_sources set(JSONCPP_SOURCES
json_tool.h json_tool.h
json_reader.cpp json_reader.cpp
json_valueiterator.inl json_valueiterator.inl
@@ -65,33 +65,10 @@ else()
set(INSTALL_EXPORT) set(INSTALL_EXPORT)
endif() endif()
if(BUILD_SHARED_LIBS)
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.12.0)
add_compile_definitions(JSON_DLL_BUILD)
else()
add_definitions(-DJSON_DLL_BUILD)
endif()
endif()
add_library(jsoncpp_lib ${PUBLIC_HEADERS} ${jsoncpp_sources})
set_target_properties( jsoncpp_lib PROPERTIES
OUTPUT_NAME jsoncpp
VERSION ${JSONCPP_VERSION}
SOVERSION ${JSONCPP_SOVERSION}
POSITION_INDEPENDENT_CODE ON
)
# Set library's runtime search path on OSX
if(APPLE)
set_target_properties(jsoncpp_lib PROPERTIES INSTALL_RPATH "@loader_path/.")
endif()
# Specify compiler features required when compiling a given target. # Specify compiler features required when compiling a given target.
# See https://cmake.org/cmake/help/v3.1/prop_gbl/CMAKE_CXX_KNOWN_FEATURES.html#prop_gbl:CMAKE_CXX_KNOWN_FEATURES # See https://cmake.org/cmake/help/v3.1/prop_gbl/CMAKE_CXX_KNOWN_FEATURES.html#prop_gbl:CMAKE_CXX_KNOWN_FEATURES
# for complete list of features available # for complete list of features available
if(CMAKE_CXX_STANDARD EQUAL "11") list(APPEND REQUIRED_FEATURES
target_compile_features(jsoncpp_lib PUBLIC
cxx_std_11 # Compiler mode is aware of C++ 11. cxx_std_11 # Compiler mode is aware of C++ 11.
#MSVC 1900 cxx_alignas # Alignment control alignas, as defined in N2341. #MSVC 1900 cxx_alignas # Alignment control alignas, as defined in N2341.
#MSVC 1900 cxx_alignof # Alignment control alignof, as defined in N2341. #MSVC 1900 cxx_alignof # Alignment control alignof, as defined in N2341.
@@ -137,21 +114,107 @@ target_compile_features(jsoncpp_lib PUBLIC
cxx_variadic_macros # Variadic macros, as defined in N1653. cxx_variadic_macros # Variadic macros, as defined in N1653.
cxx_variadic_templates # Variadic templates, as defined in N2242. cxx_variadic_templates # Variadic templates, as defined in N2242.
) )
else()
set(CMAKE_CXX_STANDARD 98)
target_compile_features(jsoncpp_lib PUBLIC) if(BUILD_SHARED_LIBS)
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.12.0)
add_compile_definitions(JSON_DLL_BUILD)
else()
add_definitions(-DJSON_DLL_BUILD)
endif()
set(SHARED_LIB ${PROJECT_NAME}_lib)
add_library(${SHARED_LIB} SHARED ${PUBLIC_HEADERS} ${JSONCPP_SOURCES})
set_target_properties(${SHARED_LIB} PROPERTIES
OUTPUT_NAME jsoncpp
VERSION ${PROJECT_VERSION}
SOVERSION ${PROJECT_SOVERSION}
POSITION_INDEPENDENT_CODE ON
)
# Set library's runtime search path on OSX
if(APPLE)
set_target_properties(${SHARED_LIB} PROPERTIES INSTALL_RPATH "@loader_path/.")
endif()
target_compile_features(${SHARED_LIB} PUBLIC ${REQUIRED_FEATURES})
if(NOT CMAKE_VERSION VERSION_LESS 2.8.11)
target_include_directories(${SHARED_LIB} PUBLIC
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/${JSONCPP_INCLUDE_DIR}>
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/include/json>
)
endif()
list(APPEND CMAKE_TARGETS ${SHARED_LIB})
endif() endif()
install(TARGETS jsoncpp_lib ${INSTALL_EXPORT} if(BUILD_STATIC_LIBS)
set(STATIC_LIB ${PROJECT_NAME}_static)
add_library(${STATIC_LIB} STATIC ${PUBLIC_HEADERS} ${JSONCPP_SOURCES})
# avoid name clashes on windows as the shared import lib is alse named jsoncpp.lib
if(NOT DEFINED STATIC_SUFFIX AND BUILD_SHARED_LIBS)
set(STATIC_SUFFIX "_static")
endif()
set_target_properties(${STATIC_LIB} PROPERTIES
OUTPUT_NAME jsoncpp${STATIC_SUFFIX}
VERSION ${PROJECT_VERSION}
)
# Set library's runtime search path on OSX
if(APPLE)
set_target_properties(${STATIC_LIB} PROPERTIES INSTALL_RPATH "@loader_path/.")
endif()
target_compile_features(${STATIC_LIB} PUBLIC ${REQUIRED_FEATURES})
if(NOT CMAKE_VERSION VERSION_LESS 2.8.11)
target_include_directories(${STATIC_LIB} PUBLIC
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/${JSONCPP_INCLUDE_DIR}>
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/include/json>
)
endif()
list(APPEND CMAKE_TARGETS ${STATIC_LIB})
endif()
if(BUILD_OBJECT_LIBS)
set(OBJECT_LIB ${PROJECT_NAME}_object)
add_library(${OBJECT_LIB} OBJECT ${PUBLIC_HEADERS} ${JSONCPP_SOURCES})
set_target_properties(${OBJECT_LIB} PROPERTIES
OUTPUT_NAME jsoncpp
VERSION ${PROJECT_VERSION}
SOVERSION ${PROJECT_SOVERSION}
POSITION_INDEPENDENT_CODE ON
)
# Set library's runtime search path on OSX
if(APPLE)
set_target_properties(${OBJECT_LIB} PROPERTIES INSTALL_RPATH "@loader_path/.")
endif()
target_compile_features(${OBJECT_LIB} PUBLIC ${REQUIRED_FEATURES})
if(NOT CMAKE_VERSION VERSION_LESS 2.8.11)
target_include_directories(${OBJECT_LIB} PUBLIC
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/${JSONCPP_INCLUDE_DIR}>
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/include/json>
)
endif()
list(APPEND CMAKE_TARGETS ${OBJECT_LIB})
endif()
install(TARGETS ${CMAKE_TARGETS} ${INSTALL_EXPORT}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
OBJECTS DESTINATION ${CMAKE_INSTALL_LIBDIR}
) )
if(NOT CMAKE_VERSION VERSION_LESS 2.8.11)
target_include_directories(jsoncpp_lib PUBLIC
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/${JSONCPP_INCLUDE_DIR}>
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/include/json>
)
endif()

View File

@@ -10,6 +10,7 @@
#include <json/reader.h> #include <json/reader.h>
#include <json/value.h> #include <json/value.h>
#endif // if !defined(JSON_IS_AMALGAMATION) #endif // if !defined(JSON_IS_AMALGAMATION)
#include <algorithm>
#include <cassert> #include <cassert>
#include <cstring> #include <cstring>
#include <iostream> #include <iostream>
@@ -51,15 +52,18 @@ static size_t const stackLimit_g =
namespace Json { namespace Json {
typedef CharReader* CharReaderPtr; #if __cplusplus >= 201103L || (defined(_CPPLIB_VER) && _CPPLIB_VER >= 520)
using CharReaderPtr = std::unique_ptr<CharReader>;
#else
using CharReaderPtr = std::auto_ptr<CharReader>;
#endif
// Implementation of class Features // Implementation of class Features
// //////////////////////////////// // ////////////////////////////////
Features::Features() Features::Features() = default;
: allowComments_(true), strictRoot_(false),
allowDroppedNullPlaceholders_(false), allowNumericKeys_(false) {} Features Features::all() { return {}; }
Features Features::all() { return Features(); }
Features Features::strictMode() { Features Features::strictMode() {
Features features; Features features;
@@ -74,24 +78,15 @@ Features Features::strictMode() {
// //////////////////////////////// // ////////////////////////////////
bool Reader::containsNewLine(Reader::Location begin, Reader::Location end) { bool Reader::containsNewLine(Reader::Location begin, Reader::Location end) {
for (; begin < end; ++begin) return std::any_of(begin, end, [](char b) { return b == '\n' || b == '\r'; });
if (*begin == '\n' || *begin == '\r')
return true;
return false;
} }
// Class Reader // Class Reader
// ////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////
Reader::Reader() Reader::Reader() : features_(Features::all()) {}
: errors_(), document_(), begin_(), end_(), current_(), lastValueEnd_(),
lastValue_(), commentsBefore_(), features_(Features::all()),
collectComments_() {}
Reader::Reader(const Features& features) Reader::Reader(const Features& features) : features_(features) {}
: errors_(), document_(), begin_(), end_(), current_(), lastValueEnd_(),
lastValue_(), commentsBefore_(), features_(features), collectComments_() {
}
bool Reader::parse(const std::string& document, Value& root, bool Reader::parse(const std::string& document, Value& root,
bool collectComments) { bool collectComments) {
@@ -124,8 +119,8 @@ bool Reader::parse(const char* beginDoc, const char* endDoc, Value& root,
end_ = endDoc; end_ = endDoc;
collectComments_ = collectComments; collectComments_ = collectComments;
current_ = begin_; current_ = begin_;
lastValueEnd_ = JSONCPP_NULL; lastValueEnd_ = nullptr;
lastValue_ = JSONCPP_NULL; lastValue_ = nullptr;
commentsBefore_.clear(); commentsBefore_.clear();
errors_.clear(); errors_.clear();
while (!nodes_.empty()) while (!nodes_.empty())
@@ -379,7 +374,7 @@ void Reader::addComment(Location begin, Location end,
assert(collectComments_); assert(collectComments_);
const String& normalized = normalizeEOL(begin, end); const String& normalized = normalizeEOL(begin, end);
if (placement == commentAfterOnSameLine) { if (placement == commentAfterOnSameLine) {
assert(lastValue_ != JSONCPP_NULL); assert(lastValue_ != nullptr);
lastValue_->setComment(normalized, placement); lastValue_->setComment(normalized, placement);
} else { } else {
commentsBefore_ += normalized; commentsBefore_ += normalized;
@@ -568,7 +563,7 @@ bool Reader::decodeNumber(Token& token, Value& decoded) {
Char c = *current++; Char c = *current++;
if (c < '0' || c > '9') if (c < '0' || c > '9')
return decodeDouble(token, decoded); return decodeDouble(token, decoded);
Value::UInt digit(static_cast<Value::UInt>(c - '0')); auto digit(static_cast<Value::UInt>(c - '0'));
if (value >= threshold) { if (value >= threshold) {
// We've hit or exceeded the max value divided by 10 (rounded down). If // We've hit or exceeded the max value divided by 10 (rounded down). If
// a) we've only just touched the limit, b) this is the last digit, and // a) we've only just touched the limit, b) this is the last digit, and
@@ -801,9 +796,7 @@ String Reader::getFormatedErrorMessages() const {
String Reader::getFormattedErrorMessages() const { String Reader::getFormattedErrorMessages() const {
String formattedMessage; String formattedMessage;
for (Errors::const_iterator itError = errors_.begin(); for (const auto& error : errors_) {
itError != errors_.end(); ++itError) {
const ErrorInfo& error = *itError;
formattedMessage += formattedMessage +=
"* " + getLocationLineAndColumn(error.token_.start_) + "\n"; "* " + getLocationLineAndColumn(error.token_.start_) + "\n";
formattedMessage += " " + error.message_ + "\n"; formattedMessage += " " + error.message_ + "\n";
@@ -816,9 +809,7 @@ String Reader::getFormattedErrorMessages() const {
std::vector<Reader::StructuredError> Reader::getStructuredErrors() const { std::vector<Reader::StructuredError> Reader::getStructuredErrors() const {
std::vector<Reader::StructuredError> allErrors; std::vector<Reader::StructuredError> allErrors;
for (Errors::const_iterator itError = errors_.begin(); for (const auto& error : errors_) {
itError != errors_.end(); ++itError) {
const ErrorInfo& error = *itError;
Reader::StructuredError structured; Reader::StructuredError structured;
structured.offset_start = error.token_.start_ - begin_; structured.offset_start = error.token_.start_ - begin_;
structured.offset_limit = error.token_.end_ - begin_; structured.offset_limit = error.token_.end_ - begin_;
@@ -839,7 +830,7 @@ bool Reader::pushError(const Value& value, const String& message) {
ErrorInfo info; ErrorInfo info;
info.token_ = token; info.token_ = token;
info.message_ = message; info.message_ = message;
info.extra_ = JSONCPP_NULL; info.extra_ = nullptr;
errors_.push_back(info); errors_.push_back(info);
return true; return true;
} }
@@ -882,7 +873,7 @@ public:
size_t stackLimit_; size_t stackLimit_;
}; // OurFeatures }; // OurFeatures
OurFeatures OurFeatures::all() { return OurFeatures(); } OurFeatures OurFeatures::all() { return {}; }
// Implementation of class Reader // Implementation of class Reader
// //////////////////////////////// // ////////////////////////////////
@@ -891,15 +882,15 @@ OurFeatures OurFeatures::all() { return OurFeatures(); }
// for implementing JSON reading. // for implementing JSON reading.
class OurReader { class OurReader {
public: public:
typedef char Char; using Char = char;
typedef const Char* Location; using Location = const Char*;
struct StructuredError { struct StructuredError {
ptrdiff_t offset_start; ptrdiff_t offset_start;
ptrdiff_t offset_limit; ptrdiff_t offset_limit;
String message; String message;
}; };
JSONCPP_OP_EXPLICIT OurReader(OurFeatures const& features); explicit OurReader(OurFeatures const& features);
bool parse(const char* beginDoc, const char* endDoc, Value& root, bool parse(const char* beginDoc, const char* endDoc, Value& root,
bool collectComments = true); bool collectComments = true);
String getFormattedErrorMessages() const; String getFormattedErrorMessages() const;
@@ -943,7 +934,7 @@ private:
Location extra_; Location extra_;
}; };
typedef std::deque<ErrorInfo> Errors; using Errors = std::deque<ErrorInfo>;
bool readToken(Token& token); bool readToken(Token& token);
void skipSpaces(); void skipSpaces();
@@ -968,8 +959,7 @@ private:
unsigned int& unicode); unsigned int& unicode);
bool decodeUnicodeEscapeSequence(Token& token, Location& current, bool decodeUnicodeEscapeSequence(Token& token, Location& current,
Location end, unsigned int& unicode); Location end, unsigned int& unicode);
bool addError(const String& message, Token& token, bool addError(const String& message, Token& token, Location extra = nullptr);
Location extra = JSONCPP_NULL);
bool recoverFromError(TokenType skipUntilToken); bool recoverFromError(TokenType skipUntilToken);
bool addErrorAndRecover(const String& message, Token& token, bool addErrorAndRecover(const String& message, Token& token,
TokenType skipUntilToken); TokenType skipUntilToken);
@@ -985,38 +975,31 @@ private:
static String normalizeEOL(Location begin, Location end); static String normalizeEOL(Location begin, Location end);
static bool containsNewLine(Location begin, Location end); static bool containsNewLine(Location begin, Location end);
typedef std::stack<Value*> Nodes; using Nodes = std::stack<Value*>;
Nodes nodes_; Nodes nodes_{};
Errors errors_; Errors errors_{};
String document_; String document_{};
Location begin_; Location begin_ = nullptr;
Location end_; Location end_ = nullptr;
Location current_; Location current_ = nullptr;
Location lastValueEnd_; Location lastValueEnd_ = nullptr;
Value* lastValue_; Value* lastValue_ = nullptr;
bool lastValueHasAComment_; bool lastValueHasAComment_ = false;
String commentsBefore_; String commentsBefore_{};
OurFeatures const features_; OurFeatures const features_;
bool collectComments_; bool collectComments_ = false;
}; // OurReader }; // OurReader
// complete copy of Read impl, for OurReader // complete copy of Read impl, for OurReader
bool OurReader::containsNewLine(OurReader::Location begin, bool OurReader::containsNewLine(OurReader::Location begin,
OurReader::Location end) { OurReader::Location end) {
for (; begin < end; ++begin) return std::any_of(begin, end, [](char b) { return b == '\n' || b == '\r'; });
if (*begin == '\n' || *begin == '\r')
return true;
return false;
} }
OurReader::OurReader(OurFeatures const& features) OurReader::OurReader(OurFeatures const& features) : features_(features) {}
: errors_(), document_(), begin_(JSONCPP_NULL), end_(JSONCPP_NULL),
current_(JSONCPP_NULL), lastValueEnd_(JSONCPP_NULL),
lastValue_(JSONCPP_NULL), lastValueHasAComment_(false), commentsBefore_(),
features_(features), collectComments_(false) {}
bool OurReader::parse(const char* beginDoc, const char* endDoc, Value& root, bool OurReader::parse(const char* beginDoc, const char* endDoc, Value& root,
bool collectComments) { bool collectComments) {
@@ -1028,8 +1011,8 @@ bool OurReader::parse(const char* beginDoc, const char* endDoc, Value& root,
end_ = endDoc; end_ = endDoc;
collectComments_ = collectComments; collectComments_ = collectComments;
current_ = begin_; current_ = begin_;
lastValueEnd_ = JSONCPP_NULL; lastValueEnd_ = nullptr;
lastValue_ = JSONCPP_NULL; lastValue_ = nullptr;
commentsBefore_.clear(); commentsBefore_.clear();
errors_.clear(); errors_.clear();
while (!nodes_.empty()) while (!nodes_.empty())
@@ -1192,8 +1175,11 @@ bool OurReader::readToken(Token& token) {
if (features_.allowSingleQuotes_) { if (features_.allowSingleQuotes_) {
token.type_ = tokenString; token.type_ = tokenString;
ok = readStringSingleQuote(); ok = readStringSingleQuote();
break; } else {
} // else fall through // If we don't allow single quotes, this is a failure case.
ok = false;
}
break;
case '/': case '/':
token.type_ = tokenComment; token.type_ = tokenComment;
ok = readComment(); ok = readComment();
@@ -1287,7 +1273,7 @@ void OurReader::skipSpaces() {
void OurReader::skipBom(bool skipBom) { void OurReader::skipBom(bool skipBom) {
// The default behavior is to skip BOM. // The default behavior is to skip BOM.
if (skipBom) { if (skipBom) {
if (strncmp(begin_, "\xEF\xBB\xBF", 3) == 0) { if ((end_ - begin_) >= 3 && strncmp(begin_, "\xEF\xBB\xBF", 3) == 0) {
begin_ += 3; begin_ += 3;
current_ = begin_; current_ = begin_;
} }
@@ -1364,7 +1350,7 @@ void OurReader::addComment(Location begin, Location end,
assert(collectComments_); assert(collectComments_);
const String& normalized = normalizeEOL(begin, end); const String& normalized = normalizeEOL(begin, end);
if (placement == commentAfterOnSameLine) { if (placement == commentAfterOnSameLine) {
assert(lastValue_ != JSONCPP_NULL); assert(lastValue_ != nullptr);
lastValue_->setComment(normalized, placement); lastValue_->setComment(normalized, placement);
} else { } else {
commentsBefore_ += normalized; commentsBefore_ += normalized;
@@ -1580,36 +1566,32 @@ bool OurReader::decodeNumber(Token& token, Value& decoded) {
// We assume we can represent the largest and smallest integer types as // We assume we can represent the largest and smallest integer types as
// unsigned integers with separate sign. This is only true if they can fit // unsigned integers with separate sign. This is only true if they can fit
// into an unsigned integer. // into an unsigned integer.
JSONCPP_STATIC_ASSERT(LargestUInt(Value::maxLargestInt) <= static_assert(Value::maxLargestInt <= Value::maxLargestUInt,
Value::maxLargestUInt, "Int must be smaller than UInt");
"Int must be smaller than Uint");
// We need to convert minLargestInt into a positive number. The easiest way // We need to convert minLargestInt into a positive number. The easiest way
// to do this conversion is to assume our "threshold" value of minLargestInt // to do this conversion is to assume our "threshold" value of minLargestInt
// divided by 10 can fit in maxLargestInt when absolute valued. This should // divided by 10 can fit in maxLargestInt when absolute valued. This should
// be a safe assumption. // be a safe assumption.
JSONCPP_STATIC_ASSERT( static_assert(Value::minLargestInt <= -Value::maxLargestInt,
Value::minLargestInt <= -Value::maxLargestInt, "The absolute value of minLargestInt must be greater than or "
"The absolute value of minLargestInt must ve greater than or" "equal to maxLargestInt");
"equal to maxLargestInt"); static_assert(Value::minLargestInt / 10 >= -Value::maxLargestInt,
"The absolute value of minLargestInt must be only 1 magnitude "
"larger than maxLargest Int");
JSONCPP_STATIC_ASSERT( static constexpr Value::LargestUInt positive_threshold =
Value::minLargestInt / 10 >= -Value::maxLargestInt,
"The absolute value of minLargestInt must be only 1 magnitude"
"larger than maxLargestInt");
static JSONCPP_CONST Value::LargestUInt positive_threshold =
Value::maxLargestUInt / 10; Value::maxLargestUInt / 10;
static JSONCPP_CONST Value::UInt positive_last_digit = static constexpr Value::UInt positive_last_digit = Value::maxLargestUInt % 10;
Value::maxLargestUInt % 10;
// For the negative values, we have to be more careful. Since typically // For the negative values, we have to be more careful. Since typically
// -Value::minLargestInt will cause an overflow, we first divide by 10 and // -Value::minLargestInt will cause an overflow, we first divide by 10 and
// then take the inverse. This assumes that minLargestInt is only a single // then take the inverse. This assumes that minLargestInt is only a single
// power of 10 different in magnitude, which we check above. For the last // power of 10 different in magnitude, which we check above. For the last
// digit, we take the modulus before negating for the same reason. // digit, we take the modulus before negating for the same reason.
static JSONCPP_CONST Value::LargestUInt negative_threshold = static constexpr auto negative_threshold =
Value::LargestUInt(-(Value::minLargestInt / 10)); Value::LargestUInt(-(Value::minLargestInt / 10));
static JSONCPP_CONST Value::UInt negative_last_digit = static constexpr auto negative_last_digit =
Value::UInt(-(Value::minLargestInt % 10)); Value::UInt(-(Value::minLargestInt % 10));
const Value::LargestUInt threshold = const Value::LargestUInt threshold =
@@ -1623,7 +1605,7 @@ bool OurReader::decodeNumber(Token& token, Value& decoded) {
if (c < '0' || c > '9') if (c < '0' || c > '9')
return decodeDouble(token, decoded); return decodeDouble(token, decoded);
const Value::UInt digit(static_cast<Value::UInt>(c - '0')); const auto digit(static_cast<Value::UInt>(c - '0'));
if (value >= threshold) { if (value >= threshold) {
// We've hit or exceeded the max value divided by 10 (rounded down). If // We've hit or exceeded the max value divided by 10 (rounded down). If
// a) we've only just touched the limit, meaing value == threshold, // a) we've only just touched the limit, meaing value == threshold,
@@ -1640,7 +1622,7 @@ bool OurReader::decodeNumber(Token& token, Value& decoded) {
if (isNegative) { if (isNegative) {
// We use the same magnitude assumption here, just in case. // We use the same magnitude assumption here, just in case.
const Value::UInt last_digit = static_cast<Value::UInt>(value % 10); const auto last_digit = static_cast<Value::UInt>(value % 10);
decoded = -Value::LargestInt(value / 10) * 10 - last_digit; decoded = -Value::LargestInt(value / 10) * 10 - last_digit;
} else if (value <= Value::LargestUInt(Value::maxLargestInt)) { } else if (value <= Value::LargestUInt(Value::maxLargestInt)) {
decoded = Value::LargestInt(value); decoded = Value::LargestInt(value);
@@ -1856,9 +1838,7 @@ String OurReader::getLocationLineAndColumn(Location location) const {
String OurReader::getFormattedErrorMessages() const { String OurReader::getFormattedErrorMessages() const {
String formattedMessage; String formattedMessage;
for (Errors::const_iterator itError = errors_.begin(); for (const auto& error : errors_) {
itError != errors_.end(); ++itError) {
const ErrorInfo& error = *itError;
formattedMessage += formattedMessage +=
"* " + getLocationLineAndColumn(error.token_.start_) + "\n"; "* " + getLocationLineAndColumn(error.token_.start_) + "\n";
formattedMessage += " " + error.message_ + "\n"; formattedMessage += " " + error.message_ + "\n";
@@ -1871,9 +1851,7 @@ String OurReader::getFormattedErrorMessages() const {
std::vector<OurReader::StructuredError> OurReader::getStructuredErrors() const { std::vector<OurReader::StructuredError> OurReader::getStructuredErrors() const {
std::vector<OurReader::StructuredError> allErrors; std::vector<OurReader::StructuredError> allErrors;
for (Errors::const_iterator itError = errors_.begin(); for (const auto& error : errors_) {
itError != errors_.end(); ++itError) {
const ErrorInfo& error = *itError;
OurReader::StructuredError structured; OurReader::StructuredError structured;
structured.offset_start = error.token_.start_ - begin_; structured.offset_start = error.token_.start_ - begin_;
structured.offset_limit = error.token_.end_ - begin_; structured.offset_limit = error.token_.end_ - begin_;
@@ -1891,7 +1869,7 @@ public:
OurCharReader(bool collectComments, OurFeatures const& features) OurCharReader(bool collectComments, OurFeatures const& features)
: collectComments_(collectComments), reader_(features) {} : collectComments_(collectComments), reader_(features) {}
bool parse(char const* beginDoc, char const* endDoc, Value* root, bool parse(char const* beginDoc, char const* endDoc, Value* root,
String* errs) JSONCPP_OVERRIDE { String* errs) override {
bool ok = reader_.parse(beginDoc, endDoc, *root, collectComments_); bool ok = reader_.parse(beginDoc, endDoc, *root, collectComments_);
if (errs) { if (errs) {
*errs = reader_.getFormattedErrorMessages(); *errs = reader_.getFormattedErrorMessages();
@@ -1901,7 +1879,7 @@ public:
}; };
CharReaderBuilder::CharReaderBuilder() { setDefaults(&settings_); } CharReaderBuilder::CharReaderBuilder() { setDefaults(&settings_); }
CharReaderBuilder::~CharReaderBuilder() {} CharReaderBuilder::~CharReaderBuilder() = default;
CharReader* CharReaderBuilder::newCharReader() const { CharReader* CharReaderBuilder::newCharReader() const {
bool collectComments = settings_["collectComments"].asBool(); bool collectComments = settings_["collectComments"].asBool();
OurFeatures features = OurFeatures::all(); OurFeatures features = OurFeatures::all();
@@ -1922,38 +1900,34 @@ CharReader* CharReaderBuilder::newCharReader() const {
features.skipBom_ = settings_["skipBom"].asBool(); features.skipBom_ = settings_["skipBom"].asBool();
return new OurCharReader(collectComments, features); return new OurCharReader(collectComments, features);
} }
static void getValidReaderKeys(std::set<String>* valid_keys) {
valid_keys->clear();
valid_keys->insert("collectComments");
valid_keys->insert("allowComments");
valid_keys->insert("allowTrailingCommas");
valid_keys->insert("strictRoot");
valid_keys->insert("allowDroppedNullPlaceholders");
valid_keys->insert("allowNumericKeys");
valid_keys->insert("allowSingleQuotes");
valid_keys->insert("stackLimit");
valid_keys->insert("failIfExtra");
valid_keys->insert("rejectDupKeys");
valid_keys->insert("allowSpecialFloats");
valid_keys->insert("skipBom");
}
bool CharReaderBuilder::validate(Json::Value* invalid) const { bool CharReaderBuilder::validate(Json::Value* invalid) const {
Json::Value my_invalid; static const auto& valid_keys = *new std::set<String>{
if (!invalid) "collectComments",
invalid = &my_invalid; // so we do not need to test for NULL "allowComments",
Json::Value& inv = *invalid; "allowTrailingCommas",
std::set<String> valid_keys; "strictRoot",
getValidReaderKeys(&valid_keys); "allowDroppedNullPlaceholders",
Value::Members keys = settings_.getMemberNames(); "allowNumericKeys",
size_t n = keys.size(); "allowSingleQuotes",
for (size_t i = 0; i < n; ++i) { "stackLimit",
String const& key = keys[i]; "failIfExtra",
if (valid_keys.find(key) == valid_keys.end()) { "rejectDupKeys",
inv[key] = settings_[key]; "allowSpecialFloats",
} "skipBom",
};
for (auto si = settings_.begin(); si != settings_.end(); ++si) {
auto key = si.name();
if (valid_keys.count(key))
continue;
if (invalid)
(*invalid)[std::move(key)] = *si;
else
return false;
} }
return inv.empty(); return invalid ? invalid->empty() : true;
} }
Value& CharReaderBuilder::operator[](const String& key) { Value& CharReaderBuilder::operator[](const String& key) {
return settings_[key]; return settings_[key];
} }
@@ -2003,9 +1977,7 @@ bool parseFromStream(CharReader::Factory const& fact, IStream& sin, Value* root,
char const* end = begin + doc.size(); char const* end = begin + doc.size();
// Note that we do not actually need a null-terminator. // Note that we do not actually need a null-terminator.
CharReaderPtr const reader(fact.newCharReader()); CharReaderPtr const reader(fact.newCharReader());
bool ret = reader->parse(begin, end, root, errs); return reader->parse(begin, end, root, errs);
delete reader;
return ret;
} }
IStream& operator>>(IStream& sin, Value& root) { IStream& operator>>(IStream& sin, Value& root) {

View File

@@ -71,7 +71,7 @@ enum {
}; };
// Defines a char buffer for use with uintToString(). // Defines a char buffer for use with uintToString().
typedef char UIntToStringBuffer[uintToStringBufferSize]; using UIntToStringBuffer = char[uintToStringBufferSize];
/** Converts an unsigned integer to string. /** Converts an unsigned integer to string.
* @param value Unsigned integer to convert to string * @param value Unsigned integer to convert to string

View File

@@ -48,6 +48,14 @@ int JSON_API msvc_pre1900_c99_snprintf(char* outBuf, size_t size,
#define JSON_ASSERT_UNREACHABLE assert(false) #define JSON_ASSERT_UNREACHABLE assert(false)
namespace Json { namespace Json {
template <typename T>
static std::unique_ptr<T> cloneUnique(const std::unique_ptr<T>& p) {
std::unique_ptr<T> r;
if (p) {
r = std::unique_ptr<T>(new T(*p));
}
return r;
}
// This is a walkaround to avoid the static initialization of Value::null. // This is a walkaround to avoid the static initialization of Value::null.
// kNull must be word-aligned to avoid crashing on ARM. We use an alignment of // kNull must be word-aligned to avoid crashing on ARM. We use an alignment of
@@ -110,8 +118,8 @@ static inline char* duplicateStringValue(const char* value, size_t length) {
if (length >= static_cast<size_t>(Value::maxInt)) if (length >= static_cast<size_t>(Value::maxInt))
length = Value::maxInt - 1; length = Value::maxInt - 1;
char* newString = static_cast<char*>(malloc(length + 1)); auto newString = static_cast<char*>(malloc(length + 1));
if (newString == JSONCPP_NULL) { if (newString == nullptr) {
throwRuntimeError("in Json::Value::duplicateStringValue(): " throwRuntimeError("in Json::Value::duplicateStringValue(): "
"Failed to allocate string value buffer"); "Failed to allocate string value buffer");
} }
@@ -131,8 +139,8 @@ static inline char* duplicateAndPrefixStringValue(const char* value,
"in Json::Value::duplicateAndPrefixStringValue(): " "in Json::Value::duplicateAndPrefixStringValue(): "
"length too big for prefixing"); "length too big for prefixing");
size_t actualLength = sizeof(length) + length + 1; size_t actualLength = sizeof(length) + length + 1;
char* newString = static_cast<char*>(malloc(actualLength)); auto newString = static_cast<char*>(malloc(actualLength));
if (newString == JSONCPP_NULL) { if (newString == nullptr) {
throwRuntimeError("in Json::Value::duplicateAndPrefixStringValue(): " throwRuntimeError("in Json::Value::duplicateAndPrefixStringValue(): "
"Failed to allocate string value buffer"); "Failed to allocate string value buffer");
} }
@@ -192,9 +200,9 @@ static inline void releaseStringValue(char* value, unsigned) { free(value); }
namespace Json { namespace Json {
#if JSON_USE_EXCEPTION #if JSON_USE_EXCEPTION
Exception::Exception(String msg) : msg_(JSONCPP_MOVE(msg)) {} Exception::Exception(String msg) : msg_(std::move(msg)) {}
Exception::~Exception() JSONCPP_NOEXCEPT {} Exception::~Exception() noexcept = default;
char const* Exception::what() const JSONCPP_NOEXCEPT { return msg_.c_str(); } char const* Exception::what() const noexcept { return msg_.c_str(); }
RuntimeError::RuntimeError(String const& msg) : Exception(msg) {} RuntimeError::RuntimeError(String const& msg) : Exception(msg) {}
LogicError::LogicError(String const& msg) : Exception(msg) {} LogicError::LogicError(String const& msg) : Exception(msg) {}
JSONCPP_NORETURN void throwRuntimeError(String const& msg) { JSONCPP_NORETURN void throwRuntimeError(String const& msg) {
@@ -225,8 +233,7 @@ JSONCPP_NORETURN void throwLogicError(String const& msg) {
// Notes: policy_ indicates if the string was allocated when // Notes: policy_ indicates if the string was allocated when
// a string is stored. // a string is stored.
Value::CZString::CZString(ArrayIndex index) Value::CZString::CZString(ArrayIndex index) : cstr_(nullptr), index_(index) {}
: cstr_(JSONCPP_NULL), index_(index) {}
Value::CZString::CZString(char const* str, unsigned length, Value::CZString::CZString(char const* str, unsigned length,
DuplicationPolicy allocate) DuplicationPolicy allocate)
@@ -237,10 +244,9 @@ Value::CZString::CZString(char const* str, unsigned length,
} }
Value::CZString::CZString(const CZString& other) { Value::CZString::CZString(const CZString& other) {
cstr_ = cstr_ = (other.storage_.policy_ != noDuplication && other.cstr_ != nullptr
(other.storage_.policy_ != noDuplication && other.cstr_ != JSONCPP_NULL ? duplicateStringValue(other.cstr_, other.storage_.length_)
? duplicateStringValue(other.cstr_, other.storage_.length_) : other.cstr_);
: other.cstr_);
storage_.policy_ = storage_.policy_ =
static_cast<unsigned>( static_cast<unsigned>(
other.cstr_ other.cstr_
@@ -252,12 +258,12 @@ Value::CZString::CZString(const CZString& other) {
3U; 3U;
storage_.length_ = other.storage_.length_; storage_.length_ = other.storage_.length_;
} }
#if JSONCPP_CXX_STD_11
Value::CZString::CZString(CZString&& other) Value::CZString::CZString(CZString&& other)
: cstr_(other.cstr_), index_(other.index_) { : cstr_(other.cstr_), index_(other.index_) {
other.cstr_ = JSONCPP_NULL; other.cstr_ = nullptr;
} }
#endif
Value::CZString::~CZString() { Value::CZString::~CZString() {
if (cstr_ && storage_.policy_ == duplicate) { if (cstr_ && storage_.policy_ == duplicate) {
releaseStringValue(const_cast<char*>(cstr_), releaseStringValue(const_cast<char*>(cstr_),
@@ -278,14 +284,14 @@ Value::CZString& Value::CZString::operator=(const CZString& other) {
index_ = other.index_; index_ = other.index_;
return *this; return *this;
} }
#if JSONCPP_CXX_STD_11
Value::CZString& Value::CZString::operator=(CZString&& other) { Value::CZString& Value::CZString::operator=(CZString&& other) {
cstr_ = other.cstr_; cstr_ = other.cstr_;
index_ = other.index_; index_ = other.index_;
other.cstr_ = JSONCPP_NULL; other.cstr_ = nullptr;
return *this; return *this;
} }
#endif
bool Value::CZString::operator<(const CZString& other) const { bool Value::CZString::operator<(const CZString& other) const {
if (!cstr_) if (!cstr_)
return index_ < other.index_; return index_ < other.index_;
@@ -394,7 +400,7 @@ Value::Value(double value) {
Value::Value(const char* value) { Value::Value(const char* value) {
initBasic(stringValue, true); initBasic(stringValue, true);
JSON_ASSERT_MESSAGE(value != JSONCPP_NULL, JSON_ASSERT_MESSAGE(value != nullptr,
"Null Value Passed to Value Constructor"); "Null Value Passed to Value Constructor");
value_.string_ = duplicateAndPrefixStringValue( value_.string_ = duplicateAndPrefixStringValue(
value, static_cast<unsigned>(strlen(value))); value, static_cast<unsigned>(strlen(value)));
@@ -426,12 +432,11 @@ Value::Value(const Value& other) {
dupPayload(other); dupPayload(other);
dupMeta(other); dupMeta(other);
} }
#if JSONCPP_CXX_STD_11
Value::Value(Value&& other) { Value::Value(Value&& other) {
initBasic(nullValue); initBasic(nullValue);
swap(other); swap(other);
} }
#endif
Value::~Value() { Value::~Value() {
releasePayload(); releasePayload();
@@ -442,12 +447,11 @@ Value& Value::operator=(const Value& other) {
Value(other).swap(*this); Value(other).swap(*this);
return *this; return *this;
} }
#if JSONCPP_CXX_STD_11
Value& Value::operator=(Value&& other) { Value& Value::operator=(Value&& other) {
other.swap(*this); other.swap(*this);
return *this; return *this;
} }
#endif
void Value::swapPayload(Value& other) { void Value::swapPayload(Value& other) {
std::swap(bits_, other.bits_); std::swap(bits_, other.bits_);
@@ -499,9 +503,8 @@ bool Value::operator<(const Value& other) const {
case booleanValue: case booleanValue:
return value_.bool_ < other.value_.bool_; return value_.bool_ < other.value_.bool_;
case stringValue: { case stringValue: {
if ((value_.string_ == JSONCPP_NULL) || if ((value_.string_ == nullptr) || (other.value_.string_ == nullptr)) {
(other.value_.string_ == JSONCPP_NULL)) { return other.value_.string_ != nullptr;
return other.value_.string_ != JSONCPP_NULL;
} }
unsigned this_len; unsigned this_len;
unsigned other_len; unsigned other_len;
@@ -522,8 +525,8 @@ bool Value::operator<(const Value& other) const {
} }
case arrayValue: case arrayValue:
case objectValue: { case objectValue: {
long unsigned int thisSize = value_.map_->size(); auto thisSize = value_.map_->size();
long unsigned int otherSize = other.value_.map_->size(); auto otherSize = other.value_.map_->size();
if (thisSize != otherSize) if (thisSize != otherSize)
return thisSize < otherSize; return thisSize < otherSize;
return (*value_.map_) < (*other.value_.map_); return (*value_.map_) < (*other.value_.map_);
@@ -555,8 +558,7 @@ bool Value::operator==(const Value& other) const {
case booleanValue: case booleanValue:
return value_.bool_ == other.value_.bool_; return value_.bool_ == other.value_.bool_;
case stringValue: { case stringValue: {
if ((value_.string_ == JSONCPP_NULL) || if ((value_.string_ == nullptr) || (other.value_.string_ == nullptr)) {
(other.value_.string_ == JSONCPP_NULL)) {
return (value_.string_ == other.value_.string_); return (value_.string_ == other.value_.string_);
} }
unsigned this_len; unsigned this_len;
@@ -588,8 +590,8 @@ bool Value::operator!=(const Value& other) const { return !(*this == other); }
const char* Value::asCString() const { const char* Value::asCString() const {
JSON_ASSERT_MESSAGE(type() == stringValue, JSON_ASSERT_MESSAGE(type() == stringValue,
"in Json::Value::asCString(): requires stringValue"); "in Json::Value::asCString(): requires stringValue");
if (value_.string_ == JSONCPP_NULL) if (value_.string_ == nullptr)
return JSONCPP_NULL; return nullptr;
unsigned this_len; unsigned this_len;
char const* this_str; char const* this_str;
decodePrefixedString(this->isAllocated(), this->value_.string_, &this_len, decodePrefixedString(this->isAllocated(), this->value_.string_, &this_len,
@@ -614,7 +616,7 @@ unsigned Value::getCStringLength() const {
bool Value::getString(char const** begin, char const** end) const { bool Value::getString(char const** begin, char const** end) const {
if (type() != stringValue) if (type() != stringValue)
return false; return false;
if (value_.string_ == JSONCPP_NULL) if (value_.string_ == nullptr)
return false; return false;
unsigned length; unsigned length;
decodePrefixedString(this->isAllocated(), this->value_.string_, &length, decodePrefixedString(this->isAllocated(), this->value_.string_, &length,
@@ -628,7 +630,7 @@ String Value::asString() const {
case nullValue: case nullValue:
return ""; return "";
case stringValue: { case stringValue: {
if (value_.string_ == JSONCPP_NULL) if (value_.string_ == nullptr)
return ""; return "";
unsigned this_len; unsigned this_len;
char const* this_str; char const* this_str;
@@ -811,7 +813,7 @@ bool Value::asBool() const {
return value_.uint_ != 0; return value_.uint_ != 0;
case realValue: { case realValue: {
// According to JavaScript language zero or NaN is regarded as false // According to JavaScript language zero or NaN is regarded as false
const int value_classification = std::fpclassify(value_.real_); const auto value_classification = std::fpclassify(value_.real_);
return value_classification != FP_ZERO && value_classification != FP_NAN; return value_classification != FP_ZERO && value_classification != FP_NAN;
} }
default: default:
@@ -926,7 +928,7 @@ Value& Value::operator[](ArrayIndex index) {
if (type() == nullValue) if (type() == nullValue)
*this = Value(arrayValue); *this = Value(arrayValue);
CZString key(index); CZString key(index);
ObjectValues::iterator it = value_.map_->lower_bound(key); auto it = value_.map_->lower_bound(key);
if (it != value_.map_->end() && (*it).first == key) if (it != value_.map_->end() && (*it).first == key)
return (*it).second; return (*it).second;
@@ -965,7 +967,7 @@ const Value& Value::operator[](int index) const {
void Value::initBasic(ValueType type, bool allocated) { void Value::initBasic(ValueType type, bool allocated) {
setType(type); setType(type);
setIsAllocated(allocated); setIsAllocated(allocated);
comments_ = Comments(); comments_ = Comments{};
start_ = 0; start_ = 0;
limit_ = 0; limit_ = 0;
} }
@@ -1040,7 +1042,7 @@ Value& Value::resolveReference(const char* key) {
*this = Value(objectValue); *this = Value(objectValue);
CZString actualKey(key, static_cast<unsigned>(strlen(key)), CZString actualKey(key, static_cast<unsigned>(strlen(key)),
CZString::noDuplication); // NOTE! CZString::noDuplication); // NOTE!
ObjectValues::iterator it = value_.map_->lower_bound(actualKey); auto it = value_.map_->lower_bound(actualKey);
if (it != value_.map_->end() && (*it).first == actualKey) if (it != value_.map_->end() && (*it).first == actualKey)
return (*it).second; return (*it).second;
@@ -1059,7 +1061,7 @@ Value& Value::resolveReference(char const* key, char const* end) {
*this = Value(objectValue); *this = Value(objectValue);
CZString actualKey(key, static_cast<unsigned>(end - key), CZString actualKey(key, static_cast<unsigned>(end - key),
CZString::duplicateOnCopy); CZString::duplicateOnCopy);
ObjectValues::iterator it = value_.map_->lower_bound(actualKey); auto it = value_.map_->lower_bound(actualKey);
if (it != value_.map_->end() && (*it).first == actualKey) if (it != value_.map_->end() && (*it).first == actualKey)
return (*it).second; return (*it).second;
@@ -1081,12 +1083,12 @@ Value const* Value::find(char const* begin, char const* end) const {
"in Json::Value::find(begin, end): requires " "in Json::Value::find(begin, end): requires "
"objectValue or nullValue"); "objectValue or nullValue");
if (type() == nullValue) if (type() == nullValue)
return JSONCPP_NULL; return nullptr;
CZString actualKey(begin, static_cast<unsigned>(end - begin), CZString actualKey(begin, static_cast<unsigned>(end - begin),
CZString::noDuplication); CZString::noDuplication);
ObjectValues::const_iterator it = value_.map_->find(actualKey); ObjectValues::const_iterator it = value_.map_->find(actualKey);
if (it == value_.map_->end()) if (it == value_.map_->end())
return JSONCPP_NULL; return nullptr;
return &(*it).second; return &(*it).second;
} }
Value* Value::demand(char const* begin, char const* end) { Value* Value::demand(char const* begin, char const* end) {
@@ -1120,8 +1122,8 @@ Value& Value::operator[](const StaticString& key) {
return resolveReference(key.c_str()); return resolveReference(key.c_str());
} }
#if JSONCPP_CXX_STD_11
Value& Value::append(const Value& value) { return append(Value(value)); } Value& Value::append(const Value& value) { return append(Value(value)); }
Value& Value::append(Value&& value) { Value& Value::append(Value&& value) {
JSON_ASSERT_MESSAGE(type() == nullValue || type() == arrayValue, JSON_ASSERT_MESSAGE(type() == nullValue || type() == arrayValue,
"in Json::Value::append: requires arrayValue"); "in Json::Value::append: requires arrayValue");
@@ -1130,19 +1132,12 @@ Value& Value::append(Value&& value) {
} }
return this->value_.map_->emplace(size(), std::move(value)).first->second; return this->value_.map_->emplace(size(), std::move(value)).first->second;
} }
#else
Value& Value::append(const Value& value) { return (*this)[size()] = value; }
#endif
#if JSONCPP_CXX_STD_11
bool Value::insert(ArrayIndex index, const Value& newValue) { bool Value::insert(ArrayIndex index, const Value& newValue) {
return insert(index, Value(newValue)); return insert(index, Value(newValue));
} }
bool Value::insert(ArrayIndex index, Value&& newValue) { bool Value::insert(ArrayIndex index, Value&& newValue) {
#else
bool Value::insert(ArrayIndex index, const Value& newValue) {
#endif
JSON_ASSERT_MESSAGE(type() == nullValue || type() == arrayValue, JSON_ASSERT_MESSAGE(type() == nullValue || type() == arrayValue,
"in Json::Value::insert: requires arrayValue"); "in Json::Value::insert: requires arrayValue");
ArrayIndex length = size(); ArrayIndex length = size();
@@ -1150,11 +1145,12 @@ bool Value::insert(ArrayIndex index, const Value& newValue) {
return false; return false;
} }
for (ArrayIndex i = length; i > index; i--) { for (ArrayIndex i = length; i > index; i--) {
(*this)[i] = JSONCPP_MOVE((*this)[i - 1]); (*this)[i] = std::move((*this)[i - 1]);
} }
(*this)[index] = JSONCPP_MOVE(newValue); (*this)[index] = std::move(newValue);
return true; return true;
} }
Value Value::get(char const* begin, char const* end, Value Value::get(char const* begin, char const* end,
Value const& defaultValue) const { Value const& defaultValue) const {
Value const* found = find(begin, end); Value const* found = find(begin, end);
@@ -1173,11 +1169,11 @@ bool Value::removeMember(const char* begin, const char* end, Value* removed) {
} }
CZString actualKey(begin, static_cast<unsigned>(end - begin), CZString actualKey(begin, static_cast<unsigned>(end - begin),
CZString::noDuplication); CZString::noDuplication);
ObjectValues::iterator it = value_.map_->find(actualKey); auto it = value_.map_->find(actualKey);
if (it == value_.map_->end()) if (it == value_.map_->end())
return false; return false;
if (removed) if (removed)
*removed = JSONCPP_MOVE(it->second); *removed = std::move(it->second);
value_.map_->erase(it); value_.map_->erase(it);
return true; return true;
} }
@@ -1203,7 +1199,7 @@ bool Value::removeIndex(ArrayIndex index, Value* removed) {
return false; return false;
} }
CZString key(index); CZString key(index);
ObjectValues::iterator it = value_.map_->find(key); auto it = value_.map_->find(key);
if (it == value_.map_->end()) { if (it == value_.map_->end()) {
return false; return false;
} }
@@ -1217,14 +1213,14 @@ bool Value::removeIndex(ArrayIndex index, Value* removed) {
} }
// erase the last one ("leftover") // erase the last one ("leftover")
CZString keyLast(oldSize - 1); CZString keyLast(oldSize - 1);
ObjectValues::iterator itLast = value_.map_->find(keyLast); auto itLast = value_.map_->find(keyLast);
value_.map_->erase(itLast); value_.map_->erase(itLast);
return true; return true;
} }
bool Value::isMember(char const* begin, char const* end) const { bool Value::isMember(char const* begin, char const* end) const {
Value const* value = find(begin, end); Value const* value = find(begin, end);
return JSONCPP_NULL != value; return nullptr != value;
} }
bool Value::isMember(char const* key) const { bool Value::isMember(char const* key) const {
return isMember(key, key + strlen(key)); return isMember(key, key + strlen(key));
@@ -1374,44 +1370,53 @@ bool Value::isArray() const { return type() == arrayValue; }
bool Value::isObject() const { return type() == objectValue; } bool Value::isObject() const { return type() == objectValue; }
Value::Comments::Comments(const Comments& that) { Value::Comments::Comments(const Comments& that)
for (size_t i = 0; i < numberOfCommentPlacement; i++) { : ptr_{cloneUnique(that.ptr_)} {}
ptr_[i] = that.ptr_[i];
} Value::Comments::Comments(Comments&& that) : ptr_{std::move(that.ptr_)} {}
}
Value::Comments& Value::Comments::operator=(const Comments& that) { Value::Comments& Value::Comments::operator=(const Comments& that) {
for (size_t i = 0; i < numberOfCommentPlacement; i++) { ptr_ = cloneUnique(that.ptr_);
ptr_[i] = that.ptr_[i];
}
return *this; return *this;
} }
bool Value::Comments::has(CommentPlacement slot) const {
return !ptr_[slot].empty(); Value::Comments& Value::Comments::operator=(Comments&& that) {
ptr_ = std::move(that.ptr_);
return *this;
} }
String Value::Comments::get(CommentPlacement slot) const { return ptr_[slot]; } bool Value::Comments::has(CommentPlacement slot) const {
return ptr_ && !(*ptr_)[slot].empty();
}
String Value::Comments::get(CommentPlacement slot) const {
if (!ptr_)
return {};
return (*ptr_)[slot];
}
void Value::Comments::set(CommentPlacement slot, String comment) { void Value::Comments::set(CommentPlacement slot, String comment) {
if (!ptr_) {
ptr_ = std::unique_ptr<Array>(new Array());
}
// check comments array boundry. // check comments array boundry.
if (slot < numberOfCommentPlacement) { if (slot < CommentPlacement::numberOfCommentPlacement) {
ptr_[slot] = comment; (*ptr_)[slot] = std::move(comment);
} }
} }
void Value::setComment(const char* comment, CommentPlacement placement) { void Value::setComment(String comment, CommentPlacement placement) {
setComment(comment, strlen(comment), placement); if (!comment.empty() && (comment.back() == '\n')) {
}
void Value::setComment(const char* comment, size_t len,
CommentPlacement placement) {
if ((len > 0) && (comment[len - 1] == '\n')) {
// Always discard trailing newline, to aid indentation. // Always discard trailing newline, to aid indentation.
len -= 1; comment.pop_back();
} }
comments_.set(placement, String(comment, len)); JSON_ASSERT(!comment.empty());
} JSON_ASSERT_MESSAGE(
void Value::setComment(const String& comment, CommentPlacement placement) { comment[0] == '\0' || comment[0] == '/',
setComment(comment.c_str(), comment.length(), placement); "in Json::Value::setComment(): Comments must start with /");
comments_.set(placement, std::move(comment));
} }
bool Value::hasComment(CommentPlacement placement) const { bool Value::hasComment(CommentPlacement placement) const {
return comments_.has(placement); return comments_.has(placement);
} }
@@ -1448,7 +1453,7 @@ Value::const_iterator Value::begin() const {
default: default:
break; break;
} }
return const_iterator(); return {};
} }
Value::const_iterator Value::end() const { Value::const_iterator Value::end() const {
@@ -1461,7 +1466,7 @@ Value::const_iterator Value::end() const {
default: default:
break; break;
} }
return const_iterator(); return {};
} }
Value::iterator Value::begin() { Value::iterator Value::begin() {
@@ -1493,15 +1498,14 @@ Value::iterator Value::end() {
// class PathArgument // class PathArgument
// ////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////
PathArgument::PathArgument() {} PathArgument::PathArgument() = default;
PathArgument::PathArgument(ArrayIndex index) PathArgument::PathArgument(ArrayIndex index)
: index_(index), kind_(kindIndex) {} : index_(index), kind_(kindIndex) {}
PathArgument::PathArgument(const char* key) : key_(key), kind_(kindKey) {} PathArgument::PathArgument(const char* key) : key_(key), kind_(kindKey) {}
PathArgument::PathArgument(String key) PathArgument::PathArgument(String key) : key_(std::move(key)), kind_(kindKey) {}
: key_(JSONCPP_MOVE(key)), kind_(kindKey) {}
// class Path // class Path
// ////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////
@@ -1522,7 +1526,7 @@ Path::Path(const String& path, const PathArgument& a1, const PathArgument& a2,
void Path::makePath(const String& path, const InArgs& in) { void Path::makePath(const String& path, const InArgs& in) {
const char* current = path.c_str(); const char* current = path.c_str();
const char* end = current + path.length(); const char* end = current + path.length();
InArgs::const_iterator itInArg = in.begin(); auto itInArg = in.begin();
while (current != end) { while (current != end) {
if (*current == '[') { if (*current == '[') {
++current; ++current;
@@ -1568,9 +1572,7 @@ void Path::invalidPath(const String& /*path*/, int /*location*/) {
const Value& Path::resolve(const Value& root) const { const Value& Path::resolve(const Value& root) const {
const Value* node = &root; const Value* node = &root;
for (Args::const_iterator itArg = args_.begin(); itArg != args_.end(); for (const auto& arg : args_) {
++itArg) {
const PathArgument& arg = *itArg;
if (arg.kind_ == PathArgument::kindIndex) { if (arg.kind_ == PathArgument::kindIndex) {
if (!node->isArray() || !node->isValidIndex(arg.index_)) { if (!node->isArray() || !node->isValidIndex(arg.index_)) {
// Error: unable to resolve path (array value expected at position... ) // Error: unable to resolve path (array value expected at position... )
@@ -1595,9 +1597,7 @@ const Value& Path::resolve(const Value& root) const {
Value Path::resolve(const Value& root, const Value& defaultValue) const { Value Path::resolve(const Value& root, const Value& defaultValue) const {
const Value* node = &root; const Value* node = &root;
for (Args::const_iterator itArg = args_.begin(); itArg != args_.end(); for (const auto& arg : args_) {
++itArg) {
const PathArgument& arg = *itArg;
if (arg.kind_ == PathArgument::kindIndex) { if (arg.kind_ == PathArgument::kindIndex) {
if (!node->isArray() || !node->isValidIndex(arg.index_)) if (!node->isArray() || !node->isValidIndex(arg.index_))
return defaultValue; return defaultValue;
@@ -1615,9 +1615,7 @@ Value Path::resolve(const Value& root, const Value& defaultValue) const {
Value& Path::make(Value& root) const { Value& Path::make(Value& root) const {
Value* node = &root; Value* node = &root;
for (Args::const_iterator itArg = args_.begin(); itArg != args_.end(); for (const auto& arg : args_) {
++itArg) {
const PathArgument& arg = *itArg;
if (arg.kind_ == PathArgument::kindIndex) { if (arg.kind_ == PathArgument::kindIndex) {
if (!node->isArray()) { if (!node->isArray()) {
// Error: node is not an array at position ... // Error: node is not an array at position ...

View File

@@ -15,7 +15,7 @@ namespace Json {
// ////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////
ValueIteratorBase::ValueIteratorBase() : current_(), isNull_(true) {} ValueIteratorBase::ValueIteratorBase() : current_() {}
ValueIteratorBase::ValueIteratorBase( ValueIteratorBase::ValueIteratorBase(
const Value::ObjectValues::iterator& current) const Value::ObjectValues::iterator& current)
@@ -98,8 +98,8 @@ char const* ValueIteratorBase::memberName() const {
char const* ValueIteratorBase::memberName(char const** end) const { char const* ValueIteratorBase::memberName(char const** end) const {
const char* cname = (*current_).first.data(); const char* cname = (*current_).first.data();
if (!cname) { if (!cname) {
*end = JSONCPP_NULL; *end = nullptr;
return JSONCPP_NULL; return nullptr;
} }
*end = cname + (*current_).first.length(); *end = cname + (*current_).first.length();
return cname; return cname;
@@ -113,7 +113,7 @@ char const* ValueIteratorBase::memberName(char const** end) const {
// ////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////
ValueConstIterator::ValueConstIterator() {} ValueConstIterator::ValueConstIterator() = default;
ValueConstIterator::ValueConstIterator( ValueConstIterator::ValueConstIterator(
const Value::ObjectValues::iterator& current) const Value::ObjectValues::iterator& current)
@@ -136,7 +136,7 @@ operator=(const ValueIteratorBase& other) {
// ////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////
ValueIterator::ValueIterator() {} ValueIterator::ValueIterator() = default;
ValueIterator::ValueIterator(const Value::ObjectValues::iterator& current) ValueIterator::ValueIterator(const Value::ObjectValues::iterator& current)
: ValueIteratorBase(current) {} : ValueIteratorBase(current) {}
@@ -146,8 +146,7 @@ ValueIterator::ValueIterator(const ValueConstIterator& other)
throwRuntimeError("ConstIterator to Iterator should never be allowed."); throwRuntimeError("ConstIterator to Iterator should never be allowed.");
} }
ValueIterator::ValueIterator(const ValueIterator& other) ValueIterator::ValueIterator(const ValueIterator& other) = default;
: ValueIteratorBase(other) {}
ValueIterator& ValueIterator::operator=(const SelfType& other) { ValueIterator& ValueIterator::operator=(const SelfType& other) {
copy(other); copy(other);

View File

@@ -7,7 +7,9 @@
#include "json_tool.h" #include "json_tool.h"
#include <json/writer.h> #include <json/writer.h>
#endif // if !defined(JSON_IS_AMALGAMATION) #endif // if !defined(JSON_IS_AMALGAMATION)
#include <algorithm>
#include <cassert> #include <cassert>
#include <cctype>
#include <cstring> #include <cstring>
#include <iomanip> #include <iomanip>
#include <memory> #include <memory>
@@ -83,7 +85,11 @@
namespace Json { namespace Json {
typedef StreamWriter* StreamWriterPtr; #if __cplusplus >= 201103L || (defined(_CPPLIB_VER) && _CPPLIB_VER >= 520)
using StreamWriterPtr = std::unique_ptr<StreamWriter>;
#else
using StreamWriterPtr = std::auto_ptr<StreamWriter>;
#endif
String valueToString(LargestInt value) { String valueToString(LargestInt value) {
UIntToStringBuffer buffer; UIntToStringBuffer buffer;
@@ -132,12 +138,12 @@ String valueToString(double value, bool useSpecialFloats,
String buffer(size_t(36), '\0'); String buffer(size_t(36), '\0');
while (true) { while (true) {
int len = int len = jsoncpp_snprintf(
jsoncpp_snprintf(&*buffer.begin(), buffer.size(), &*buffer.begin(), buffer.size(),
(precisionType == significantDigits) ? "%.*g" : "%.*f", (precisionType == PrecisionType::significantDigits) ? "%.*g" : "%.*f",
precision, value); precision, value);
assert(len >= 0); assert(len >= 0);
size_t wouldPrint = static_cast<size_t>(len); auto wouldPrint = static_cast<size_t>(len);
if (wouldPrint >= buffer.size()) { if (wouldPrint >= buffer.size()) {
buffer.resize(wouldPrint + 1); buffer.resize(wouldPrint + 1);
continue; continue;
@@ -149,7 +155,7 @@ String valueToString(double value, bool useSpecialFloats,
buffer.erase(fixNumericLocale(buffer.begin(), buffer.end()), buffer.end()); buffer.erase(fixNumericLocale(buffer.begin(), buffer.end()), buffer.end());
// strip the zero padding from the right // strip the zero padding from the right
if (precisionType == decimalPlaces) { if (precisionType == PrecisionType::decimalPlaces) {
buffer.erase(fixZerosInTheEnd(buffer.begin(), buffer.end()), buffer.end()); buffer.erase(fixZerosInTheEnd(buffer.begin(), buffer.end()), buffer.end());
} }
@@ -169,17 +175,12 @@ String valueToString(double value, unsigned int precision,
String valueToString(bool value) { return value ? "true" : "false"; } String valueToString(bool value) { return value ? "true" : "false"; }
static bool isAnyCharRequiredQuoting(char const* s, size_t n) { static bool doesAnyCharRequireEscaping(char const* s, size_t n) {
assert(s || !n); assert(s || !n);
char const* const end = s + n; return std::any_of(s, s + n, [](unsigned char c) {
for (char const* cur = s; cur < end; ++cur) { return c == '\\' || c == '"' || c < 0x20 || c > 0x7F;
if (*cur == '\\' || *cur == '\"' || });
static_cast<unsigned char>(*cur) < ' ' ||
static_cast<unsigned char>(*cur) >= 0x80)
return true;
}
return false;
} }
static unsigned int utf8ToCodepoint(const char*& s, const char* e) { static unsigned int utf8ToCodepoint(const char*& s, const char* e) {
@@ -261,12 +262,20 @@ static String toHex16Bit(unsigned int x) {
return result; return result;
} }
static void appendRaw(String& result, unsigned ch) {
result += static_cast<char>(ch);
}
static void appendHex(String& result, unsigned ch) {
result.append("\\u").append(toHex16Bit(ch));
}
static String valueToQuotedStringN(const char* value, unsigned length, static String valueToQuotedStringN(const char* value, unsigned length,
bool emitUTF8 = false) { bool emitUTF8 = false) {
if (value == JSONCPP_NULL) if (value == nullptr)
return ""; return "";
if (!isAnyCharRequiredQuoting(value, length)) if (!doesAnyCharRequireEscaping(value, length))
return String("\"") + value + "\""; return String("\"") + value + "\"";
// We have to walk value and escape any special characters. // We have to walk value and escape any special characters.
// Appending to String is not efficient, but this should be rare. // Appending to String is not efficient, but this should be rare.
@@ -309,29 +318,26 @@ static String valueToQuotedStringN(const char* value, unsigned length,
// sequence from occurring. // sequence from occurring.
default: { default: {
if (emitUTF8) { if (emitUTF8) {
result += *c; unsigned codepoint = static_cast<unsigned char>(*c);
if (codepoint < 0x20) {
appendHex(result, codepoint);
} else {
appendRaw(result, codepoint);
}
} else { } else {
unsigned int codepoint = utf8ToCodepoint(c, end); unsigned codepoint = utf8ToCodepoint(c, end); // modifies `c`
const unsigned int FIRST_NON_CONTROL_CODEPOINT = 0x20; if (codepoint < 0x20) {
const unsigned int LAST_NON_CONTROL_CODEPOINT = 0x7F; appendHex(result, codepoint);
const unsigned int FIRST_SURROGATE_PAIR_CODEPOINT = 0x10000; } else if (codepoint < 0x80) {
// don't escape non-control characters appendRaw(result, codepoint);
// (short escape sequence are applied above) } else if (codepoint < 0x10000) {
if (FIRST_NON_CONTROL_CODEPOINT <= codepoint && // Basic Multilingual Plane
codepoint <= LAST_NON_CONTROL_CODEPOINT) { appendHex(result, codepoint);
result += static_cast<char>(codepoint); } else {
} else if (codepoint < // Extended Unicode. Encode 20 bits as a surrogate pair.
FIRST_SURROGATE_PAIR_CODEPOINT) { // codepoint is in Basic codepoint -= 0x10000;
// Multilingual Plane appendHex(result, 0xd800 + ((codepoint >> 10) & 0x3ff));
result += "\\u"; appendHex(result, 0xdc00 + (codepoint & 0x3ff));
result += toHex16Bit(codepoint);
} else { // codepoint is not in Basic Multilingual Plane
// convert to surrogate pair first
codepoint -= FIRST_SURROGATE_PAIR_CODEPOINT;
result += "\\u";
result += toHex16Bit((codepoint >> 10) + 0xD800);
result += "\\u";
result += toHex16Bit((codepoint & 0x3FF) + 0xDC00);
} }
} }
} break; } break;
@@ -347,14 +353,14 @@ String valueToQuotedString(const char* value) {
// Class Writer // Class Writer
// ////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////
Writer::~Writer() {} Writer::~Writer() = default;
// Class FastWriter // Class FastWriter
// ////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////
FastWriter::FastWriter() FastWriter::FastWriter()
: yamlCompatibilityEnabled_(false), dropNullPlaceholders_(false),
omitEndingLineFeed_(false) {} = default;
void FastWriter::enableYAMLCompatibility() { yamlCompatibilityEnabled_ = true; } void FastWriter::enableYAMLCompatibility() { yamlCompatibilityEnabled_ = true; }
@@ -410,8 +416,7 @@ void FastWriter::writeValue(const Value& value) {
case objectValue: { case objectValue: {
Value::Members members(value.getMemberNames()); Value::Members members(value.getMemberNames());
document_ += '{'; document_ += '{';
for (Value::Members::const_iterator it = members.begin(); for (auto it = members.begin(); it != members.end(); ++it) {
it != members.end(); ++it) {
const String& name = *it; const String& name = *it;
if (it != members.begin()) if (it != members.begin())
document_ += ','; document_ += ',';
@@ -428,8 +433,7 @@ void FastWriter::writeValue(const Value& value) {
// Class StyledWriter // Class StyledWriter
// ////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////
StyledWriter::StyledWriter() StyledWriter::StyledWriter() = default;
: rightMargin_(74), indentSize_(3), addChildValues_() {}
String StyledWriter::write(const Value& root) { String StyledWriter::write(const Value& root) {
document_.clear(); document_.clear();
@@ -480,7 +484,7 @@ void StyledWriter::writeValue(const Value& value) {
else { else {
writeWithIndent("{"); writeWithIndent("{");
indent(); indent();
Value::Members::const_iterator it = members.begin(); auto it = members.begin();
for (;;) { for (;;) {
const String& name = *it; const String& name = *it;
const Value& childValue = value[name]; const Value& childValue = value[name];
@@ -642,9 +646,8 @@ bool StyledWriter::hasCommentForValue(const Value& value) {
// ////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////
StyledStreamWriter::StyledStreamWriter(String indentation) StyledStreamWriter::StyledStreamWriter(String indentation)
: document_(JSONCPP_NULL), rightMargin_(74), : document_(nullptr), indentation_(std::move(indentation)),
indentation_(JSONCPP_MOVE(indentation)), addChildValues_(), addChildValues_(), indented_(false) {}
indented_(false) {}
void StyledStreamWriter::write(OStream& out, const Value& root) { void StyledStreamWriter::write(OStream& out, const Value& root) {
document_ = &out; document_ = &out;
@@ -658,7 +661,7 @@ void StyledStreamWriter::write(OStream& out, const Value& root) {
writeValue(root); writeValue(root);
writeCommentAfterValueOnSameLine(root); writeCommentAfterValueOnSameLine(root);
*document_ << "\n"; *document_ << "\n";
document_ = JSONCPP_NULL; // Forget the stream, for safety. document_ = nullptr; // Forget the stream, for safety.
} }
void StyledStreamWriter::writeValue(const Value& value) { void StyledStreamWriter::writeValue(const Value& value) {
@@ -699,7 +702,7 @@ void StyledStreamWriter::writeValue(const Value& value) {
else { else {
writeWithIndent("{"); writeWithIndent("{");
indent(); indent();
Value::Members::const_iterator it = members.begin(); auto it = members.begin();
for (;;) { for (;;) {
const String& name = *it; const String& name = *it;
const Value& childValue = value[name]; const Value& childValue = value[name];
@@ -877,7 +880,7 @@ struct BuiltStyledStreamWriter : public StreamWriter {
String endingLineFeedSymbol, bool useSpecialFloats, String endingLineFeedSymbol, bool useSpecialFloats,
bool emitUTF8, unsigned int precision, bool emitUTF8, unsigned int precision,
PrecisionType precisionType); PrecisionType precisionType);
int write(Value const& root, OStream* sout) JSONCPP_OVERRIDE; int write(Value const& root, OStream* sout) override;
private: private:
void writeValue(Value const& value); void writeValue(Value const& value);
@@ -892,7 +895,7 @@ private:
void writeCommentAfterValueOnSameLine(Value const& root); void writeCommentAfterValueOnSameLine(Value const& root);
static bool hasCommentForValue(const Value& value); static bool hasCommentForValue(const Value& value);
typedef std::vector<String> ChildValues; using ChildValues = std::vector<String>;
ChildValues childValues_; ChildValues childValues_;
String indentString_; String indentString_;
@@ -913,10 +916,9 @@ BuiltStyledStreamWriter::BuiltStyledStreamWriter(
String indentation, CommentStyle::Enum cs, String colonSymbol, String indentation, CommentStyle::Enum cs, String colonSymbol,
String nullSymbol, String endingLineFeedSymbol, bool useSpecialFloats, String nullSymbol, String endingLineFeedSymbol, bool useSpecialFloats,
bool emitUTF8, unsigned int precision, PrecisionType precisionType) bool emitUTF8, unsigned int precision, PrecisionType precisionType)
: rightMargin_(74), indentation_(JSONCPP_MOVE(indentation)), cs_(cs), : rightMargin_(74), indentation_(std::move(indentation)), cs_(cs),
colonSymbol_(JSONCPP_MOVE(colonSymbol)), colonSymbol_(std::move(colonSymbol)), nullSymbol_(std::move(nullSymbol)),
nullSymbol_(JSONCPP_MOVE(nullSymbol)), endingLineFeedSymbol_(std::move(endingLineFeedSymbol)),
endingLineFeedSymbol_(JSONCPP_MOVE(endingLineFeedSymbol)),
addChildValues_(false), indented_(false), addChildValues_(false), indented_(false),
useSpecialFloats_(useSpecialFloats), emitUTF8_(emitUTF8), useSpecialFloats_(useSpecialFloats), emitUTF8_(emitUTF8),
precision_(precision), precisionType_(precisionType) {} precision_(precision), precisionType_(precisionType) {}
@@ -932,7 +934,7 @@ int BuiltStyledStreamWriter::write(Value const& root, OStream* sout) {
writeValue(root); writeValue(root);
writeCommentAfterValueOnSameLine(root); writeCommentAfterValueOnSameLine(root);
*sout_ << endingLineFeedSymbol_; *sout_ << endingLineFeedSymbol_;
sout_ = JSONCPP_NULL; sout_ = nullptr;
return 0; return 0;
} }
void BuiltStyledStreamWriter::writeValue(Value const& value) { void BuiltStyledStreamWriter::writeValue(Value const& value) {
@@ -975,7 +977,7 @@ void BuiltStyledStreamWriter::writeValue(Value const& value) {
else { else {
writeWithIndent("{"); writeWithIndent("{");
indent(); indent();
Value::Members::const_iterator it = members.begin(); auto it = members.begin();
for (;;) { for (;;) {
String const& name = *it; String const& name = *it;
Value const& childValue = value[name]; Value const& childValue = value[name];
@@ -1151,11 +1153,11 @@ bool BuiltStyledStreamWriter::hasCommentForValue(const Value& value) {
/////////////// ///////////////
// StreamWriter // StreamWriter
StreamWriter::StreamWriter() : sout_(JSONCPP_NULL) {} StreamWriter::StreamWriter() : sout_(nullptr) {}
StreamWriter::~StreamWriter() {} StreamWriter::~StreamWriter() = default;
StreamWriter::Factory::~Factory() {} StreamWriter::Factory::~Factory() = default;
StreamWriterBuilder::StreamWriterBuilder() { setDefaults(&settings_); } StreamWriterBuilder::StreamWriterBuilder() { setDefaults(&settings_); }
StreamWriterBuilder::~StreamWriterBuilder() {} StreamWriterBuilder::~StreamWriterBuilder() = default;
StreamWriter* StreamWriterBuilder::newStreamWriter() const { StreamWriter* StreamWriterBuilder::newStreamWriter() const {
const String indentation = settings_["indentation"].asString(); const String indentation = settings_["indentation"].asString();
const String cs_str = settings_["commentStyle"].asString(); const String cs_str = settings_["commentStyle"].asString();
@@ -1175,9 +1177,9 @@ StreamWriter* StreamWriterBuilder::newStreamWriter() const {
} }
PrecisionType precisionType(significantDigits); PrecisionType precisionType(significantDigits);
if (pt_str == "significant") { if (pt_str == "significant") {
precisionType = significantDigits; precisionType = PrecisionType::significantDigits;
} else if (pt_str == "decimal") { } else if (pt_str == "decimal") {
precisionType = decimalPlaces; precisionType = PrecisionType::decimalPlaces;
} else { } else {
throwRuntimeError("precisionType must be 'significant' or 'decimal'"); throwRuntimeError("precisionType must be 'significant' or 'decimal'");
} }
@@ -1198,34 +1200,30 @@ StreamWriter* StreamWriterBuilder::newStreamWriter() const {
endingLineFeedSymbol, usf, emitUTF8, pre, endingLineFeedSymbol, usf, emitUTF8, pre,
precisionType); precisionType);
} }
static void getValidWriterKeys(std::set<String>* valid_keys) {
valid_keys->clear();
valid_keys->insert("indentation");
valid_keys->insert("commentStyle");
valid_keys->insert("enableYAMLCompatibility");
valid_keys->insert("dropNullPlaceholders");
valid_keys->insert("useSpecialFloats");
valid_keys->insert("emitUTF8");
valid_keys->insert("precision");
valid_keys->insert("precisionType");
}
bool StreamWriterBuilder::validate(Json::Value* invalid) const { bool StreamWriterBuilder::validate(Json::Value* invalid) const {
Json::Value my_invalid; static const auto& valid_keys = *new std::set<String>{
if (!invalid) "indentation",
invalid = &my_invalid; // so we do not need to test for NULL "commentStyle",
Json::Value& inv = *invalid; "enableYAMLCompatibility",
std::set<String> valid_keys; "dropNullPlaceholders",
getValidWriterKeys(&valid_keys); "useSpecialFloats",
Value::Members keys = settings_.getMemberNames(); "emitUTF8",
size_t n = keys.size(); "precision",
for (size_t i = 0; i < n; ++i) { "precisionType",
String const& key = keys[i]; };
if (valid_keys.find(key) == valid_keys.end()) { for (auto si = settings_.begin(); si != settings_.end(); ++si) {
inv[key] = settings_[key]; auto key = si.name();
} if (valid_keys.count(key))
continue;
if (invalid)
(*invalid)[std::move(key)] = *si;
else
return false;
} }
return inv.empty(); return invalid ? invalid->empty() : true;
} }
Value& StreamWriterBuilder::operator[](const String& key) { Value& StreamWriterBuilder::operator[](const String& key) {
return settings_[key]; return settings_[key];
} }
@@ -1247,7 +1245,6 @@ String writeString(StreamWriter::Factory const& factory, Value const& root) {
OStringStream sout; OStringStream sout;
StreamWriterPtr const writer(factory.newStreamWriter()); StreamWriterPtr const writer(factory.newStreamWriter());
writer->write(root, &sout); writer->write(root, &sout);
delete writer;
return sout.str(); return sout.str();
} }
@@ -1255,7 +1252,6 @@ OStream& operator<<(OStream& sout, Value const& root) {
StreamWriterBuilder builder; StreamWriterBuilder builder;
StreamWriterPtr const writer(builder.newStreamWriter()); StreamWriterPtr const writer(builder.newStreamWriter());
writer->write(root, &sout); writer->write(root, &sout);
delete writer;
return sout; return sout;
} }

View File

@@ -15,8 +15,10 @@ if(BUILD_SHARED_LIBS)
else() else()
add_definitions( -DJSON_DLL ) add_definitions( -DJSON_DLL )
endif() endif()
target_link_libraries(jsoncpp_test jsoncpp_lib)
else()
target_link_libraries(jsoncpp_test jsoncpp_static)
endif() endif()
target_link_libraries(jsoncpp_test jsoncpp_lib)
# another way to solve issue #90 # another way to solve issue #90
#set_target_properties(jsoncpp_test PROPERTIES COMPILE_FLAGS -ffloat-store) #set_target_properties(jsoncpp_test PROPERTIES COMPILE_FLAGS -ffloat-store)

View File

@@ -5,6 +5,7 @@
#include "fuzz.h" #include "fuzz.h"
#include <cstdint>
#include <json/config.h> #include <json/config.h>
#include <json/json.h> #include <json/json.h>
#include <memory> #include <memory>
@@ -40,14 +41,14 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
builder.settings_["collectComments"] = hash_settings & (1 << 9); builder.settings_["collectComments"] = hash_settings & (1 << 9);
builder.settings_["allowTrailingCommas_"] = hash_settings & (1 << 10); builder.settings_["allowTrailingCommas_"] = hash_settings & (1 << 10);
Json::CharReader* reader(builder.newCharReader()); std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
Json::Value root; Json::Value root;
const char* data_str = reinterpret_cast<const char*>(data); const auto data_str = reinterpret_cast<const char*>(data);
try { try {
reader->parse(data_str, data_str + size, &root, JSONCPP_NULL); reader->parse(data_str, data_str + size, &root, nullptr);
} catch (Json::Exception const&) { } catch (Json::Exception const&) {
} }
delete reader;
// Whether it succeeded or not doesn't matter. // Whether it succeeded or not doesn't matter.
return 0; return 0;
} }

View File

@@ -73,11 +73,10 @@ namespace JsonTest {
// class TestResult // class TestResult
// ////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////
TestResult::TestResult() TestResult::TestResult() {
: predicateId_(1), lastUsedPredicateId_(0), messageTarget_(JSONCPP_NULL) {
// The root predicate has id 0 // The root predicate has id 0
rootPredicateNode_.id_ = 0; rootPredicateNode_.id_ = 0;
rootPredicateNode_.next_ = JSONCPP_NULL; rootPredicateNode_.next_ = nullptr;
predicateStackTail_ = &rootPredicateNode_; predicateStackTail_ = &rootPredicateNode_;
} }
@@ -89,7 +88,7 @@ TestResult& TestResult::addFailure(const char* file, unsigned int line,
/// added. /// added.
unsigned int nestingLevel = 0; unsigned int nestingLevel = 0;
PredicateContext* lastNode = rootPredicateNode_.next_; PredicateContext* lastNode = rootPredicateNode_.next_;
for (; lastNode != JSONCPP_NULL; lastNode = lastNode->next_) { for (; lastNode != nullptr; lastNode = lastNode->next_) {
if (lastNode->id_ > lastUsedPredicateId_) // new PredicateContext if (lastNode->id_ > lastUsedPredicateId_) // new PredicateContext
{ {
lastUsedPredicateId_ = lastNode->id_; lastUsedPredicateId_ = lastNode->id_;
@@ -122,18 +121,17 @@ void TestResult::addFailureInfo(const char* file, unsigned int line,
TestResult& TestResult::popPredicateContext() { TestResult& TestResult::popPredicateContext() {
PredicateContext* lastNode = &rootPredicateNode_; PredicateContext* lastNode = &rootPredicateNode_;
while (lastNode->next_ != JSONCPP_NULL && while (lastNode->next_ != nullptr && lastNode->next_->next_ != nullptr) {
lastNode->next_->next_ != JSONCPP_NULL) {
lastNode = lastNode->next_; lastNode = lastNode->next_;
} }
// Set message target to popped failure // Set message target to popped failure
PredicateContext* tail = lastNode->next_; PredicateContext* tail = lastNode->next_;
if (tail != JSONCPP_NULL && tail->failure_ != JSONCPP_NULL) { if (tail != nullptr && tail->failure_ != nullptr) {
messageTarget_ = tail->failure_; messageTarget_ = tail->failure_;
} }
// Remove tail from list // Remove tail from list
predicateStackTail_ = lastNode; predicateStackTail_ = lastNode;
lastNode->next_ = JSONCPP_NULL; lastNode->next_ = nullptr;
return *this; return *this;
} }
@@ -149,9 +147,7 @@ void TestResult::printFailure(bool printTestName) const {
} }
// Print in reverse to display the callstack in the right order // Print in reverse to display the callstack in the right order
for (Failures::const_iterator it = failures_.begin(); it != failures_.end(); for (const auto& failure : failures_) {
++it) {
const Failure& failure = *it;
Json::String indent(failure.nestingLevel_ * 2, ' '); Json::String indent(failure.nestingLevel_ * 2, ' ');
if (failure.file_) { if (failure.file_) {
printf("%s%s(%u): ", indent.c_str(), failure.file_, failure.line_); printf("%s%s(%u): ", indent.c_str(), failure.file_, failure.line_);
@@ -185,7 +181,7 @@ Json::String TestResult::indentText(const Json::String& text,
} }
TestResult& TestResult::addToLastFailure(const Json::String& message) { TestResult& TestResult::addToLastFailure(const Json::String& message) {
if (messageTarget_ != JSONCPP_NULL) { if (messageTarget_ != nullptr) {
messageTarget_->message_ += message; messageTarget_->message_ += message;
} }
return *this; return *this;
@@ -206,9 +202,9 @@ TestResult& TestResult::operator<<(bool value) {
// class TestCase // class TestCase
// ////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////
TestCase::TestCase() : result_(JSONCPP_NULL) {} TestCase::TestCase() = default;
TestCase::~TestCase() {} TestCase::~TestCase() = default;
void TestCase::run(TestResult& result) { void TestCase::run(TestResult& result) {
result_ = &result; result_ = &result;
@@ -218,7 +214,7 @@ void TestCase::run(TestResult& result) {
// class Runner // class Runner
// ////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////
Runner::Runner() {} Runner::Runner() = default;
Runner& Runner::add(TestCaseFactory factory) { Runner& Runner::add(TestCaseFactory factory) {
tests_.push_back(factory); tests_.push_back(factory);
@@ -272,8 +268,7 @@ bool Runner::runAllTest(bool printSummary) const {
} }
return true; return true;
} }
for (size_t index = 0; index < failures.size(); ++index) { for (auto& result : failures) {
TestResult& result = failures[index];
result.printFailure(count > 1); result.printFailure(count > 1);
} }

View File

@@ -42,7 +42,7 @@ public:
/// Must be a POD to allow inline initialisation without stepping /// Must be a POD to allow inline initialisation without stepping
/// into the debugger. /// into the debugger.
struct PredicateContext { struct PredicateContext {
typedef unsigned int Id; using Id = unsigned int;
Id id_; Id id_;
const char* file_; const char* file_;
unsigned int line_; unsigned int line_;
@@ -61,7 +61,7 @@ public:
/// Not encapsulated to prevent step into when debugging failed assertions /// Not encapsulated to prevent step into when debugging failed assertions
/// Incremented by one on assertion predicate entry, decreased by one /// Incremented by one on assertion predicate entry, decreased by one
/// by addPredicateContext(). /// by addPredicateContext().
PredicateContext::Id predicateId_; PredicateContext::Id predicateId_{1};
/// \internal Implementation detail for predicate macros /// \internal Implementation detail for predicate macros
PredicateContext* predicateStackTail_; PredicateContext* predicateStackTail_;
@@ -70,7 +70,7 @@ public:
/// Adds an assertion failure. /// Adds an assertion failure.
TestResult& addFailure(const char* file, unsigned int line, TestResult& addFailure(const char* file, unsigned int line,
const char* expr = JSONCPP_NULL); const char* expr = nullptr);
/// Removes the last PredicateContext added to the predicate stack /// Removes the last PredicateContext added to the predicate stack
/// chained list. /// chained list.
@@ -84,9 +84,7 @@ public:
// Generic operator that will work with anything ostream can deal with. // Generic operator that will work with anything ostream can deal with.
template <typename T> TestResult& operator<<(const T& value) { template <typename T> TestResult& operator<<(const T& value) {
Json::OStringStream oss; Json::OStringStream oss;
oss.precision(16); oss << std::setprecision(16) << std::hexfloat << value;
oss.setf(std::ios_base::floatfield);
oss << value;
return addToLastFailure(oss.str()); return addToLastFailure(oss.str());
} }
@@ -104,13 +102,13 @@ private:
static Json::String indentText(const Json::String& text, static Json::String indentText(const Json::String& text,
const Json::String& indent); const Json::String& indent);
typedef std::deque<Failure> Failures; using Failures = std::deque<Failure>;
Failures failures_; Failures failures_;
Json::String name_; Json::String name_;
PredicateContext rootPredicateNode_; PredicateContext rootPredicateNode_;
PredicateContext::Id lastUsedPredicateId_; PredicateContext::Id lastUsedPredicateId_{0};
/// Failure which is the target of the messages added using operator << /// Failure which is the target of the messages added using operator <<
Failure* messageTarget_; Failure* messageTarget_{nullptr};
}; };
class TestCase { class TestCase {
@@ -124,14 +122,14 @@ public:
virtual const char* testName() const = 0; virtual const char* testName() const = 0;
protected: protected:
TestResult* result_; TestResult* result_{nullptr};
private: private:
virtual void runTestCase() = 0; virtual void runTestCase() = 0;
}; };
/// Function pointer type for TestCase factory /// Function pointer type for TestCase factory
typedef TestCase* (*TestCaseFactory)(); using TestCaseFactory = TestCase* (*)();
class Runner { class Runner {
public: public:
@@ -161,8 +159,8 @@ public:
static void printUsage(const char* appName); static void printUsage(const char* appName);
private: // prevents copy construction and assignment private: // prevents copy construction and assignment
Runner(const Runner& other) JSONCPP_CTOR_DELETE; Runner(const Runner& other) = delete;
Runner& operator=(const Runner& other) JSONCPP_CTOR_DELETE; Runner& operator=(const Runner& other) = delete;
private: private:
void listTests() const; void listTests() const;
@@ -170,7 +168,7 @@ private:
static void preventDialogOnCrash(); static void preventDialogOnCrash();
private: private:
typedef std::deque<TestCaseFactory> Factories; using Factories = std::deque<TestCaseFactory>;
Factories tests_; Factories tests_;
}; };
@@ -253,10 +251,8 @@ TestResult& checkStringEqual(TestResult& result, const Json::String& expected,
} \ } \
\ \
public: /* overridden from TestCase */ \ public: /* overridden from TestCase */ \
const char* testName() const JSONCPP_OVERRIDE { \ const char* testName() const override { return #FixtureType "/" #name; } \
return #FixtureType "/" #name; \ void runTestCase() override; \
} \
void runTestCase() JSONCPP_OVERRIDE; \
}; \ }; \
\ \
void Test##FixtureType##name::runTestCase() void Test##FixtureType##name::runTestCase()
@@ -280,10 +276,8 @@ TestResult& checkStringEqual(TestResult& result, const Json::String& expected,
} \ } \
\ \
public: /* overridden from TestCase */ \ public: /* overridden from TestCase */ \
const char* testName() const JSONCPP_OVERRIDE { \ const char* testName() const override { return #FixtureType "/" #name; } \
return #FixtureType "/" #name; \ void runTestCase() override; \
} \
void runTestCase() JSONCPP_OVERRIDE; \
}; \ }; \
\ \
static bool test##FixtureType##name##collect = \ static bool test##FixtureType##name##collect = \

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1 @@
{'//this is bad JSON.'}