mirror of
https://github.com/json-c/json-c.git
synced 2026-04-03 04:19:07 +08:00
Compare commits
127 Commits
json-c-0.1
...
disallow-c
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6bfab90c87 | ||
|
|
ecb7a4c9a3 | ||
|
|
906d1581de | ||
|
|
9b8d170d1f | ||
|
|
6e481aa061 | ||
|
|
781675758f | ||
|
|
833233faa8 | ||
|
|
31a22fb2da | ||
|
|
0051f2dbe0 | ||
|
|
0741ce3918 | ||
|
|
e3464a2ab4 | ||
|
|
61016412fb | ||
|
|
f2eac07129 | ||
|
|
e93ae70417 | ||
|
|
0bc2dd5e60 | ||
|
|
9c44c7ed52 | ||
|
|
401249cbdf | ||
|
|
66f7869219 | ||
|
|
dabed80523 | ||
|
|
f6c8eba84e | ||
|
|
76f819e51d | ||
|
|
eef983b1df | ||
|
|
c0cf4bc23b | ||
|
|
e85c8ada54 | ||
|
|
2c84476c77 | ||
|
|
407ddb4350 | ||
|
|
33337523f5 | ||
|
|
7ff7eab603 | ||
|
|
88bce2b38e | ||
|
|
a907f7dd1b | ||
|
|
ad8b8afa7d | ||
|
|
4186537557 | ||
|
|
9ff6b68baf | ||
|
|
1a7defaa69 | ||
|
|
c4a615a052 | ||
|
|
159822dd8a | ||
|
|
990acb4e19 | ||
|
|
502522a93d | ||
|
|
12ea386989 | ||
|
|
6dd8618170 | ||
|
|
3dad6941da | ||
|
|
f7e9d8e216 | ||
|
|
85ed501a3d | ||
|
|
077661f3d1 | ||
|
|
87127d31c6 | ||
|
|
1ee12100b6 | ||
|
|
0bcfb6bc84 | ||
|
|
bef40a342e | ||
|
|
2316dbae85 | ||
|
|
612ba56f06 | ||
|
|
469bc0e4bb | ||
|
|
ce3184243a | ||
|
|
9dbf2880cc | ||
|
|
a14a3a680c | ||
|
|
efc530594b | ||
|
|
3b8363fcdc | ||
|
|
538b046884 | ||
|
|
e4d9fbd52a | ||
|
|
1c38dea651 | ||
|
|
5a46a3b76d | ||
|
|
43d3118935 | ||
|
|
d5c5b2caec | ||
|
|
a86d7a8f5a | ||
|
|
5568916eb1 | ||
|
|
71d845e819 | ||
|
|
c8b0a90e95 | ||
|
|
9b36c72945 | ||
|
|
9803032b9d | ||
|
|
bdfdb5fe10 | ||
|
|
11311ed2a3 | ||
|
|
e9d3ab209a | ||
|
|
4d529f92dc | ||
|
|
d0f32a5a43 | ||
|
|
efd536af48 | ||
|
|
4d5507f5dc | ||
|
|
d1716fe431 | ||
|
|
1741bcd3ea | ||
|
|
343f24f920 | ||
|
|
6ec5e5842d | ||
|
|
b6ba9429d0 | ||
|
|
3cc0c47221 | ||
|
|
7b971b52aa | ||
|
|
debe8b70b7 | ||
|
|
79c147203e | ||
|
|
1a2fdc49e7 | ||
|
|
b1fb1508ee | ||
|
|
bc35549f90 | ||
|
|
d6f46ae104 | ||
|
|
c50bf9df9c | ||
|
|
57bef5edc4 | ||
|
|
777dd06be8 | ||
|
|
84248a7884 | ||
|
|
81f0807b63 | ||
|
|
57ea393004 | ||
|
|
9417f4e726 | ||
|
|
257b29c991 | ||
|
|
d1deed499f | ||
|
|
6eca65617a | ||
|
|
ac4dfa44cb | ||
|
|
9462c0a7b5 | ||
|
|
bdd5e03d6e | ||
|
|
4b0c6de760 | ||
|
|
253a5fa99d | ||
|
|
9749b0cb66 | ||
|
|
2e9b7456a5 | ||
|
|
5282e73600 | ||
|
|
8b35a78d29 | ||
|
|
22773b1e51 | ||
|
|
16208fc01a | ||
|
|
2a2d861bc0 | ||
|
|
213bb5caa1 | ||
|
|
d28ac67dde | ||
|
|
5d98f7825a | ||
|
|
f63573460c | ||
|
|
6ee0a35a20 | ||
|
|
11546bfd07 | ||
|
|
5d00384e0d | ||
|
|
49c3721a5a | ||
|
|
2f0942bdd7 | ||
|
|
6ba1adf8ef | ||
|
|
86b46cac07 | ||
|
|
f2fc1ca00a | ||
|
|
acccefd770 | ||
|
|
de5a64888a | ||
|
|
7e3eba72db | ||
|
|
9e6acc9a4e | ||
|
|
5accae04bb |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -27,6 +27,7 @@
|
||||
/tests/test_double_serializer
|
||||
/tests/test_float
|
||||
/tests/test_int_add
|
||||
/tests/test_int_get
|
||||
/tests/test_json_pointer
|
||||
/tests/test_locale
|
||||
/tests/test_null
|
||||
|
||||
58
.travis.yml
58
.travis.yml
@@ -1,8 +1,8 @@
|
||||
language: cpp
|
||||
matrix:
|
||||
include:
|
||||
# gcc
|
||||
# xenial
|
||||
|
||||
# ubuntu xenial 16.04
|
||||
# gcc 5 is the default on xenial
|
||||
- os: linux
|
||||
dist: xenial
|
||||
@@ -15,35 +15,37 @@ matrix:
|
||||
- doxygen
|
||||
- cmake
|
||||
env: CHECK="true"
|
||||
# bionic
|
||||
- os: linux
|
||||
dist: bionic
|
||||
compiler: gcc
|
||||
env: MATRIX_EVAL="CC=gcc-6 && CXX=g++-6"
|
||||
|
||||
# gcc 7 is the default on bionic
|
||||
# ubuntu bionic 18.04
|
||||
# gcc 7 is the default on bionic
|
||||
- os: linux
|
||||
dist: bionic
|
||||
compiler: gcc
|
||||
addons:
|
||||
apt:
|
||||
packages:
|
||||
- valgrind
|
||||
- cppcheck
|
||||
- doxygen
|
||||
- cmake
|
||||
env: CHECK="true"
|
||||
|
||||
# ubuntu focal fossa 20.04
|
||||
# gcc 9 is the default on bionic
|
||||
- os: linux
|
||||
dist: bionic
|
||||
dist: focal
|
||||
compiler: gcc
|
||||
env: MATRIX_EVAL="CC=gcc-8 && CXX=g++-8"
|
||||
addons:
|
||||
apt:
|
||||
packages:
|
||||
- valgrind
|
||||
- cppcheck
|
||||
- doxygen
|
||||
- cmake
|
||||
env: CHECK="true"
|
||||
|
||||
# clang
|
||||
# xenial
|
||||
- os: linux
|
||||
dist: xenial
|
||||
compiler: clang
|
||||
addons:
|
||||
apt:
|
||||
sources:
|
||||
- llvm-toolchain-xenial-5.0
|
||||
packages:
|
||||
- clang-5.0
|
||||
- cmake
|
||||
env: MATRIX_EVAL="CC=clang-5.0 && CXX=clang++-5.0"
|
||||
- os: linux
|
||||
dist: xenial
|
||||
compiler: clang
|
||||
@@ -56,9 +58,9 @@ matrix:
|
||||
- cmake
|
||||
env: MATRIX_EVAL="CC=clang-6.0 && CXX=clang++-6.0"
|
||||
|
||||
# clang-7 is the default on xenial and bionic
|
||||
# clang-7 is the default on focal, xenial and bionic
|
||||
- os: linux
|
||||
dist: xenial
|
||||
dist: focal
|
||||
compiler: clang
|
||||
addons:
|
||||
apt:
|
||||
@@ -69,17 +71,9 @@ matrix:
|
||||
- cmake
|
||||
env: CHECK="true"
|
||||
|
||||
# bionic
|
||||
- os: linux
|
||||
dist: bionic
|
||||
compiler: clang
|
||||
env: CHECK="true"
|
||||
# osx
|
||||
- os: osx
|
||||
osx_image: xcode9.4
|
||||
env: XCODE="true"
|
||||
- os: osx
|
||||
osx_image: xcode12.5
|
||||
osx_image: xcode13.4
|
||||
env: XCODE="true" CHECK="true"
|
||||
|
||||
# run coveralls
|
||||
|
||||
10
AUTHORS
10
AUTHORS
@@ -1,6 +1,7 @@
|
||||
Alan Coopersmith <alan.coopersmith@oracle.com>
|
||||
Alexander Dahl <post@lespocky.de>
|
||||
Alexandru Ardelean <ardeleanalex@gmail.com>
|
||||
An7ar35 <eadavison@protonmail.com>
|
||||
andy5995 <andy400-dev@yahoo.com>
|
||||
Aram Poghosyan <Aram.Poghosyan@teamviewer.com>
|
||||
Björn Esser <besser82@fedoraproject.org>
|
||||
@@ -11,13 +12,16 @@ Chris Lamb <lamby@debian.org>
|
||||
Christopher Head <chead@chead.ca>
|
||||
Chris Wolfe <chriswwolfe@gmail.com>
|
||||
C. Watford (christopher.watford@gmail.com)
|
||||
Daniel Danzberger <daniel@dd-wrt.com>
|
||||
Darjan Krijan <darjan_krijan@gmx.de>
|
||||
David McCann <mccannd@uk.ibm.com>
|
||||
DeX77 <dex@dragonslave.de>
|
||||
Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com>
|
||||
dota17 <chenguopingdota@163.com>
|
||||
Eric Haszlakiewicz <erh+git@nimenees.com>
|
||||
Eric Hawicz <erh+git@nimenees.com>
|
||||
Even Rouault <even.rouault@spatialys.com>
|
||||
Federico Francescon <federico.francescon@higeco.com>
|
||||
Gianluigi Tiesi <sherpya@netfarm.it>
|
||||
grdowns <grdowns@microsoft.com>
|
||||
Hex052 <elijahiff@gmail.com>
|
||||
@@ -27,6 +31,7 @@ Ivan Romanov <drizt@land.ru>
|
||||
Jaap Keuter <jaap.keuter@xs4all.nl>
|
||||
Jakov Smolic <jakov.smolic@sartura.hr>
|
||||
janczer <menshikov.ivn@gmail.com>
|
||||
JC (Jonathan Chen) <jc@dijonkitchen.org>
|
||||
Jehan <jehan@girinstud.io>
|
||||
Jehiah Czebotar <jehiah@gmail.com>
|
||||
Jonathan Wiens <j.wiens@teles.com>
|
||||
@@ -34,10 +39,13 @@ Jose Bollo <jose.bollo@iot.bzh>
|
||||
José Bollo <jose.bollo@iot.bzh>
|
||||
Juuso Alasuutari <juuso.alasuutari@gmail.com>
|
||||
Keith Holman <keith.holman@windriver.com>
|
||||
Khem Raj <raj.khem@gmail.com>
|
||||
Kizuna-Meraki <z9@kizunameraki.de>
|
||||
Leon Gross <leon.gross@rub.de>
|
||||
Liang, Gao <liang.gao@intel.com>
|
||||
Luca Mannella <luca.mannella@studenti.polito.it>
|
||||
Marc <34656315+MarcT512@users.noreply.github.com>
|
||||
Matthias Gatto <matthias.gatto@protonmail.com>
|
||||
max <mpano91@gmail.com>
|
||||
Micah Snyder <micasnyd@cisco.com>
|
||||
Michael Clark <michael@metaparadigm.com>
|
||||
@@ -53,9 +61,11 @@ Robert Bielik <robert.bielik@dirac.com>
|
||||
Robert <roby_p97@yahoo.com>
|
||||
Rosen Penev <rosenp@gmail.com>
|
||||
Rubasri Kalidas <rubasri.kalidas@intel.com>
|
||||
Sergey Sharshunov <s.sharshunov@gmail.com>
|
||||
Simon McVittie <smcv@collabora.com>
|
||||
ssrlive <30760636+ssrlive@users.noreply.github.com>
|
||||
Tobias Nießen <tniessen@users.noreply.github.com>
|
||||
Tobias Stoeckmann <tobias@stoeckmann.org>
|
||||
Tudor Brindus <me@tbrindus.ca>
|
||||
Unmanned Player <36690541+unmanned-player@users.noreply.github.com>
|
||||
Yurii Rashkovskii <yrashk@gmail.com>
|
||||
|
||||
159
CMakeLists.txt
159
CMakeLists.txt
@@ -1,31 +1,13 @@
|
||||
# Many projects still are stuck using CMake 2.8 is several places so it's good to provide backward support too. This is
|
||||
# specially true in old embedded systems (OpenWRT and friends) where CMake isn't necessarily upgraded.
|
||||
cmake_minimum_required(VERSION 2.8)
|
||||
|
||||
if(POLICY CMP0048)
|
||||
cmake_policy(SET CMP0048 NEW)
|
||||
endif()
|
||||
# CMake 3.9 was released in 2017/07
|
||||
# As of 2023, many versions of Linux, NetBSD and FreeBSD provide,
|
||||
# and many OpenWRT packages require, much newer CMake packages.
|
||||
# We're stopping before 3.10 because that version starts requiring
|
||||
# c++11, which isn't available on e.g HPUX.
|
||||
cmake_minimum_required(VERSION 3.9...3.12)
|
||||
|
||||
# JSON-C library is C only project.
|
||||
if (CMAKE_VERSION VERSION_LESS 3.0)
|
||||
project(json-c)
|
||||
set(PROJECT_VERSION_MAJOR "0")
|
||||
set(PROJECT_VERSION_MINOR "15")
|
||||
set(PROJECT_VERSION_PATCH "99")
|
||||
set(PROJECT_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}")
|
||||
else()
|
||||
project(json-c LANGUAGES C VERSION 0.15.99)
|
||||
endif()
|
||||
|
||||
# If we've got 3.0 then it's good, let's provide support. Otherwise, leave it be.
|
||||
if(POLICY CMP0038)
|
||||
# Policy CMP0038 introduced was in CMake 3.0
|
||||
cmake_policy(SET CMP0038 NEW)
|
||||
endif()
|
||||
|
||||
if(POLICY CMP0054)
|
||||
cmake_policy(SET CMP0054 NEW)
|
||||
endif()
|
||||
# PROJECT_VERSION{,_MAJOR,_MINOR,_PATCH} set by project():
|
||||
project(json-c LANGUAGES C VERSION 0.17.99)
|
||||
|
||||
# set default build type if not specified by user
|
||||
if(NOT CMAKE_BUILD_TYPE)
|
||||
@@ -34,24 +16,8 @@ endif()
|
||||
|
||||
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O2")
|
||||
|
||||
# Include file check macros honor CMAKE_REQUIRED_LIBRARIES
|
||||
# i.e. the check_include_file() calls will include -lm when checking.
|
||||
if(POLICY CMP0075)
|
||||
cmake_policy(SET CMP0075 NEW)
|
||||
endif()
|
||||
|
||||
include(CTest)
|
||||
|
||||
if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_TESTING AND
|
||||
(NOT MSVC OR NOT (MSVC_VERSION LESS 1800)) # Tests need at least VS2013
|
||||
)
|
||||
add_subdirectory(tests)
|
||||
endif()
|
||||
|
||||
if (NOT MSVC) # cmd line apps don't built on Windows currently.
|
||||
add_subdirectory(apps)
|
||||
endif()
|
||||
|
||||
# Set some packaging variables.
|
||||
set(CPACK_PACKAGE_NAME "${PROJECT_NAME}")
|
||||
set(CPACK_PACKAGE_VERSION_MAJOR "${PROJECT_VERSION_MAJOR}")
|
||||
@@ -102,7 +68,10 @@ option(ENABLE_RDRAND "Enable RDRAND Hardware RNG Hash Seed."
|
||||
option(ENABLE_THREADING "Enable partial threading support." OFF)
|
||||
option(OVERRIDE_GET_RANDOM_SEED "Override json_c_get_random_seed() with custom code." OFF)
|
||||
option(DISABLE_EXTRA_LIBS "Avoid linking against extra libraries, such as libbsd." OFF)
|
||||
option(DISABLE_JSON_POINTER "Disable JSON pointer (RFC6901) support." OFF)
|
||||
option(DISABLE_JSON_POINTER "Disable JSON pointer (RFC6901) and JSON patch support." OFF)
|
||||
option(DISABLE_JSON_PATCH "Disable JSON patch (RFC6902) support." OFF)
|
||||
option(NEWLOCALE_NEEDS_FREELOCALE "Work around newlocale bugs in old FreeBSD by calling freelocale" OFF)
|
||||
option(BUILD_APPS "Default to building apps" ON)
|
||||
|
||||
|
||||
if (UNIX OR MINGW OR CYGWIN)
|
||||
@@ -154,11 +123,14 @@ check_include_file(sys/random.h HAVE_SYS_RANDOM_H)
|
||||
check_include_file(sys/stat.h HAVE_SYS_STAT_H)
|
||||
check_include_file(xlocale.h HAVE_XLOCALE_H)
|
||||
|
||||
# Set json-c specific vars to stamp into json_config.h
|
||||
# in a way that hopefully won't conflict with other
|
||||
# projects that use json-c.
|
||||
if (HAVE_INTTYPES_H)
|
||||
# Set a json-c specific var to stamp into json_config.h
|
||||
# in a way that hopefully won't conflict with other
|
||||
# projects that use json-c.
|
||||
set(JSON_C_HAVE_INTTYPES_H 1)
|
||||
set(JSON_C_HAVE_INTTYPES_H 1)
|
||||
endif()
|
||||
if (HAVE_STDINT_H)
|
||||
set(JSON_C_HAVE_STDINT_H 1)
|
||||
endif()
|
||||
|
||||
check_symbol_exists(_isnan "float.h" HAVE_DECL__ISNAN)
|
||||
@@ -183,10 +155,12 @@ check_symbol_exists(arc4random "stdlib.h" HAVE_ARC4RANDOM)
|
||||
if (NOT HAVE_ARC4RANDOM AND DISABLE_EXTRA_LIBS STREQUAL "OFF")
|
||||
check_include_file(bsd/stdlib.h HAVE_BSD_STDLIB_H)
|
||||
if (HAVE_BSD_STDLIB_H)
|
||||
list(APPEND CMAKE_REQUIRED_LIBRARIES "-lbsd")
|
||||
link_libraries(bsd)
|
||||
list(APPEND CMAKE_REQUIRED_LIBRARIES "bsd")
|
||||
unset(HAVE_ARC4RANDOM CACHE)
|
||||
check_symbol_exists(arc4random "bsd/stdlib.h" HAVE_ARC4RANDOM)
|
||||
if (NOT HAVE_ARC4RANDOM)
|
||||
list(REMOVE_ITEM CMAKE_REQUIRED_LIBRARIES "bsd")
|
||||
endif()
|
||||
endif()
|
||||
endif()
|
||||
|
||||
@@ -200,6 +174,19 @@ if (HAVE_LOCALE_H)
|
||||
check_symbol_exists(setlocale "locale.h" HAVE_SETLOCALE)
|
||||
check_symbol_exists(uselocale "locale.h" HAVE_USELOCALE)
|
||||
endif()
|
||||
|
||||
# uClibc *intentionally* crashes in duplocale(), at least as of:
|
||||
# https://github.com/ffainelli/uClibc/blob/266bdc1/libc/misc/locale/locale.c#L1322
|
||||
# So, if it looks like we're compiling for a system like that just disable
|
||||
# locale handling entirely.
|
||||
execute_process (COMMAND ${CMAKE_C_COMPILER} -dumpmachine ERROR_QUIET OUTPUT_VARIABLE CMAKE_GNU_C_MACHINE)
|
||||
|
||||
if (CMAKE_GNU_C_MACHINE MATCHES "uclibc")
|
||||
message(STATUS "Detected uClibc compiler, disabling locale handling")
|
||||
set(HAVE_SETLOCALE 0)
|
||||
set(HAVE_USELOCALE 0)
|
||||
endif()
|
||||
|
||||
if (HAVE_STRINGS_H)
|
||||
check_symbol_exists(strcasecmp "strings.h" HAVE_STRCASECMP)
|
||||
check_symbol_exists(strncasecmp "strings.h" HAVE_STRNCASECMP)
|
||||
@@ -289,7 +276,24 @@ message(STATUS "Wrote ${PROJECT_BINARY_DIR}/config.h")
|
||||
configure_file(${PROJECT_SOURCE_DIR}/cmake/json_config.h.in ${PROJECT_BINARY_DIR}/json_config.h)
|
||||
message(STATUS "Wrote ${PROJECT_BINARY_DIR}/json_config.h")
|
||||
|
||||
if ("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU" OR "${CMAKE_C_COMPILER_ID}" STREQUAL "Clang")
|
||||
if (NOT DEFINED CMAKE_C_COMPILER_FRONTEND_VARIANT OR "${CMAKE_C_COMPILER_FRONTEND_VARIANT}" STREQUAL "") # only available in cmake 3.14+
|
||||
if ("${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC")
|
||||
set(CMAKE_C_COMPILER_FRONTEND_VARIANT "MSVC")
|
||||
elseif ("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU")
|
||||
set(CMAKE_C_COMPILER_FRONTEND_VARIANT "GNU")
|
||||
elseif ("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang")
|
||||
# Detect clang-cl.exe, it is Clang but with MSVC compatible command line arguments
|
||||
execute_process (COMMAND ${CMAKE_C_COMPILER} -? ERROR_QUIET OUTPUT_QUIET RESULT_VARIABLE _clang_result)
|
||||
if (_clang_result EQUAL 0)
|
||||
set(CMAKE_C_COMPILER_FRONTEND_VARIANT "MSVC")
|
||||
else()
|
||||
set(CMAKE_C_COMPILER_FRONTEND_VARIANT "GNU")
|
||||
message(STATUS, "clang result is NOT 0")
|
||||
endif()
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if ("${CMAKE_C_COMPILER_FRONTEND_VARIANT}" STREQUAL "GNU" OR "${CMAKE_C_COMPILER_FRONTEND_VARIANT}" STREQUAL "Clang")
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ffunction-sections -fdata-sections")
|
||||
if ("${DISABLE_WERROR}" STREQUAL "OFF")
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror")
|
||||
@@ -305,7 +309,12 @@ if ("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU" OR "${CMAKE_C_COMPILER_ID}" STREQUAL
|
||||
endif()
|
||||
|
||||
add_definitions(-D_GNU_SOURCE)
|
||||
elseif ("${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC")
|
||||
|
||||
if ("${CMAKE_C_COMPILER_FRONTEND_VARIANT}" STREQUAL "Clang")
|
||||
# Remove this for 1.0 when we can bump the ABI and actually fix these warnings.
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-shorten-64-to-32")
|
||||
endif()
|
||||
elseif ("${CMAKE_C_COMPILER_FRONTEND_VARIANT}" STREQUAL "MSVC")
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /DEBUG")
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4100")
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4996")
|
||||
@@ -316,7 +325,7 @@ elseif ("${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC")
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4701")
|
||||
endif()
|
||||
|
||||
if (NOT ("${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC"))
|
||||
if (NOT ("${CMAKE_C_COMPILER_FRONTEND_VARIANT}" STREQUAL "MSVC"))
|
||||
check_c_source_compiles(
|
||||
"
|
||||
/* uClibc toolchains without threading barf when _REENTRANT is defined */
|
||||
@@ -396,9 +405,9 @@ set(JSON_C_PUBLIC_HEADERS
|
||||
set(JSON_C_HEADERS
|
||||
${JSON_C_PUBLIC_HEADERS}
|
||||
${PROJECT_SOURCE_DIR}/json_object_private.h
|
||||
${PROJECT_SOURCE_DIR}/json_pointer_private.h
|
||||
${PROJECT_SOURCE_DIR}/random_seed.h
|
||||
${PROJECT_SOURCE_DIR}/strerror_override.h
|
||||
${PROJECT_SOURCE_DIR}/strerror_override_private.h
|
||||
${PROJECT_SOURCE_DIR}/math_compat.h
|
||||
${PROJECT_SOURCE_DIR}/snprintf_compat.h
|
||||
${PROJECT_SOURCE_DIR}/strdup_compat.h
|
||||
@@ -424,8 +433,15 @@ if (NOT DISABLE_JSON_POINTER)
|
||||
set(JSON_C_PUBLIC_HEADERS ${JSON_C_PUBLIC_HEADERS} ${PROJECT_SOURCE_DIR}/json_pointer.h)
|
||||
set(JSON_C_SOURCES ${JSON_C_SOURCES} ${PROJECT_SOURCE_DIR}/json_pointer.c)
|
||||
set(JSON_H_JSON_POINTER "#include \"json_pointer.h\"")
|
||||
|
||||
if (NOT DISABLE_JSON_PATCH)
|
||||
set(JSON_C_PUBLIC_HEADERS ${JSON_C_PUBLIC_HEADERS} ${PROJECT_SOURCE_DIR}/json_patch.h)
|
||||
set(JSON_C_SOURCES ${JSON_C_SOURCES} ${PROJECT_SOURCE_DIR}/json_patch.c)
|
||||
set(JSON_H_JSON_PATCH "#include \"json_patch.h\"")
|
||||
endif()
|
||||
else()
|
||||
set(JSON_H_JSON_POINTER "")
|
||||
set(JSON_H_JSON_PATCH "")
|
||||
endif()
|
||||
|
||||
configure_file(json.h.cmakein ${PROJECT_BINARY_DIR}/json.h @ONLY)
|
||||
@@ -454,7 +470,7 @@ add_library(${PROJECT_NAME}
|
||||
${JSON_C_HEADERS}
|
||||
)
|
||||
set_target_properties(${PROJECT_NAME} PROPERTIES
|
||||
VERSION 5.1.0
|
||||
VERSION 5.3.0
|
||||
SOVERSION 5)
|
||||
list(APPEND CMAKE_TARGETS ${PROJECT_NAME})
|
||||
# If json-c is used as subroject it set to target correct interface -I flags and allow
|
||||
@@ -465,6 +481,8 @@ target_include_directories(${PROJECT_NAME}
|
||||
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}>
|
||||
)
|
||||
|
||||
target_link_libraries(${PROJECT_NAME} PUBLIC ${CMAKE_REQUIRED_LIBRARIES})
|
||||
|
||||
# Allow to build static and shared libraries at the same time
|
||||
if (BUILD_STATIC_LIBS AND BUILD_SHARED_LIBS)
|
||||
set(STATIC_LIB ${PROJECT_NAME}-static)
|
||||
@@ -478,6 +496,8 @@ if (BUILD_STATIC_LIBS AND BUILD_SHARED_LIBS)
|
||||
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}>
|
||||
)
|
||||
|
||||
target_link_libraries(${PROJECT_NAME}-static PUBLIC ${CMAKE_REQUIRED_LIBRARIES})
|
||||
|
||||
# rename the static library
|
||||
if (NOT MSVC)
|
||||
set_target_properties(${STATIC_LIB} PROPERTIES
|
||||
@@ -523,7 +543,6 @@ install(
|
||||
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
|
||||
)
|
||||
|
||||
if (UNIX OR MINGW OR CYGWIN)
|
||||
SET(prefix ${CMAKE_INSTALL_PREFIX})
|
||||
# exec_prefix is prefix by default and CMake does not have the
|
||||
# concept.
|
||||
@@ -531,10 +550,38 @@ if (UNIX OR MINGW OR CYGWIN)
|
||||
SET(libdir ${CMAKE_INSTALL_FULL_LIBDIR})
|
||||
SET(includedir ${CMAKE_INSTALL_FULL_INCLUDEDIR})
|
||||
SET(VERSION ${PROJECT_VERSION})
|
||||
|
||||
# Linking against the static json-c requires
|
||||
# dependent packages to include additional libs:
|
||||
SET(LIBS_LIST ${CMAKE_REQUIRED_LIBRARIES})
|
||||
|
||||
# Note: We would need cmake >= 3.12 in order to use list(TRANSFORM ...)
|
||||
function(list_transform_prepend var prefix)
|
||||
set(temp "")
|
||||
foreach(f ${${var}})
|
||||
list(APPEND temp "${prefix}${f}")
|
||||
endforeach()
|
||||
set(${var} "${temp}" PARENT_SCOPE)
|
||||
endfunction()
|
||||
list_transform_prepend(LIBS_LIST "-l")
|
||||
|
||||
string(REPLACE ";" " " LIBS "${LIBS_LIST}")
|
||||
|
||||
configure_file(json-c.pc.in json-c.pc @ONLY)
|
||||
set(INSTALL_PKGCONFIG_DIR "${CMAKE_INSTALL_LIBDIR}/pkgconfig" CACHE PATH "Installation directory for pkgconfig (.pc) files")
|
||||
install(FILES ${PROJECT_BINARY_DIR}/json-c.pc DESTINATION "${INSTALL_PKGCONFIG_DIR}")
|
||||
endif ()
|
||||
|
||||
install(FILES ${JSON_C_PUBLIC_HEADERS} DESTINATION ${CMAKE_INSTALL_FULL_INCLUDEDIR}/json-c)
|
||||
|
||||
if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_TESTING AND
|
||||
(NOT MSVC OR NOT (MSVC_VERSION LESS 1800)) # Tests need at least VS2013
|
||||
)
|
||||
add_subdirectory(tests)
|
||||
endif()
|
||||
|
||||
if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_APPS)
|
||||
# skip apps when we're included in someone else's build
|
||||
if (NOT MSVC) # cmd line apps don't built on Windows currently.
|
||||
add_subdirectory(apps)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
35
ChangeLog
35
ChangeLog
@@ -1,4 +1,39 @@
|
||||
|
||||
0.17 (up to commit 077661f, 2023-08-08)
|
||||
========================================
|
||||
|
||||
Deprecated and removed features:
|
||||
--------------------------------
|
||||
* None
|
||||
|
||||
New features
|
||||
------------
|
||||
* json_patch: add first implementation only with patch application
|
||||
* Add --disable-static and --disable-dynamic options to the cmake-configure script.
|
||||
* Add -DBUILD_APPS=NO option to disable app build
|
||||
* Minimum cmake version is now 3.9
|
||||
|
||||
Significant changes and bug fixes
|
||||
---------------------------------
|
||||
* When serializing with JSON_C_TO_STRING_PRETTY set, keep the opening and
|
||||
closing curly or square braces on same line for empty objects or arrays.
|
||||
* Disable locale handling when targeting a uClibc system due to problems
|
||||
with its duplocale() function.
|
||||
* When parsing with JSON_TOKENER_STRICT set, integer overflow/underflow
|
||||
now result in a json_tokener_error_parse_number. Without that flag
|
||||
values are capped at INT64_MIN/UINT64_MAX.
|
||||
* Fix memory leak with emtpy strings in json_object_set_string
|
||||
* json_object_from_fd_ex: fail if file is too large (>=INT_MAX bytes)
|
||||
* Add back json_number_chars, but only because it's part of the public API.
|
||||
* Entirely drop mode bits from open(O_RDONLY) to avoid warnings on certain
|
||||
platforms.
|
||||
* Specify dependent libraries, including -lbsd, in a more consistent way so
|
||||
linking against a static json-c works better
|
||||
* Fix a variety of build problems and add & improve tests
|
||||
* Update RFC reference to https://www.rfc-editor.org/rfc/rfc8259
|
||||
|
||||
***
|
||||
|
||||
0.16 (up to commit 66dcdf5, 2022-04-13)
|
||||
========================================
|
||||
|
||||
|
||||
102
README.md
102
README.md
@@ -1,25 +1,28 @@
|
||||
\mainpage
|
||||
|
||||
`json-c`
|
||||
json-c
|
||||
========
|
||||
|
||||
1. [Overview and Build Status](#overview)
|
||||
2. [Building on Unix](#buildunix)
|
||||
2. [Getting Help](#gettinghelp)
|
||||
3. [Building on Unix](#buildunix)
|
||||
* [Prerequisites](#installprereq)
|
||||
* [Build commands](#buildcmds)
|
||||
3. [CMake options](#CMake)
|
||||
4. [Testing](#testing)
|
||||
5. [Building with `vcpkg`](#buildvcpkg)
|
||||
6. [Linking to libjson-c](#linking)
|
||||
7. [Using json-c](#using)
|
||||
4. [CMake options](#CMake)
|
||||
5. [Testing](#testing)
|
||||
6. [Building with `vcpkg`](#buildvcpkg)
|
||||
7. [Building for Android](#android)
|
||||
7. [Linking to libjson-c](#linking)
|
||||
8. [Using json-c](#using)
|
||||
|
||||
JSON-C - A JSON implementation in C <a name="overview"></a>
|
||||
<a name="overview"></a>
|
||||
JSON-C - A JSON implementation in C
|
||||
-----------------------------------
|
||||
|
||||
JSON-C implements a reference counting object model that allows you to easily
|
||||
construct JSON objects in C, output them as JSON formatted strings and parse
|
||||
JSON formatted strings back into the C representation of JSON objects.
|
||||
It aims to conform to [RFC 7159](https://tools.ietf.org/html/rfc7159).
|
||||
It aims to conform to [RFC 8259](https://www.rfc-editor.org/rfc/rfc8259).
|
||||
|
||||
Skip down to [Using json-c](#using)
|
||||
or check out the [API docs](https://json-c.github.io/json-c/),
|
||||
@@ -27,20 +30,41 @@ if you already have json-c installed and ready to use.
|
||||
|
||||
Home page for json-c: https://github.com/json-c/json-c/wiki
|
||||
|
||||
Build Status
|
||||
* [AppVeyor Build](https://ci.appveyor.com/project/hawicz/json-c) 
|
||||
* [Travis Build](https://travis-ci.org/json-c/json-c) 
|
||||
<a name="gettinghelp"></a>
|
||||
Getting Help
|
||||
------------
|
||||
|
||||
Test Status
|
||||
* [Coveralls](https://coveralls.io/github/json-c/json-c?branch=master) [](https://coveralls.io/github/json-c/json-c?branch=master)
|
||||
If you have questions about using json-c, please start a thread on
|
||||
our forums at: https://groups.google.com/forum/#!forum/json-c
|
||||
|
||||
Building on Unix with `git`, `gcc` and `cmake` <a name="buildunix"></a>
|
||||
If you believe you've discovered a bug, report it at
|
||||
(https://github.com/json-c/json-c/issues). Please be sure to include
|
||||
the version of json-c you're using, the OS you're running on, and any
|
||||
other relevant details. Fully reproducible test cases and/or patches
|
||||
to fix problems are greatly appreciated.
|
||||
|
||||
Fixes for bugs, or small new features can be directly submitted as a
|
||||
[pull request](https://github.com/json-c/json-c/pulls). For major new
|
||||
features or large changes of any kind, please first start a discussion
|
||||
on the [forums](https://groups.google.com/forum/#!forum/json-c).
|
||||
|
||||
|
||||
<a name="buildunix"></a>
|
||||
Building on Unix with `git`, `gcc` and `cmake`
|
||||
--------------------------------------------------
|
||||
|
||||
If you already have json-c installed, see [Linking to `libjson-c`](#linking)
|
||||
for how to build and link your program against it.
|
||||
|
||||
### Prerequisites: <a name="installprereq"></a>
|
||||
Build Status
|
||||
* [AppVeyor Build](https://ci.appveyor.com/project/hawicz/json-c) 
|
||||
* [Travis Build](https://app.travis-ci.com/github/json-c/json-c) 
|
||||
|
||||
Test Status
|
||||
* [Coveralls](https://coveralls.io/github/json-c/json-c?branch=master) [](https://coveralls.io/github/json-c/json-c?branch=master)
|
||||
|
||||
<a name="installprereq"></a>
|
||||
### Prerequisites:
|
||||
|
||||
- `gcc`, `clang`, or another C compiler
|
||||
|
||||
@@ -60,7 +84,8 @@ sudo apt install doxygen # optional
|
||||
sudo apt install valgrind # optional
|
||||
```
|
||||
|
||||
### Build instructions: <a name="buildcmds"></a>
|
||||
<a name="buildcmds"></a>
|
||||
### Build instructions:
|
||||
|
||||
`json-c` GitHub repo: https://github.com/json-c/json-c
|
||||
|
||||
@@ -81,7 +106,7 @@ Then:
|
||||
$ make
|
||||
$ make test
|
||||
$ make USE_VALGRIND=0 test # optionally skip using valgrind
|
||||
$ make install
|
||||
$ sudo make install # it could be necessary to execute make install
|
||||
```
|
||||
|
||||
|
||||
@@ -96,7 +121,8 @@ google-chrome doc/html/index.html
|
||||
```
|
||||
|
||||
|
||||
CMake Options <a name="CMake"></a>
|
||||
<a name="CMake"></a>
|
||||
CMake Options
|
||||
--------------------
|
||||
|
||||
The json-c library is built with [CMake](https://cmake.org/cmake-tutorial/),
|
||||
@@ -112,6 +138,8 @@ DISABLE_STATIC_FPIC | Bool | The default builds position independent
|
||||
DISABLE_BSYMBOLIC | Bool | Disable use of -Bsymbolic-functions.
|
||||
DISABLE_THREAD_LOCAL_STORAGE | Bool | Disable use of Thread-Local Storage (HAVE___THREAD).
|
||||
DISABLE_WERROR | Bool | Disable use of -Werror.
|
||||
DISABLE_EXTRA_LIBS | Bool | Disable use of extra libraries, libbsd
|
||||
DISABLE_JSON_POINTER | Bool | Omit json_pointer support from the build.
|
||||
ENABLE_RDRAND | Bool | Enable RDRAND Hardware RNG Hash Seed.
|
||||
ENABLE_THREADING | Bool | Enable partial threading support.
|
||||
OVERRIDE_GET_RANDOM_SEED | String | A block of code to use instead of the default implementation of json_c_get_random_seed(), e.g. on embedded platforms where not even the fallback to time() works. Must be a single line.
|
||||
@@ -166,7 +194,8 @@ cmake-configure can take a few options.
|
||||
| disable-werror | Avoid treating compiler warnings as fatal errors |
|
||||
|
||||
|
||||
Testing: <a name="testing"></a>
|
||||
<a name="testing"></a>
|
||||
Testing:
|
||||
----------
|
||||
|
||||
By default, if valgrind is available running tests uses it.
|
||||
@@ -202,7 +231,8 @@ JSONC_TEST_TRACE=1 make test
|
||||
and check the log files again.
|
||||
|
||||
|
||||
Building on Unix and Windows with `vcpkg` <a name="buildvcpkg"></a>
|
||||
<a name="buildvcpkg"></a>
|
||||
Building on Unix and Windows with `vcpkg`
|
||||
--------------------------------------------------
|
||||
|
||||
You can download and install JSON-C using the [vcpkg](https://github.com/Microsoft/vcpkg/) dependency manager:
|
||||
@@ -215,8 +245,33 @@ You can download and install JSON-C using the [vcpkg](https://github.com/Microso
|
||||
|
||||
The JSON-C 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.
|
||||
|
||||
<a name="android">
|
||||
Building for Android
|
||||
----------------------
|
||||
|
||||
Linking to `libjson-c` <a name="linking">
|
||||
Building on Android is now particularly well supported, but there
|
||||
have been some reports of success using
|
||||
https://developer.android.com/ndk/guides/cmake
|
||||
|
||||
```
|
||||
mkdir json-c-build
|
||||
cd json-c-build/
|
||||
export NDK_HOME=~/Library/Android/sdk/ndk/22.1.7171670/
|
||||
cmake \
|
||||
--toolchain=$NDK_HOME/build/cmake/android.toolchain.cmake \
|
||||
-DANDROID_STL=none \
|
||||
-DANDROID_ABI=arm64-v8a \
|
||||
-DANDROID_PLATFORM=android-29 \
|
||||
-DANDROID_LD=lld \
|
||||
-DCMAKE_BUILD_TYPE=MinSizeRel \
|
||||
-DCMAKE_INSTALL_PREFIX=<install prefix> \
|
||||
-DENABLE_THREADING=true \
|
||||
..
|
||||
make install
|
||||
```
|
||||
|
||||
<a name="linking">
|
||||
Linking to `libjson-c`
|
||||
----------------------
|
||||
|
||||
If your system has `pkgconfig`,
|
||||
@@ -253,7 +308,8 @@ cd build
|
||||
cmake -DCMAKE_PREFIX_PATH=/path/to/json_c/install/lib64/cmake ..
|
||||
```
|
||||
|
||||
Using json-c <a name="using">
|
||||
<a name="using">
|
||||
Using json-c
|
||||
------------
|
||||
|
||||
To use json-c you can either include json.h, or preferably, one of the
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
changes (added/removed/updated funcs, etc...), and detect backwards compat
|
||||
issues.
|
||||
* https://github.com/lvc/abi-compliance-checker
|
||||
* See also `abi-check.sh`
|
||||
* If the new release is not backwards compatible, then this is a MAJOR release.
|
||||
* Mention removed features in ChangeLog
|
||||
* Consider re-adding backwards compatible support, through symbol
|
||||
@@ -40,9 +41,15 @@
|
||||
## Release creation
|
||||
|
||||
Start creating the new release:
|
||||
release=0.16
|
||||
|
||||
PREV=$(git tag | tail -1)
|
||||
PREV=${PREV#json-c-}
|
||||
PREV=${PREV%-*}
|
||||
release=0.$((${PREV#*.} + 1))
|
||||
cd ~
|
||||
git clone https://github.com/json-c/json-c json-c-${release}
|
||||
|
||||
rm -rf distcheck
|
||||
mkdir distcheck
|
||||
cd distcheck
|
||||
# Note, the build directory *must* be entirely separate from
|
||||
@@ -54,8 +61,7 @@ Start creating the new release:
|
||||
Make any fixes/changes *before* branching.
|
||||
|
||||
cd json-c-${release}
|
||||
git branch json-c-${release}
|
||||
git checkout json-c-${release}
|
||||
git checkout -b json-c-${release}
|
||||
|
||||
------------
|
||||
|
||||
@@ -94,7 +100,11 @@ Create the release tarballs:
|
||||
echo .git > excludes
|
||||
tar -czf json-c-${release}.tar.gz -X excludes json-c-${release}
|
||||
|
||||
echo 'doc/*' >> excludes
|
||||
echo 'doc/*.cmake' >> excludes
|
||||
echo 'doc/CMakeFiles' >> excludes
|
||||
echo 'doc/Makefile' >> excludes
|
||||
echo 'doc/Doxyfile' >> excludes
|
||||
echo 'doc/html' >> excludes
|
||||
tar -czf json-c-${release}-nodoc.tar.gz -X excludes json-c-${release}
|
||||
|
||||
------------
|
||||
@@ -113,7 +123,8 @@ Go to Amazon S3 service at:
|
||||
https://console.aws.amazon.com/s3/
|
||||
|
||||
Upload the two tarballs in the json-c_releases/releases folder.
|
||||
When uploading, use "Standard" storage class, and make the uploaded files publicly accessible.
|
||||
* Expand "Permissions", pick "Grant public-read access"
|
||||
* Expand "Properties", ensure "Standard" storage class is picked.
|
||||
|
||||
Logout of Amazon S3, and verify that the files are visible.
|
||||
https://s3.amazonaws.com/json-c_releases/releases/index.html
|
||||
@@ -130,12 +141,10 @@ Use ${release}.99 to indicate a version "newer" than anything on the branch:
|
||||
Update the version in json_c_version.h
|
||||
Update the version in CMakeLists.txt
|
||||
|
||||
Update RELEASE_CHECKLIST.txt, set release=${release}+1
|
||||
Update the set_target_properties() line in CmakeLists.txt to match the release branch.
|
||||
|
||||
Add a new empty section to the json-c.sym file, for ${release}+1
|
||||
|
||||
Update the set_target_properties() line in CmakeLists.txt to match the release branch.
|
||||
|
||||
git commit -a -m "Update the master branch to version ${release}.99"
|
||||
git push
|
||||
|
||||
@@ -172,7 +181,8 @@ Update checksums on wiki page.
|
||||
openssl md5 json-c*gz
|
||||
|
||||
Copy and paste this output into the wiki page at:
|
||||
https://github.com/json-c/json-c/wiki
|
||||
* https://github.com/json-c/json-c/wiki
|
||||
* https://github.com/json-c/json-c/wiki/Old-Releases
|
||||
|
||||
------------
|
||||
|
||||
|
||||
42
abi-check.sh
Normal file
42
abi-check.sh
Normal file
@@ -0,0 +1,42 @@
|
||||
#!/bin/sh
|
||||
|
||||
prev=0.16
|
||||
release=0.17
|
||||
|
||||
# ... clone json-c, abi-compliance-checker, abi-dumper
|
||||
|
||||
mkdir build
|
||||
cd build
|
||||
CFLAGS=-Og cmake -DCMAKE_INSTALL_PREFIX=~/json-c-installs/json-c-${release} ..
|
||||
make && make test && make install
|
||||
|
||||
# Assume the old version has already been built
|
||||
|
||||
cd ~/abi-compliance-checker
|
||||
mkxml()
|
||||
{
|
||||
ver="$1"
|
||||
cat <<EOF > json-c-${ver}.xml
|
||||
<foo>
|
||||
<version>
|
||||
${ver}
|
||||
</version>
|
||||
|
||||
<headers>
|
||||
../json-c-installs/json-c-${ver}/include/json-c
|
||||
</headers>
|
||||
|
||||
<libs>
|
||||
../json-c-installs/json-c-${ver}/lib64/libjson-c.so
|
||||
</libs>
|
||||
</foo>
|
||||
EOF
|
||||
}
|
||||
mkxml ${release}
|
||||
mkxml ${prev}
|
||||
|
||||
perl abi-compliance-checker.pl -lib json-c -dump json-c-${prev}.xml -dump-path ./ABI-${prev}.dump
|
||||
perl abi-compliance-checker.pl -lib json-c -dump json-c-${release}.xml -dump-path ./ABI-${release}.dump
|
||||
perl abi-compliance-checker.pl -l json-c -old ABI-${prev}.dump -new ABI-${release}.dump
|
||||
|
||||
echo "look in compat_reports/json-c/..."
|
||||
@@ -22,16 +22,30 @@
|
||||
#include <sys/time.h>
|
||||
#endif
|
||||
|
||||
static int formatted_output = 0;
|
||||
#ifndef JSON_NORETURN
|
||||
#if defined(_MSC_VER)
|
||||
#define JSON_NORETURN __declspec(noreturn)
|
||||
#elif defined(__OS400__)
|
||||
#define JSON_NORETURN
|
||||
#else
|
||||
/* 'cold' attribute is for optimization, telling the computer this code
|
||||
* path is unlikely.
|
||||
*/
|
||||
#define JSON_NORETURN __attribute__((noreturn, cold))
|
||||
#endif
|
||||
#endif
|
||||
|
||||
static int formatted_output = JSON_C_TO_STRING_SPACED;
|
||||
static int show_output = 1;
|
||||
static int strict_mode = 0;
|
||||
static int color = 0;
|
||||
static const char *fname = NULL;
|
||||
|
||||
#ifndef HAVE_JSON_TOKENER_GET_PARSE_END
|
||||
#define json_tokener_get_parse_end(tok) ((tok)->char_offset)
|
||||
#endif
|
||||
|
||||
static void usage(const char *argv0, int exitval, const char *errmsg);
|
||||
JSON_NORETURN static void usage(const char *argv0, int exitval, const char *errmsg);
|
||||
static void showmem(void);
|
||||
static int parseit(int fd, int (*callback)(struct json_object *));
|
||||
static int showobj(struct json_object *new_obj);
|
||||
@@ -42,7 +56,7 @@ static void showmem(void)
|
||||
struct rusage rusage;
|
||||
memset(&rusage, 0, sizeof(rusage));
|
||||
getrusage(RUSAGE_SELF, &rusage);
|
||||
printf("maxrss: %ld KB\n", rusage.ru_maxrss);
|
||||
fprintf(stderr, "maxrss: %ld KB\n", rusage.ru_maxrss);
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -50,7 +64,7 @@ static int parseit(int fd, int (*callback)(struct json_object *))
|
||||
{
|
||||
struct json_object *obj;
|
||||
char buf[32768];
|
||||
int ret;
|
||||
ssize_t ret;
|
||||
int depth = JSON_TOKENER_DEFAULT_DEPTH;
|
||||
json_tokener *tok;
|
||||
|
||||
@@ -73,20 +87,21 @@ static int parseit(int fd, int (*callback)(struct json_object *))
|
||||
size_t total_read = 0;
|
||||
while ((ret = read(fd, buf, sizeof(buf))) > 0)
|
||||
{
|
||||
total_read += ret;
|
||||
int start_pos = 0;
|
||||
while (start_pos != ret)
|
||||
size_t retu = (size_t)ret; // We know it's positive
|
||||
total_read += retu;
|
||||
size_t start_pos = 0;
|
||||
while (start_pos != retu)
|
||||
{
|
||||
obj = json_tokener_parse_ex(tok, &buf[start_pos], ret - start_pos);
|
||||
obj = json_tokener_parse_ex(tok, &buf[start_pos], retu - start_pos);
|
||||
enum json_tokener_error jerr = json_tokener_get_error(tok);
|
||||
int parse_end = json_tokener_get_parse_end(tok);
|
||||
size_t parse_end = json_tokener_get_parse_end(tok);
|
||||
if (obj == NULL && jerr != json_tokener_continue)
|
||||
{
|
||||
char *aterr = (start_pos + parse_end < sizeof(buf)) ?
|
||||
const char *aterr = (start_pos + parse_end < (int)sizeof(buf)) ?
|
||||
&buf[start_pos + parse_end] : "";
|
||||
fflush(stdout);
|
||||
int fail_offset = total_read - ret + start_pos + parse_end;
|
||||
fprintf(stderr, "Failed at offset %d: %s %c\n", fail_offset,
|
||||
size_t fail_offset = total_read - retu + start_pos + parse_end;
|
||||
fprintf(stderr, "Failed at offset %lu: %s %c\n", (unsigned long)fail_offset,
|
||||
json_tokener_error_desc(jerr), aterr[0]);
|
||||
json_tokener_free(tok);
|
||||
return 1;
|
||||
@@ -102,7 +117,7 @@ static int parseit(int fd, int (*callback)(struct json_object *))
|
||||
}
|
||||
}
|
||||
start_pos += json_tokener_get_parse_end(tok);
|
||||
assert(start_pos <= ret);
|
||||
assert(start_pos <= retu);
|
||||
}
|
||||
}
|
||||
if (ret < 0)
|
||||
@@ -122,15 +137,12 @@ static int showobj(struct json_object *new_obj)
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("Successfully parsed object from %s\n", fname);
|
||||
fprintf(stderr, "Successfully parsed object from %s\n", fname);
|
||||
|
||||
if (show_output)
|
||||
{
|
||||
const char *output;
|
||||
if (formatted_output)
|
||||
output = json_object_to_json_string(new_obj);
|
||||
else
|
||||
output = json_object_to_json_string_ext(new_obj, JSON_C_TO_STRING_PRETTY);
|
||||
output = json_object_to_json_string_ext(new_obj, formatted_output | color);
|
||||
printf("%s\n", output);
|
||||
}
|
||||
|
||||
@@ -145,11 +157,14 @@ static void usage(const char *argv0, int exitval, const char *errmsg)
|
||||
fp = stderr;
|
||||
if (errmsg != NULL)
|
||||
fprintf(fp, "ERROR: %s\n\n", errmsg);
|
||||
fprintf(fp, "Usage: %s [-f] [-n] [-s]\n", argv0);
|
||||
fprintf(fp, " -f - Format the output with JSON_C_TO_STRING_PRETTY\n");
|
||||
fprintf(fp, "Usage: %s [-f|-F <arg>] [-n] [-s]\n", argv0);
|
||||
fprintf(fp, " -f - Format the output to stdout with JSON_C_TO_STRING_PRETTY (default is JSON_C_TO_STRING_SPACED)\n");
|
||||
fprintf(fp, " -F - Format the output to stdout with <arg>, e.g. 0 for JSON_C_TO_STRING_PLAIN\n");
|
||||
fprintf(fp, " -n - No output\n");
|
||||
fprintf(fp, " -c - color\n");
|
||||
fprintf(fp, " -s - Parse in strict mode, flags:\n");
|
||||
fprintf(fp, " JSON_TOKENER_STRICT|JSON_TOKENER_ALLOW_TRAILING_CHARS\n");
|
||||
fprintf(fp, " Diagnostic information will be emitted to stderr\n");
|
||||
|
||||
fprintf(fp, "\nWARNING WARNING WARNING\n");
|
||||
fprintf(fp, "This is a prototype, it may change or be removed at any time!\n");
|
||||
@@ -158,16 +173,17 @@ static void usage(const char *argv0, int exitval, const char *errmsg)
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
json_object *new_obj;
|
||||
int opt;
|
||||
|
||||
while ((opt = getopt(argc, argv, "fhns")) != -1)
|
||||
while ((opt = getopt(argc, argv, "fF:hnsc")) != -1)
|
||||
{
|
||||
switch (opt)
|
||||
{
|
||||
case 'f': formatted_output = 1; break;
|
||||
case 'f': formatted_output = JSON_C_TO_STRING_PRETTY; break;
|
||||
case 'F': formatted_output = atoi(optarg); break;
|
||||
case 'n': show_output = 0; break;
|
||||
case 's': strict_mode = 1; break;
|
||||
case 'c': color = JSON_C_TO_STRING_COLOR; break;
|
||||
case 'h': usage(argv[0], 0, NULL);
|
||||
default: /* '?' */ usage(argv[0], EXIT_FAILURE, "Unknown arguments");
|
||||
}
|
||||
|
||||
21
arraylist.c
21
arraylist.c
@@ -125,6 +125,27 @@ int array_list_shrink(struct array_list *arr, size_t empty_slots)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int array_list_insert_idx(struct array_list *arr, size_t idx, void *data)
|
||||
{
|
||||
size_t move_amount;
|
||||
|
||||
if (idx >= arr->length)
|
||||
return array_list_put_idx(arr, idx, data);
|
||||
|
||||
/* we're at full size, what size_t can support */
|
||||
if (arr->length == SIZE_T_MAX)
|
||||
return -1;
|
||||
|
||||
if (array_list_expand_internal(arr, arr->length + 1))
|
||||
return -1;
|
||||
|
||||
move_amount = (arr->length - idx) * sizeof(void *);
|
||||
memmove(arr->array + idx + 1, arr->array + idx, move_amount);
|
||||
arr->array[idx] = data;
|
||||
arr->length++;
|
||||
return 0;
|
||||
}
|
||||
|
||||
//static inline int _array_list_put_idx(struct array_list *arr, size_t idx, void *data)
|
||||
int array_list_put_idx(struct array_list *arr, size_t idx, void *data)
|
||||
{
|
||||
|
||||
@@ -62,6 +62,8 @@ extern void array_list_free(struct array_list *al);
|
||||
|
||||
extern void *array_list_get_idx(struct array_list *al, size_t i);
|
||||
|
||||
extern int array_list_insert_idx(struct array_list *al, size_t i, void *data);
|
||||
|
||||
extern int array_list_put_idx(struct array_list *al, size_t i, void *data);
|
||||
|
||||
extern int array_list_add(struct array_list *al, void *data);
|
||||
|
||||
@@ -65,9 +65,15 @@ while [ $# -gt 0 ] ; do
|
||||
--enable-shared)
|
||||
FLAGS+=(-DBUILD_SHARED_LIBS=ON)
|
||||
;;
|
||||
--disable-shared)
|
||||
FLAGS+=(-DBUILD_SHARED_LIBS=OFF)
|
||||
;;
|
||||
--enable-static)
|
||||
FLAGS+=(-DBUILD_STATIC_LIBS=ON)
|
||||
;;
|
||||
--disable-static)
|
||||
FLAGS+=(-DBUILD_STATIC_LIBS=OFF)
|
||||
;;
|
||||
--disable-Bsymbolic)
|
||||
FLAGS+=(-DDISABLE_BSYMBOLIC=ON)
|
||||
;;
|
||||
|
||||
@@ -137,6 +137,9 @@
|
||||
/* Define to 1 if you have the `uselocale' function. */
|
||||
#cmakedefine HAVE_USELOCALE
|
||||
|
||||
/* Define to 1 if newlocale() needs freelocale() called on it's `base` argument */
|
||||
#cmakedefine NEWLOCALE_NEEDS_FREELOCALE
|
||||
|
||||
/* Define to 1 if you have the `vasprintf' function. */
|
||||
#cmakedefine HAVE_VASPRINTF
|
||||
|
||||
|
||||
@@ -1,2 +1,5 @@
|
||||
/* Define to 1 if you have the <inttypes.h> header file. */
|
||||
#cmakedefine JSON_C_HAVE_INTTYPES_H @JSON_C_HAVE_INTTYPES_H@
|
||||
|
||||
/* Define to 1 if you have the <stdint.h> header file. */
|
||||
#cmakedefine JSON_C_HAVE_STDINT_H @JSON_C_HAVE_STDINT_H@
|
||||
|
||||
@@ -1103,7 +1103,7 @@ HTML_STYLESHEET =
|
||||
# cascading style sheets that are included after the standard style sheets
|
||||
# created by doxygen. Using this option one can overrule certain style aspects.
|
||||
# This is preferred over using HTML_STYLESHEET since it does not replace the
|
||||
# standard style sheet and is therefor more robust against future updates.
|
||||
# standard style sheet and is therefore more robust against future updates.
|
||||
# Doxygen will copy the style sheet files to the output directory.
|
||||
# Note: The order of the extra stylesheet files is of importance (e.g. the last
|
||||
# stylesheet in the list overrules the setting of the previous ones in the
|
||||
|
||||
@@ -8,11 +8,13 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
|
||||
json_tokener *tok = json_tokener_new();
|
||||
json_object *obj = json_tokener_parse_ex(tok, data1, size);
|
||||
|
||||
json_object_object_foreach(jobj, key, val) {
|
||||
(void)json_object_get_type(val);
|
||||
(void)json_object_get_string(val);
|
||||
if (json_object_is_type(obj, json_type_object)) {
|
||||
json_object_object_foreach(obj, key, val) {
|
||||
(void)json_object_get_type(val);
|
||||
(void)json_object_get_string(val);
|
||||
}
|
||||
}
|
||||
(void)json_object_to_json_string(obj, JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_SPACED);
|
||||
(void)json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_SPACED);
|
||||
|
||||
json_object_put(obj);
|
||||
json_tokener_free(tok);
|
||||
|
||||
88
issues_closed_for_0.17.md
Normal file
88
issues_closed_for_0.17.md
Normal file
@@ -0,0 +1,88 @@
|
||||
This list was created with:
|
||||
|
||||
```
|
||||
PREV=2022-04-13
|
||||
NOW=2023-08-12
|
||||
curl "https://api.github.com/search/issues?q=repo%3Ajson-c%2Fjson-c+closed%3A>${PREV}+created%3A<${NOW}&sort=created&order=asc&per_page=100&page=1" > issues1.out
|
||||
jq -r '.items[] | "[" + .title + "](" + .url + ")" | tostring' issues?.out > issues.md
|
||||
sed -e's,^\[ *\(.*\)\](https://api.github.com/.*/\([0-9].*\)),* [Issue #\2](https://github.com/json-c/json-c/issues/\2) - \1,' -i issues.md
|
||||
cat issues.md >> issues_closed_for_0.17.md
|
||||
```
|
||||
|
||||
* [Issue #191](https://github.com/json-c/json-c/issues/191) - Override int64 to only display uint64 strings
|
||||
* [Issue #537](https://github.com/json-c/json-c/issues/537) - Replace '\0' only when parsing key, not change data in value.
|
||||
* [Issue #570](https://github.com/json-c/json-c/issues/570) - Figure out what needs to be done with Android.configure.mk
|
||||
* [Issue #587](https://github.com/json-c/json-c/issues/587) - Store the hashValue to avoid repeating the hash calculation during the hash resize.
|
||||
* [Issue #612](https://github.com/json-c/json-c/issues/612) - json-c-0.11: Fix CVE-2020-12762 - json-c through 0.14 has an integer overflow and out-of-bounds write ...
|
||||
* [Issue #620](https://github.com/json-c/json-c/issues/620) - Introduce json_object_new_string_{ext,noalloc}().
|
||||
* [Issue #624](https://github.com/json-c/json-c/issues/624) - json-c-0.14: Detect broken RDRAND during initialization.
|
||||
* [Issue #625](https://github.com/json-c/json-c/issues/625) - json-c-0.13.x: Detect broken RDRAND during initialization.
|
||||
* [Issue #668](https://github.com/json-c/json-c/issues/668) - Memory usage regression due to newlocal() on older FreeBSD releases
|
||||
* [Issue #676](https://github.com/json-c/json-c/issues/676) - dereferencing type-punned pointer might break strict-aliasing rules [-Werror=strict-aliasing]
|
||||
* [Issue #677](https://github.com/json-c/json-c/issues/677) - Naming conflict when using both json-c and jansson
|
||||
* [Issue #679](https://github.com/json-c/json-c/issues/679) - Let json-c be used with obsolete compilers
|
||||
* [Issue #681](https://github.com/json-c/json-c/issues/681) - json_tokener_parse_ex: `null` (4 bytes) only parses as valid JSON when passed with null terminator (5 bytes). Documentation issue?
|
||||
* [Issue #686](https://github.com/json-c/json-c/issues/686) - Remove dependency on libM::getrandom
|
||||
* [Issue #687](https://github.com/json-c/json-c/issues/687) - Does not build on Apple Silicon M1
|
||||
* [Issue #688](https://github.com/json-c/json-c/issues/688) - json-c-0.15-nodoc.tar.gz build fails
|
||||
* [Issue #702](https://github.com/json-c/json-c/issues/702) - json_patch: add first implementation only with patch application
|
||||
* [Issue #704](https://github.com/json-c/json-c/issues/704) - add json_object_array_insert_idx() + test-cases + fix json_pointer doc-strings
|
||||
* [Issue #705](https://github.com/json-c/json-c/issues/705) - segmentation fault on json-c parsing methods in cross compiled target
|
||||
* [Issue #721](https://github.com/json-c/json-c/issues/721) - cmake test fails with building json-c with icc
|
||||
* [Issue #730](https://github.com/json-c/json-c/issues/730) - Need a comparison with other JSON libraries in C
|
||||
* [Issue #733](https://github.com/json-c/json-c/issues/733) - Official release? 1.0?
|
||||
* [Issue #756](https://github.com/json-c/json-c/issues/756) - Question: Is there any way to build this with Gnu Make?
|
||||
* [Issue #757](https://github.com/json-c/json-c/issues/757) - json_object_from_fd_ex: fail if file is too large
|
||||
* [Issue #759](https://github.com/json-c/json-c/issues/759) - json_tokener_parse_ex: handle out of memory errors
|
||||
* [Issue #766](https://github.com/json-c/json-c/issues/766) - Some people have trouble with undefined references to arc4random
|
||||
* [Issue #767](https://github.com/json-c/json-c/issues/767) - How to create a character array using json-c
|
||||
* [Issue #768](https://github.com/json-c/json-c/issues/768) - commits from May 30, 2022 killed my docker build process
|
||||
* [Issue #769](https://github.com/json-c/json-c/issues/769) - Issue #768
|
||||
* [Issue #770](https://github.com/json-c/json-c/issues/770) - json_parse.c:170:13: error: this statement may fall through
|
||||
* [Issue #771](https://github.com/json-c/json-c/issues/771) - fix fallthough warning
|
||||
* [Issue #772](https://github.com/json-c/json-c/issues/772) - add JSON_C_TO_STRING_COLOR option
|
||||
* [Issue #773](https://github.com/json-c/json-c/issues/773) - problem with u_int64_t
|
||||
* [Issue #774](https://github.com/json-c/json-c/issues/774) - The function add_compile_options was added to CMake version 2.8.12 and later but your minimum is 2.8 which will not work
|
||||
* [Issue #775](https://github.com/json-c/json-c/issues/775) - list(TRANSFORM ...) is not available prior to CMake 3.12.
|
||||
* [Issue #776](https://github.com/json-c/json-c/issues/776) - Fix typo
|
||||
* [Issue #777](https://github.com/json-c/json-c/issues/777) - Don't try to change locale when libc only supports the C locale
|
||||
* [Issue #778](https://github.com/json-c/json-c/issues/778) - Do not insert newlines when converting empty arrays to json string and JSON_C_TO_STRING_PRETTY is used
|
||||
* [Issue #779](https://github.com/json-c/json-c/issues/779) - Fix compiling for Android
|
||||
* [Issue #780](https://github.com/json-c/json-c/issues/780) - Memory Leak when setting empty strings when c_string.pdata is used
|
||||
* [Issue #781](https://github.com/json-c/json-c/issues/781) - Fix memory leak with emtpy strings in json_object_set_string
|
||||
* [Issue #782](https://github.com/json-c/json-c/issues/782) - Fix typos found by codespell
|
||||
* [Issue #783](https://github.com/json-c/json-c/issues/783) - Fix build with clang-15+
|
||||
* [Issue #784](https://github.com/json-c/json-c/issues/784) - get_time_seed(): silence warning emitted by Coverity Scan static analyzer
|
||||
* [Issue #786](https://github.com/json-c/json-c/issues/786) - ghpages update was not published for json-c-0.16
|
||||
* [Issue #787](https://github.com/json-c/json-c/issues/787) - -static linker flag result in building failed
|
||||
* [Issue #788](https://github.com/json-c/json-c/issues/788) - Clear sensitive information.
|
||||
* [Issue #789](https://github.com/json-c/json-c/issues/789) - Unnecessary struct declaration and unsafe function usage
|
||||
* [Issue #790](https://github.com/json-c/json-c/issues/790) - Small update to README file
|
||||
* [Issue #791](https://github.com/json-c/json-c/issues/791) - json_object_object_foreach not ISO-C compliant
|
||||
* [Issue #792](https://github.com/json-c/json-c/issues/792) - ` json_object_get_int` does not set `EINVAL` on invalid string
|
||||
* [Issue #794](https://github.com/json-c/json-c/issues/794) - replaced
|
||||
* [Issue #796](https://github.com/json-c/json-c/issues/796) - Added Test for get int functions
|
||||
* [Issue #797](https://github.com/json-c/json-c/issues/797) - make uninstall
|
||||
* [Issue #798](https://github.com/json-c/json-c/issues/798) - API to deal with enums is missing
|
||||
* [Issue #799](https://github.com/json-c/json-c/issues/799) - json_object_put: Assertion `jso->_ref_count > 0' failed.
|
||||
* [Issue #800](https://github.com/json-c/json-c/issues/800) - String converted to scientific notation
|
||||
* [Issue #801](https://github.com/json-c/json-c/issues/801) - #error You do not have strncasecmp on your system.
|
||||
* [Issue #802](https://github.com/json-c/json-c/issues/802) - Problem: modern CMake warns about version 2.8
|
||||
* [Issue #803](https://github.com/json-c/json-c/issues/803) - Problem: confusing error message in snprintf_compat.h
|
||||
* [Issue #804](https://github.com/json-c/json-c/issues/804) - Problem: cmake 3.25.1 warns about CMP0042 not being set
|
||||
* [Issue #806](https://github.com/json-c/json-c/issues/806) - The problem is libjson-c.dylib incompatible with OS version
|
||||
* [Issue #807](https://github.com/json-c/json-c/issues/807) - json simple parse syntax
|
||||
* [Issue #808](https://github.com/json-c/json-c/issues/808) - iOS Build using cmake fails due to 64 to 32bits conversion precision loss
|
||||
* [Issue #809](https://github.com/json-c/json-c/issues/809) - Feature request json_object_new_uint()
|
||||
* [Issue #810](https://github.com/json-c/json-c/issues/810) - docs: update to Internet Standard reference
|
||||
* [Issue #811](https://github.com/json-c/json-c/issues/811) - dependence on execution character set
|
||||
* [Issue #812](https://github.com/json-c/json-c/issues/812) - Duplicate symbol when compiling with clang-cl
|
||||
* [Issue #813](https://github.com/json-c/json-c/issues/813) - Build apps only in project itself.
|
||||
* [Issue #814](https://github.com/json-c/json-c/issues/814) - Code execution order
|
||||
* [Issue #816](https://github.com/json-c/json-c/issues/816) - Hi I need to generate libjson-c.so.3 and libjson-c.so.3.0.1, please help with steps
|
||||
* [Issue #818](https://github.com/json-c/json-c/issues/818) - error: a function declaration without a prototype is deprecated in all versions of C
|
||||
* [Issue #819](https://github.com/json-c/json-c/issues/819) - build with intel 2023 fails on vasprintf
|
||||
* [Issue #820](https://github.com/json-c/json-c/issues/820) - ISO C forbids in
|
||||
* [Issue #821](https://github.com/json-c/json-c/issues/821) - Any release planing for 0.17?
|
||||
* [Issue #822](https://github.com/json-c/json-c/issues/822) - Added option to disable app build
|
||||
* [Issue #823](https://github.com/json-c/json-c/issues/823) - Symbol not found during linking stage of libjson-c.so
|
||||
16
json-c.sym
16
json-c.sym
@@ -41,6 +41,8 @@ JSONC_PRIVATE {
|
||||
printbuf_new;
|
||||
printbuf_reset;
|
||||
sprintbuf;
|
||||
# Used by tests:
|
||||
_json_c_strerror;
|
||||
};
|
||||
|
||||
JSONC_0.14 {
|
||||
@@ -165,6 +167,16 @@ JSONC_0.15 {
|
||||
} JSONC_0.14;
|
||||
|
||||
JSONC_0.16 {
|
||||
# global:
|
||||
# ...new symbols here...
|
||||
# No new symbols in 0.16
|
||||
} JSONC_0.15;
|
||||
|
||||
JSONC_0.17 {
|
||||
global:
|
||||
json_object_array_insert_idx;
|
||||
json_patch_apply;
|
||||
# array_list_insert_idx is intentionally not exported
|
||||
} JSONC_0.16;
|
||||
|
||||
JSONC_0.18 {
|
||||
# global:
|
||||
} JSONC_0.17;
|
||||
|
||||
@@ -26,6 +26,7 @@ extern "C" {
|
||||
#include "json_c_version.h"
|
||||
#include "json_object.h"
|
||||
#include "json_object_iterator.h"
|
||||
@JSON_H_JSON_PATCH@
|
||||
@JSON_H_JSON_POINTER@
|
||||
#include "json_tokener.h"
|
||||
#include "json_util.h"
|
||||
|
||||
@@ -17,11 +17,11 @@ extern "C" {
|
||||
#endif
|
||||
|
||||
#define JSON_C_MAJOR_VERSION 0
|
||||
#define JSON_C_MINOR_VERSION 15
|
||||
#define JSON_C_MINOR_VERSION 17
|
||||
#define JSON_C_MICRO_VERSION 99
|
||||
#define JSON_C_VERSION_NUM \
|
||||
((JSON_C_MAJOR_VERSION << 16) | (JSON_C_MINOR_VERSION << 8) | JSON_C_MICRO_VERSION)
|
||||
#define JSON_C_VERSION "0.15.99"
|
||||
#define JSON_C_VERSION "0.17.99"
|
||||
|
||||
#ifndef JSON_EXPORT
|
||||
#if defined(_MSC_VER) && defined(JSON_C_DLL)
|
||||
|
||||
@@ -13,7 +13,15 @@
|
||||
#include <inttypes.h>
|
||||
|
||||
#else
|
||||
#ifdef JSON_C_HAVE_STDINT_H
|
||||
#include <stdint.h>
|
||||
#else
|
||||
/* Really only valid for old MS compilers, VS2008 and earlier: */
|
||||
typedef __int32 int32_t;
|
||||
typedef unsigned __int32 uint32_t;
|
||||
typedef __int64 int64_t;
|
||||
typedef unsigned __int64 uint64_t;
|
||||
#endif
|
||||
|
||||
#define PRId64 "I64d"
|
||||
#define SCNd64 "I64d"
|
||||
@@ -21,4 +29,9 @@
|
||||
|
||||
#endif
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#include <BaseTsd.h>
|
||||
typedef SSIZE_T ssize_t;
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
@@ -53,6 +53,7 @@
|
||||
#endif
|
||||
#endif
|
||||
|
||||
const char *json_number_chars = "0123456789.+-eE"; /* Unused, but part of public API, drop for 1.0 */
|
||||
const char *json_hex_chars = "0123456789abcdefABCDEF";
|
||||
|
||||
static void json_object_generic_delete(struct json_object *jso);
|
||||
@@ -64,6 +65,12 @@ static void json_object_generic_delete(struct json_object *jso);
|
||||
#define inline
|
||||
#endif
|
||||
|
||||
/* define colors */
|
||||
#define ANSI_COLOR_RESET "\033[0m"
|
||||
#define ANSI_COLOR_FG_GREEN "\033[0;32m"
|
||||
#define ANSI_COLOR_FG_BLUE "\033[0;34m"
|
||||
#define ANSI_COLOR_FG_MAGENTA "\033[0;35m"
|
||||
|
||||
/*
|
||||
* Helper functions to more safely cast to a particular type of json_object
|
||||
*/
|
||||
@@ -460,35 +467,45 @@ static int json_object_object_to_json_string(struct json_object *jso, struct pri
|
||||
struct json_object_iter iter;
|
||||
|
||||
printbuf_strappend(pb, "{" /*}*/);
|
||||
if (flags & JSON_C_TO_STRING_PRETTY)
|
||||
printbuf_strappend(pb, "\n");
|
||||
json_object_object_foreachC(jso, iter)
|
||||
{
|
||||
if (had_children)
|
||||
{
|
||||
printbuf_strappend(pb, ",");
|
||||
if (flags & JSON_C_TO_STRING_PRETTY)
|
||||
printbuf_strappend(pb, "\n");
|
||||
}
|
||||
if (flags & JSON_C_TO_STRING_PRETTY)
|
||||
printbuf_strappend(pb, "\n");
|
||||
had_children = 1;
|
||||
if (flags & JSON_C_TO_STRING_SPACED && !(flags & JSON_C_TO_STRING_PRETTY))
|
||||
printbuf_strappend(pb, " ");
|
||||
indent(pb, level + 1, flags);
|
||||
if (flags & JSON_C_TO_STRING_COLOR)
|
||||
printbuf_strappend(pb, ANSI_COLOR_FG_BLUE);
|
||||
|
||||
printbuf_strappend(pb, "\"");
|
||||
json_escape_str(pb, iter.key, strlen(iter.key), flags);
|
||||
printbuf_strappend(pb, "\"");
|
||||
|
||||
if (flags & JSON_C_TO_STRING_COLOR)
|
||||
printbuf_strappend(pb, ANSI_COLOR_RESET);
|
||||
|
||||
if (flags & JSON_C_TO_STRING_SPACED)
|
||||
printbuf_strappend(pb, "\": ");
|
||||
printbuf_strappend(pb, ": ");
|
||||
else
|
||||
printbuf_strappend(pb, "\":");
|
||||
if (iter.val == NULL)
|
||||
printbuf_strappend(pb, ":");
|
||||
|
||||
if (iter.val == NULL) {
|
||||
if (flags & JSON_C_TO_STRING_COLOR)
|
||||
printbuf_strappend(pb, ANSI_COLOR_FG_MAGENTA);
|
||||
printbuf_strappend(pb, "null");
|
||||
else if (iter.val->_to_json_string(iter.val, pb, level + 1, flags) < 0)
|
||||
if (flags & JSON_C_TO_STRING_COLOR)
|
||||
printbuf_strappend(pb, ANSI_COLOR_RESET);
|
||||
} else if (iter.val->_to_json_string(iter.val, pb, level + 1, flags) < 0)
|
||||
return -1;
|
||||
}
|
||||
if (flags & JSON_C_TO_STRING_PRETTY)
|
||||
if ((flags & JSON_C_TO_STRING_PRETTY) && had_children)
|
||||
{
|
||||
if (had_children)
|
||||
printbuf_strappend(pb, "\n");
|
||||
printbuf_strappend(pb, "\n");
|
||||
indent(pb, level, flags);
|
||||
}
|
||||
if (flags & JSON_C_TO_STRING_SPACED && !(flags & JSON_C_TO_STRING_PRETTY))
|
||||
@@ -629,9 +646,18 @@ void json_object_object_del(struct json_object *jso, const char *key)
|
||||
static int json_object_boolean_to_json_string(struct json_object *jso, struct printbuf *pb,
|
||||
int level, int flags)
|
||||
{
|
||||
int ret;
|
||||
|
||||
if (flags & JSON_C_TO_STRING_COLOR)
|
||||
printbuf_strappend(pb, ANSI_COLOR_FG_MAGENTA);
|
||||
|
||||
if (JC_BOOL(jso)->c_boolean)
|
||||
return printbuf_strappend(pb, "true");
|
||||
return printbuf_strappend(pb, "false");
|
||||
ret = printbuf_strappend(pb, "true");
|
||||
else
|
||||
ret = printbuf_strappend(pb, "false");
|
||||
if (ret > -1 && flags & JSON_C_TO_STRING_COLOR)
|
||||
return printbuf_strappend(pb, ANSI_COLOR_RESET);
|
||||
return ret;
|
||||
}
|
||||
|
||||
struct json_object *json_object_new_boolean(json_bool b)
|
||||
@@ -1220,9 +1246,13 @@ static int json_object_string_to_json_string(struct json_object *jso, struct pri
|
||||
int level, int flags)
|
||||
{
|
||||
ssize_t len = JC_STRING(jso)->len;
|
||||
if (flags & JSON_C_TO_STRING_COLOR)
|
||||
printbuf_strappend(pb, ANSI_COLOR_FG_GREEN);
|
||||
printbuf_strappend(pb, "\"");
|
||||
json_escape_str(pb, get_string_component(jso), len < 0 ? -(ssize_t)len : len, flags);
|
||||
printbuf_strappend(pb, "\"");
|
||||
if (flags & JSON_C_TO_STRING_COLOR)
|
||||
printbuf_strappend(pb, ANSI_COLOR_RESET);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1323,11 +1353,18 @@ static int _json_object_set_string_len(json_object *jso, const char *s, size_t l
|
||||
// length as int, cap length at INT_MAX.
|
||||
return 0;
|
||||
|
||||
dstbuf = get_string_component_mutable(jso);
|
||||
curlen = JC_STRING(jso)->len;
|
||||
if (curlen < 0)
|
||||
curlen = -curlen;
|
||||
if (curlen < 0) {
|
||||
if (len == 0) {
|
||||
free(JC_STRING(jso)->c_string.pdata);
|
||||
JC_STRING(jso)->len = curlen = 0;
|
||||
} else {
|
||||
curlen = -curlen;
|
||||
}
|
||||
}
|
||||
|
||||
newlen = len;
|
||||
dstbuf = get_string_component_mutable(jso);
|
||||
|
||||
if ((ssize_t)len > curlen)
|
||||
{
|
||||
@@ -1374,31 +1411,34 @@ static int json_object_array_to_json_string(struct json_object *jso, struct prin
|
||||
size_t ii;
|
||||
|
||||
printbuf_strappend(pb, "[");
|
||||
if (flags & JSON_C_TO_STRING_PRETTY)
|
||||
printbuf_strappend(pb, "\n");
|
||||
for (ii = 0; ii < json_object_array_length(jso); ii++)
|
||||
{
|
||||
struct json_object *val;
|
||||
if (had_children)
|
||||
{
|
||||
printbuf_strappend(pb, ",");
|
||||
if (flags & JSON_C_TO_STRING_PRETTY)
|
||||
printbuf_strappend(pb, "\n");
|
||||
}
|
||||
if (flags & JSON_C_TO_STRING_PRETTY)
|
||||
printbuf_strappend(pb, "\n");
|
||||
had_children = 1;
|
||||
if (flags & JSON_C_TO_STRING_SPACED && !(flags & JSON_C_TO_STRING_PRETTY))
|
||||
printbuf_strappend(pb, " ");
|
||||
indent(pb, level + 1, flags);
|
||||
val = json_object_array_get_idx(jso, ii);
|
||||
if (val == NULL)
|
||||
if (val == NULL) {
|
||||
|
||||
if (flags & JSON_C_TO_STRING_COLOR)
|
||||
printbuf_strappend(pb, ANSI_COLOR_FG_MAGENTA);
|
||||
printbuf_strappend(pb, "null");
|
||||
else if (val->_to_json_string(val, pb, level + 1, flags) < 0)
|
||||
if (flags & JSON_C_TO_STRING_COLOR)
|
||||
printbuf_strappend(pb, ANSI_COLOR_RESET);
|
||||
|
||||
} else if (val->_to_json_string(val, pb, level + 1, flags) < 0)
|
||||
return -1;
|
||||
}
|
||||
if (flags & JSON_C_TO_STRING_PRETTY)
|
||||
if ((flags & JSON_C_TO_STRING_PRETTY) && had_children)
|
||||
{
|
||||
if (had_children)
|
||||
printbuf_strappend(pb, "\n");
|
||||
printbuf_strappend(pb, "\n");
|
||||
indent(pb, level, flags);
|
||||
}
|
||||
|
||||
@@ -1480,6 +1520,12 @@ int json_object_array_add(struct json_object *jso, struct json_object *val)
|
||||
return array_list_add(JC_ARRAY(jso)->c_array, val);
|
||||
}
|
||||
|
||||
int json_object_array_insert_idx(struct json_object *jso, size_t idx, struct json_object *val)
|
||||
{
|
||||
assert(json_object_get_type(jso) == json_type_array);
|
||||
return array_list_insert_idx(JC_ARRAY(jso)->c_array, idx, val);
|
||||
}
|
||||
|
||||
int json_object_array_put_idx(struct json_object *jso, size_t idx, struct json_object *val)
|
||||
{
|
||||
assert(json_object_get_type(jso) == json_type_array);
|
||||
|
||||
@@ -74,6 +74,15 @@ extern "C" {
|
||||
*/
|
||||
#define JSON_C_TO_STRING_NOSLASHESCAPE (1 << 4)
|
||||
|
||||
/**
|
||||
* A flag for the json_object_to_json_string_ext() and
|
||||
* json_object_to_file_ext() functions which causes
|
||||
* the output to be formatted.
|
||||
*
|
||||
* Use color for printing json.
|
||||
*/
|
||||
#define JSON_C_TO_STRING_COLOR (1 << 5)
|
||||
|
||||
/**
|
||||
* A flag for the json_object_object_add_ex function which
|
||||
* causes the value to be added without a check if it already exists.
|
||||
@@ -163,10 +172,10 @@ JSON_EXPORT struct json_object *json_object_get(struct json_object *obj);
|
||||
* is a member of (unless you know you've called `json_object_get(obj)` to
|
||||
* explicitly increment the refcount).
|
||||
*
|
||||
* NULL may be passed, which which case this is a no-op.
|
||||
* NULL may be passed, in which case this is a no-op.
|
||||
*
|
||||
* @param obj the json_object instance
|
||||
* @returns 1 if the object was freed.
|
||||
* @returns 1 if the object was freed, 0 if only the refcount was decremented
|
||||
* @see json_object_get()
|
||||
*/
|
||||
JSON_EXPORT int json_object_put(struct json_object *obj);
|
||||
@@ -182,6 +191,7 @@ JSON_EXPORT int json_object_put(struct json_object *obj);
|
||||
json_type_object,
|
||||
json_type_array,
|
||||
json_type_string
|
||||
* @returns 1 if the object is of the specified type, 0 otherwise
|
||||
*/
|
||||
JSON_EXPORT int json_object_is_type(const struct json_object *obj, enum json_type type);
|
||||
|
||||
@@ -449,9 +459,9 @@ JSON_EXPORT struct json_object *json_object_object_get(const struct json_object
|
||||
* associated with the given field name.
|
||||
*
|
||||
* It is safe to pass a NULL value.
|
||||
* @returns whether or not the key exists
|
||||
* @returns 1 if the key exists, 0 otherwise
|
||||
*/
|
||||
JSON_EXPORT json_bool json_object_object_get_ex(const struct json_object *obj, const char *key,
|
||||
JSON_EXPORT int json_object_object_get_ex(const struct json_object *obj, const char *key,
|
||||
struct json_object **value);
|
||||
|
||||
/** Delete the given json_object field
|
||||
@@ -551,7 +561,7 @@ JSON_EXPORT struct array_list *json_object_get_array(const struct json_object *o
|
||||
|
||||
/** Get the length of a json_object of type json_type_array
|
||||
* @param obj the json_object instance
|
||||
* @returns an int
|
||||
* @returns the length of the array
|
||||
*/
|
||||
JSON_EXPORT size_t json_object_array_length(const struct json_object *obj);
|
||||
|
||||
@@ -613,6 +623,25 @@ JSON_EXPORT int json_object_array_add(struct json_object *obj, struct json_objec
|
||||
JSON_EXPORT int json_object_array_put_idx(struct json_object *obj, size_t idx,
|
||||
struct json_object *val);
|
||||
|
||||
/** Insert an element at a specified index in an array (a json_object of type json_type_array)
|
||||
*
|
||||
* The reference count will *not* be incremented. This is to make adding
|
||||
* fields to objects in code more compact. If you want to retain a reference
|
||||
* to an added object you must wrap the passed object with json_object_get
|
||||
*
|
||||
* The array size will be automatically be expanded to the size of the
|
||||
* index if the index is larger than the current size.
|
||||
* If the index is within the existing array limits, then the element will be
|
||||
* inserted and all elements will be shifted. This is the only difference between
|
||||
* this function and json_object_array_put_idx().
|
||||
*
|
||||
* @param obj the json_object instance
|
||||
* @param idx the index to insert the element at
|
||||
* @param val the json_object to be added
|
||||
*/
|
||||
JSON_EXPORT int json_object_array_insert_idx(struct json_object *obj, size_t idx,
|
||||
struct json_object *val);
|
||||
|
||||
/** Get the element at specified index of array `obj` (which must be a json_object of type json_type_array)
|
||||
*
|
||||
* *No* reference counts will be changed, and ownership of the returned
|
||||
@@ -1014,7 +1043,7 @@ JSON_EXPORT struct json_object *json_object_new_null(void);
|
||||
*
|
||||
* @param obj1 the first json_object instance
|
||||
* @param obj2 the second json_object instance
|
||||
* @returns whether both objects are equal or not
|
||||
* @returns 1 if both objects are equal, 0 otherwise
|
||||
*/
|
||||
JSON_EXPORT int json_object_equal(struct json_object *obj1, struct json_object *obj2);
|
||||
|
||||
|
||||
332
json_patch.c
Normal file
332
json_patch.c
Normal file
@@ -0,0 +1,332 @@
|
||||
/*
|
||||
* Copyright (c) 2021 Alexandru Ardelean.
|
||||
* Copyright (c) 2023 Eric Hawicz
|
||||
*
|
||||
* This is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the MIT license. See COPYING for details.
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "json_patch.h"
|
||||
#include "json_object_private.h"
|
||||
#include "json_pointer_private.h"
|
||||
|
||||
#include <limits.h>
|
||||
#ifndef SIZE_T_MAX
|
||||
#if SIZEOF_SIZE_T == SIZEOF_INT
|
||||
#define SIZE_T_MAX UINT_MAX
|
||||
#elif SIZEOF_SIZE_T == SIZEOF_LONG
|
||||
#define SIZE_T_MAX ULONG_MAX
|
||||
#elif SIZEOF_SIZE_T == SIZEOF_LONG_LONG
|
||||
#define SIZE_T_MAX ULLONG_MAX
|
||||
#else
|
||||
#error Unable to determine size of size_t
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#define _set_err(_errval, _errmsg) do { \
|
||||
patch_error->errno_code = (_errval); \
|
||||
patch_error->errmsg = (_errmsg); \
|
||||
errno = 0; /* To avoid confusion */ \
|
||||
} while (0)
|
||||
|
||||
#define _set_err_from_ptrget(_errval, _fieldname) do { \
|
||||
patch_error->errno_code = (_errval); \
|
||||
patch_error->errmsg = (_errval) == ENOENT ? \
|
||||
"Did not find element referenced by " _fieldname " field" : \
|
||||
"Invalid " _fieldname " field"; \
|
||||
errno = 0; /* To avoid confusion */ \
|
||||
} while(0)
|
||||
|
||||
/**
|
||||
* JavaScript Object Notation (JSON) Patch
|
||||
* RFC 6902 - https://tools.ietf.org/html/rfc6902
|
||||
*/
|
||||
|
||||
static int json_patch_apply_test(struct json_object **res,
|
||||
struct json_object *patch_elem,
|
||||
const char *path, struct json_patch_error *patch_error)
|
||||
{
|
||||
struct json_object *value1, *value2;
|
||||
|
||||
if (!json_object_object_get_ex(patch_elem, "value", &value1)) {
|
||||
_set_err(EINVAL, "Patch object does not contain a 'value' field");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (json_pointer_get(*res, path, &value2))
|
||||
{
|
||||
_set_err_from_ptrget(errno, "path");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!json_object_equal(value1, value2)) {
|
||||
_set_err(ENOENT, "Value of element referenced by 'path' field did not match 'value' field");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int __json_patch_apply_remove(struct json_pointer_get_result *jpres)
|
||||
{
|
||||
if (json_object_is_type(jpres->parent, json_type_array)) {
|
||||
return json_object_array_del_idx(jpres->parent, jpres->index_in_parent, 1);
|
||||
} else if (jpres->parent && jpres->key_in_parent) {
|
||||
json_object_object_del(jpres->parent, jpres->key_in_parent);
|
||||
return 0;
|
||||
} else {
|
||||
// We're removing the root object
|
||||
(void)json_object_put(jpres->obj);
|
||||
jpres->obj = NULL;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
static int json_patch_apply_remove(struct json_object **res, const char *path, struct json_patch_error *patch_error)
|
||||
{
|
||||
struct json_pointer_get_result jpres;
|
||||
int rc;
|
||||
|
||||
if (json_pointer_get_internal(*res, path, &jpres))
|
||||
{
|
||||
_set_err_from_ptrget(errno, "path");
|
||||
return -1;
|
||||
}
|
||||
|
||||
rc = __json_patch_apply_remove(&jpres);
|
||||
if (rc < 0)
|
||||
_set_err(EINVAL, "Unable to remove path referenced by 'path' field");
|
||||
// This means we removed and freed the root object, i.e. *res
|
||||
if (jpres.parent == NULL)
|
||||
*res = NULL;
|
||||
return rc;
|
||||
}
|
||||
|
||||
// callback for json_pointer_set_with_array_cb()
|
||||
static int json_object_array_insert_idx_cb(struct json_object *parent, size_t idx,
|
||||
struct json_object *value, void *priv)
|
||||
{
|
||||
int rc;
|
||||
int *add = priv;
|
||||
|
||||
if (idx > json_object_array_length(parent))
|
||||
{
|
||||
// Note: will propagate back out through json_pointer_set_with_array_cb()
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (*add)
|
||||
rc = json_object_array_insert_idx(parent, idx, value);
|
||||
else
|
||||
rc = json_object_array_put_idx(parent, idx, value);
|
||||
if (rc < 0)
|
||||
errno = EINVAL;
|
||||
return rc;
|
||||
}
|
||||
|
||||
static int json_patch_apply_add_replace(struct json_object **res,
|
||||
struct json_object *patch_elem,
|
||||
const char *path, int add, struct json_patch_error *patch_error)
|
||||
{
|
||||
struct json_object *value;
|
||||
int rc;
|
||||
|
||||
if (!json_object_object_get_ex(patch_elem, "value", &value)) {
|
||||
_set_err(EINVAL, "Patch object does not contain a 'value' field");
|
||||
return -1;
|
||||
}
|
||||
/* if this is a replace op, then we need to make sure it exists before replacing */
|
||||
if (!add && json_pointer_get(*res, path, NULL)) {
|
||||
_set_err_from_ptrget(errno, "path");
|
||||
return -1;
|
||||
}
|
||||
|
||||
rc = json_pointer_set_with_array_cb(res, path, json_object_get(value),
|
||||
json_object_array_insert_idx_cb, &add);
|
||||
if (rc)
|
||||
{
|
||||
_set_err(errno, "Failed to set value at path referenced by 'path' field");
|
||||
json_object_put(value);
|
||||
}
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
// callback for json_pointer_set_with_array_cb()
|
||||
static int json_object_array_move_cb(struct json_object *parent, size_t idx,
|
||||
struct json_object *value, void *priv)
|
||||
{
|
||||
int rc;
|
||||
struct json_pointer_get_result *from = priv;
|
||||
size_t len = json_object_array_length(parent);
|
||||
|
||||
/**
|
||||
* If it's the same array parent, it means that we removed
|
||||
* and element from it, so the length is temporarily reduced
|
||||
* by 1, which means that if we try to move an element to
|
||||
* the last position, we need to check the current length + 1
|
||||
*/
|
||||
if (parent == from->parent)
|
||||
len++;
|
||||
|
||||
if (idx > len)
|
||||
{
|
||||
// Note: will propagate back out through json_pointer_set_with_array_cb()
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
rc = json_object_array_insert_idx(parent, idx, value);
|
||||
if (rc < 0)
|
||||
errno = EINVAL;
|
||||
return rc;
|
||||
}
|
||||
|
||||
static int json_patch_apply_move_copy(struct json_object **res,
|
||||
struct json_object *patch_elem,
|
||||
const char *path, int move, struct json_patch_error *patch_error)
|
||||
{
|
||||
json_pointer_array_set_cb array_set_cb;
|
||||
struct json_pointer_get_result from;
|
||||
struct json_object *jfrom;
|
||||
const char *from_s;
|
||||
size_t from_s_len;
|
||||
int rc;
|
||||
|
||||
if (!json_object_object_get_ex(patch_elem, "from", &jfrom)) {
|
||||
_set_err(EINVAL, "Patch does not contain a 'from' field");
|
||||
return -1;
|
||||
}
|
||||
|
||||
from_s = json_object_get_string(jfrom);
|
||||
|
||||
from_s_len = strlen(from_s);
|
||||
if (strncmp(from_s, path, from_s_len) == 0) {
|
||||
/**
|
||||
* If lengths match, it's a noop, if they don't,
|
||||
* then we're trying to move a parent under a child
|
||||
* which is not allowed as per RFC 6902 section 4.4
|
||||
* The "from" location MUST NOT be a proper prefix of the "path"
|
||||
* location; i.e., a location cannot be moved into one of its children.
|
||||
*/
|
||||
if (from_s_len == strlen(path))
|
||||
return 0;
|
||||
_set_err(EINVAL, "Invalid attempt to move parent under a child");
|
||||
return -1;
|
||||
}
|
||||
|
||||
rc = json_pointer_get_internal(*res, from_s, &from);
|
||||
if (rc)
|
||||
{
|
||||
_set_err_from_ptrget(errno, "from");
|
||||
return rc;
|
||||
}
|
||||
|
||||
// Note: it's impossible for json_pointer to find the root obj, due
|
||||
// to the path check above, so from.parent is guaranteed non-NULL
|
||||
json_object_get(from.obj);
|
||||
|
||||
if (!move) {
|
||||
array_set_cb = json_object_array_insert_idx_cb;
|
||||
} else {
|
||||
rc = __json_patch_apply_remove(&from);
|
||||
if (rc < 0) {
|
||||
json_object_put(from.obj);
|
||||
return rc;
|
||||
}
|
||||
array_set_cb = json_object_array_move_cb;
|
||||
}
|
||||
|
||||
rc = json_pointer_set_with_array_cb(res, path, from.obj, array_set_cb, &from);
|
||||
if (rc)
|
||||
{
|
||||
_set_err(errno, "Failed to set value at path referenced by 'path' field");
|
||||
json_object_put(from.obj);
|
||||
}
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
int json_patch_apply(struct json_object *copy_from, struct json_object *patch,
|
||||
struct json_object **base, struct json_patch_error *patch_error)
|
||||
{
|
||||
size_t ii;
|
||||
int rc = 0;
|
||||
struct json_patch_error placeholder;
|
||||
|
||||
if (!patch_error)
|
||||
patch_error = &placeholder;
|
||||
|
||||
patch_error->patch_failure_idx = SIZE_T_MAX;
|
||||
patch_error->errno_code = 0;
|
||||
|
||||
if (base == NULL||
|
||||
(*base == NULL && copy_from == NULL) ||
|
||||
(*base != NULL && copy_from != NULL))
|
||||
{
|
||||
_set_err(EFAULT, "Exactly one of *base or copy_from must be non-NULL");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!json_object_is_type(patch, json_type_array)) {
|
||||
_set_err(EFAULT, "Patch object is not of type json_type_array");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (copy_from != NULL)
|
||||
{
|
||||
if (json_object_deep_copy(copy_from, base, NULL) < 0)
|
||||
{
|
||||
_set_err(ENOMEM, "Unable to copy copy_from using json_object_deep_copy()");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/* Go through all operations ; apply them on res */
|
||||
for (ii = 0; ii < json_object_array_length(patch); ii++) {
|
||||
struct json_object *jop, *jpath;
|
||||
struct json_object *patch_elem = json_object_array_get_idx(patch, ii);
|
||||
const char *op, *path;
|
||||
|
||||
patch_error->patch_failure_idx = ii;
|
||||
|
||||
if (!json_object_object_get_ex(patch_elem, "op", &jop)) {
|
||||
_set_err(EINVAL, "Patch object does not contain 'op' field");
|
||||
return -1;
|
||||
}
|
||||
op = json_object_get_string(jop);
|
||||
if (!json_object_object_get_ex(patch_elem, "path", &jpath)) {
|
||||
_set_err(EINVAL, "Patch object does not contain 'path' field");
|
||||
return -1;
|
||||
}
|
||||
path = json_object_get_string(jpath); // Note: empty string is ok!
|
||||
|
||||
if (!strcmp(op, "test"))
|
||||
rc = json_patch_apply_test(base, patch_elem, path, patch_error);
|
||||
else if (!strcmp(op, "remove"))
|
||||
rc = json_patch_apply_remove(base, path, patch_error);
|
||||
else if (!strcmp(op, "add"))
|
||||
rc = json_patch_apply_add_replace(base, patch_elem, path, 1, patch_error);
|
||||
else if (!strcmp(op, "replace"))
|
||||
rc = json_patch_apply_add_replace(base, patch_elem, path, 0, patch_error);
|
||||
else if (!strcmp(op, "move"))
|
||||
rc = json_patch_apply_move_copy(base, patch_elem, path, 1, patch_error);
|
||||
else if (!strcmp(op, "copy"))
|
||||
rc = json_patch_apply_move_copy(base, patch_elem, path, 0, patch_error);
|
||||
else {
|
||||
_set_err(EINVAL, "Patch object has invalid 'op' field");
|
||||
return -1;
|
||||
}
|
||||
if (rc < 0)
|
||||
break;
|
||||
}
|
||||
|
||||
return rc;
|
||||
}
|
||||
80
json_patch.h
Normal file
80
json_patch.h
Normal file
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* Copyright (c) 2021 Alexadru Ardelean.
|
||||
*
|
||||
* This is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the MIT license. See COPYING for details.
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file
|
||||
* @brief JSON Patch (RFC 6902) implementation for manipulating JSON objects
|
||||
*/
|
||||
#ifndef _json_patch_h_
|
||||
#define _json_patch_h_
|
||||
|
||||
#include "json_pointer.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Details of an error that occurred during json_patch_apply()
|
||||
*/
|
||||
struct json_patch_error {
|
||||
/**
|
||||
* An errno value indicating what kind of error occurred.
|
||||
* Possible values include:
|
||||
* - ENOENT - A path referenced in the operation does not exist.
|
||||
* - EINVAL - An invalid operation or with invalid path was attempted
|
||||
* - ENOMEM - Unable to allocate memory
|
||||
* - EFAULT - Invalid arguments were passed to json_patch_apply()
|
||||
* (i.e. a C API error, vs. a data error like EINVAL)
|
||||
*/
|
||||
int errno_code;
|
||||
|
||||
/**
|
||||
* The index into the patch array of the operation that failed,
|
||||
* or SIZE_T_MAX for overall errors.
|
||||
*/
|
||||
size_t patch_failure_idx;
|
||||
|
||||
/**
|
||||
* A human readable error message.
|
||||
* Allocated from static storage, does not need to be freed.
|
||||
*/
|
||||
const char *errmsg;
|
||||
};
|
||||
|
||||
/**
|
||||
* Apply the JSON patch to the base object.
|
||||
* The patch object must be formatted as per RFC 6902, i.e.
|
||||
* a json_type_array containing patch operations.
|
||||
* If the patch is not correctly formatted, an error will
|
||||
* be returned.
|
||||
*
|
||||
* The json_object at *base will be modified in place.
|
||||
* Exactly one of *base or copy_from must be non-NULL.
|
||||
* If *base is NULL, a new copy of copy_from will allocated and populated
|
||||
* using json_object_deep_copy(). In this case json_object_put() _must_ be
|
||||
* used to free *base even if the overall patching operation fails.
|
||||
*
|
||||
* If anything fails during patching a negative value will be returned,
|
||||
* and patch_error (if non-NULL) will be populated with error details.
|
||||
*
|
||||
* @param base a pointer to the JSON object which to patch
|
||||
* @param patch the JSON object that describes the patch to be applied
|
||||
* @param copy_from a JSON object to copy to *base
|
||||
* @param patch_error optional, details about errors
|
||||
*
|
||||
* @return negative if an error (or not found), or 0 if patch completely applied
|
||||
*/
|
||||
JSON_EXPORT int json_patch_apply(struct json_object *copy_from, struct json_object *patch,
|
||||
struct json_object **base, struct json_patch_error *patch_error);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
143
json_pointer.c
143
json_pointer.c
@@ -15,7 +15,9 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "json_object_private.h"
|
||||
#include "json_pointer.h"
|
||||
#include "json_pointer_private.h"
|
||||
#include "strdup_compat.h"
|
||||
#include "vasprintf_compat.h"
|
||||
|
||||
@@ -29,8 +31,8 @@
|
||||
|
||||
static void string_replace_all_occurrences_with_char(char *s, const char *occur, char repl_char)
|
||||
{
|
||||
int slen = strlen(s);
|
||||
int skip = strlen(occur) - 1; /* length of the occurrence, minus the char we're replacing */
|
||||
size_t slen = strlen(s);
|
||||
size_t skip = strlen(occur) - 1; /* length of the occurrence, minus the char we're replacing */
|
||||
char *p = s;
|
||||
while ((p = strstr(p, occur)))
|
||||
{
|
||||
@@ -41,9 +43,9 @@ static void string_replace_all_occurrences_with_char(char *s, const char *occur,
|
||||
}
|
||||
}
|
||||
|
||||
static int is_valid_index(struct json_object *jo, const char *path, int32_t *idx)
|
||||
static int is_valid_index(const char *path, size_t *idx)
|
||||
{
|
||||
int i, len = strlen(path);
|
||||
size_t i, len = strlen(path);
|
||||
/* this code-path optimizes a bit, for when we reference the 0-9 index range
|
||||
* in a JSON array and because leading zeros not allowed
|
||||
*/
|
||||
@@ -52,7 +54,7 @@ static int is_valid_index(struct json_object *jo, const char *path, int32_t *idx
|
||||
if (is_plain_digit(path[0]))
|
||||
{
|
||||
*idx = (path[0] - '0');
|
||||
goto check_oob;
|
||||
return 1;
|
||||
}
|
||||
errno = EINVAL;
|
||||
return 0;
|
||||
@@ -73,32 +75,27 @@ static int is_valid_index(struct json_object *jo, const char *path, int32_t *idx
|
||||
}
|
||||
}
|
||||
|
||||
*idx = strtol(path, NULL, 10);
|
||||
if (*idx < 0)
|
||||
{
|
||||
errno = EINVAL;
|
||||
return 0;
|
||||
}
|
||||
check_oob:
|
||||
len = json_object_array_length(jo);
|
||||
if (*idx >= len)
|
||||
{
|
||||
errno = ENOENT;
|
||||
return 0;
|
||||
}
|
||||
// We know it's all digits, so the only error case here is overflow,
|
||||
// but ULLONG_MAX will be longer than any array length so that's ok.
|
||||
*idx = strtoull(path, NULL, 10);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int json_pointer_get_single_path(struct json_object *obj, char *path,
|
||||
struct json_object **value)
|
||||
struct json_object **value, size_t *idx)
|
||||
{
|
||||
if (json_object_is_type(obj, json_type_array))
|
||||
{
|
||||
int32_t idx;
|
||||
if (!is_valid_index(obj, path, &idx))
|
||||
if (!is_valid_index(path, idx))
|
||||
return -1;
|
||||
obj = json_object_array_get_idx(obj, idx);
|
||||
if (*idx >= json_object_array_length(obj))
|
||||
{
|
||||
errno = ENOENT;
|
||||
return -1;
|
||||
}
|
||||
|
||||
obj = json_object_array_get_idx(obj, *idx);
|
||||
if (obj)
|
||||
{
|
||||
if (value)
|
||||
@@ -123,18 +120,25 @@ static int json_pointer_get_single_path(struct json_object *obj, char *path,
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int json_object_array_put_idx_cb(struct json_object *parent, size_t idx,
|
||||
struct json_object *value, void *priv)
|
||||
{
|
||||
return json_object_array_put_idx(parent, idx, value);
|
||||
}
|
||||
|
||||
static int json_pointer_set_single_path(struct json_object *parent, const char *path,
|
||||
struct json_object *value)
|
||||
struct json_object *value,
|
||||
json_pointer_array_set_cb array_set_cb, void *priv)
|
||||
{
|
||||
if (json_object_is_type(parent, json_type_array))
|
||||
{
|
||||
int32_t idx;
|
||||
size_t idx;
|
||||
/* RFC (Chapter 4) states that '-' may be used to add new elements to an array */
|
||||
if (path[0] == '-' && path[1] == '\0')
|
||||
return json_object_array_add(parent, value);
|
||||
if (!is_valid_index(parent, path, &idx))
|
||||
if (!is_valid_index(path, &idx))
|
||||
return -1;
|
||||
return json_object_array_put_idx(parent, idx, value);
|
||||
return array_set_cb(parent, idx, value, priv);
|
||||
}
|
||||
|
||||
/* path replacements should have been done in json_pointer_get_single_path(),
|
||||
@@ -150,9 +154,11 @@ static int json_pointer_set_single_path(struct json_object *parent, const char *
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int json_pointer_get_recursive(struct json_object *obj, char *path,
|
||||
struct json_object **value)
|
||||
static int json_pointer_result_get_recursive(struct json_object *obj, char *path,
|
||||
struct json_pointer_get_result *res)
|
||||
{
|
||||
struct json_object *parent_obj = obj;
|
||||
size_t idx = 0;
|
||||
char *endp;
|
||||
int rc;
|
||||
|
||||
@@ -169,24 +175,47 @@ static int json_pointer_get_recursive(struct json_object *obj, char *path,
|
||||
*endp = '\0';
|
||||
|
||||
/* If we err-ed here, return here */
|
||||
if ((rc = json_pointer_get_single_path(obj, path, &obj)))
|
||||
if ((rc = json_pointer_get_single_path(obj, path, &obj, &idx)))
|
||||
return rc;
|
||||
|
||||
if (endp)
|
||||
{
|
||||
/* Put the slash back, so that the sanity check passes on next recursion level */
|
||||
*endp = '/';
|
||||
return json_pointer_get_recursive(obj, endp, value);
|
||||
return json_pointer_result_get_recursive(obj, endp, res);
|
||||
}
|
||||
|
||||
/* We should be at the end of the recursion here */
|
||||
if (value)
|
||||
*value = obj;
|
||||
if (res) {
|
||||
res->parent = parent_obj;
|
||||
res->obj = obj;
|
||||
if (json_object_is_type(res->parent, json_type_array))
|
||||
res->index_in_parent = idx;
|
||||
else
|
||||
res->key_in_parent = path;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int json_pointer_get(struct json_object *obj, const char *path, struct json_object **res)
|
||||
static int json_pointer_object_get_recursive(struct json_object *obj, char *path,
|
||||
struct json_object **value)
|
||||
{
|
||||
struct json_pointer_get_result res;
|
||||
int rc;
|
||||
|
||||
rc = json_pointer_result_get_recursive(obj, path, &res);
|
||||
if (rc)
|
||||
return rc;
|
||||
|
||||
if (value)
|
||||
*value = res.obj;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int json_pointer_get_internal(struct json_object *obj, const char *path,
|
||||
struct json_pointer_get_result *res)
|
||||
{
|
||||
char *path_copy = NULL;
|
||||
int rc;
|
||||
@@ -199,8 +228,10 @@ int json_pointer_get(struct json_object *obj, const char *path, struct json_obje
|
||||
|
||||
if (path[0] == '\0')
|
||||
{
|
||||
if (res)
|
||||
*res = obj;
|
||||
res->parent = NULL;
|
||||
res->obj = obj;
|
||||
res->key_in_parent = NULL;
|
||||
res->index_in_parent = UINT32_MAX;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -210,12 +241,30 @@ int json_pointer_get(struct json_object *obj, const char *path, struct json_obje
|
||||
errno = ENOMEM;
|
||||
return -1;
|
||||
}
|
||||
rc = json_pointer_get_recursive(obj, path_copy, res);
|
||||
rc = json_pointer_result_get_recursive(obj, path_copy, res);
|
||||
/* re-map the path string to the const-path string */
|
||||
if (rc == 0 && json_object_is_type(res->parent, json_type_object) && res->key_in_parent)
|
||||
res->key_in_parent = path + (res->key_in_parent - path_copy);
|
||||
free(path_copy);
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
int json_pointer_get(struct json_object *obj, const char *path, struct json_object **res)
|
||||
{
|
||||
struct json_pointer_get_result jpres;
|
||||
int rc;
|
||||
|
||||
rc = json_pointer_get_internal(obj, path, &jpres);
|
||||
if (rc)
|
||||
return rc;
|
||||
|
||||
if (res)
|
||||
*res = jpres.obj;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int json_pointer_getf(struct json_object *obj, struct json_object **res, const char *path_fmt, ...)
|
||||
{
|
||||
char *path_copy = NULL;
|
||||
@@ -242,14 +291,16 @@ int json_pointer_getf(struct json_object *obj, struct json_object **res, const c
|
||||
goto out;
|
||||
}
|
||||
|
||||
rc = json_pointer_get_recursive(obj, path_copy, res);
|
||||
rc = json_pointer_object_get_recursive(obj, path_copy, res);
|
||||
out:
|
||||
free(path_copy);
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
int json_pointer_set(struct json_object **obj, const char *path, struct json_object *value)
|
||||
int json_pointer_set_with_array_cb(struct json_object **obj, const char *path,
|
||||
struct json_object *value,
|
||||
json_pointer_array_set_cb array_set_cb, void *priv)
|
||||
{
|
||||
const char *endp;
|
||||
char *path_copy = NULL;
|
||||
@@ -279,7 +330,7 @@ int json_pointer_set(struct json_object **obj, const char *path, struct json_obj
|
||||
if ((endp = strrchr(path, '/')) == path)
|
||||
{
|
||||
path++;
|
||||
return json_pointer_set_single_path(*obj, path, value);
|
||||
return json_pointer_set_single_path(*obj, path, value, array_set_cb, priv);
|
||||
}
|
||||
|
||||
/* pass a working copy to the recursive call */
|
||||
@@ -289,14 +340,19 @@ int json_pointer_set(struct json_object **obj, const char *path, struct json_obj
|
||||
return -1;
|
||||
}
|
||||
path_copy[endp - path] = '\0';
|
||||
rc = json_pointer_get_recursive(*obj, path_copy, &set);
|
||||
rc = json_pointer_object_get_recursive(*obj, path_copy, &set);
|
||||
free(path_copy);
|
||||
|
||||
if (rc)
|
||||
return rc;
|
||||
|
||||
endp++;
|
||||
return json_pointer_set_single_path(set, endp, value);
|
||||
return json_pointer_set_single_path(set, endp, value, array_set_cb, priv);
|
||||
}
|
||||
|
||||
int json_pointer_set(struct json_object **obj, const char *path, struct json_object *value)
|
||||
{
|
||||
return json_pointer_set_with_array_cb(obj, path, value, json_object_array_put_idx_cb, NULL);
|
||||
}
|
||||
|
||||
int json_pointer_setf(struct json_object **obj, struct json_object *value, const char *path_fmt,
|
||||
@@ -344,14 +400,15 @@ int json_pointer_setf(struct json_object **obj, struct json_object *value, const
|
||||
}
|
||||
|
||||
*endp = '\0';
|
||||
rc = json_pointer_get_recursive(*obj, path_copy, &set);
|
||||
rc = json_pointer_object_get_recursive(*obj, path_copy, &set);
|
||||
|
||||
if (rc)
|
||||
goto out;
|
||||
|
||||
set_single_path:
|
||||
endp++;
|
||||
rc = json_pointer_set_single_path(set, endp, value);
|
||||
rc = json_pointer_set_single_path(set, endp, value,
|
||||
json_object_array_put_idx_cb, NULL);
|
||||
out:
|
||||
free(path_copy);
|
||||
return rc;
|
||||
|
||||
@@ -32,11 +32,6 @@ extern "C" {
|
||||
* Internally, this is equivalent to doing a series of 'json_object_object_get()'
|
||||
* and 'json_object_array_get_idx()' along the given 'path'.
|
||||
*
|
||||
* Note that the 'path' string supports 'printf()' type arguments, so, whatever
|
||||
* is added after the 'res' param will be treated as an argument for 'path'
|
||||
* Example: json_pointer_get(obj, "/foo/%d/%s", &res, 0, bar)
|
||||
* This means, that you need to escape '%' with '%%' (just like in printf())
|
||||
*
|
||||
* @param obj the json_object instance/tree from where to retrieve sub-objects
|
||||
* @param path a (RFC6901) string notation for the sub-object to retrieve
|
||||
* @param res a pointer that stores a reference to the json_object
|
||||
@@ -50,7 +45,9 @@ JSON_EXPORT int json_pointer_get(struct json_object *obj, const char *path,
|
||||
/**
|
||||
* This is a variant of 'json_pointer_get()' that supports printf() style arguments.
|
||||
*
|
||||
* Example: json_pointer_getf(obj, res, "/foo/%d/%s", 0, bak)
|
||||
* Variable arguments go after the 'path_fmt' parameter.
|
||||
*
|
||||
* Example: json_pointer_getf(obj, res, "/foo/%d/%s", 0, "bar")
|
||||
* This also means that you need to escape '%' with '%%' (just like in printf())
|
||||
*
|
||||
* Please take into consideration all recommended 'printf()' format security
|
||||
@@ -84,11 +81,6 @@ JSON_EXPORT int json_pointer_getf(struct json_object *obj, struct json_object **
|
||||
* That also implies that 'json_pointer_set()' does not do any refcount incrementing.
|
||||
* (Just that single decrement that was mentioned above).
|
||||
*
|
||||
* Note that the 'path' string supports 'printf()' type arguments, so, whatever
|
||||
* is added after the 'value' param will be treated as an argument for 'path'
|
||||
* Example: json_pointer_set(obj, "/foo/%d/%s", value, 0, bak)
|
||||
* This means, that you need to escape '%' with '%%' (just like in printf())
|
||||
*
|
||||
* @param obj the json_object instance/tree to which to add a sub-object
|
||||
* @param path a (RFC6901) string notation for the sub-object to set in the tree
|
||||
* @param value object to set at path
|
||||
@@ -101,7 +93,9 @@ JSON_EXPORT int json_pointer_set(struct json_object **obj, const char *path,
|
||||
/**
|
||||
* This is a variant of 'json_pointer_set()' that supports printf() style arguments.
|
||||
*
|
||||
* Example: json_pointer_setf(obj, value, "/foo/%d/%s", 0, bak)
|
||||
* Variable arguments go after the 'path_fmt' parameter.
|
||||
*
|
||||
* Example: json_pointer_setf(obj, value, "/foo/%d/%s", 0, "bar")
|
||||
* This also means that you need to escape '%' with '%%' (just like in printf())
|
||||
*
|
||||
* Please take into consideration all recommended 'printf()' format security
|
||||
|
||||
43
json_pointer_private.h
Normal file
43
json_pointer_private.h
Normal file
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright (c) 2023 Eric Hawicz
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the MIT license. See COPYING for details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file
|
||||
* @brief Do not use, json-c internal, may be changed or removed at any time.
|
||||
*/
|
||||
#ifndef _json_pointer_private_h_
|
||||
#define _json_pointer_private_h_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct json_pointer_get_result {
|
||||
struct json_object *parent;
|
||||
struct json_object *obj;
|
||||
// The key of the found object; only valid when parent is json_type_object
|
||||
// Caution: re-uses tail end of the `path` argument to json_pointer_get_internal
|
||||
const char *key_in_parent;
|
||||
// the index of the found object; only valid when parent is json_type_array
|
||||
uint32_t index_in_parent;
|
||||
};
|
||||
|
||||
int json_pointer_get_internal(struct json_object *obj, const char *path,
|
||||
struct json_pointer_get_result *res);
|
||||
|
||||
typedef int(*json_pointer_array_set_cb)(json_object *parent, size_t idx,
|
||||
json_object *value, void *priv);
|
||||
|
||||
int json_pointer_set_with_array_cb(struct json_object **obj, const char *path,
|
||||
struct json_object *value,
|
||||
json_pointer_array_set_cb array_set_cb, void *priv);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
187
json_tokener.c
187
json_tokener.c
@@ -17,6 +17,7 @@
|
||||
|
||||
#include "math_compat.h"
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
#include <math.h>
|
||||
#include <stddef.h>
|
||||
@@ -115,7 +116,8 @@ static const char *json_tokener_errors[] = {
|
||||
"invalid string sequence",
|
||||
"expected comment",
|
||||
"invalid utf-8 string",
|
||||
"buffer size overflow"
|
||||
"buffer size overflow",
|
||||
"out of memory"
|
||||
};
|
||||
/* clang-format on */
|
||||
|
||||
@@ -224,7 +226,10 @@ struct json_object *json_tokener_parse_verbose(const char *str, enum json_tokene
|
||||
|
||||
tok = json_tokener_new();
|
||||
if (!tok)
|
||||
{
|
||||
*error = json_tokener_error_memory;
|
||||
return NULL;
|
||||
}
|
||||
obj = json_tokener_parse_ex(tok, str, -1);
|
||||
*error = tok->err;
|
||||
if (tok->err != json_tokener_success
|
||||
@@ -284,11 +289,24 @@ struct json_object *json_tokener_parse_verbose(const char *str, enum json_tokene
|
||||
|
||||
/* ADVANCE_CHAR() macro:
|
||||
* Increments str & tok->char_offset.
|
||||
* For convenience of existing conditionals, returns the old value of c (0 on eof)
|
||||
* For convenience of existing conditionals, returns the old value of c (0 on eof).
|
||||
* Implicit inputs: c var
|
||||
*/
|
||||
#define ADVANCE_CHAR(str, tok) (++(str), ((tok)->char_offset)++, c)
|
||||
|
||||
/* printbuf_memappend_checked(p, s, l) macro:
|
||||
* Add string s of length l to printbuffer p.
|
||||
* If operation fails abort parse operation with memory error.
|
||||
*/
|
||||
#define printbuf_memappend_checked(p, s, l) \
|
||||
do { \
|
||||
if (printbuf_memappend((p), (s), (l)) < 0) \
|
||||
{ \
|
||||
tok->err = json_tokener_error_memory; \
|
||||
goto out; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
/* End optimization macro defs */
|
||||
|
||||
struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *str, int len)
|
||||
@@ -323,12 +341,23 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
|
||||
#ifdef HAVE_USELOCALE
|
||||
{
|
||||
locale_t duploc = duplocale(oldlocale);
|
||||
if (duploc == NULL && errno == ENOMEM)
|
||||
{
|
||||
tok->err = json_tokener_error_memory;
|
||||
return NULL;
|
||||
}
|
||||
newloc = newlocale(LC_NUMERIC_MASK, "C", duploc);
|
||||
if (newloc == NULL)
|
||||
{
|
||||
tok->err = json_tokener_error_memory;
|
||||
freelocale(duploc);
|
||||
return NULL;
|
||||
}
|
||||
#ifdef NEWLOCALE_NEEDS_FREELOCALE
|
||||
// Older versions of FreeBSD (<12.4) don't free the locale
|
||||
// passed to newlocale(), so do it here
|
||||
freelocale(duploc);
|
||||
#endif
|
||||
uselocale(newloc);
|
||||
}
|
||||
#elif defined(HAVE_SETLOCALE)
|
||||
@@ -336,7 +365,14 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
|
||||
char *tmplocale;
|
||||
tmplocale = setlocale(LC_NUMERIC, NULL);
|
||||
if (tmplocale)
|
||||
{
|
||||
oldlocale = strdup(tmplocale);
|
||||
if (oldlocale == NULL)
|
||||
{
|
||||
tok->err = json_tokener_error_memory;
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
setlocale(LC_NUMERIC, "C");
|
||||
}
|
||||
#endif
|
||||
@@ -358,7 +394,7 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
|
||||
if (c == '/' && !(tok->flags & JSON_TOKENER_STRICT))
|
||||
{
|
||||
printbuf_reset(tok->pb);
|
||||
printbuf_memappend_fast(tok->pb, &c, 1);
|
||||
printbuf_memappend_checked(tok->pb, &c, 1);
|
||||
state = json_tokener_state_comment_start;
|
||||
}
|
||||
else
|
||||
@@ -376,14 +412,20 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
|
||||
saved_state = json_tokener_state_object_field_start;
|
||||
current = json_object_new_object();
|
||||
if (current == NULL)
|
||||
{
|
||||
tok->err = json_tokener_error_memory;
|
||||
goto out;
|
||||
}
|
||||
break;
|
||||
case '[':
|
||||
state = json_tokener_state_eatws;
|
||||
saved_state = json_tokener_state_array;
|
||||
current = json_object_new_array();
|
||||
if (current == NULL)
|
||||
{
|
||||
tok->err = json_tokener_error_memory;
|
||||
goto out;
|
||||
}
|
||||
break;
|
||||
case 'I':
|
||||
case 'i':
|
||||
@@ -486,7 +528,10 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
|
||||
}
|
||||
current = json_object_new_double(is_negative ? -INFINITY : INFINITY);
|
||||
if (current == NULL)
|
||||
{
|
||||
tok->err = json_tokener_error_memory;
|
||||
goto out;
|
||||
}
|
||||
saved_state = json_tokener_state_finish;
|
||||
state = json_tokener_state_eatws;
|
||||
goto redo_char;
|
||||
@@ -496,7 +541,7 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
|
||||
{
|
||||
int size;
|
||||
int size_nan;
|
||||
printbuf_memappend_fast(tok->pb, &c, 1);
|
||||
printbuf_memappend_checked(tok->pb, &c, 1);
|
||||
size = json_min(tok->st_pos + 1, json_null_str_len);
|
||||
size_nan = json_min(tok->st_pos + 1, json_nan_str_len);
|
||||
if ((!(tok->flags & JSON_TOKENER_STRICT) &&
|
||||
@@ -519,7 +564,10 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
|
||||
{
|
||||
current = json_object_new_double(NAN);
|
||||
if (current == NULL)
|
||||
{
|
||||
tok->err = json_tokener_error_memory;
|
||||
goto out;
|
||||
}
|
||||
saved_state = json_tokener_state_finish;
|
||||
state = json_tokener_state_eatws;
|
||||
goto redo_char;
|
||||
@@ -548,7 +596,7 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
|
||||
tok->err = json_tokener_error_parse_comment;
|
||||
goto out;
|
||||
}
|
||||
printbuf_memappend_fast(tok->pb, &c, 1);
|
||||
printbuf_memappend_checked(tok->pb, &c, 1);
|
||||
break;
|
||||
|
||||
case json_tokener_state_comment:
|
||||
@@ -559,12 +607,12 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
|
||||
{
|
||||
if (!ADVANCE_CHAR(str, tok) || !PEEK_CHAR(c, tok))
|
||||
{
|
||||
printbuf_memappend_fast(tok->pb, case_start,
|
||||
str - case_start);
|
||||
printbuf_memappend_checked(tok->pb, case_start,
|
||||
str - case_start);
|
||||
goto out;
|
||||
}
|
||||
}
|
||||
printbuf_memappend_fast(tok->pb, case_start, 1 + str - case_start);
|
||||
printbuf_memappend_checked(tok->pb, case_start, 1 + str - case_start);
|
||||
state = json_tokener_state_comment_end;
|
||||
}
|
||||
break;
|
||||
@@ -577,19 +625,19 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
|
||||
{
|
||||
if (!ADVANCE_CHAR(str, tok) || !PEEK_CHAR(c, tok))
|
||||
{
|
||||
printbuf_memappend_fast(tok->pb, case_start,
|
||||
str - case_start);
|
||||
printbuf_memappend_checked(tok->pb, case_start,
|
||||
str - case_start);
|
||||
goto out;
|
||||
}
|
||||
}
|
||||
printbuf_memappend_fast(tok->pb, case_start, str - case_start);
|
||||
printbuf_memappend_checked(tok->pb, case_start, str - case_start);
|
||||
MC_DEBUG("json_tokener_comment: %s\n", tok->pb->buf);
|
||||
state = json_tokener_state_eatws;
|
||||
}
|
||||
break;
|
||||
|
||||
case json_tokener_state_comment_end:
|
||||
printbuf_memappend_fast(tok->pb, &c, 1);
|
||||
printbuf_memappend_checked(tok->pb, &c, 1);
|
||||
if (c == '/')
|
||||
{
|
||||
MC_DEBUG("json_tokener_comment: %s\n", tok->pb->buf);
|
||||
@@ -609,28 +657,37 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
|
||||
{
|
||||
if (c == tok->quote_char)
|
||||
{
|
||||
printbuf_memappend_fast(tok->pb, case_start,
|
||||
str - case_start);
|
||||
printbuf_memappend_checked(tok->pb, case_start,
|
||||
str - case_start);
|
||||
current =
|
||||
json_object_new_string_len(tok->pb->buf, tok->pb->bpos);
|
||||
if (current == NULL)
|
||||
{
|
||||
tok->err = json_tokener_error_memory;
|
||||
goto out;
|
||||
}
|
||||
saved_state = json_tokener_state_finish;
|
||||
state = json_tokener_state_eatws;
|
||||
break;
|
||||
}
|
||||
else if (c == '\\')
|
||||
{
|
||||
printbuf_memappend_fast(tok->pb, case_start,
|
||||
str - case_start);
|
||||
printbuf_memappend_checked(tok->pb, case_start,
|
||||
str - case_start);
|
||||
saved_state = json_tokener_state_string;
|
||||
state = json_tokener_state_string_escape;
|
||||
break;
|
||||
}
|
||||
else if ((tok->flags & JSON_TOKENER_STRICT) && c <= 0x1f)
|
||||
{
|
||||
// Disallow control characters in strict mode
|
||||
tok->err = json_tokener_error_parse_string;
|
||||
goto out;
|
||||
}
|
||||
if (!ADVANCE_CHAR(str, tok) || !PEEK_CHAR(c, tok))
|
||||
{
|
||||
printbuf_memappend_fast(tok->pb, case_start,
|
||||
str - case_start);
|
||||
printbuf_memappend_checked(tok->pb, case_start,
|
||||
str - case_start);
|
||||
goto out;
|
||||
}
|
||||
}
|
||||
@@ -643,7 +700,7 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
|
||||
case '"':
|
||||
case '\\':
|
||||
case '/':
|
||||
printbuf_memappend_fast(tok->pb, &c, 1);
|
||||
printbuf_memappend_checked(tok->pb, &c, 1);
|
||||
state = saved_state;
|
||||
break;
|
||||
case 'b':
|
||||
@@ -652,15 +709,15 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
|
||||
case 't':
|
||||
case 'f':
|
||||
if (c == 'b')
|
||||
printbuf_memappend_fast(tok->pb, "\b", 1);
|
||||
printbuf_memappend_checked(tok->pb, "\b", 1);
|
||||
else if (c == 'n')
|
||||
printbuf_memappend_fast(tok->pb, "\n", 1);
|
||||
printbuf_memappend_checked(tok->pb, "\n", 1);
|
||||
else if (c == 'r')
|
||||
printbuf_memappend_fast(tok->pb, "\r", 1);
|
||||
printbuf_memappend_checked(tok->pb, "\r", 1);
|
||||
else if (c == 't')
|
||||
printbuf_memappend_fast(tok->pb, "\t", 1);
|
||||
printbuf_memappend_checked(tok->pb, "\t", 1);
|
||||
else if (c == 'f')
|
||||
printbuf_memappend_fast(tok->pb, "\f", 1);
|
||||
printbuf_memappend_checked(tok->pb, "\f", 1);
|
||||
state = saved_state;
|
||||
break;
|
||||
case 'u':
|
||||
@@ -720,8 +777,8 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
|
||||
/* High surrogate was not followed by a low surrogate
|
||||
* Replace the high and process the rest normally
|
||||
*/
|
||||
printbuf_memappend_fast(tok->pb,
|
||||
(char *)utf8_replacement_char, 3);
|
||||
printbuf_memappend_checked(tok->pb,
|
||||
(char *)utf8_replacement_char, 3);
|
||||
}
|
||||
tok->high_surrogate = 0;
|
||||
}
|
||||
@@ -730,14 +787,14 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
|
||||
{
|
||||
unsigned char unescaped_utf[1];
|
||||
unescaped_utf[0] = tok->ucs_char;
|
||||
printbuf_memappend_fast(tok->pb, (char *)unescaped_utf, 1);
|
||||
printbuf_memappend_checked(tok->pb, (char *)unescaped_utf, 1);
|
||||
}
|
||||
else if (tok->ucs_char < 0x800)
|
||||
{
|
||||
unsigned char unescaped_utf[2];
|
||||
unescaped_utf[0] = 0xc0 | (tok->ucs_char >> 6);
|
||||
unescaped_utf[1] = 0x80 | (tok->ucs_char & 0x3f);
|
||||
printbuf_memappend_fast(tok->pb, (char *)unescaped_utf, 2);
|
||||
printbuf_memappend_checked(tok->pb, (char *)unescaped_utf, 2);
|
||||
}
|
||||
else if (IS_HIGH_SURROGATE(tok->ucs_char))
|
||||
{
|
||||
@@ -763,7 +820,7 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
|
||||
else if (IS_LOW_SURROGATE(tok->ucs_char))
|
||||
{
|
||||
/* Got a low surrogate not preceded by a high */
|
||||
printbuf_memappend_fast(tok->pb, (char *)utf8_replacement_char, 3);
|
||||
printbuf_memappend_checked(tok->pb, (char *)utf8_replacement_char, 3);
|
||||
}
|
||||
else if (tok->ucs_char < 0x10000)
|
||||
{
|
||||
@@ -771,7 +828,7 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
|
||||
unescaped_utf[0] = 0xe0 | (tok->ucs_char >> 12);
|
||||
unescaped_utf[1] = 0x80 | ((tok->ucs_char >> 6) & 0x3f);
|
||||
unescaped_utf[2] = 0x80 | (tok->ucs_char & 0x3f);
|
||||
printbuf_memappend_fast(tok->pb, (char *)unescaped_utf, 3);
|
||||
printbuf_memappend_checked(tok->pb, (char *)unescaped_utf, 3);
|
||||
}
|
||||
else if (tok->ucs_char < 0x110000)
|
||||
{
|
||||
@@ -780,12 +837,12 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
|
||||
unescaped_utf[1] = 0x80 | ((tok->ucs_char >> 12) & 0x3f);
|
||||
unescaped_utf[2] = 0x80 | ((tok->ucs_char >> 6) & 0x3f);
|
||||
unescaped_utf[3] = 0x80 | (tok->ucs_char & 0x3f);
|
||||
printbuf_memappend_fast(tok->pb, (char *)unescaped_utf, 4);
|
||||
printbuf_memappend_checked(tok->pb, (char *)unescaped_utf, 4);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Don't know what we got--insert the replacement char */
|
||||
printbuf_memappend_fast(tok->pb, (char *)utf8_replacement_char, 3);
|
||||
printbuf_memappend_checked(tok->pb, (char *)utf8_replacement_char, 3);
|
||||
}
|
||||
state = saved_state; // i.e. _state_string or _state_object_field
|
||||
}
|
||||
@@ -800,7 +857,7 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
|
||||
* it. Put a replacement char in for the high surrogate
|
||||
* and pop back up to _state_string or _state_object_field.
|
||||
*/
|
||||
printbuf_memappend_fast(tok->pb, (char *)utf8_replacement_char, 3);
|
||||
printbuf_memappend_checked(tok->pb, (char *)utf8_replacement_char, 3);
|
||||
tok->high_surrogate = 0;
|
||||
tok->ucs_char = 0;
|
||||
tok->st_pos = 0;
|
||||
@@ -819,7 +876,7 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
|
||||
* Put a replacement char in for the high surrogate
|
||||
* and handle the escape sequence normally.
|
||||
*/
|
||||
printbuf_memappend_fast(tok->pb, (char *)utf8_replacement_char, 3);
|
||||
printbuf_memappend_checked(tok->pb, (char *)utf8_replacement_char, 3);
|
||||
tok->high_surrogate = 0;
|
||||
tok->ucs_char = 0;
|
||||
tok->st_pos = 0;
|
||||
@@ -834,7 +891,7 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
|
||||
case json_tokener_state_boolean:
|
||||
{
|
||||
int size1, size2;
|
||||
printbuf_memappend_fast(tok->pb, &c, 1);
|
||||
printbuf_memappend_checked(tok->pb, &c, 1);
|
||||
size1 = json_min(tok->st_pos + 1, json_true_str_len);
|
||||
size2 = json_min(tok->st_pos + 1, json_false_str_len);
|
||||
if ((!(tok->flags & JSON_TOKENER_STRICT) &&
|
||||
@@ -845,7 +902,10 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
|
||||
{
|
||||
current = json_object_new_boolean(1);
|
||||
if (current == NULL)
|
||||
{
|
||||
tok->err = json_tokener_error_memory;
|
||||
goto out;
|
||||
}
|
||||
saved_state = json_tokener_state_finish;
|
||||
state = json_tokener_state_eatws;
|
||||
goto redo_char;
|
||||
@@ -859,7 +919,10 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
|
||||
{
|
||||
current = json_object_new_boolean(0);
|
||||
if (current == NULL)
|
||||
{
|
||||
tok->err = json_tokener_error_memory;
|
||||
goto out;
|
||||
}
|
||||
saved_state = json_tokener_state_finish;
|
||||
state = json_tokener_state_eatws;
|
||||
goto redo_char;
|
||||
@@ -939,7 +1002,7 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
|
||||
|
||||
if (!ADVANCE_CHAR(str, tok) || !PEEK_CHAR(c, tok))
|
||||
{
|
||||
printbuf_memappend_fast(tok->pb, case_start, case_len);
|
||||
printbuf_memappend_checked(tok->pb, case_start, case_len);
|
||||
goto out;
|
||||
}
|
||||
}
|
||||
@@ -948,7 +1011,7 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
|
||||
it might have been intended to be, and return a potentially
|
||||
more understandable error right away.
|
||||
However, if we're at the top-level, use the number as-is
|
||||
because c can be part of a new object to parse on the
|
||||
because c can be part of a new object to parse on the
|
||||
next call to json_tokener_parse().
|
||||
*/
|
||||
if (tok->depth > 0 && c != ',' && c != ']' && c != '}' && c != '/' &&
|
||||
@@ -958,7 +1021,7 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
|
||||
goto out;
|
||||
}
|
||||
if (case_len > 0)
|
||||
printbuf_memappend_fast(tok->pb, case_start, case_len);
|
||||
printbuf_memappend_checked(tok->pb, case_start, case_len);
|
||||
|
||||
// Check for -Infinity
|
||||
if (tok->pb->buf[0] == '-' && case_len <= 1 && (c == 'i' || c == 'I'))
|
||||
@@ -991,13 +1054,26 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
|
||||
if (!tok->is_double && tok->pb->buf[0] == '-' &&
|
||||
json_parse_int64(tok->pb->buf, &num64) == 0)
|
||||
{
|
||||
if (errno == ERANGE && (tok->flags & JSON_TOKENER_STRICT))
|
||||
{
|
||||
tok->err = json_tokener_error_parse_number;
|
||||
goto out;
|
||||
}
|
||||
current = json_object_new_int64(num64);
|
||||
if (current == NULL)
|
||||
{
|
||||
tok->err = json_tokener_error_memory;
|
||||
goto out;
|
||||
}
|
||||
}
|
||||
else if (!tok->is_double && tok->pb->buf[0] != '-' &&
|
||||
json_parse_uint64(tok->pb->buf, &numuint64) == 0)
|
||||
{
|
||||
if (errno == ERANGE && (tok->flags & JSON_TOKENER_STRICT))
|
||||
{
|
||||
tok->err = json_tokener_error_parse_number;
|
||||
goto out;
|
||||
}
|
||||
if (numuint64 && tok->pb->buf[0] == '0' &&
|
||||
(tok->flags & JSON_TOKENER_STRICT))
|
||||
{
|
||||
@@ -1009,13 +1085,19 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
|
||||
num64 = (uint64_t)numuint64;
|
||||
current = json_object_new_int64(num64);
|
||||
if (current == NULL)
|
||||
{
|
||||
tok->err = json_tokener_error_memory;
|
||||
goto out;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
current = json_object_new_uint64(numuint64);
|
||||
if (current == NULL)
|
||||
{
|
||||
tok->err = json_tokener_error_memory;
|
||||
goto out;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (tok->is_double &&
|
||||
@@ -1024,7 +1106,10 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
|
||||
{
|
||||
current = json_object_new_double_s(numd, tok->pb->buf);
|
||||
if (current == NULL)
|
||||
{
|
||||
tok->err = json_tokener_error_memory;
|
||||
goto out;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -1069,7 +1154,10 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
|
||||
|
||||
case json_tokener_state_array_add:
|
||||
if (json_object_array_add(current, obj) != 0)
|
||||
{
|
||||
tok->err = json_tokener_error_memory;
|
||||
goto out;
|
||||
}
|
||||
saved_state = json_tokener_state_array_sep;
|
||||
state = json_tokener_state_eatws;
|
||||
goto redo_char;
|
||||
@@ -1129,25 +1217,30 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
|
||||
{
|
||||
if (c == tok->quote_char)
|
||||
{
|
||||
printbuf_memappend_fast(tok->pb, case_start,
|
||||
str - case_start);
|
||||
printbuf_memappend_checked(tok->pb, case_start,
|
||||
str - case_start);
|
||||
obj_field_name = strdup(tok->pb->buf);
|
||||
if (obj_field_name == NULL)
|
||||
{
|
||||
tok->err = json_tokener_error_memory;
|
||||
goto out;
|
||||
}
|
||||
saved_state = json_tokener_state_object_field_end;
|
||||
state = json_tokener_state_eatws;
|
||||
break;
|
||||
}
|
||||
else if (c == '\\')
|
||||
{
|
||||
printbuf_memappend_fast(tok->pb, case_start,
|
||||
str - case_start);
|
||||
printbuf_memappend_checked(tok->pb, case_start,
|
||||
str - case_start);
|
||||
saved_state = json_tokener_state_object_field;
|
||||
state = json_tokener_state_string_escape;
|
||||
break;
|
||||
}
|
||||
if (!ADVANCE_CHAR(str, tok) || !PEEK_CHAR(c, tok))
|
||||
{
|
||||
printbuf_memappend_fast(tok->pb, case_start,
|
||||
str - case_start);
|
||||
printbuf_memappend_checked(tok->pb, case_start,
|
||||
str - case_start);
|
||||
goto out;
|
||||
}
|
||||
}
|
||||
@@ -1179,7 +1272,11 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
|
||||
goto redo_char;
|
||||
|
||||
case json_tokener_state_object_value_add:
|
||||
json_object_object_add(current, obj_field_name, obj);
|
||||
if (json_object_object_add(current, obj_field_name, obj) != 0)
|
||||
{
|
||||
tok->err = json_tokener_error_memory;
|
||||
goto out;
|
||||
}
|
||||
free(obj_field_name);
|
||||
obj_field_name = NULL;
|
||||
saved_state = json_tokener_state_object_sep;
|
||||
|
||||
@@ -40,7 +40,8 @@ enum json_tokener_error
|
||||
json_tokener_error_parse_string,
|
||||
json_tokener_error_parse_comment,
|
||||
json_tokener_error_parse_utf8_string,
|
||||
json_tokener_error_size
|
||||
json_tokener_error_size, /* A string longer than INT32_MAX was passed as input */
|
||||
json_tokener_error_memory /* Failed to allocate memory */
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -229,7 +230,7 @@ JSON_EXPORT void json_tokener_reset(struct json_tokener *tok);
|
||||
JSON_EXPORT struct json_object *json_tokener_parse(const char *str);
|
||||
|
||||
/**
|
||||
* Parser a json_object out of the string `str`, but if it fails
|
||||
* Parse a json_object out of the string `str`, but if it fails
|
||||
* return the error in `*error`.
|
||||
* @see json_tokener_parse()
|
||||
* @see json_tokener_parse_ex()
|
||||
|
||||
51
json_util.c
51
json_util.c
@@ -37,13 +37,13 @@
|
||||
#include <unistd.h>
|
||||
#endif /* HAVE_UNISTD_H */
|
||||
|
||||
#ifdef WIN32
|
||||
#ifdef _WIN32
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <io.h>
|
||||
#include <windows.h>
|
||||
#endif /* defined(WIN32) */
|
||||
#endif /* defined(_WIN32) */
|
||||
|
||||
#if !defined(HAVE_OPEN) && defined(WIN32)
|
||||
#if !defined(HAVE_OPEN) && defined(_WIN32)
|
||||
#define open _open
|
||||
#endif
|
||||
|
||||
@@ -60,7 +60,7 @@ static int _json_object_to_fd(int fd, struct json_object *obj, int flags, const
|
||||
|
||||
static char _last_err[256] = "";
|
||||
|
||||
const char *json_util_get_last_err()
|
||||
const char *json_util_get_last_err(void)
|
||||
{
|
||||
if (_last_err[0] == '\0')
|
||||
return NULL;
|
||||
@@ -85,7 +85,7 @@ struct json_object *json_object_from_fd_ex(int fd, int in_depth)
|
||||
struct printbuf *pb;
|
||||
struct json_object *obj;
|
||||
char buf[JSON_FILE_BUF_SIZE];
|
||||
int ret;
|
||||
ssize_t ret;
|
||||
int depth = JSON_TOKENER_DEFAULT_DEPTH;
|
||||
json_tokener *tok;
|
||||
|
||||
@@ -101,15 +101,25 @@ struct json_object *json_object_from_fd_ex(int fd, int in_depth)
|
||||
if (!tok)
|
||||
{
|
||||
_json_c_set_last_err(
|
||||
"json_object_from_fd_ex: unable to allocate json_tokener(depth=%d): %s\n", depth,
|
||||
strerror(errno));
|
||||
"json_object_from_fd_ex: unable to allocate json_tokener(depth=%d): %s\n",
|
||||
depth, strerror(errno));
|
||||
printbuf_free(pb);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
while ((ret = read(fd, buf, JSON_FILE_BUF_SIZE)) > 0)
|
||||
while ((ret = read(fd, buf, sizeof(buf))) > 0)
|
||||
{
|
||||
printbuf_memappend(pb, buf, ret);
|
||||
if (printbuf_memappend(pb, buf, ret) < 0)
|
||||
{
|
||||
#if JSON_FILE_BUF_SIZE > INT_MAX
|
||||
#error "Can't append more than INT_MAX bytes at a time"
|
||||
#endif
|
||||
_json_c_set_last_err(
|
||||
"json_object_from_fd_ex: failed to printbuf_memappend after reading %d+%d bytes: %s", printbuf_length(pb), (int)ret, strerror(errno));
|
||||
json_tokener_free(tok);
|
||||
printbuf_free(pb);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
if (ret < 0)
|
||||
{
|
||||
@@ -184,9 +194,9 @@ int json_object_to_fd(int fd, struct json_object *obj, int flags)
|
||||
}
|
||||
static int _json_object_to_fd(int fd, struct json_object *obj, int flags, const char *filename)
|
||||
{
|
||||
int ret;
|
||||
ssize_t ret;
|
||||
const char *json_str;
|
||||
unsigned int wpos, wsize;
|
||||
size_t wpos, wsize;
|
||||
|
||||
filename = filename ? filename : "(fd)";
|
||||
|
||||
@@ -195,8 +205,7 @@ static int _json_object_to_fd(int fd, struct json_object *obj, int flags, const
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* CAW: probably unnecessary, but the most 64bit safe */
|
||||
wsize = (unsigned int)(strlen(json_str) & UINT_MAX);
|
||||
wsize = strlen(json_str);
|
||||
wpos = 0;
|
||||
while (wpos < wsize)
|
||||
{
|
||||
@@ -208,7 +217,7 @@ static int _json_object_to_fd(int fd, struct json_object *obj, int flags, const
|
||||
}
|
||||
|
||||
/* because of the above check for ret < 0, we can safely cast and add */
|
||||
wpos += (unsigned int)ret;
|
||||
wpos += (size_t)ret;
|
||||
}
|
||||
|
||||
return 0;
|
||||
@@ -238,7 +247,12 @@ int json_parse_int64(const char *buf, int64_t *retval)
|
||||
val = strtoll(buf, &end, 10);
|
||||
if (end != buf)
|
||||
*retval = val;
|
||||
return ((val == 0 && errno != 0) || (end == buf)) ? 1 : 0;
|
||||
if ((val == 0 && errno != 0) || (end == buf))
|
||||
{
|
||||
errno = EINVAL;
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int json_parse_uint64(const char *buf, uint64_t *retval)
|
||||
@@ -255,7 +269,12 @@ int json_parse_uint64(const char *buf, uint64_t *retval)
|
||||
val = strtoull(buf, &end, 10);
|
||||
if (end != buf)
|
||||
*retval = val;
|
||||
return ((val == 0 && errno != 0) || (end == buf)) ? 1 : 0;
|
||||
if ((val == 0 && errno != 0) || (end == buf))
|
||||
{
|
||||
errno = EINVAL;
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifndef HAVE_REALLOC
|
||||
|
||||
11
json_util.h
11
json_util.h
@@ -100,8 +100,17 @@ JSON_EXPORT int json_object_to_fd(int fd, struct json_object *obj, int flags);
|
||||
*/
|
||||
JSON_EXPORT const char *json_util_get_last_err(void);
|
||||
|
||||
/* these parsing helpers return zero on success */
|
||||
/**
|
||||
* A parsing helper for integer values. Returns 0 on success,
|
||||
* with the parsed value assigned to *retval. Overflow/underflow
|
||||
* are NOT considered errors, but errno will be set to ERANGE,
|
||||
* just like the strtol/strtoll functions do.
|
||||
*/
|
||||
JSON_EXPORT int json_parse_int64(const char *buf, int64_t *retval);
|
||||
/**
|
||||
* A parsing help for integer values, providing one extra bit of
|
||||
* magnitude beyond json_parse_int64().
|
||||
*/
|
||||
JSON_EXPORT int json_parse_uint64(const char *buf, uint64_t *retval);
|
||||
/**
|
||||
* @deprecated
|
||||
|
||||
14
printbuf.c
14
printbuf.c
@@ -15,6 +15,7 @@
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@@ -56,6 +57,8 @@ struct printbuf *printbuf_new(void)
|
||||
*
|
||||
* If the current size is large enough, nothing is changed.
|
||||
*
|
||||
* If extension failed, errno is set to indicate the error.
|
||||
*
|
||||
* Note: this does not check the available space! The caller
|
||||
* is responsible for performing those calculations.
|
||||
*/
|
||||
@@ -68,7 +71,10 @@ static int printbuf_extend(struct printbuf *p, int min_size)
|
||||
return 0;
|
||||
/* Prevent signed integer overflows with large buffers. */
|
||||
if (min_size > INT_MAX - 8)
|
||||
{
|
||||
errno = EFBIG;
|
||||
return -1;
|
||||
}
|
||||
if (p->size > INT_MAX / 2)
|
||||
new_size = min_size + 8;
|
||||
else {
|
||||
@@ -77,7 +83,7 @@ static int printbuf_extend(struct printbuf *p, int min_size)
|
||||
new_size = min_size + 8;
|
||||
}
|
||||
#ifdef PRINTBUF_DEBUG
|
||||
MC_DEBUG("printbuf_memappend: realloc "
|
||||
MC_DEBUG("printbuf_extend: realloc "
|
||||
"bpos=%d min_size=%d old_size=%d new_size=%d\n",
|
||||
p->bpos, min_size, p->size, new_size);
|
||||
#endif /* PRINTBUF_DEBUG */
|
||||
@@ -92,7 +98,10 @@ int printbuf_memappend(struct printbuf *p, const char *buf, int size)
|
||||
{
|
||||
/* Prevent signed integer overflows with large buffers. */
|
||||
if (size < 0 || size > INT_MAX - p->bpos - 1)
|
||||
{
|
||||
errno = EFBIG;
|
||||
return -1;
|
||||
}
|
||||
if (p->size <= p->bpos + size + 1)
|
||||
{
|
||||
if (printbuf_extend(p, p->bpos + size + 1) < 0)
|
||||
@@ -112,7 +121,10 @@ int printbuf_memset(struct printbuf *pb, int offset, int charvalue, int len)
|
||||
offset = pb->bpos;
|
||||
/* Prevent signed integer overflows with large buffers. */
|
||||
if (len < 0 || offset < -1 || len > INT_MAX - offset)
|
||||
{
|
||||
errno = EFBIG;
|
||||
return -1;
|
||||
}
|
||||
size_needed = offset + len;
|
||||
if (pb->size < size_needed)
|
||||
{
|
||||
|
||||
@@ -229,6 +229,7 @@ static int get_dev_random_seed(int *seed)
|
||||
if ((buf.st_mode & S_IFCHR) == 0)
|
||||
return -1;
|
||||
|
||||
/* coverity[toctou] */
|
||||
int fd = open(dev_random_file, O_RDONLY);
|
||||
if (fd < 0)
|
||||
{
|
||||
@@ -253,7 +254,7 @@ static int get_dev_random_seed(int *seed)
|
||||
|
||||
/* get_cryptgenrandom_seed */
|
||||
|
||||
#ifdef WIN32
|
||||
#ifdef _WIN32
|
||||
|
||||
#define HAVE_CRYPTGENRANDOM 1
|
||||
|
||||
@@ -310,6 +311,7 @@ static int get_time_seed(void)
|
||||
{
|
||||
DEBUG_SEED("get_time_seed");
|
||||
|
||||
/* coverity[store_truncates_time_t] */
|
||||
return (unsigned)time(NULL) * 433494437;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -35,7 +35,7 @@ static int json_c_snprintf(char *str, size_t size, const char *format, ...)
|
||||
#define snprintf json_c_snprintf
|
||||
|
||||
#elif !defined(HAVE_SNPRINTF) /* !HAVE_SNPRINTF */
|
||||
#error Need vsnprintf!
|
||||
#endif /* !HAVE_SNPRINTF && defined(WIN32) */
|
||||
#error snprintf is required but was not found
|
||||
#endif /* !HAVE_SNPRINTF */
|
||||
|
||||
#endif /* __snprintf_compat_h */
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
#ifndef __json_strerror_override_private_h__
|
||||
#define __json_strerror_override_private_h__
|
||||
|
||||
/**
|
||||
* @file
|
||||
* @brief Do not use, json-c internal, may be changed or removed at any time.
|
||||
*/
|
||||
|
||||
#include "json_types.h"
|
||||
|
||||
/* Used by tests to get consistent output */
|
||||
JSON_EXPORT int _json_c_strerror_enable;
|
||||
|
||||
#endif
|
||||
@@ -1,4 +1,4 @@
|
||||
cmake_minimum_required(VERSION 3.1)
|
||||
cmake_minimum_required(VERSION 3.9)
|
||||
add_executable(test1Formatted test1.c parse_flags.c parse_flags.h)
|
||||
target_compile_definitions(test1Formatted PRIVATE TEST_FORMATTED=1)
|
||||
target_link_libraries(test1Formatted PRIVATE ${PROJECT_NAME})
|
||||
@@ -24,6 +24,7 @@ set(ALL_TEST_NAMES
|
||||
test_double_serializer
|
||||
test_float
|
||||
test_int_add
|
||||
test_int_get
|
||||
test_locale
|
||||
test_null
|
||||
test_parse
|
||||
@@ -38,15 +39,14 @@ set(ALL_TEST_NAMES
|
||||
|
||||
if (NOT DISABLE_JSON_POINTER)
|
||||
set(ALL_TEST_NAMES ${ALL_TEST_NAMES} test_json_pointer)
|
||||
if (NOT DISABLE_JSON_PATCH)
|
||||
set(ALL_TEST_NAMES ${ALL_TEST_NAMES} test_json_patch)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
foreach(TESTNAME ${ALL_TEST_NAMES})
|
||||
|
||||
add_executable(${TESTNAME} ${TESTNAME}.c)
|
||||
if(${TESTNAME} STREQUAL test_strerror OR ${TESTNAME} STREQUAL test_util_file)
|
||||
# For output consistency, we need _json_c_strerror() in some tests:
|
||||
target_sources(${TESTNAME} PRIVATE ../strerror_override.c)
|
||||
endif()
|
||||
add_test(NAME ${TESTNAME} COMMAND ${PROJECT_SOURCE_DIR}/tests/${TESTNAME}.test)
|
||||
|
||||
# XXX using the non-target_ versions of these doesn't work :(
|
||||
|
||||
233
tests/json_patch_spec_tests.json
Normal file
233
tests/json_patch_spec_tests.json
Normal file
@@ -0,0 +1,233 @@
|
||||
[
|
||||
{
|
||||
"comment": "4.1. add with missing object",
|
||||
"doc": { "q": { "bar": 2 } },
|
||||
"patch": [ {"op": "add", "path": "/a/b", "value": 1} ],
|
||||
"error":
|
||||
"path /a does not exist -- missing objects are not created recursively"
|
||||
},
|
||||
|
||||
{
|
||||
"comment": "A.1. Adding an Object Member",
|
||||
"doc": {
|
||||
"foo": "bar"
|
||||
},
|
||||
"patch": [
|
||||
{ "op": "add", "path": "/baz", "value": "qux" }
|
||||
],
|
||||
"expected": {
|
||||
"baz": "qux",
|
||||
"foo": "bar"
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
"comment": "A.2. Adding an Array Element",
|
||||
"doc": {
|
||||
"foo": [ "bar", "baz" ]
|
||||
},
|
||||
"patch": [
|
||||
{ "op": "add", "path": "/foo/1", "value": "qux" }
|
||||
],
|
||||
"expected": {
|
||||
"foo": [ "bar", "qux", "baz" ]
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
"comment": "A.3. Removing an Object Member",
|
||||
"doc": {
|
||||
"baz": "qux",
|
||||
"foo": "bar"
|
||||
},
|
||||
"patch": [
|
||||
{ "op": "remove", "path": "/baz" }
|
||||
],
|
||||
"expected": {
|
||||
"foo": "bar"
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
"comment": "A.4. Removing an Array Element",
|
||||
"doc": {
|
||||
"foo": [ "bar", "qux", "baz" ]
|
||||
},
|
||||
"patch": [
|
||||
{ "op": "remove", "path": "/foo/1" }
|
||||
],
|
||||
"expected": {
|
||||
"foo": [ "bar", "baz" ]
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
"comment": "A.5. Replacing a Value",
|
||||
"doc": {
|
||||
"baz": "qux",
|
||||
"foo": "bar"
|
||||
},
|
||||
"patch": [
|
||||
{ "op": "replace", "path": "/baz", "value": "boo" }
|
||||
],
|
||||
"expected": {
|
||||
"baz": "boo",
|
||||
"foo": "bar"
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
"comment": "A.6. Moving a Value",
|
||||
"doc": {
|
||||
"foo": {
|
||||
"bar": "baz",
|
||||
"waldo": "fred"
|
||||
},
|
||||
"qux": {
|
||||
"corge": "grault"
|
||||
}
|
||||
},
|
||||
"patch": [
|
||||
{ "op": "move", "from": "/foo/waldo", "path": "/qux/thud" }
|
||||
],
|
||||
"expected": {
|
||||
"foo": {
|
||||
"bar": "baz"
|
||||
},
|
||||
"qux": {
|
||||
"corge": "grault",
|
||||
"thud": "fred"
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
"comment": "A.7. Moving an Array Element",
|
||||
"doc": {
|
||||
"foo": [ "all", "grass", "cows", "eat" ]
|
||||
},
|
||||
"patch": [
|
||||
{ "op": "move", "from": "/foo/1", "path": "/foo/3" }
|
||||
],
|
||||
"expected": {
|
||||
"foo": [ "all", "cows", "eat", "grass" ]
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
"comment": "A.8. Testing a Value: Success",
|
||||
"doc": {
|
||||
"baz": "qux",
|
||||
"foo": [ "a", 2, "c" ]
|
||||
},
|
||||
"patch": [
|
||||
{ "op": "test", "path": "/baz", "value": "qux" },
|
||||
{ "op": "test", "path": "/foo/1", "value": 2 }
|
||||
],
|
||||
"expected": {
|
||||
"baz": "qux",
|
||||
"foo": [ "a", 2, "c" ]
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
"comment": "A.9. Testing a Value: Error",
|
||||
"doc": {
|
||||
"baz": "qux"
|
||||
},
|
||||
"patch": [
|
||||
{ "op": "test", "path": "/baz", "value": "bar" }
|
||||
],
|
||||
"error": "string not equivalent"
|
||||
},
|
||||
|
||||
{
|
||||
"comment": "A.10. Adding a nested Member Object",
|
||||
"doc": {
|
||||
"foo": "bar"
|
||||
},
|
||||
"patch": [
|
||||
{ "op": "add", "path": "/child", "value": { "grandchild": { } } }
|
||||
],
|
||||
"expected": {
|
||||
"foo": "bar",
|
||||
"child": {
|
||||
"grandchild": {
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
"comment": "A.11. Ignoring Unrecognized Elements",
|
||||
"doc": {
|
||||
"foo":"bar"
|
||||
},
|
||||
"patch": [
|
||||
{ "op": "add", "path": "/baz", "value": "qux", "xyz": 123 }
|
||||
],
|
||||
"expected": {
|
||||
"foo":"bar",
|
||||
"baz":"qux"
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
"comment": "A.12. Adding to a Non-existent Target",
|
||||
"doc": {
|
||||
"foo": "bar"
|
||||
},
|
||||
"patch": [
|
||||
{ "op": "add", "path": "/baz/bat", "value": "qux" }
|
||||
],
|
||||
"error": "add to a non-existent target"
|
||||
},
|
||||
|
||||
{
|
||||
"comment": "A.13 Invalid JSON Patch Document",
|
||||
"doc": {
|
||||
"foo": "bar"
|
||||
},
|
||||
"patch": [
|
||||
{ "op": "add", "path": "/baz", "value": "qux", "op": "remove" }
|
||||
],
|
||||
"error_wont_happen_in_jsonc": "operation has two 'op' members",
|
||||
"error": "Did not find element referenced by path field"
|
||||
},
|
||||
|
||||
{
|
||||
"comment": "A.14. ~ Escape Ordering",
|
||||
"doc": {
|
||||
"/": 9,
|
||||
"~1": 10
|
||||
},
|
||||
"patch": [{"op": "test", "path": "/~01", "value": 10}],
|
||||
"expected": {
|
||||
"/": 9,
|
||||
"~1": 10
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
"comment": "A.15. Comparing Strings and Numbers",
|
||||
"doc": {
|
||||
"/": 9,
|
||||
"~1": 10
|
||||
},
|
||||
"patch": [{"op": "test", "path": "/~01", "value": "10"}],
|
||||
"error": "number is not equal to string"
|
||||
},
|
||||
|
||||
{
|
||||
"comment": "A.16. Adding an Array Value",
|
||||
"doc": {
|
||||
"foo": ["bar"]
|
||||
},
|
||||
"patch": [{ "op": "add", "path": "/foo/-", "value": ["abc", "def"] }],
|
||||
"expected": {
|
||||
"foo": ["bar", ["abc", "def"]]
|
||||
}
|
||||
}
|
||||
|
||||
]
|
||||
540
tests/json_patch_tests.json
Normal file
540
tests/json_patch_tests.json
Normal file
@@ -0,0 +1,540 @@
|
||||
[
|
||||
{ "comment": "empty list, empty docs",
|
||||
"doc": {},
|
||||
"patch": [],
|
||||
"expected": {} },
|
||||
|
||||
{ "comment": "empty patch list",
|
||||
"doc": {"foo": 1},
|
||||
"patch": [],
|
||||
"expected": {"foo": 1} },
|
||||
|
||||
{ "comment": "rearrangements OK?",
|
||||
"doc": {"foo": 1, "bar": 2},
|
||||
"patch": [],
|
||||
"expected": {"bar":2, "foo": 1} },
|
||||
|
||||
{ "comment": "rearrangements OK? How about one level down ... array",
|
||||
"doc": [{"foo": 1, "bar": 2}],
|
||||
"patch": [],
|
||||
"expected": [{"bar":2, "foo": 1}] },
|
||||
|
||||
{ "comment": "rearrangements OK? How about one level down...",
|
||||
"doc": {"foo":{"foo": 1, "bar": 2}},
|
||||
"patch": [],
|
||||
"expected": {"foo":{"bar":2, "foo": 1}} },
|
||||
|
||||
{ "comment": "add replaces any existing field",
|
||||
"doc": {"foo": null},
|
||||
"patch": [{"op": "add", "path": "/foo", "value":1}],
|
||||
"expected": {"foo": 1} },
|
||||
|
||||
{ "comment": "toplevel array",
|
||||
"doc": [],
|
||||
"patch": [{"op": "add", "path": "/0", "value": "foo"}],
|
||||
"expected": ["foo"] },
|
||||
|
||||
{ "comment": "toplevel array, no change",
|
||||
"doc": ["foo"],
|
||||
"patch": [],
|
||||
"expected": ["foo"] },
|
||||
|
||||
{ "comment": "toplevel object, numeric string",
|
||||
"doc": {},
|
||||
"patch": [{"op": "add", "path": "/foo", "value": "1"}],
|
||||
"expected": {"foo":"1"} },
|
||||
|
||||
{ "comment": "toplevel object, integer",
|
||||
"doc": {},
|
||||
"patch": [{"op": "add", "path": "/foo", "value": 1}],
|
||||
"expected": {"foo":1} },
|
||||
|
||||
{ "comment": "Toplevel scalar values OK?",
|
||||
"doc": "foo",
|
||||
"patch": [{"op": "replace", "path": "", "value": "bar"}],
|
||||
"expected": "bar"
|
||||
},
|
||||
|
||||
{ "comment": "replace object document with array document?",
|
||||
"doc": {},
|
||||
"patch": [{"op": "add", "path": "", "value": []}],
|
||||
"expected": [] },
|
||||
|
||||
{ "comment": "replace array document with object document?",
|
||||
"doc": [],
|
||||
"patch": [{"op": "add", "path": "", "value": {}}],
|
||||
"expected": {} },
|
||||
|
||||
{ "comment": "append to root array document?",
|
||||
"doc": [],
|
||||
"patch": [{"op": "add", "path": "/-", "value": "hi"}],
|
||||
"expected": ["hi"] },
|
||||
|
||||
{ "comment": "Add, / target",
|
||||
"doc": {},
|
||||
"patch": [ {"op": "add", "path": "/", "value":1 } ],
|
||||
"expected": {"":1} },
|
||||
|
||||
{ "comment": "Add, /foo/ deep target (trailing slash)",
|
||||
"doc": {"foo": {}},
|
||||
"patch": [ {"op": "add", "path": "/foo/", "value":1 } ],
|
||||
"expected": {"foo":{"": 1}} },
|
||||
|
||||
{ "comment": "Add composite value at top level",
|
||||
"doc": {"foo": 1},
|
||||
"patch": [{"op": "add", "path": "/bar", "value": [1, 2]}],
|
||||
"expected": {"foo": 1, "bar": [1, 2]} },
|
||||
|
||||
{ "comment": "Add into composite value",
|
||||
"doc": {"foo": 1, "baz": [{"qux": "hello"}]},
|
||||
"patch": [{"op": "add", "path": "/baz/0/foo", "value": "world"}],
|
||||
"expected": {"foo": 1, "baz": [{"qux": "hello", "foo": "world"}]} },
|
||||
|
||||
{ "doc": {"bar": [1, 2]},
|
||||
"patch": [{"op": "add", "path": "/bar/8", "value": "5"}],
|
||||
"error": "Out of bounds (upper)" },
|
||||
|
||||
{ "doc": {"bar": [1, 2]},
|
||||
"patch": [{"op": "add", "path": "/bar/-1", "value": "5"}],
|
||||
"error": "Out of bounds (lower)" },
|
||||
|
||||
{ "doc": {"foo": 1},
|
||||
"patch": [{"op": "add", "path": "/bar", "value": true}],
|
||||
"expected": {"foo": 1, "bar": true} },
|
||||
|
||||
{ "doc": {"foo": 1},
|
||||
"patch": [{"op": "add", "path": "/bar", "value": false}],
|
||||
"expected": {"foo": 1, "bar": false} },
|
||||
|
||||
{ "doc": {"foo": 1},
|
||||
"patch": [{"op": "add", "path": "/bar", "value": null}],
|
||||
"expected": {"foo": 1, "bar": null} },
|
||||
|
||||
{ "comment": "0 can be an array index or object element name",
|
||||
"doc": {"foo": 1},
|
||||
"patch": [{"op": "add", "path": "/0", "value": "bar"}],
|
||||
"expected": {"foo": 1, "0": "bar" } },
|
||||
|
||||
{ "doc": ["foo"],
|
||||
"patch": [{"op": "add", "path": "/1", "value": "bar"}],
|
||||
"expected": ["foo", "bar"] },
|
||||
|
||||
{ "doc": ["foo", "sil"],
|
||||
"patch": [{"op": "add", "path": "/1", "value": "bar"}],
|
||||
"expected": ["foo", "bar", "sil"] },
|
||||
|
||||
{ "doc": ["foo", "sil"],
|
||||
"patch": [{"op": "add", "path": "/0", "value": "bar"}],
|
||||
"expected": ["bar", "foo", "sil"] },
|
||||
|
||||
{ "comment": "push item to array via last index + 1",
|
||||
"doc": ["foo", "sil"],
|
||||
"patch": [{"op":"add", "path": "/2", "value": "bar"}],
|
||||
"expected": ["foo", "sil", "bar"] },
|
||||
|
||||
{ "comment": "add item to array at index > length should fail",
|
||||
"doc": ["foo", "sil"],
|
||||
"patch": [{"op":"add", "path": "/3", "value": "bar"}],
|
||||
"error": "index is greater than number of items in array" },
|
||||
|
||||
{ "comment": "test against implementation-specific numeric parsing",
|
||||
"doc": {"1e0": "foo"},
|
||||
"patch": [{"op": "test", "path": "/1e0", "value": "foo"}],
|
||||
"expected": {"1e0": "foo"} },
|
||||
|
||||
{ "comment": "test with bad number should fail",
|
||||
"doc": ["foo", "bar"],
|
||||
"patch": [{"op": "test", "path": "/1e0", "value": "bar"}],
|
||||
"error": "test op shouldn't get array element 1" },
|
||||
|
||||
{ "doc": ["foo", "sil"],
|
||||
"patch": [{"op": "add", "path": "/bar", "value": 42}],
|
||||
"error": "Object operation on array target" },
|
||||
|
||||
{ "doc": ["foo", "sil"],
|
||||
"patch": [{"op": "add", "path": "/1", "value": ["bar", "baz"]}],
|
||||
"expected": ["foo", ["bar", "baz"], "sil"],
|
||||
"comment": "value in array add not flattened" },
|
||||
|
||||
{ "doc": {"foo": 1, "bar": [1, 2, 3, 4]},
|
||||
"patch": [{"op": "remove", "path": "/bar"}],
|
||||
"expected": {"foo": 1} },
|
||||
|
||||
{ "doc": {"foo": 1, "baz": [{"qux": "hello"}]},
|
||||
"patch": [{"op": "remove", "path": "/baz/0/qux"}],
|
||||
"expected": {"foo": 1, "baz": [{}]} },
|
||||
|
||||
{ "doc": {"foo": 1, "baz": [{"qux": "hello"}]},
|
||||
"patch": [{"op": "replace", "path": "/foo", "value": [1, 2, 3, 4]}],
|
||||
"expected": {"foo": [1, 2, 3, 4], "baz": [{"qux": "hello"}]} },
|
||||
|
||||
{ "doc": {"foo": [1, 2, 3, 4], "baz": [{"qux": "hello"}]},
|
||||
"patch": [{"op": "replace", "path": "/baz/0/qux", "value": "world"}],
|
||||
"expected": {"foo": [1, 2, 3, 4], "baz": [{"qux": "world"}]} },
|
||||
|
||||
{ "doc": ["foo"],
|
||||
"patch": [{"op": "replace", "path": "/0", "value": "bar"}],
|
||||
"expected": ["bar"] },
|
||||
|
||||
{ "doc": [""],
|
||||
"patch": [{"op": "replace", "path": "/0", "value": 0}],
|
||||
"expected": [0] },
|
||||
|
||||
{ "doc": [""],
|
||||
"patch": [{"op": "replace", "path": "/0", "value": true}],
|
||||
"expected": [true] },
|
||||
|
||||
{ "doc": [""],
|
||||
"patch": [{"op": "replace", "path": "/0", "value": false}],
|
||||
"expected": [false] },
|
||||
|
||||
{ "doc": [""],
|
||||
"patch": [{"op": "replace", "path": "/0", "value": null}],
|
||||
"expected": [null] },
|
||||
|
||||
{ "doc": ["foo", "sil"],
|
||||
"patch": [{"op": "replace", "path": "/1", "value": ["bar", "baz"]}],
|
||||
"expected": ["foo", ["bar", "baz"]],
|
||||
"comment": "value in array replace not flattened" },
|
||||
|
||||
{ "comment": "replace whole document",
|
||||
"doc": {"foo": "bar"},
|
||||
"patch": [{"op": "replace", "path": "", "value": {"baz": "qux"}}],
|
||||
"expected": {"baz": "qux"} },
|
||||
|
||||
{ "comment": "add whole document, null",
|
||||
"doc": {},
|
||||
"Note1": "We can't pass null in to json_patch_apply, so start with _something_ and remove it",
|
||||
"patch": [
|
||||
{"op": "remove", "path": ""},
|
||||
{"op": "add", "path": "", "value": {"baz": "qux"}}
|
||||
],
|
||||
"expected": {"baz": "qux"} },
|
||||
|
||||
{ "comment": "replace whole document, null",
|
||||
"doc": {},
|
||||
"Note1": "We can't pass null in to json_patch_apply, so start with _something_ and remove it",
|
||||
"patch": [
|
||||
{"op": "remove", "path": ""},
|
||||
{"op": "replace", "path": "", "value": {"baz": "qux"}}
|
||||
],
|
||||
"error": "The spec says the target location must exist, so replacing a null document fails"
|
||||
},
|
||||
|
||||
{ "comment": "remove whole document",
|
||||
"doc": {"foo": "bar"},
|
||||
"patch": [{"op": "remove", "path": ""}],
|
||||
"expected": null },
|
||||
|
||||
{ "comment": "remove whole document",
|
||||
"doc": {"foo": "bar"},
|
||||
"patch": [{"op": "remove", "path": ""}],
|
||||
"expected": null },
|
||||
|
||||
{ "comment": "remove whole document, array",
|
||||
"doc": ["foo", "bar"],
|
||||
"patch": [{"op": "remove", "path": ""}],
|
||||
"expected": null },
|
||||
|
||||
{ "comment": "remove whole document, string",
|
||||
"doc": "foo",
|
||||
"patch": [{"op": "remove", "path": ""}],
|
||||
"expected": null },
|
||||
|
||||
{ "comment": "remove whole document, null",
|
||||
"doc": {},
|
||||
"Note1": "We can't pass null in to json_patch_apply, so start with _something_ and remove it",
|
||||
"patch": [
|
||||
{"op": "remove", "path": ""},
|
||||
{"op": "remove", "path": ""},
|
||||
],
|
||||
"error": "The spec says the target location must exist, so removing a null document fails"
|
||||
},
|
||||
|
||||
{ "comment": "test replace with missing parent key should fail",
|
||||
"doc": {"bar": "baz"},
|
||||
"patch": [{"op": "replace", "path": "/foo/bar", "value": false}],
|
||||
"error": "replace op should fail with missing parent key" },
|
||||
|
||||
{ "comment": "spurious patch properties",
|
||||
"doc": {"foo": 1},
|
||||
"patch": [{"op": "test", "path": "/foo", "value": 1, "spurious": 1}],
|
||||
"expected": {"foo": 1} },
|
||||
|
||||
{ "doc": {"foo": null},
|
||||
"patch": [{"op": "test", "path": "/foo", "value": null}],
|
||||
"expected": {"foo": null},
|
||||
"comment": "null value should be valid obj property" },
|
||||
|
||||
{ "doc": {"foo": null},
|
||||
"patch": [{"op": "replace", "path": "/foo", "value": "truthy"}],
|
||||
"expected": {"foo": "truthy"},
|
||||
"comment": "null value should be valid obj property to be replaced with something truthy" },
|
||||
|
||||
{ "doc": {"foo": null},
|
||||
"patch": [{"op": "move", "from": "/foo", "path": "/bar"}],
|
||||
"expected": {"bar": null},
|
||||
"comment": "null value should be valid obj property to be moved" },
|
||||
|
||||
{ "doc": {"foo": null},
|
||||
"patch": [{"op": "copy", "from": "/foo", "path": "/bar"}],
|
||||
"expected": {"foo": null, "bar": null},
|
||||
"comment": "null value should be valid obj property to be copied" },
|
||||
|
||||
{ "doc": {"foo": null},
|
||||
"patch": [{"op": "remove", "path": "/foo"}],
|
||||
"expected": {},
|
||||
"comment": "null value should be valid obj property to be removed" },
|
||||
|
||||
{ "doc": {"foo": "bar"},
|
||||
"patch": [{"op": "replace", "path": "/foo", "value": null}],
|
||||
"expected": {"foo": null},
|
||||
"comment": "null value should still be valid obj property replace other value" },
|
||||
|
||||
{ "doc": {"foo": {"foo": 1, "bar": 2}},
|
||||
"patch": [{"op": "test", "path": "/foo", "value": {"bar": 2, "foo": 1}}],
|
||||
"expected": {"foo": {"foo": 1, "bar": 2}},
|
||||
"comment": "test should pass despite rearrangement" },
|
||||
|
||||
{ "doc": {"foo": [{"foo": 1, "bar": 2}]},
|
||||
"patch": [{"op": "test", "path": "/foo", "value": [{"bar": 2, "foo": 1}]}],
|
||||
"expected": {"foo": [{"foo": 1, "bar": 2}]},
|
||||
"comment": "test should pass despite (nested) rearrangement" },
|
||||
|
||||
{ "doc": {"foo": {"bar": [1, 2, 5, 4]}},
|
||||
"patch": [{"op": "test", "path": "/foo", "value": {"bar": [1, 2, 5, 4]}}],
|
||||
"expected": {"foo": {"bar": [1, 2, 5, 4]}},
|
||||
"comment": "test should pass - no error" },
|
||||
|
||||
{ "doc": {"foo": {"bar": [1, 2, 5, 4]}},
|
||||
"patch": [{"op": "test", "path": "/foo", "value": [1, 2]}],
|
||||
"error": "test op should fail" },
|
||||
|
||||
{ "comment": "Test the whole document",
|
||||
"doc": { "foo": 1 },
|
||||
"patch": [{"op": "test", "path": "", "value": {"foo": 1}}],
|
||||
"expected": { "foo": 1 } },
|
||||
|
||||
{ "comment": "Test the whole document, no match",
|
||||
"doc": { "foo": 1 },
|
||||
"patch": [{"op": "test", "path": "", "value": {"foo": 2}}],
|
||||
"expected": { "foo": 1 },
|
||||
"error": "Tested value does not match original doc" },
|
||||
|
||||
{ "comment": "Empty-string element",
|
||||
"doc": { "": 1 },
|
||||
"patch": [{"op": "test", "path": "/", "value": 1}],
|
||||
"expected": { "": 1 } },
|
||||
|
||||
{ "doc": {
|
||||
"foo": ["bar", "baz"],
|
||||
"": 0,
|
||||
"a/b": 1,
|
||||
"c%d": 2,
|
||||
"e^f": 3,
|
||||
"g|h": 4,
|
||||
"i\\j": 5,
|
||||
"k\"l": 6,
|
||||
" ": 7,
|
||||
"m~n": 8
|
||||
},
|
||||
"patch": [{"op": "test", "path": "/foo", "value": ["bar", "baz"]},
|
||||
{"op": "test", "path": "/foo/0", "value": "bar"},
|
||||
{"op": "test", "path": "/", "value": 0},
|
||||
{"op": "test", "path": "/a~1b", "value": 1},
|
||||
{"op": "test", "path": "/c%d", "value": 2},
|
||||
{"op": "test", "path": "/e^f", "value": 3},
|
||||
{"op": "test", "path": "/g|h", "value": 4},
|
||||
{"op": "test", "path": "/i\\j", "value": 5},
|
||||
{"op": "test", "path": "/k\"l", "value": 6},
|
||||
{"op": "test", "path": "/ ", "value": 7},
|
||||
{"op": "test", "path": "/m~0n", "value": 8}],
|
||||
"expected": {
|
||||
"": 0,
|
||||
" ": 7,
|
||||
"a/b": 1,
|
||||
"c%d": 2,
|
||||
"e^f": 3,
|
||||
"foo": [
|
||||
"bar",
|
||||
"baz"
|
||||
],
|
||||
"g|h": 4,
|
||||
"i\\j": 5,
|
||||
"k\"l": 6,
|
||||
"m~n": 8
|
||||
}
|
||||
},
|
||||
{ "comment": "Move to same location has no effect",
|
||||
"doc": {"foo": 1},
|
||||
"patch": [{"op": "move", "from": "/foo", "path": "/foo"}],
|
||||
"expected": {"foo": 1} },
|
||||
|
||||
{ "doc": {"foo": 1, "baz": [{"qux": "hello"}]},
|
||||
"patch": [{"op": "move", "from": "/foo", "path": "/bar"}],
|
||||
"expected": {"baz": [{"qux": "hello"}], "bar": 1} },
|
||||
|
||||
{ "doc": {"baz": [{"qux": "hello"}], "bar": 1},
|
||||
"patch": [{"op": "move", "from": "/baz/0/qux", "path": "/baz/1"}],
|
||||
"expected": {"baz": [{}, "hello"], "bar": 1} },
|
||||
|
||||
{ "doc": {"baz": [{"qux": "hello"}], "bar": 1},
|
||||
"patch": [{"op": "copy", "from": "/baz/0", "path": "/boo"}],
|
||||
"expected": {"baz":[{"qux":"hello"}],"bar":1,"boo":{"qux":"hello"}} },
|
||||
|
||||
{ "comment": "replacing the root of the document is possible with add",
|
||||
"doc": {"foo": "bar"},
|
||||
"patch": [{"op": "add", "path": "", "value": {"baz": "qux"}}],
|
||||
"expected": {"baz":"qux"}},
|
||||
|
||||
{ "comment": "Adding to \"/-\" adds to the end of the array",
|
||||
"doc": [ 1, 2 ],
|
||||
"patch": [ { "op": "add", "path": "/-", "value": { "foo": [ "bar", "baz" ] } } ],
|
||||
"expected": [ 1, 2, { "foo": [ "bar", "baz" ] } ]},
|
||||
|
||||
{ "comment": "Adding to \"/-\" adds to the end of the array, even n levels down",
|
||||
"doc": [ 1, 2, [ 3, [ 4, 5 ] ] ],
|
||||
"patch": [ { "op": "add", "path": "/2/1/-", "value": { "foo": [ "bar", "baz" ] } } ],
|
||||
"expected": [ 1, 2, [ 3, [ 4, 5, { "foo": [ "bar", "baz" ] } ] ] ]},
|
||||
|
||||
{ "comment": "test remove with bad number should fail",
|
||||
"doc": {"foo": 1, "baz": [{"qux": "hello"}]},
|
||||
"patch": [{"op": "remove", "path": "/baz/1e0/qux"}],
|
||||
"error": "remove op shouldn't remove from array with bad number" },
|
||||
|
||||
{ "comment": "test remove on array",
|
||||
"doc": [1, 2, 3, 4],
|
||||
"patch": [{"op": "remove", "path": "/0"}],
|
||||
"expected": [2, 3, 4] },
|
||||
|
||||
{ "comment": "test repeated removes",
|
||||
"doc": [1, 2, 3, 4],
|
||||
"patch": [{ "op": "remove", "path": "/1" },
|
||||
{ "op": "remove", "path": "/2" }],
|
||||
"expected": [1, 3] },
|
||||
|
||||
{ "comment": "test remove with bad index should fail",
|
||||
"doc": [1, 2, 3, 4],
|
||||
"patch": [{"op": "remove", "path": "/1e0"}],
|
||||
"error": "remove op shouldn't remove from array with bad number" },
|
||||
|
||||
{ "comment": "test replace with bad number should fail",
|
||||
"doc": [""],
|
||||
"patch": [{"op": "replace", "path": "/1e0", "value": false}],
|
||||
"error": "replace op shouldn't replace in array with bad number" },
|
||||
|
||||
{ "comment": "test copy with bad number should fail",
|
||||
"doc": {"baz": [1,2,3], "bar": 1},
|
||||
"patch": [{"op": "copy", "from": "/baz/1e0", "path": "/boo"}],
|
||||
"error": "copy op shouldn't work with bad number" },
|
||||
|
||||
{ "comment": "test move with bad number should fail",
|
||||
"doc": {"foo": 1, "baz": [1,2,3,4]},
|
||||
"patch": [{"op": "move", "from": "/baz/1e0", "path": "/foo"}],
|
||||
"error": "move op shouldn't work with bad number" },
|
||||
|
||||
{ "comment": "test add with bad number should fail",
|
||||
"doc": ["foo", "sil"],
|
||||
"patch": [{"op": "add", "path": "/1e0", "value": "bar"}],
|
||||
"error": "add op shouldn't add to array with bad number" },
|
||||
|
||||
{ "comment": "missing 'path' parameter",
|
||||
"doc": {},
|
||||
"patch": [ { "op": "add", "value": "bar" } ],
|
||||
"error": "missing 'path' parameter" },
|
||||
|
||||
{ "comment": "'path' parameter with null value",
|
||||
"doc": {},
|
||||
"patch": [ { "op": "add", "path": null, "value": "bar" } ],
|
||||
"error": "null is not valid value for 'path'" },
|
||||
|
||||
{ "comment": "invalid JSON Pointer token",
|
||||
"doc": {},
|
||||
"patch": [ { "op": "add", "path": "foo", "value": "bar" } ],
|
||||
"error": "JSON Pointer should start with a slash" },
|
||||
|
||||
{ "comment": "missing 'value' parameter to add",
|
||||
"doc": [ 1 ],
|
||||
"patch": [ { "op": "add", "path": "/-" } ],
|
||||
"error": "missing 'value' parameter" },
|
||||
|
||||
{ "comment": "missing 'value' parameter to replace",
|
||||
"doc": [ 1 ],
|
||||
"patch": [ { "op": "replace", "path": "/0" } ],
|
||||
"error": "missing 'value' parameter" },
|
||||
|
||||
{ "comment": "missing 'value' parameter to test",
|
||||
"doc": [ null ],
|
||||
"patch": [ { "op": "test", "path": "/0" } ],
|
||||
"error": "missing 'value' parameter" },
|
||||
|
||||
{ "comment": "missing value parameter to test - where undef is falsy",
|
||||
"doc": [ false ],
|
||||
"patch": [ { "op": "test", "path": "/0" } ],
|
||||
"error": "missing 'value' parameter" },
|
||||
|
||||
{ "comment": "missing from parameter to copy",
|
||||
"doc": [ 1 ],
|
||||
"patch": [ { "op": "copy", "path": "/-" } ],
|
||||
"error": "missing 'from' parameter" },
|
||||
|
||||
{ "comment": "missing from location to copy",
|
||||
"doc": { "foo": 1 },
|
||||
"patch": [ { "op": "copy", "from": "/bar", "path": "/foo" } ],
|
||||
"error": "missing 'from' location" },
|
||||
|
||||
{ "comment": "missing from parameter to move",
|
||||
"doc": { "foo": 1 },
|
||||
"patch": [ { "op": "move", "path": "" } ],
|
||||
"error": "missing 'from' parameter" },
|
||||
|
||||
{ "comment": "missing from location to move",
|
||||
"doc": { "foo": 1 },
|
||||
"patch": [ { "op": "move", "from": "/bar", "path": "/foo" } ],
|
||||
"error": "missing 'from' location" },
|
||||
|
||||
{ "comment": "duplicate ops, json-c parses this as op:move",
|
||||
"doc": { "foo": "bar" },
|
||||
"patch": [ { "op": "add", "path": "/baz", "value": "qux",
|
||||
"op": "move", "from":"/foo" } ],
|
||||
"error_wont_happen_in_jsonc": "patch has two 'op' members",
|
||||
"expected": { "baz": "bar" }
|
||||
},
|
||||
|
||||
{ "comment": "unrecognized op should fail",
|
||||
"doc": {"foo": 1},
|
||||
"patch": [{"op": "spam", "path": "/foo", "value": 1}],
|
||||
"error": "Unrecognized op 'spam'" },
|
||||
|
||||
{ "comment": "test with bad array number that has leading zeros",
|
||||
"doc": ["foo", "bar"],
|
||||
"patch": [{"op": "test", "path": "/00", "value": "foo"}],
|
||||
"error": "test op should reject the array value, it has leading zeros" },
|
||||
|
||||
{ "comment": "test with bad array number that has leading zeros",
|
||||
"doc": ["foo", "bar"],
|
||||
"patch": [{"op": "test", "path": "/01", "value": "bar"}],
|
||||
"error": "test op should reject the array value, it has leading zeros" },
|
||||
|
||||
{ "comment": "Removing nonexistent field",
|
||||
"doc": {"foo" : "bar"},
|
||||
"patch": [{"op": "remove", "path": "/baz"}],
|
||||
"error": "removing a nonexistent field should fail" },
|
||||
|
||||
{ "comment": "Removing deep nonexistent path",
|
||||
"doc": {"foo" : "bar"},
|
||||
"patch": [{"op": "remove", "path": "/missing1/missing2"}],
|
||||
"error": "removing a nonexistent field should fail" },
|
||||
|
||||
{ "comment": "Removing nonexistent index",
|
||||
"doc": ["foo", "bar"],
|
||||
"patch": [{"op": "remove", "path": "/2"}],
|
||||
"error": "removing a nonexistent index should fail" },
|
||||
|
||||
{ "comment": "Patch with different capitalisation than doc",
|
||||
"doc": {"foo":"bar"},
|
||||
"patch": [{"op": "add", "path": "/FOO", "value": "BAR"}],
|
||||
"expected": {"foo": "bar", "FOO": "BAR"}
|
||||
}
|
||||
|
||||
]
|
||||
@@ -1,3 +1,6 @@
|
||||
#ifdef NDEBUG
|
||||
#undef NDEBUG
|
||||
#endif
|
||||
#include "config.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
#ifdef NDEBUG
|
||||
#undef NDEBUG
|
||||
#endif
|
||||
#include <assert.h>
|
||||
#include <limits.h>
|
||||
#include <stddef.h>
|
||||
@@ -58,7 +61,7 @@ static const char *to_json_string(json_object *obj, int flags)
|
||||
#endif
|
||||
|
||||
json_object *make_array(void);
|
||||
json_object *make_array()
|
||||
json_object *make_array(void)
|
||||
{
|
||||
json_object *my_array;
|
||||
|
||||
@@ -74,7 +77,7 @@ json_object *make_array()
|
||||
}
|
||||
|
||||
void test_array_del_idx(void);
|
||||
void test_array_del_idx()
|
||||
void test_array_del_idx(void)
|
||||
{
|
||||
int rc;
|
||||
size_t ii;
|
||||
@@ -140,7 +143,7 @@ void test_array_del_idx()
|
||||
}
|
||||
|
||||
void test_array_list_expand_internal(void);
|
||||
void test_array_list_expand_internal()
|
||||
void test_array_list_expand_internal(void)
|
||||
{
|
||||
int rc;
|
||||
size_t ii;
|
||||
@@ -186,6 +189,42 @@ void test_array_list_expand_internal()
|
||||
json_object_put(my_array);
|
||||
}
|
||||
|
||||
void test_array_insert_idx(void);
|
||||
void test_array_insert_idx(void)
|
||||
{
|
||||
json_object *my_array;
|
||||
struct json_object *jo1;
|
||||
|
||||
my_array = json_object_new_array();
|
||||
json_object_array_add(my_array, json_object_new_int(1));
|
||||
json_object_array_add(my_array, json_object_new_int(2));
|
||||
json_object_array_add(my_array, json_object_new_int(5));
|
||||
|
||||
json_object_array_insert_idx(my_array, 2, json_object_new_int(4));
|
||||
jo1 = json_tokener_parse("[1, 2, 4, 5]");
|
||||
assert(1 == json_object_equal(my_array, jo1));
|
||||
json_object_put(jo1);
|
||||
|
||||
json_object_array_insert_idx(my_array, 2, json_object_new_int(3));
|
||||
|
||||
jo1 = json_tokener_parse("[1, 2, 3, 4, 5]");
|
||||
assert(1 == json_object_equal(my_array, jo1));
|
||||
json_object_put(jo1);
|
||||
|
||||
json_object_array_insert_idx(my_array, 5, json_object_new_int(6));
|
||||
|
||||
jo1 = json_tokener_parse("[1, 2, 3, 4, 5, 6]");
|
||||
assert(1 == json_object_equal(my_array, jo1));
|
||||
json_object_put(jo1);
|
||||
|
||||
json_object_array_insert_idx(my_array, 7, json_object_new_int(8));
|
||||
jo1 = json_tokener_parse("[1, 2, 3, 4, 5, 6, null, 8]");
|
||||
assert(1 == json_object_equal(my_array, jo1));
|
||||
json_object_put(jo1);
|
||||
|
||||
json_object_put(my_array);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
json_object *my_string, *my_int, *my_null, *my_object, *my_array;
|
||||
@@ -250,6 +289,8 @@ int main(int argc, char **argv)
|
||||
|
||||
json_object_put(my_array);
|
||||
|
||||
test_array_insert_idx();
|
||||
|
||||
test_array_del_idx();
|
||||
test_array_list_expand_internal();
|
||||
|
||||
@@ -308,6 +349,11 @@ int main(int argc, char **argv)
|
||||
{
|
||||
printf("\t%s: %s\n", key, json_object_to_json_string(val));
|
||||
}
|
||||
|
||||
json_object *empty_array = json_object_new_array();
|
||||
json_object *empty_obj = json_object_new_object();
|
||||
json_object_object_add(my_object, "empty_array", empty_array);
|
||||
json_object_object_add(my_object, "empty_obj", empty_obj);
|
||||
printf("my_object.to_string()=%s\n", json_object_to_json_string(my_object));
|
||||
|
||||
json_object_put(my_array);
|
||||
|
||||
@@ -75,4 +75,4 @@ my_object=
|
||||
foo: "bar"
|
||||
bool0: false
|
||||
bool1: true
|
||||
my_object.to_string()={ "abc": 12, "foo": "bar", "bool0": false, "bool1": true }
|
||||
my_object.to_string()={ "abc": 12, "foo": "bar", "bool0": false, "bool1": true, "empty_array": [ ], "empty_obj": { } }
|
||||
|
||||
@@ -75,4 +75,4 @@ my_object=
|
||||
foo: "bar"
|
||||
bool0: false
|
||||
bool1: true
|
||||
my_object.to_string()={"abc":12,"foo":"bar","bool0":false,"bool1":true}
|
||||
my_object.to_string()={"abc":12,"foo":"bar","bool0":false,"bool1":true,"empty_array":[],"empty_obj":{}}
|
||||
|
||||
@@ -97,5 +97,7 @@ my_object.to_string()={
|
||||
"abc":12,
|
||||
"foo":"bar",
|
||||
"bool0":false,
|
||||
"bool1":true
|
||||
"bool1":true,
|
||||
"empty_array":[],
|
||||
"empty_obj":{}
|
||||
}
|
||||
|
||||
@@ -75,4 +75,4 @@ my_object=
|
||||
foo: "bar"
|
||||
bool0: false
|
||||
bool1: true
|
||||
my_object.to_string()={ "abc": 12, "foo": "bar", "bool0": false, "bool1": true }
|
||||
my_object.to_string()={ "abc": 12, "foo": "bar", "bool0": false, "bool1": true, "empty_array": [ ], "empty_obj": { } }
|
||||
|
||||
@@ -97,5 +97,7 @@ my_object.to_string()={
|
||||
"abc": 12,
|
||||
"foo": "bar",
|
||||
"bool0": false,
|
||||
"bool1": true
|
||||
"bool1": true,
|
||||
"empty_array": [],
|
||||
"empty_obj": {}
|
||||
}
|
||||
|
||||
@@ -97,5 +97,7 @@ my_object.to_string()={
|
||||
"abc": 12,
|
||||
"foo": "bar",
|
||||
"bool0": false,
|
||||
"bool1": true
|
||||
"bool1": true,
|
||||
"empty_array": [],
|
||||
"empty_obj": {}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
#ifdef NDEBUG
|
||||
#undef NDEBUG
|
||||
#endif
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
@@ -2,6 +2,9 @@
|
||||
* gcc -o utf8 utf8.c -I/home/y/include -L./.libs -ljson
|
||||
*/
|
||||
|
||||
#ifdef NDEBUG
|
||||
#undef NDEBUG
|
||||
#endif
|
||||
#include "config.h"
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
@@ -28,7 +31,7 @@ void print_hex(const char *s)
|
||||
}
|
||||
|
||||
static void test_lot_of_adds(void);
|
||||
static void test_lot_of_adds()
|
||||
static void test_lot_of_adds(void)
|
||||
{
|
||||
int ii;
|
||||
char key[50];
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
#ifdef NDEBUG
|
||||
#undef NDEBUG
|
||||
#endif
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
@@ -3,6 +3,9 @@
|
||||
* Also checks the json_object_get_type and json_object_is_type functions.
|
||||
*/
|
||||
|
||||
#ifdef NDEBUG
|
||||
#undef NDEBUG
|
||||
#endif
|
||||
#include "config.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@@ -94,7 +97,7 @@ static void getit(struct json_object *new_obj, const char *field)
|
||||
printf("new_obj.%s json_object_get_double()=%f\n", field, json_object_get_double(o));
|
||||
}
|
||||
|
||||
static void checktype_header()
|
||||
static void checktype_header(void)
|
||||
{
|
||||
printf("json_object_is_type: %s,%s,%s,%s,%s,%s,%s\n", json_type_to_name(json_type_null),
|
||||
json_type_to_name(json_type_boolean), json_type_to_name(json_type_double),
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
#ifdef NDEBUG
|
||||
#undef NDEBUG
|
||||
#endif
|
||||
#include <assert.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
@@ -19,7 +22,7 @@ int main(int argc, char **argv)
|
||||
}
|
||||
|
||||
/* make sure only lowercase forms are parsed in strict mode */
|
||||
static void test_case_parse()
|
||||
static void test_case_parse(void)
|
||||
{
|
||||
struct json_tokener *tok;
|
||||
json_object *new_obj;
|
||||
|
||||
@@ -2,6 +2,9 @@
|
||||
* Tests if json_object_equal behaves correct.
|
||||
*/
|
||||
|
||||
#ifdef NDEBUG
|
||||
#undef NDEBUG
|
||||
#endif
|
||||
#include "config.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
@@ -9,7 +12,7 @@
|
||||
#include "json_inttypes.h"
|
||||
#include "json_object.h"
|
||||
|
||||
int main()
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
/* integer tests */
|
||||
struct json_object *int1 = json_object_new_int(0);
|
||||
|
||||
@@ -22,7 +22,7 @@ static const char *json_str1 =
|
||||
" \"number\": 16446744073709551615,"
|
||||
" \"title\": \"S\","
|
||||
" \"null_obj\": null, "
|
||||
" \"exixt\": false,"
|
||||
" \"exist\": false,"
|
||||
" \"quantity\":20,"
|
||||
" \"univalent\":19.8,"
|
||||
" \"GlossList\": {"
|
||||
@@ -136,7 +136,7 @@ int main(int argc, char **argv)
|
||||
assert(0 == json_object_deep_copy(src2, &dst2, NULL));
|
||||
assert(0 == json_object_deep_copy(src3, &dst3, NULL));
|
||||
|
||||
printf("PASSED - all json_object_deep_copy() returned succesful\n");
|
||||
printf("PASSED - all json_object_deep_copy() returned successful\n");
|
||||
|
||||
assert(-1 == json_object_deep_copy(src1, &dst1, NULL));
|
||||
assert(errno == EINVAL);
|
||||
@@ -151,7 +151,7 @@ int main(int argc, char **argv)
|
||||
assert(1 == json_object_equal(src2, dst2));
|
||||
assert(1 == json_object_equal(src3, dst3));
|
||||
|
||||
printf("PASSED - all json_object_equal() tests returned succesful\n");
|
||||
printf("PASSED - all json_object_equal() tests returned successful\n");
|
||||
|
||||
assert(0 == strcmp(json_object_to_json_string_ext(src1, JSON_C_TO_STRING_PRETTY),
|
||||
json_object_to_json_string_ext(dst1, JSON_C_TO_STRING_PRETTY)));
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
PASSED - loaded input data
|
||||
PASSED - all json_object_deep_copy() returned succesful
|
||||
PASSED - all json_object_deep_copy() returned successful
|
||||
PASSED - all json_object_deep_copy() returned EINVAL for non-null pointer
|
||||
PASSED - all json_object_equal() tests returned succesful
|
||||
PASSED - all json_object_equal() tests returned successful
|
||||
PASSED - comparison of string output
|
||||
PASSED - trying to overrwrite an object that has refcount > 1
|
||||
Printing JSON objects for visual inspection
|
||||
@@ -14,7 +14,7 @@ Printing JSON objects for visual inspection
|
||||
"number":16446744073709551615,
|
||||
"title":"S",
|
||||
"null_obj":null,
|
||||
"exixt":false,
|
||||
"exist":false,
|
||||
"quantity":20,
|
||||
"univalent":19.8,
|
||||
"GlossList":{
|
||||
|
||||
@@ -2,6 +2,9 @@
|
||||
* Tests if the format string for double serialization is handled correctly
|
||||
*/
|
||||
|
||||
#ifdef NDEBUG
|
||||
#undef NDEBUG
|
||||
#endif
|
||||
#include "config.h"
|
||||
#include <stdio.h>
|
||||
|
||||
@@ -11,7 +14,7 @@
|
||||
/* Avoid compiler warnings about diving by constant zero */
|
||||
double zero_dot_zero = 0.0;
|
||||
|
||||
int main()
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
struct json_object *obj = json_object_new_double(0.5);
|
||||
char udata[] = "test";
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
/* Copyright (C) 2016 by Rainer Gerhards
|
||||
* Released under ASL 2.0 */
|
||||
#ifdef NDEBUG
|
||||
#undef NDEBUG
|
||||
#endif
|
||||
#include "config.h"
|
||||
#include "json_object.h"
|
||||
#include "json_tokener.h"
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
#ifdef NDEBUG
|
||||
#undef NDEBUG
|
||||
#endif
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
65
tests/test_int_get.c
Normal file
65
tests/test_int_get.c
Normal file
@@ -0,0 +1,65 @@
|
||||
#ifdef NDEBUG
|
||||
#undef NDEBUG
|
||||
#endif
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "json.h"
|
||||
|
||||
#define I64_MAX_S "9223372036854775807"
|
||||
#define I64_OVER "9223372036854775808"
|
||||
#define I64_MIN_S "-9223372036854775808"
|
||||
#define I64_UNDER "-9223372036854775809"
|
||||
#define U64_MAX_S "18446744073709551615"
|
||||
#define U64_OUT_S "18446744073709551616"
|
||||
|
||||
#define CHECK_GET(GET_F, J, EXPECTED) { struct json_object *jtmp = J; errno = 0; assert(GET_F(jtmp) == EXPECTED); json_object_put(jtmp); }
|
||||
#define CHECK_GET_INT(J, EXPECTED) CHECK_GET(json_object_get_int, J, EXPECTED)
|
||||
#define CHECK_GET_INT64(J, EXPECTED) CHECK_GET(json_object_get_int64, J, EXPECTED)
|
||||
#define CHECK_GET_UINT64(J, EXPECTED) CHECK_GET(json_object_get_uint64, J, EXPECTED)
|
||||
|
||||
#define CHECK_BASE(J, EXPECTED) CHECK_GET_INT(J, EXPECTED); CHECK_GET_INT64(J, EXPECTED); CHECK_GET_UINT64(J, EXPECTED)
|
||||
|
||||
#define N_INT json_object_new_int
|
||||
#define N_I64 json_object_new_int64
|
||||
#define N_U64 json_object_new_uint64
|
||||
#define N_STR json_object_new_string
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
CHECK_BASE(N_INT(5), 5);
|
||||
CHECK_BASE(N_INT(0), 0);
|
||||
CHECK_BASE(N_STR("0"), 0);
|
||||
CHECK_BASE(N_STR("00000"), 0);
|
||||
CHECK_BASE(N_STR("000004568789"), 4568789);
|
||||
CHECK_BASE(N_STR("0xFF"), 0 && errno == 0); // Hex-string values being parsed as 0 is the intended behavior
|
||||
CHECK_BASE(N_STR("333this_seems_a_valid_string"), 333);
|
||||
CHECK_BASE(N_STR("this_is_not_a_number"), 0 && errno == EINVAL);
|
||||
CHECK_BASE(N_STR("B0"), 0 && errno == EINVAL);
|
||||
printf("BASE CHECK PASSED\n");
|
||||
|
||||
CHECK_GET_INT(N_I64(INT32_MAX), INT32_MAX && errno == 0);
|
||||
CHECK_GET_INT(N_I64(INT32_MIN), INT32_MIN && errno == 0);
|
||||
CHECK_GET_INT(N_I64(INT64_MAX), INT32_MAX && errno == 0);
|
||||
CHECK_GET_INT(N_I64(INT64_MIN), INT32_MIN && errno == 0);
|
||||
CHECK_GET_INT(N_STR(I64_MAX_S), INT32_MAX && errno == 0);
|
||||
CHECK_GET_INT(N_STR(I64_MIN_S), INT32_MIN && errno == 0);
|
||||
printf("INT GET PASSED\n");
|
||||
|
||||
CHECK_GET_INT64(N_I64(INT64_MAX), INT64_MAX && errno == 0);
|
||||
CHECK_GET_INT64(N_I64(INT64_MIN), INT64_MIN && errno == 0);
|
||||
CHECK_GET_INT64(N_STR(I64_MAX_S), INT64_MAX && errno == 0);
|
||||
CHECK_GET_INT64(N_STR(I64_MIN_S), INT64_MIN && errno == 0);
|
||||
CHECK_GET_INT64(N_STR(I64_OVER), INT64_MAX && errno == ERANGE);
|
||||
CHECK_GET_INT64(N_STR(I64_UNDER), INT64_MIN && errno == ERANGE);
|
||||
printf("INT64 GET PASSED\n");
|
||||
|
||||
CHECK_GET_UINT64(N_U64(UINT64_MAX), UINT64_MAX && errno == 0);
|
||||
CHECK_GET_UINT64(N_U64(-1), UINT64_MAX && errno == 0);
|
||||
CHECK_GET_UINT64(N_STR(U64_OUT_S), UINT64_MAX && errno == ERANGE);
|
||||
printf("UINT64 GET PASSED\n");
|
||||
|
||||
printf("PASSED\n");
|
||||
return 0;
|
||||
}
|
||||
5
tests/test_int_get.expected
Normal file
5
tests/test_int_get.expected
Normal file
@@ -0,0 +1,5 @@
|
||||
BASE CHECK PASSED
|
||||
INT GET PASSED
|
||||
INT64 GET PASSED
|
||||
UINT64 GET PASSED
|
||||
PASSED
|
||||
1
tests/test_int_get.test
Symbolic link
1
tests/test_int_get.test
Symbolic link
@@ -0,0 +1 @@
|
||||
test_basic.test
|
||||
127
tests/test_json_patch.c
Normal file
127
tests/test_json_patch.c
Normal file
@@ -0,0 +1,127 @@
|
||||
#ifdef NDEBUG
|
||||
#undef NDEBUG
|
||||
#endif
|
||||
#include "strerror_override.h"
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "config.h"
|
||||
#include "json.h"
|
||||
#include "snprintf_compat.h"
|
||||
|
||||
#ifndef PATH_MAX
|
||||
#define PATH_MAX 256
|
||||
#endif
|
||||
|
||||
void test_json_patch_op(struct json_object *jo)
|
||||
{
|
||||
const char *comment = json_object_get_string(json_object_object_get(jo, "comment"));
|
||||
struct json_object *doc = json_object_object_get(jo, "doc");
|
||||
struct json_object *patch = json_object_object_get(jo, "patch");
|
||||
struct json_object *expected = NULL;
|
||||
json_bool have_expected = json_object_object_get_ex(jo, "expected", &expected);
|
||||
struct json_object *error = json_object_object_get(jo, "error");
|
||||
const char *error_s = json_object_get_string(error);
|
||||
struct json_object *res = NULL;
|
||||
int ret;
|
||||
|
||||
printf("Testing '%s', doc '%s' patch '%s' : ",
|
||||
comment ? comment : error_s,
|
||||
json_object_get_string(doc),
|
||||
json_object_get_string(patch));
|
||||
if (!error && !have_expected) {
|
||||
printf("BAD TEST - no expected or error conditions in test: %s\n", json_object_to_json_string(jo));
|
||||
assert(0);
|
||||
}
|
||||
fflush(stdout);
|
||||
struct json_patch_error jperr;
|
||||
if (error) {
|
||||
assert(-1 == json_patch_apply(doc, patch, &res, &jperr));
|
||||
assert(jperr.errno_code != 0);
|
||||
printf("OK\n");
|
||||
printf(" => json_patch_apply failed as expected: %s at patch idx %zu: %s\n",
|
||||
strerror(jperr.errno_code), jperr.patch_failure_idx, jperr.errmsg);
|
||||
json_object_put(res);
|
||||
} else {
|
||||
ret = json_patch_apply(doc, patch, &res, &jperr);
|
||||
if (ret) {
|
||||
fprintf(stderr, "json_patch_apply() returned '%d'\n", ret);
|
||||
fprintf(stderr, "Expected: %s\n", json_object_get_string(expected));
|
||||
fprintf(stderr, "Got: %s\n", res ? json_object_get_string(res) : "(null)");
|
||||
fprintf(stderr, "json_patch_apply failed: %s at patch idx %zu: %s\n",
|
||||
strerror(jperr.errno_code), jperr.patch_failure_idx, jperr.errmsg);
|
||||
fflush(stderr);
|
||||
assert(0);
|
||||
}
|
||||
// Note: res might be NULL if the whole document was removed
|
||||
assert(jperr.errno_code == 0);
|
||||
ret = json_object_equal(expected, res);
|
||||
if (ret == 0) {
|
||||
fprintf(stderr, "json_object_equal() returned '%d'\n", ret);
|
||||
fprintf(stderr, "Expected: %s\n", json_object_get_string(expected));
|
||||
fprintf(stderr, "Got: %s\n", json_object_get_string(res));
|
||||
fflush(stderr);
|
||||
assert(0);
|
||||
}
|
||||
json_object_put(res);
|
||||
res = NULL;
|
||||
printf("OK\n");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void test_json_patch_using_file(const char *testdir, const char *filename)
|
||||
{
|
||||
char full_filename[PATH_MAX];
|
||||
(void)snprintf(full_filename, sizeof(full_filename), "%s/%s", testdir, filename);
|
||||
size_t ii;
|
||||
|
||||
printf("Testing using file %s\n", filename);
|
||||
json_object *jo = json_object_from_file(full_filename);
|
||||
if (!jo) {
|
||||
fprintf(stderr, "FAIL: unable to open %s: %s\n", full_filename, strerror(errno));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
for (ii = 0; ii < json_object_array_length(jo); ii++) {
|
||||
struct json_object *jo1 = json_object_array_get_idx(jo, ii);
|
||||
test_json_patch_op(jo1);
|
||||
}
|
||||
|
||||
json_object_put(jo);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
const char *testdir;
|
||||
if (argc < 2)
|
||||
{
|
||||
fprintf(stderr,
|
||||
"Usage: %s <testdir>\n"
|
||||
" <testdir> is the location of input files\n",
|
||||
argv[0]);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
testdir = argv[1];
|
||||
|
||||
// Test json_c_version.c
|
||||
if (strncmp(json_c_version(), JSON_C_VERSION, sizeof(JSON_C_VERSION)))
|
||||
{
|
||||
printf("FAIL: Output from json_c_version(): %s does not match %s",
|
||||
json_c_version(), JSON_C_VERSION);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
if (json_c_version_num() != JSON_C_VERSION_NUM)
|
||||
{
|
||||
printf("FAIL: Output from json_c_version_num(): %d does not match %d",
|
||||
json_c_version_num(), JSON_C_VERSION_NUM);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
test_json_patch_using_file(testdir, "json_patch_spec_tests.json");
|
||||
test_json_patch_using_file(testdir, "json_patch_tests.json");
|
||||
return 0;
|
||||
}
|
||||
158
tests/test_json_patch.expected
Normal file
158
tests/test_json_patch.expected
Normal file
@@ -0,0 +1,158 @@
|
||||
Testing using file json_patch_spec_tests.json
|
||||
Testing '4.1. add with missing object', doc '{ "q": { "bar": 2 } }' patch '[ { "op": "add", "path": "\/a\/b", "value": 1 } ]' : OK
|
||||
=> json_patch_apply failed as expected: ERRNO=ENOENT at patch idx 0: Failed to set value at path referenced by 'path' field
|
||||
Testing 'A.1. Adding an Object Member', doc '{ "foo": "bar" }' patch '[ { "op": "add", "path": "\/baz", "value": "qux" } ]' : OK
|
||||
Testing 'A.2. Adding an Array Element', doc '{ "foo": [ "bar", "baz" ] }' patch '[ { "op": "add", "path": "\/foo\/1", "value": "qux" } ]' : OK
|
||||
Testing 'A.3. Removing an Object Member', doc '{ "baz": "qux", "foo": "bar" }' patch '[ { "op": "remove", "path": "\/baz" } ]' : OK
|
||||
Testing 'A.4. Removing an Array Element', doc '{ "foo": [ "bar", "qux", "baz" ] }' patch '[ { "op": "remove", "path": "\/foo\/1" } ]' : OK
|
||||
Testing 'A.5. Replacing a Value', doc '{ "baz": "qux", "foo": "bar" }' patch '[ { "op": "replace", "path": "\/baz", "value": "boo" } ]' : OK
|
||||
Testing 'A.6. Moving a Value', doc '{ "foo": { "bar": "baz", "waldo": "fred" }, "qux": { "corge": "grault" } }' patch '[ { "op": "move", "from": "\/foo\/waldo", "path": "\/qux\/thud" } ]' : OK
|
||||
Testing 'A.7. Moving an Array Element', doc '{ "foo": [ "all", "grass", "cows", "eat" ] }' patch '[ { "op": "move", "from": "\/foo\/1", "path": "\/foo\/3" } ]' : OK
|
||||
Testing 'A.8. Testing a Value: Success', doc '{ "baz": "qux", "foo": [ "a", 2, "c" ] }' patch '[ { "op": "test", "path": "\/baz", "value": "qux" }, { "op": "test", "path": "\/foo\/1", "value": 2 } ]' : OK
|
||||
Testing 'A.9. Testing a Value: Error', doc '{ "baz": "qux" }' patch '[ { "op": "test", "path": "\/baz", "value": "bar" } ]' : OK
|
||||
=> json_patch_apply failed as expected: ERRNO=ENOENT at patch idx 0: Value of element referenced by 'path' field did not match 'value' field
|
||||
Testing 'A.10. Adding a nested Member Object', doc '{ "foo": "bar" }' patch '[ { "op": "add", "path": "\/child", "value": { "grandchild": { } } } ]' : OK
|
||||
Testing 'A.11. Ignoring Unrecognized Elements', doc '{ "foo": "bar" }' patch '[ { "op": "add", "path": "\/baz", "value": "qux", "xyz": 123 } ]' : OK
|
||||
Testing 'A.12. Adding to a Non-existent Target', doc '{ "foo": "bar" }' patch '[ { "op": "add", "path": "\/baz\/bat", "value": "qux" } ]' : OK
|
||||
=> json_patch_apply failed as expected: ERRNO=ENOENT at patch idx 0: Failed to set value at path referenced by 'path' field
|
||||
Testing 'A.13 Invalid JSON Patch Document', doc '{ "foo": "bar" }' patch '[ { "op": "remove", "path": "\/baz", "value": "qux" } ]' : OK
|
||||
=> json_patch_apply failed as expected: ERRNO=ENOENT at patch idx 0: Did not find element referenced by path field
|
||||
Testing 'A.14. ~ Escape Ordering', doc '{ "\/": 9, "~1": 10 }' patch '[ { "op": "test", "path": "\/~01", "value": 10 } ]' : OK
|
||||
Testing 'A.15. Comparing Strings and Numbers', doc '{ "\/": 9, "~1": 10 }' patch '[ { "op": "test", "path": "\/~01", "value": "10" } ]' : OK
|
||||
=> json_patch_apply failed as expected: ERRNO=ENOENT at patch idx 0: Value of element referenced by 'path' field did not match 'value' field
|
||||
Testing 'A.16. Adding an Array Value', doc '{ "foo": [ "bar" ] }' patch '[ { "op": "add", "path": "\/foo\/-", "value": [ "abc", "def" ] } ]' : OK
|
||||
Testing using file json_patch_tests.json
|
||||
Testing 'empty list, empty docs', doc '{ }' patch '[ ]' : OK
|
||||
Testing 'empty patch list', doc '{ "foo": 1 }' patch '[ ]' : OK
|
||||
Testing 'rearrangements OK?', doc '{ "foo": 1, "bar": 2 }' patch '[ ]' : OK
|
||||
Testing 'rearrangements OK? How about one level down ... array', doc '[ { "foo": 1, "bar": 2 } ]' patch '[ ]' : OK
|
||||
Testing 'rearrangements OK? How about one level down...', doc '{ "foo": { "foo": 1, "bar": 2 } }' patch '[ ]' : OK
|
||||
Testing 'add replaces any existing field', doc '{ "foo": null }' patch '[ { "op": "add", "path": "\/foo", "value": 1 } ]' : OK
|
||||
Testing 'toplevel array', doc '[ ]' patch '[ { "op": "add", "path": "\/0", "value": "foo" } ]' : OK
|
||||
Testing 'toplevel array, no change', doc '[ "foo" ]' patch '[ ]' : OK
|
||||
Testing 'toplevel object, numeric string', doc '{ }' patch '[ { "op": "add", "path": "\/foo", "value": "1" } ]' : OK
|
||||
Testing 'toplevel object, integer', doc '{ }' patch '[ { "op": "add", "path": "\/foo", "value": 1 } ]' : OK
|
||||
Testing 'Toplevel scalar values OK?', doc 'foo' patch '[ { "op": "replace", "path": "", "value": "bar" } ]' : OK
|
||||
Testing 'replace object document with array document?', doc '{ }' patch '[ { "op": "add", "path": "", "value": [ ] } ]' : OK
|
||||
Testing 'replace array document with object document?', doc '[ ]' patch '[ { "op": "add", "path": "", "value": { } } ]' : OK
|
||||
Testing 'append to root array document?', doc '[ ]' patch '[ { "op": "add", "path": "\/-", "value": "hi" } ]' : OK
|
||||
Testing 'Add, / target', doc '{ }' patch '[ { "op": "add", "path": "\/", "value": 1 } ]' : OK
|
||||
Testing 'Add, /foo/ deep target (trailing slash)', doc '{ "foo": { } }' patch '[ { "op": "add", "path": "\/foo\/", "value": 1 } ]' : OK
|
||||
Testing 'Add composite value at top level', doc '{ "foo": 1 }' patch '[ { "op": "add", "path": "\/bar", "value": [ 1, 2 ] } ]' : OK
|
||||
Testing 'Add into composite value', doc '{ "foo": 1, "baz": [ { "qux": "hello" } ] }' patch '[ { "op": "add", "path": "\/baz\/0\/foo", "value": "world" } ]' : OK
|
||||
Testing 'Out of bounds (upper)', doc '{ "bar": [ 1, 2 ] }' patch '[ { "op": "add", "path": "\/bar\/8", "value": "5" } ]' : OK
|
||||
=> json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Failed to set value at path referenced by 'path' field
|
||||
Testing 'Out of bounds (lower)', doc '{ "bar": [ 1, 2 ] }' patch '[ { "op": "add", "path": "\/bar\/-1", "value": "5" } ]' : OK
|
||||
=> json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Failed to set value at path referenced by 'path' field
|
||||
Testing '(null)', doc '{ "foo": 1 }' patch '[ { "op": "add", "path": "\/bar", "value": true } ]' : OK
|
||||
Testing '(null)', doc '{ "foo": 1 }' patch '[ { "op": "add", "path": "\/bar", "value": false } ]' : OK
|
||||
Testing '(null)', doc '{ "foo": 1 }' patch '[ { "op": "add", "path": "\/bar", "value": null } ]' : OK
|
||||
Testing '0 can be an array index or object element name', doc '{ "foo": 1 }' patch '[ { "op": "add", "path": "\/0", "value": "bar" } ]' : OK
|
||||
Testing '(null)', doc '[ "foo" ]' patch '[ { "op": "add", "path": "\/1", "value": "bar" } ]' : OK
|
||||
Testing '(null)', doc '[ "foo", "sil" ]' patch '[ { "op": "add", "path": "\/1", "value": "bar" } ]' : OK
|
||||
Testing '(null)', doc '[ "foo", "sil" ]' patch '[ { "op": "add", "path": "\/0", "value": "bar" } ]' : OK
|
||||
Testing 'push item to array via last index + 1', doc '[ "foo", "sil" ]' patch '[ { "op": "add", "path": "\/2", "value": "bar" } ]' : OK
|
||||
Testing 'add item to array at index > length should fail', doc '[ "foo", "sil" ]' patch '[ { "op": "add", "path": "\/3", "value": "bar" } ]' : OK
|
||||
=> json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Failed to set value at path referenced by 'path' field
|
||||
Testing 'test against implementation-specific numeric parsing', doc '{ "1e0": "foo" }' patch '[ { "op": "test", "path": "\/1e0", "value": "foo" } ]' : OK
|
||||
Testing 'test with bad number should fail', doc '[ "foo", "bar" ]' patch '[ { "op": "test", "path": "\/1e0", "value": "bar" } ]' : OK
|
||||
=> json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Invalid path field
|
||||
Testing 'Object operation on array target', doc '[ "foo", "sil" ]' patch '[ { "op": "add", "path": "\/bar", "value": 42 } ]' : OK
|
||||
=> json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Failed to set value at path referenced by 'path' field
|
||||
Testing 'value in array add not flattened', doc '[ "foo", "sil" ]' patch '[ { "op": "add", "path": "\/1", "value": [ "bar", "baz" ] } ]' : OK
|
||||
Testing '(null)', doc '{ "foo": 1, "bar": [ 1, 2, 3, 4 ] }' patch '[ { "op": "remove", "path": "\/bar" } ]' : OK
|
||||
Testing '(null)', doc '{ "foo": 1, "baz": [ { "qux": "hello" } ] }' patch '[ { "op": "remove", "path": "\/baz\/0\/qux" } ]' : OK
|
||||
Testing '(null)', doc '{ "foo": 1, "baz": [ { "qux": "hello" } ] }' patch '[ { "op": "replace", "path": "\/foo", "value": [ 1, 2, 3, 4 ] } ]' : OK
|
||||
Testing '(null)', doc '{ "foo": [ 1, 2, 3, 4 ], "baz": [ { "qux": "hello" } ] }' patch '[ { "op": "replace", "path": "\/baz\/0\/qux", "value": "world" } ]' : OK
|
||||
Testing '(null)', doc '[ "foo" ]' patch '[ { "op": "replace", "path": "\/0", "value": "bar" } ]' : OK
|
||||
Testing '(null)', doc '[ "" ]' patch '[ { "op": "replace", "path": "\/0", "value": 0 } ]' : OK
|
||||
Testing '(null)', doc '[ "" ]' patch '[ { "op": "replace", "path": "\/0", "value": true } ]' : OK
|
||||
Testing '(null)', doc '[ "" ]' patch '[ { "op": "replace", "path": "\/0", "value": false } ]' : OK
|
||||
Testing '(null)', doc '[ "" ]' patch '[ { "op": "replace", "path": "\/0", "value": null } ]' : OK
|
||||
Testing 'value in array replace not flattened', doc '[ "foo", "sil" ]' patch '[ { "op": "replace", "path": "\/1", "value": [ "bar", "baz" ] } ]' : OK
|
||||
Testing 'replace whole document', doc '{ "foo": "bar" }' patch '[ { "op": "replace", "path": "", "value": { "baz": "qux" } } ]' : OK
|
||||
Testing 'add whole document, null', doc '{ }' patch '[ { "op": "remove", "path": "" }, { "op": "add", "path": "", "value": { "baz": "qux" } } ]' : OK
|
||||
Testing 'replace whole document, null', doc '{ }' patch '[ { "op": "remove", "path": "" }, { "op": "replace", "path": "", "value": { "baz": "qux" } } ]' : OK
|
||||
=> json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 1: Invalid path field
|
||||
Testing 'remove whole document', doc '{ "foo": "bar" }' patch '[ { "op": "remove", "path": "" } ]' : OK
|
||||
Testing 'remove whole document', doc '{ "foo": "bar" }' patch '[ { "op": "remove", "path": "" } ]' : OK
|
||||
Testing 'remove whole document, array', doc '[ "foo", "bar" ]' patch '[ { "op": "remove", "path": "" } ]' : OK
|
||||
Testing 'remove whole document, string', doc 'foo' patch '[ { "op": "remove", "path": "" } ]' : OK
|
||||
Testing 'remove whole document, null', doc '{ }' patch '[ { "op": "remove", "path": "" }, { "op": "remove", "path": "" } ]' : OK
|
||||
=> json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 1: Invalid path field
|
||||
Testing 'test replace with missing parent key should fail', doc '{ "bar": "baz" }' patch '[ { "op": "replace", "path": "\/foo\/bar", "value": false } ]' : OK
|
||||
=> json_patch_apply failed as expected: ERRNO=ENOENT at patch idx 0: Did not find element referenced by path field
|
||||
Testing 'spurious patch properties', doc '{ "foo": 1 }' patch '[ { "op": "test", "path": "\/foo", "value": 1, "spurious": 1 } ]' : OK
|
||||
Testing 'null value should be valid obj property', doc '{ "foo": null }' patch '[ { "op": "test", "path": "\/foo", "value": null } ]' : OK
|
||||
Testing 'null value should be valid obj property to be replaced with something truthy', doc '{ "foo": null }' patch '[ { "op": "replace", "path": "\/foo", "value": "truthy" } ]' : OK
|
||||
Testing 'null value should be valid obj property to be moved', doc '{ "foo": null }' patch '[ { "op": "move", "from": "\/foo", "path": "\/bar" } ]' : OK
|
||||
Testing 'null value should be valid obj property to be copied', doc '{ "foo": null }' patch '[ { "op": "copy", "from": "\/foo", "path": "\/bar" } ]' : OK
|
||||
Testing 'null value should be valid obj property to be removed', doc '{ "foo": null }' patch '[ { "op": "remove", "path": "\/foo" } ]' : OK
|
||||
Testing 'null value should still be valid obj property replace other value', doc '{ "foo": "bar" }' patch '[ { "op": "replace", "path": "\/foo", "value": null } ]' : OK
|
||||
Testing 'test should pass despite rearrangement', doc '{ "foo": { "foo": 1, "bar": 2 } }' patch '[ { "op": "test", "path": "\/foo", "value": { "bar": 2, "foo": 1 } } ]' : OK
|
||||
Testing 'test should pass despite (nested) rearrangement', doc '{ "foo": [ { "foo": 1, "bar": 2 } ] }' patch '[ { "op": "test", "path": "\/foo", "value": [ { "bar": 2, "foo": 1 } ] } ]' : OK
|
||||
Testing 'test should pass - no error', doc '{ "foo": { "bar": [ 1, 2, 5, 4 ] } }' patch '[ { "op": "test", "path": "\/foo", "value": { "bar": [ 1, 2, 5, 4 ] } } ]' : OK
|
||||
Testing 'test op should fail', doc '{ "foo": { "bar": [ 1, 2, 5, 4 ] } }' patch '[ { "op": "test", "path": "\/foo", "value": [ 1, 2 ] } ]' : OK
|
||||
=> json_patch_apply failed as expected: ERRNO=ENOENT at patch idx 0: Value of element referenced by 'path' field did not match 'value' field
|
||||
Testing 'Test the whole document', doc '{ "foo": 1 }' patch '[ { "op": "test", "path": "", "value": { "foo": 1 } } ]' : OK
|
||||
Testing 'Test the whole document, no match', doc '{ "foo": 1 }' patch '[ { "op": "test", "path": "", "value": { "foo": 2 } } ]' : OK
|
||||
=> json_patch_apply failed as expected: ERRNO=ENOENT at patch idx 0: Value of element referenced by 'path' field did not match 'value' field
|
||||
Testing 'Empty-string element', doc '{ "": 1 }' patch '[ { "op": "test", "path": "\/", "value": 1 } ]' : OK
|
||||
Testing '(null)', doc '{ "foo": [ "bar", "baz" ], "": 0, "a\/b": 1, "c%d": 2, "e^f": 3, "g|h": 4, "i\\j": 5, "k\"l": 6, " ": 7, "m~n": 8 }' patch '[ { "op": "test", "path": "\/foo", "value": [ "bar", "baz" ] }, { "op": "test", "path": "\/foo\/0", "value": "bar" }, { "op": "test", "path": "\/", "value": 0 }, { "op": "test", "path": "\/a~1b", "value": 1 }, { "op": "test", "path": "\/c%d", "value": 2 }, { "op": "test", "path": "\/e^f", "value": 3 }, { "op": "test", "path": "\/g|h", "value": 4 }, { "op": "test", "path": "\/i\\j", "value": 5 }, { "op": "test", "path": "\/k\"l", "value": 6 }, { "op": "test", "path": "\/ ", "value": 7 }, { "op": "test", "path": "\/m~0n", "value": 8 } ]' : OK
|
||||
Testing 'Move to same location has no effect', doc '{ "foo": 1 }' patch '[ { "op": "move", "from": "\/foo", "path": "\/foo" } ]' : OK
|
||||
Testing '(null)', doc '{ "foo": 1, "baz": [ { "qux": "hello" } ] }' patch '[ { "op": "move", "from": "\/foo", "path": "\/bar" } ]' : OK
|
||||
Testing '(null)', doc '{ "baz": [ { "qux": "hello" } ], "bar": 1 }' patch '[ { "op": "move", "from": "\/baz\/0\/qux", "path": "\/baz\/1" } ]' : OK
|
||||
Testing '(null)', doc '{ "baz": [ { "qux": "hello" } ], "bar": 1 }' patch '[ { "op": "copy", "from": "\/baz\/0", "path": "\/boo" } ]' : OK
|
||||
Testing 'replacing the root of the document is possible with add', doc '{ "foo": "bar" }' patch '[ { "op": "add", "path": "", "value": { "baz": "qux" } } ]' : OK
|
||||
Testing 'Adding to "/-" adds to the end of the array', doc '[ 1, 2 ]' patch '[ { "op": "add", "path": "\/-", "value": { "foo": [ "bar", "baz" ] } } ]' : OK
|
||||
Testing 'Adding to "/-" adds to the end of the array, even n levels down', doc '[ 1, 2, [ 3, [ 4, 5 ] ] ]' patch '[ { "op": "add", "path": "\/2\/1\/-", "value": { "foo": [ "bar", "baz" ] } } ]' : OK
|
||||
Testing 'test remove with bad number should fail', doc '{ "foo": 1, "baz": [ { "qux": "hello" } ] }' patch '[ { "op": "remove", "path": "\/baz\/1e0\/qux" } ]' : OK
|
||||
=> json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Invalid path field
|
||||
Testing 'test remove on array', doc '[ 1, 2, 3, 4 ]' patch '[ { "op": "remove", "path": "\/0" } ]' : OK
|
||||
Testing 'test repeated removes', doc '[ 1, 2, 3, 4 ]' patch '[ { "op": "remove", "path": "\/1" }, { "op": "remove", "path": "\/2" } ]' : OK
|
||||
Testing 'test remove with bad index should fail', doc '[ 1, 2, 3, 4 ]' patch '[ { "op": "remove", "path": "\/1e0" } ]' : OK
|
||||
=> json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Invalid path field
|
||||
Testing 'test replace with bad number should fail', doc '[ "" ]' patch '[ { "op": "replace", "path": "\/1e0", "value": false } ]' : OK
|
||||
=> json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Invalid path field
|
||||
Testing 'test copy with bad number should fail', doc '{ "baz": [ 1, 2, 3 ], "bar": 1 }' patch '[ { "op": "copy", "from": "\/baz\/1e0", "path": "\/boo" } ]' : OK
|
||||
=> json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Invalid from field
|
||||
Testing 'test move with bad number should fail', doc '{ "foo": 1, "baz": [ 1, 2, 3, 4 ] }' patch '[ { "op": "move", "from": "\/baz\/1e0", "path": "\/foo" } ]' : OK
|
||||
=> json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Invalid from field
|
||||
Testing 'test add with bad number should fail', doc '[ "foo", "sil" ]' patch '[ { "op": "add", "path": "\/1e0", "value": "bar" } ]' : OK
|
||||
=> json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Failed to set value at path referenced by 'path' field
|
||||
Testing 'missing 'path' parameter', doc '{ }' patch '[ { "op": "add", "value": "bar" } ]' : OK
|
||||
=> json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Patch object does not contain 'path' field
|
||||
Testing ''path' parameter with null value', doc '{ }' patch '[ { "op": "add", "path": null, "value": "bar" } ]' : OK
|
||||
=> json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Failed to set value at path referenced by 'path' field
|
||||
Testing 'invalid JSON Pointer token', doc '{ }' patch '[ { "op": "add", "path": "foo", "value": "bar" } ]' : OK
|
||||
=> json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Failed to set value at path referenced by 'path' field
|
||||
Testing 'missing 'value' parameter to add', doc '[ 1 ]' patch '[ { "op": "add", "path": "\/-" } ]' : OK
|
||||
=> json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Patch object does not contain a 'value' field
|
||||
Testing 'missing 'value' parameter to replace', doc '[ 1 ]' patch '[ { "op": "replace", "path": "\/0" } ]' : OK
|
||||
=> json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Patch object does not contain a 'value' field
|
||||
Testing 'missing 'value' parameter to test', doc '[ null ]' patch '[ { "op": "test", "path": "\/0" } ]' : OK
|
||||
=> json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Patch object does not contain a 'value' field
|
||||
Testing 'missing value parameter to test - where undef is falsy', doc '[ false ]' patch '[ { "op": "test", "path": "\/0" } ]' : OK
|
||||
=> json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Patch object does not contain a 'value' field
|
||||
Testing 'missing from parameter to copy', doc '[ 1 ]' patch '[ { "op": "copy", "path": "\/-" } ]' : OK
|
||||
=> json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Patch does not contain a 'from' field
|
||||
Testing 'missing from location to copy', doc '{ "foo": 1 }' patch '[ { "op": "copy", "from": "\/bar", "path": "\/foo" } ]' : OK
|
||||
=> json_patch_apply failed as expected: ERRNO=ENOENT at patch idx 0: Did not find element referenced by from field
|
||||
Testing 'missing from parameter to move', doc '{ "foo": 1 }' patch '[ { "op": "move", "path": "" } ]' : OK
|
||||
=> json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Patch does not contain a 'from' field
|
||||
Testing 'missing from location to move', doc '{ "foo": 1 }' patch '[ { "op": "move", "from": "\/bar", "path": "\/foo" } ]' : OK
|
||||
=> json_patch_apply failed as expected: ERRNO=ENOENT at patch idx 0: Did not find element referenced by from field
|
||||
Testing 'duplicate ops, json-c parses this as op:move', doc '{ "foo": "bar" }' patch '[ { "op": "move", "path": "\/baz", "value": "qux", "from": "\/foo" } ]' : OK
|
||||
Testing 'unrecognized op should fail', doc '{ "foo": 1 }' patch '[ { "op": "spam", "path": "\/foo", "value": 1 } ]' : OK
|
||||
=> json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Patch object has invalid 'op' field
|
||||
Testing 'test with bad array number that has leading zeros', doc '[ "foo", "bar" ]' patch '[ { "op": "test", "path": "\/00", "value": "foo" } ]' : OK
|
||||
=> json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Invalid path field
|
||||
Testing 'test with bad array number that has leading zeros', doc '[ "foo", "bar" ]' patch '[ { "op": "test", "path": "\/01", "value": "bar" } ]' : OK
|
||||
=> json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Invalid path field
|
||||
Testing 'Removing nonexistent field', doc '{ "foo": "bar" }' patch '[ { "op": "remove", "path": "\/baz" } ]' : OK
|
||||
=> json_patch_apply failed as expected: ERRNO=ENOENT at patch idx 0: Did not find element referenced by path field
|
||||
Testing 'Removing deep nonexistent path', doc '{ "foo": "bar" }' patch '[ { "op": "remove", "path": "\/missing1\/missing2" } ]' : OK
|
||||
=> json_patch_apply failed as expected: ERRNO=ENOENT at patch idx 0: Did not find element referenced by path field
|
||||
Testing 'Removing nonexistent index', doc '[ "foo", "bar" ]' patch '[ { "op": "remove", "path": "\/2" } ]' : OK
|
||||
=> json_patch_apply failed as expected: ERRNO=ENOENT at patch idx 0: Did not find element referenced by path field
|
||||
Testing 'Patch with different capitalisation than doc', doc '{ "foo": "bar" }' patch '[ { "op": "add", "path": "\/FOO", "value": "BAR" } ]' : OK
|
||||
17
tests/test_json_patch.test
Executable file
17
tests/test_json_patch.test
Executable file
@@ -0,0 +1,17 @@
|
||||
#!/bin/sh
|
||||
|
||||
export _JSON_C_STRERROR_ENABLE=1
|
||||
|
||||
# Common definitions
|
||||
if test -z "$srcdir"; then
|
||||
srcdir="${0%/*}"
|
||||
test "$srcdir" = "$0" && srcdir=.
|
||||
test -z "$srcdir" && srcdir=.
|
||||
fi
|
||||
. "$srcdir/test-defs.sh"
|
||||
|
||||
filename=$(basename "$0")
|
||||
filename="${filename%.*}"
|
||||
|
||||
run_output_test $filename "$srcdir"
|
||||
exit $?
|
||||
@@ -269,6 +269,22 @@ static void test_example_set(void)
|
||||
printf("%s\n", json_object_get_string(jo1));
|
||||
|
||||
json_object_put(jo1);
|
||||
|
||||
jo1 = json_tokener_parse("[0, 1, 2, 3]");
|
||||
jo2 = json_tokener_parse("[0, 1, 2, 3, null, null, null, 7]");
|
||||
|
||||
assert(0 == json_pointer_set(&jo1, "/7", json_object_new_int(7)));
|
||||
assert(1 == json_object_equal(jo1, jo2));
|
||||
|
||||
json_object_put(jo1);
|
||||
|
||||
jo1 = json_tokener_parse("[0, 1, 2, 3]");
|
||||
|
||||
assert(0 == json_pointer_setf(&jo1, json_object_new_int(7), "/%u", 7));
|
||||
assert(1 == json_object_equal(jo1, jo2));
|
||||
|
||||
json_object_put(jo1);
|
||||
json_object_put(jo2);
|
||||
}
|
||||
|
||||
static void test_wrong_inputs_set(void)
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
#ifdef NDEBUG
|
||||
#undef NDEBUG
|
||||
#endif
|
||||
#include <assert.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
@@ -21,8 +24,6 @@ int main(int argc, char **argv)
|
||||
json_object *new_obj;
|
||||
#ifdef HAVE_SETLOCALE
|
||||
setlocale(LC_NUMERIC, "de_DE");
|
||||
#else
|
||||
printf("No locale\n");
|
||||
#endif
|
||||
|
||||
char buf1[10], buf2[10];
|
||||
|
||||
@@ -2,6 +2,9 @@
|
||||
* Tests if binary strings are supported.
|
||||
*/
|
||||
|
||||
#ifdef NDEBUG
|
||||
#undef NDEBUG
|
||||
#endif
|
||||
#include "config.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
#ifdef NDEBUG
|
||||
#undef NDEBUG
|
||||
#endif
|
||||
#include "config.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
#ifdef NDEBUG
|
||||
#undef NDEBUG
|
||||
#endif
|
||||
#include <assert.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
@@ -35,13 +38,14 @@ static void do_clear_serializer(json_object *jso);
|
||||
|
||||
static void single_incremental_parse(const char *test_string, int clear_serializer)
|
||||
{
|
||||
int ii;
|
||||
size_t ii;
|
||||
int chunksize = atoi(getenv("TEST_PARSE_CHUNKSIZE"));
|
||||
struct json_tokener *tok;
|
||||
enum json_tokener_error jerr;
|
||||
json_object *all_at_once_obj, *new_obj;
|
||||
const char *all_at_once_str, *new_str;
|
||||
|
||||
new_obj = NULL;
|
||||
assert(chunksize > 0);
|
||||
all_at_once_obj = json_tokener_parse(test_string);
|
||||
if (clear_serializer)
|
||||
@@ -49,7 +53,7 @@ static void single_incremental_parse(const char *test_string, int clear_serializ
|
||||
all_at_once_str = json_object_to_json_string(all_at_once_obj);
|
||||
|
||||
tok = json_tokener_new();
|
||||
int test_string_len = strlen(test_string) + 1; // Including '\0' !
|
||||
size_t test_string_len = strlen(test_string) + 1; // Including '\0' !
|
||||
for (ii = 0; ii < test_string_len; ii += chunksize)
|
||||
{
|
||||
int len_to_parse = chunksize;
|
||||
@@ -92,7 +96,7 @@ static void single_basic_parse(const char *test_string, int clear_serializer)
|
||||
if (getenv("TEST_PARSE_CHUNKSIZE") != NULL)
|
||||
single_incremental_parse(test_string, clear_serializer);
|
||||
}
|
||||
static void test_basic_parse()
|
||||
static void test_basic_parse(void)
|
||||
{
|
||||
single_basic_parse("\"\003\"", 0);
|
||||
single_basic_parse("/* hello */\"foo\"", 0);
|
||||
@@ -195,13 +199,13 @@ static void test_basic_parse()
|
||||
single_basic_parse("[18446744073709551616]", 1);
|
||||
}
|
||||
|
||||
static void test_utf8_parse()
|
||||
static void test_utf8_parse(void)
|
||||
{
|
||||
// json_tokener_parse doesn't support checking for byte order marks.
|
||||
// It's the responsibility of the caller to detect and skip a BOM.
|
||||
// Both of these checks return null.
|
||||
char *utf8_bom = "\xEF\xBB\xBF";
|
||||
char *utf8_bom_and_chars = "\xEF\xBB\xBF{}";
|
||||
const char *utf8_bom = "\xEF\xBB\xBF";
|
||||
const char *utf8_bom_and_chars = "\xEF\xBB\xBF{}";
|
||||
single_basic_parse(utf8_bom, 0);
|
||||
single_basic_parse(utf8_bom_and_chars, 0);
|
||||
}
|
||||
@@ -222,7 +226,7 @@ static int clear_serializer(json_object *jso, int flags, json_object *parent_jso
|
||||
return JSON_C_VISIT_RETURN_CONTINUE;
|
||||
}
|
||||
|
||||
static void test_verbose_parse()
|
||||
static void test_verbose_parse(void)
|
||||
{
|
||||
json_object *new_obj;
|
||||
enum json_tokener_error error = json_tokener_success;
|
||||
@@ -256,24 +260,24 @@ struct incremental_step
|
||||
} incremental_steps[] = {
|
||||
|
||||
/* Check that full json messages can be parsed, both w/ and w/o a reset */
|
||||
{"{ \"foo\": 123 }", -1, -1, json_tokener_success, 0},
|
||||
{"{ \"foo\": 456 }", -1, -1, json_tokener_success, 1},
|
||||
{"{ \"foo\": 789 }", -1, -1, json_tokener_success, 1},
|
||||
{"{ \"foo\": 123 }", -1, -1, json_tokener_success, 0, 0},
|
||||
{"{ \"foo\": 456 }", -1, -1, json_tokener_success, 1, 0},
|
||||
{"{ \"foo\": 789 }", -1, -1, json_tokener_success, 1, 0},
|
||||
|
||||
/* Check the comment parse*/
|
||||
{"/* hello */{ \"foo\"", -1, -1, json_tokener_continue, 0},
|
||||
{"/* hello */:/* hello */", -1, -1, json_tokener_continue, 0},
|
||||
{"\"bar\"/* hello */", -1, -1, json_tokener_continue, 0},
|
||||
{"}/* hello */", -1, -1, json_tokener_success, 1},
|
||||
{"/ hello ", -1, 1, json_tokener_error_parse_comment, 1},
|
||||
{"/* hello\"foo\"", -1, -1, json_tokener_continue, 1},
|
||||
{"/* hello*\"foo\"", -1, -1, json_tokener_continue, 1},
|
||||
{"// hello\"foo\"", -1, -1, json_tokener_continue, 1},
|
||||
{"/* hello */{ \"foo\"", -1, -1, json_tokener_continue, 0, 0},
|
||||
{"/* hello */:/* hello */", -1, -1, json_tokener_continue, 0, 0},
|
||||
{"\"bar\"/* hello */", -1, -1, json_tokener_continue, 0, 0},
|
||||
{"}/* hello */", -1, -1, json_tokener_success, 1, 0},
|
||||
{"/ hello ", -1, 1, json_tokener_error_parse_comment, 1, 0},
|
||||
{"/* hello\"foo\"", -1, -1, json_tokener_continue, 1, 0},
|
||||
{"/* hello*\"foo\"", -1, -1, json_tokener_continue, 1, 0},
|
||||
{"// hello\"foo\"", -1, -1, json_tokener_continue, 1, 0},
|
||||
|
||||
/* Check a basic incremental parse */
|
||||
{"{ \"foo", -1, -1, json_tokener_continue, 0},
|
||||
{"\": {\"bar", -1, -1, json_tokener_continue, 0},
|
||||
{"\":13}}", -1, -1, json_tokener_success, 1},
|
||||
{"{ \"foo", -1, -1, json_tokener_continue, 0, 0},
|
||||
{"\": {\"bar", -1, -1, json_tokener_continue, 0, 0},
|
||||
{"\":13}}", -1, -1, json_tokener_success, 1, 0},
|
||||
|
||||
/* Check the UTF-16 surrogate pair handling in various ways.
|
||||
* Note: \ud843\udd1e is u+1D11E, Musical Symbol G Clef
|
||||
@@ -281,56 +285,56 @@ struct incremental_step
|
||||
* PuTTY doesn't currently show this character.
|
||||
*/
|
||||
/* parse one char at every time */
|
||||
{"\"\\", -1, -1, json_tokener_continue, 0},
|
||||
{"u", -1, -1, json_tokener_continue, 0},
|
||||
{"d", -1, -1, json_tokener_continue, 0},
|
||||
{"8", -1, -1, json_tokener_continue, 0},
|
||||
{"3", -1, -1, json_tokener_continue, 0},
|
||||
{"4", -1, -1, json_tokener_continue, 0},
|
||||
{"\\", -1, -1, json_tokener_continue, 0},
|
||||
{"u", -1, -1, json_tokener_continue, 0},
|
||||
{"d", -1, -1, json_tokener_continue, 0},
|
||||
{"d", -1, -1, json_tokener_continue, 0},
|
||||
{"1", -1, -1, json_tokener_continue, 0},
|
||||
{"e\"", -1, -1, json_tokener_success, 1},
|
||||
{"\"\\", -1, -1, json_tokener_continue, 0, 0},
|
||||
{"u", -1, -1, json_tokener_continue, 0, 0},
|
||||
{"d", -1, -1, json_tokener_continue, 0, 0},
|
||||
{"8", -1, -1, json_tokener_continue, 0, 0},
|
||||
{"3", -1, -1, json_tokener_continue, 0, 0},
|
||||
{"4", -1, -1, json_tokener_continue, 0, 0},
|
||||
{"\\", -1, -1, json_tokener_continue, 0, 0},
|
||||
{"u", -1, -1, json_tokener_continue, 0, 0},
|
||||
{"d", -1, -1, json_tokener_continue, 0, 0},
|
||||
{"d", -1, -1, json_tokener_continue, 0, 0},
|
||||
{"1", -1, -1, json_tokener_continue, 0, 0},
|
||||
{"e\"", -1, -1, json_tokener_success, 1, 0},
|
||||
/* parse two char at every time */
|
||||
{"\"\\u", -1, -1, json_tokener_continue, 0},
|
||||
{"d8", -1, -1, json_tokener_continue, 0},
|
||||
{"34", -1, -1, json_tokener_continue, 0},
|
||||
{"\\u", -1, -1, json_tokener_continue, 0},
|
||||
{"dd", -1, -1, json_tokener_continue, 0},
|
||||
{"1e\"", -1, -1, json_tokener_success, 1},
|
||||
{"\"\\u", -1, -1, json_tokener_continue, 0, 0},
|
||||
{"d8", -1, -1, json_tokener_continue, 0, 0},
|
||||
{"34", -1, -1, json_tokener_continue, 0, 0},
|
||||
{"\\u", -1, -1, json_tokener_continue, 0, 0},
|
||||
{"dd", -1, -1, json_tokener_continue, 0, 0},
|
||||
{"1e\"", -1, -1, json_tokener_success, 1, 0},
|
||||
/* check the low surrogate pair */
|
||||
{"\"\\ud834", -1, -1, json_tokener_continue, 0},
|
||||
{"\\udd1e\"", -1, -1, json_tokener_success, 1},
|
||||
{"\"\\ud834\\", -1, -1, json_tokener_continue, 0},
|
||||
{"udd1e\"", -1, -1, json_tokener_success, 1},
|
||||
{"\"\\ud834\\u", -1, -1, json_tokener_continue, 0},
|
||||
{"dd1e\"", -1, -1, json_tokener_success, 1},
|
||||
{"\"fff \\ud834\\ud", -1, -1, json_tokener_continue, 0},
|
||||
{"d1e bar\"", -1, -1, json_tokener_success, 1},
|
||||
{"\"fff \\ud834\\udd", -1, -1, json_tokener_continue, 0},
|
||||
{"1e bar\"", -1, -1, json_tokener_success, 1},
|
||||
{"\"\\ud834", -1, -1, json_tokener_continue, 0, 0},
|
||||
{"\\udd1e\"", -1, -1, json_tokener_success, 1, 0},
|
||||
{"\"\\ud834\\", -1, -1, json_tokener_continue, 0, 0},
|
||||
{"udd1e\"", -1, -1, json_tokener_success, 1, 0},
|
||||
{"\"\\ud834\\u", -1, -1, json_tokener_continue, 0, 0},
|
||||
{"dd1e\"", -1, -1, json_tokener_success, 1, 0},
|
||||
{"\"fff \\ud834\\ud", -1, -1, json_tokener_continue, 0, 0},
|
||||
{"d1e bar\"", -1, -1, json_tokener_success, 1, 0},
|
||||
{"\"fff \\ud834\\udd", -1, -1, json_tokener_continue, 0, 0},
|
||||
{"1e bar\"", -1, -1, json_tokener_success, 1, 0},
|
||||
|
||||
/* \ud83d\ude00 is U+1F600, Grinning Face
|
||||
* Displays fine in PuTTY, though you may need "less -r"
|
||||
*/
|
||||
{"\"fff \\ud83d\\ude", -1, -1, json_tokener_continue, 0},
|
||||
{"00 bar\"", -1, -1, json_tokener_success, 1},
|
||||
{"\"fff \\ud83d\\ude", -1, -1, json_tokener_continue, 0, 0},
|
||||
{"00 bar\"", -1, -1, json_tokener_success, 1, 0},
|
||||
|
||||
/* Check that json_tokener_reset actually resets */
|
||||
{"{ \"foo", -1, -1, json_tokener_continue, 1},
|
||||
{": \"bar\"}", -1, 0, json_tokener_error_parse_unexpected, 1},
|
||||
{"{ \"foo", -1, -1, json_tokener_continue, 1, 0},
|
||||
{": \"bar\"}", -1, 0, json_tokener_error_parse_unexpected, 1, 0},
|
||||
|
||||
/* Check incremental parsing with trailing characters */
|
||||
{"{ \"foo", -1, -1, json_tokener_continue, 0},
|
||||
{"\": {\"bar", -1, -1, json_tokener_continue, 0},
|
||||
{"\":13}}XXXX", 10, 6, json_tokener_success, 0},
|
||||
{"XXXX", 4, 0, json_tokener_error_parse_unexpected, 1},
|
||||
{"{ \"foo", -1, -1, json_tokener_continue, 0, 0},
|
||||
{"\": {\"bar", -1, -1, json_tokener_continue, 0, 0},
|
||||
{"\":13}}XXXX", 10, 6, json_tokener_success, 0, 0},
|
||||
{"XXXX", 4, 0, json_tokener_error_parse_unexpected, 1, 0},
|
||||
|
||||
/* Check that trailing characters can change w/o a reset */
|
||||
{"{\"x\": 123 }\"X\"", -1, 11, json_tokener_success, 0},
|
||||
{"\"Y\"", -1, -1, json_tokener_success, 1},
|
||||
{"{\"x\": 123 }\"X\"", -1, 11, json_tokener_success, 0, 0},
|
||||
{"\"Y\"", -1, -1, json_tokener_success, 1, 0},
|
||||
|
||||
/* Trailing characters should cause a failure in strict mode */
|
||||
{"{\"foo\":9}{\"bar\":8}", -1, 9, json_tokener_error_parse_unexpected, 1, JSON_TOKENER_STRICT},
|
||||
@@ -342,116 +346,148 @@ struct incremental_step
|
||||
JSON_TOKENER_STRICT | JSON_TOKENER_ALLOW_TRAILING_CHARS},
|
||||
|
||||
/* To stop parsing a number we need to reach a non-digit, e.g. a \0 */
|
||||
{"1", 1, 1, json_tokener_continue, 0},
|
||||
{"1", 1, 1, json_tokener_continue, 0, 0},
|
||||
/* This should parse as the number 12, since it continues the "1" */
|
||||
{"2", 2, 1, json_tokener_success, 0},
|
||||
{"12{", 3, 2, json_tokener_success, 1},
|
||||
{"2", 2, 1, json_tokener_success, 0, 0},
|
||||
{"12{", 3, 2, json_tokener_success, 1, 0},
|
||||
/* Parse number in strict mode */
|
||||
{"[02]", -1, 3, json_tokener_error_parse_number, 1, JSON_TOKENER_STRICT},
|
||||
|
||||
{"0e+0", 5, 4, json_tokener_success, 1},
|
||||
{"[0e+0]", -1, -1, json_tokener_success, 1},
|
||||
{"0e+0", 5, 4, json_tokener_success, 1, 0},
|
||||
{"[0e+0]", -1, -1, json_tokener_success, 1, 0},
|
||||
|
||||
/* The behavior when missing the exponent varies slightly */
|
||||
{"0e", 2, 2, json_tokener_continue, 1},
|
||||
{"0e", 3, 2, json_tokener_success, 1},
|
||||
{"0e", 2, 2, json_tokener_continue, 1, 0},
|
||||
{"0e", 3, 2, json_tokener_success, 1, 0},
|
||||
{"0e", 3, 2, json_tokener_error_parse_eof, 1, JSON_TOKENER_STRICT},
|
||||
{"[0e]", -1, -1, json_tokener_success, 1},
|
||||
{"[0e]", -1, -1, json_tokener_success, 1, 0},
|
||||
{"[0e]", -1, 3, json_tokener_error_parse_number, 1, JSON_TOKENER_STRICT},
|
||||
|
||||
{"0e+", 3, 3, json_tokener_continue, 1},
|
||||
{"0e+", 4, 3, json_tokener_success, 1},
|
||||
{"0e+", 3, 3, json_tokener_continue, 1, 0},
|
||||
{"0e+", 4, 3, json_tokener_success, 1, 0},
|
||||
{"0e+", 4, 3, json_tokener_error_parse_eof, 1, JSON_TOKENER_STRICT},
|
||||
{"[0e+]", -1, -1, json_tokener_success, 1},
|
||||
{"[0e+]", -1, -1, json_tokener_success, 1, 0},
|
||||
{"[0e+]", -1, 4, json_tokener_error_parse_number, 1, JSON_TOKENER_STRICT},
|
||||
|
||||
{"0e-", 3, 3, json_tokener_continue, 1},
|
||||
{"0e-", 4, 3, json_tokener_success, 1},
|
||||
{"0e-", 3, 3, json_tokener_continue, 1, 0},
|
||||
{"0e-", 4, 3, json_tokener_success, 1, 0},
|
||||
{"0e-", 4, 3, json_tokener_error_parse_eof, 1, JSON_TOKENER_STRICT},
|
||||
{"[0e-]", -1, -1, json_tokener_success, 1},
|
||||
{"[0e-]", -1, -1, json_tokener_success, 1, 0},
|
||||
{"[0e-]", -1, 4, json_tokener_error_parse_number, 1, JSON_TOKENER_STRICT},
|
||||
|
||||
/* You might expect this to fail, but it won't because
|
||||
it's a valid partial parse; note the char_offset: */
|
||||
{"0e+-", 5, 3, json_tokener_success, 1},
|
||||
{"0e+-", 5, 3, json_tokener_success, 1, 0},
|
||||
{"0e+-", 5, 3, json_tokener_error_parse_number, 1, JSON_TOKENER_STRICT},
|
||||
{"[0e+-]", -1, 4, json_tokener_error_parse_number, 1},
|
||||
{"[0e+-]", -1, 4, json_tokener_error_parse_number, 1, 0},
|
||||
|
||||
/* Similar tests for other kinds of objects: */
|
||||
/* These could all return success immediately, since regardless of
|
||||
what follows the false/true/null token we *will* return a json object,
|
||||
but it currently doesn't work that way. hmm... */
|
||||
{"false", 5, 5, json_tokener_continue, 1},
|
||||
{"false", 6, 5, json_tokener_success, 1},
|
||||
{"true", 4, 4, json_tokener_continue, 1},
|
||||
{"true", 5, 4, json_tokener_success, 1},
|
||||
{"null", 4, 4, json_tokener_continue, 1},
|
||||
{"null", 5, 4, json_tokener_success, 1},
|
||||
{"false", 5, 5, json_tokener_continue, 1, 0},
|
||||
{"false", 6, 5, json_tokener_success, 1, 0},
|
||||
{"true", 4, 4, json_tokener_continue, 1, 0},
|
||||
{"true", 5, 4, json_tokener_success, 1, 0},
|
||||
{"null", 4, 4, json_tokener_continue, 1, 0},
|
||||
{"null", 5, 4, json_tokener_success, 1, 0},
|
||||
|
||||
{"Infinity", 9, 8, json_tokener_success, 1},
|
||||
{"infinity", 9, 8, json_tokener_success, 1},
|
||||
{"-infinity", 10, 9, json_tokener_success, 1},
|
||||
{"Infinity", 9, 8, json_tokener_success, 1, 0},
|
||||
{"infinity", 9, 8, json_tokener_success, 1, 0},
|
||||
{"-infinity", 10, 9, json_tokener_success, 1, 0},
|
||||
{"infinity", 9, 0, json_tokener_error_parse_unexpected, 1, JSON_TOKENER_STRICT},
|
||||
{"-infinity", 10, 1, json_tokener_error_parse_unexpected, 1, JSON_TOKENER_STRICT},
|
||||
|
||||
{"inf", 3, 3, json_tokener_continue, 0},
|
||||
{"inity", 6, 5, json_tokener_success, 1},
|
||||
{"-inf", 4, 4, json_tokener_continue, 0},
|
||||
{"inity", 6, 5, json_tokener_success, 1},
|
||||
{"inf", 3, 3, json_tokener_continue, 0, 0},
|
||||
{"inity", 6, 5, json_tokener_success, 1, 0},
|
||||
{"-inf", 4, 4, json_tokener_continue, 0, 0},
|
||||
{"inity", 6, 5, json_tokener_success, 1, 0},
|
||||
|
||||
{"i", 1, 1, json_tokener_continue, 0},
|
||||
{"n", 1, 1, json_tokener_continue, 0},
|
||||
{"f", 1, 1, json_tokener_continue, 0},
|
||||
{"i", 1, 1, json_tokener_continue, 0},
|
||||
{"n", 1, 1, json_tokener_continue, 0},
|
||||
{"i", 1, 1, json_tokener_continue, 0},
|
||||
{"t", 1, 1, json_tokener_continue, 0},
|
||||
{"y", 1, 1, json_tokener_continue, 0},
|
||||
{"", 1, 0, json_tokener_success, 1},
|
||||
{"i", 1, 1, json_tokener_continue, 0, 0},
|
||||
{"n", 1, 1, json_tokener_continue, 0, 0},
|
||||
{"f", 1, 1, json_tokener_continue, 0, 0},
|
||||
{"i", 1, 1, json_tokener_continue, 0, 0},
|
||||
{"n", 1, 1, json_tokener_continue, 0, 0},
|
||||
{"i", 1, 1, json_tokener_continue, 0, 0},
|
||||
{"t", 1, 1, json_tokener_continue, 0, 0},
|
||||
{"y", 1, 1, json_tokener_continue, 0, 0},
|
||||
{"", 1, 0, json_tokener_success, 1, 0},
|
||||
|
||||
{"-", 1, 1, json_tokener_continue, 0},
|
||||
{"inf", 3, 3, json_tokener_continue, 0},
|
||||
{"ini", 3, 3, json_tokener_continue, 0},
|
||||
{"ty", 3, 2, json_tokener_success, 1},
|
||||
{"-", 1, 1, json_tokener_continue, 0, 0},
|
||||
{"inf", 3, 3, json_tokener_continue, 0, 0},
|
||||
{"ini", 3, 3, json_tokener_continue, 0, 0},
|
||||
{"ty", 3, 2, json_tokener_success, 1, 0},
|
||||
|
||||
{"-", 1, 1, json_tokener_continue, 0},
|
||||
{"i", 1, 1, json_tokener_continue, 0},
|
||||
{"nfini", 5, 5, json_tokener_continue, 0},
|
||||
{"ty", 3, 2, json_tokener_success, 1},
|
||||
{"-", 1, 1, json_tokener_continue, 0, 0},
|
||||
{"i", 1, 1, json_tokener_continue, 0, 0},
|
||||
{"nfini", 5, 5, json_tokener_continue, 0, 0},
|
||||
{"ty", 3, 2, json_tokener_success, 1, 0},
|
||||
|
||||
{"-i", 2, 2, json_tokener_continue, 0},
|
||||
{"nfinity", 8, 7, json_tokener_success, 1},
|
||||
{"-i", 2, 2, json_tokener_continue, 0, 0},
|
||||
{"nfinity", 8, 7, json_tokener_success, 1, 0},
|
||||
|
||||
{"InfinityX", 10, 8, json_tokener_success, 0},
|
||||
{"X", 1, 0, json_tokener_error_parse_unexpected, 1},
|
||||
{"InfinityX", 10, 8, json_tokener_success, 0, 0},
|
||||
{"X", 1, 0, json_tokener_error_parse_unexpected, 1, 0},
|
||||
|
||||
{"Infinity1234", 13, 8, json_tokener_success, 0},
|
||||
{"1234", 5, 4, json_tokener_success, 1},
|
||||
{"Infinity1234", 13, 8, json_tokener_success, 0, 0},
|
||||
{"1234", 5, 4, json_tokener_success, 1, 0},
|
||||
|
||||
{"Infinity9999", 8, 8, json_tokener_continue, 0},
|
||||
{"Infinity9999", 8, 8, json_tokener_continue, 0, 0},
|
||||
|
||||
/* returns the Infinity loaded up by the previous call: */
|
||||
{"1234", 5, 0, json_tokener_success, 0},
|
||||
{"1234", 5, 4, json_tokener_success, 1},
|
||||
{"1234", 5, 0, json_tokener_success, 0, 0},
|
||||
{"1234", 5, 4, json_tokener_success, 1, 0},
|
||||
|
||||
/* INT64_MAX */
|
||||
{"[9223372036854775807]", 22, 21, json_tokener_success, 1, 0},
|
||||
/* INT64_MAX+1 => parsed as uint64 */
|
||||
{"[9223372036854775808]", 22, 21, json_tokener_success, 1, 0},
|
||||
|
||||
/* INT64_MIN */
|
||||
{"[-9223372036854775808]", 23, 22, json_tokener_success, 1, 0},
|
||||
|
||||
/* INT64_MIN-1 => success, but value ends up capped */
|
||||
{"[-9223372036854775809]", 23, 22, json_tokener_success, 1, 0},
|
||||
|
||||
/* INT64_MIN-1 => failure due to underflow detected */
|
||||
{"[-9223372036854775809]", 23, 21, json_tokener_error_parse_number, 1, JSON_TOKENER_STRICT},
|
||||
|
||||
/* UINT64_MAX */
|
||||
{"[18446744073709551615]", 23, 22, json_tokener_success, 1, 0},
|
||||
|
||||
/* UINT64_MAX+1 => success, but value ends up capped */
|
||||
{"[18446744073709551616]", 23, 22, json_tokener_success, 1, 0},
|
||||
|
||||
/* UINT64_MAX+1 => failure due to overflow detected */
|
||||
{"[18446744073709551616]", 23, 21, json_tokener_error_parse_number, 1, JSON_TOKENER_STRICT},
|
||||
|
||||
/* XXX this seems like a bug, should fail with _error_parse_number instead */
|
||||
{"18446744073709551616", 21, 20, json_tokener_error_parse_eof, 1, JSON_TOKENER_STRICT},
|
||||
|
||||
/* Exceeding integer limits as double parse OK */
|
||||
{"[9223372036854775808.0]", 24, 23, json_tokener_success, 1, 0},
|
||||
{"[-9223372036854775809.0]", 25, 24, json_tokener_success, 1, JSON_TOKENER_STRICT},
|
||||
{"[18446744073709551615.0]", 25, 24, json_tokener_success, 1, 0},
|
||||
{"[18446744073709551616.0]", 25, 24, json_tokener_success, 1, JSON_TOKENER_STRICT},
|
||||
|
||||
/* offset=1 because "n" is the start of "null". hmm... */
|
||||
{"noodle", 7, 1, json_tokener_error_parse_null, 1},
|
||||
{"noodle", 7, 1, json_tokener_error_parse_null, 1, 0},
|
||||
/* offset=2 because "na" is the start of "nan". hmm... */
|
||||
{"naodle", 7, 2, json_tokener_error_parse_null, 1},
|
||||
{"naodle", 7, 2, json_tokener_error_parse_null, 1, 0},
|
||||
/* offset=2 because "tr" is the start of "true". hmm... */
|
||||
{"track", 6, 2, json_tokener_error_parse_boolean, 1},
|
||||
{"fail", 5, 2, json_tokener_error_parse_boolean, 1},
|
||||
{"track", 6, 2, json_tokener_error_parse_boolean, 1, 0},
|
||||
{"fail", 5, 2, json_tokener_error_parse_boolean, 1, 0},
|
||||
|
||||
/* Although they may initially look like they should fail,
|
||||
* the next few tests check that parsing multiple sequential
|
||||
* json objects in the input works as expected
|
||||
*/
|
||||
{"null123", 8, 4, json_tokener_success, 0},
|
||||
{&"null123"[4], 4, 3, json_tokener_success, 1},
|
||||
{"nullx", 6, 4, json_tokener_success, 0},
|
||||
{&"nullx"[4], 2, 0, json_tokener_error_parse_unexpected, 1},
|
||||
{"{\"a\":1}{\"b\":2}", 15, 7, json_tokener_success, 0},
|
||||
{&"{\"a\":1}{\"b\":2}"[7], 8, 7, json_tokener_success, 1},
|
||||
{"null123", 8, 4, json_tokener_success, 0, 0},
|
||||
{&"null123"[4], 4, 3, json_tokener_success, 1, 0},
|
||||
{"nullx", 6, 4, json_tokener_success, 0, 0},
|
||||
{&"nullx"[4], 2, 0, json_tokener_error_parse_unexpected, 1, 0},
|
||||
{"{\"a\":1}{\"b\":2}", 15, 7, json_tokener_success, 0, 0},
|
||||
{&"{\"a\":1}{\"b\":2}"[7], 8, 7, json_tokener_success, 1, 0},
|
||||
|
||||
/*
|
||||
* Though this may seem invalid at first glance, it
|
||||
@@ -463,52 +499,52 @@ struct incremental_step
|
||||
* or json_type_double objects without some other delimiter.
|
||||
* e.g. whitespace
|
||||
*/
|
||||
{&"2015-01-15"[0], 11, 4, json_tokener_success, 1},
|
||||
{&"2015-01-15"[4], 7, 3, json_tokener_success, 1},
|
||||
{&"2015-01-15"[7], 4, 3, json_tokener_success, 1},
|
||||
{&"2015 01 15"[0], 11, 5, json_tokener_success, 1},
|
||||
{&"2015 01 15"[4], 7, 4, json_tokener_success, 1},
|
||||
{&"2015 01 15"[7], 4, 3, json_tokener_success, 1},
|
||||
{&"2015-01-15"[0], 11, 4, json_tokener_success, 1, 0},
|
||||
{&"2015-01-15"[4], 7, 3, json_tokener_success, 1, 0},
|
||||
{&"2015-01-15"[7], 4, 3, json_tokener_success, 1, 0},
|
||||
{&"2015 01 15"[0], 11, 5, json_tokener_success, 1, 0},
|
||||
{&"2015 01 15"[4], 7, 4, json_tokener_success, 1, 0},
|
||||
{&"2015 01 15"[7], 4, 3, json_tokener_success, 1, 0},
|
||||
|
||||
/* Strings have a well defined end point, so we can stop at the quote */
|
||||
{"\"blue\"", -1, -1, json_tokener_success, 0},
|
||||
{"\"blue\"", -1, -1, json_tokener_success, 0, 0},
|
||||
|
||||
/* Check each of the escape sequences defined by the spec */
|
||||
{"\"\\\"\"", -1, -1, json_tokener_success, 0},
|
||||
{"\"\\\\\"", -1, -1, json_tokener_success, 0},
|
||||
{"\"\\b\"", -1, -1, json_tokener_success, 0},
|
||||
{"\"\\f\"", -1, -1, json_tokener_success, 0},
|
||||
{"\"\\n\"", -1, -1, json_tokener_success, 0},
|
||||
{"\"\\r\"", -1, -1, json_tokener_success, 0},
|
||||
{"\"\\t\"", -1, -1, json_tokener_success, 0},
|
||||
{"\"\\/\"", -1, -1, json_tokener_success, 0},
|
||||
{"\"\\\"\"", -1, -1, json_tokener_success, 0, 0},
|
||||
{"\"\\\\\"", -1, -1, json_tokener_success, 0, 0},
|
||||
{"\"\\b\"", -1, -1, json_tokener_success, 0, 0},
|
||||
{"\"\\f\"", -1, -1, json_tokener_success, 0, 0},
|
||||
{"\"\\n\"", -1, -1, json_tokener_success, 0, 0},
|
||||
{"\"\\r\"", -1, -1, json_tokener_success, 0, 0},
|
||||
{"\"\\t\"", -1, -1, json_tokener_success, 0, 0},
|
||||
{"\"\\/\"", -1, -1, json_tokener_success, 0, 0},
|
||||
// Escaping a forward slash is optional
|
||||
{"\"/\"", -1, -1, json_tokener_success, 0},
|
||||
{"\"/\"", -1, -1, json_tokener_success, 0, 0},
|
||||
/* Check wrong escape sequences */
|
||||
{"\"\\a\"", -1, 2, json_tokener_error_parse_string, 1},
|
||||
{"\"\\a\"", -1, 2, json_tokener_error_parse_string, 1, 0},
|
||||
|
||||
/* Check '\'' in strict model */
|
||||
{"\'foo\'", -1, 0, json_tokener_error_parse_unexpected, 1, JSON_TOKENER_STRICT},
|
||||
|
||||
/* Parse array/object */
|
||||
{"[1,2,3]", -1, -1, json_tokener_success, 0},
|
||||
{"[1,2,3}", -1, 6, json_tokener_error_parse_array, 1},
|
||||
{"{\"a\"}", -1, 4, json_tokener_error_parse_object_key_sep, 1},
|
||||
{"{\"a\":1]", -1, 6, json_tokener_error_parse_object_value_sep, 1},
|
||||
{"{\"a\"::1}", -1, 5, json_tokener_error_parse_unexpected, 1},
|
||||
{"{\"a\":}", -1, 5, json_tokener_error_parse_unexpected, 1},
|
||||
{"{\"a\":1,\"a\":2}", -1, -1, json_tokener_success, 1},
|
||||
{"\"a\":1}", -1, 3, json_tokener_success, 1},
|
||||
{"{\"a\":1", -1, -1, json_tokener_continue, 1},
|
||||
{"[,]", -1, 1, json_tokener_error_parse_unexpected, 1},
|
||||
{"[,1]", -1, 1, json_tokener_error_parse_unexpected, 1},
|
||||
{"[1,2,3]", -1, -1, json_tokener_success, 0, 0},
|
||||
{"[1,2,3}", -1, 6, json_tokener_error_parse_array, 1, 0},
|
||||
{"{\"a\"}", -1, 4, json_tokener_error_parse_object_key_sep, 1, 0},
|
||||
{"{\"a\":1]", -1, 6, json_tokener_error_parse_object_value_sep, 1, 0},
|
||||
{"{\"a\"::1}", -1, 5, json_tokener_error_parse_unexpected, 1, 0},
|
||||
{"{\"a\":}", -1, 5, json_tokener_error_parse_unexpected, 1, 0},
|
||||
{"{\"a\":1,\"a\":2}", -1, -1, json_tokener_success, 1, 0},
|
||||
{"\"a\":1}", -1, 3, json_tokener_success, 1, 0},
|
||||
{"{\"a\":1", -1, -1, json_tokener_continue, 1, 0}, //}
|
||||
{"[,]", -1, 1, json_tokener_error_parse_unexpected, 1, 0},
|
||||
{"[,1]", -1, 1, json_tokener_error_parse_unexpected, 1, 0},
|
||||
|
||||
/* This behaviour doesn't entirely follow the json spec, but until we have
|
||||
* a way to specify how strict to be we follow Postel's Law and be liberal
|
||||
* in what we accept (up to a point).
|
||||
*/
|
||||
{"[1,2,3,]", -1, -1, json_tokener_success, 0},
|
||||
{"[1,2,,3,]", -1, 5, json_tokener_error_parse_unexpected, 0},
|
||||
{"[1,2,3,]", -1, -1, json_tokener_success, 0, 0},
|
||||
{"[1,2,,3,]", -1, 5, json_tokener_error_parse_unexpected, 0, 0},
|
||||
|
||||
{"[1,2,3,]", -1, 7, json_tokener_error_parse_unexpected, 1, JSON_TOKENER_STRICT},
|
||||
{"{\"a\":1,}", -1, 7, json_tokener_error_parse_unexpected, 1, JSON_TOKENER_STRICT},
|
||||
@@ -517,24 +553,24 @@ struct incremental_step
|
||||
// acsll encoding
|
||||
{"\x22\x31\x32\x33\x61\x73\x63\x24\x25\x26\x22", -1, -1, json_tokener_success, 1,
|
||||
JSON_TOKENER_VALIDATE_UTF8},
|
||||
{"\x22\x31\x32\x33\x61\x73\x63\x24\x25\x26\x22", -1, -1, json_tokener_success, 1},
|
||||
{"\x22\x31\x32\x33\x61\x73\x63\x24\x25\x26\x22", -1, -1, json_tokener_success, 1, 0},
|
||||
// utf-8 encoding
|
||||
{"\x22\xe4\xb8\x96\xe7\x95\x8c\x22", -1, -1, json_tokener_success, 1,
|
||||
JSON_TOKENER_VALIDATE_UTF8},
|
||||
{"\x22\xe4\xb8", -1, 3, json_tokener_error_parse_utf8_string, 0, JSON_TOKENER_VALIDATE_UTF8},
|
||||
{"\x96\xe7\x95\x8c\x22", -1, 0, json_tokener_error_parse_utf8_string, 1,
|
||||
JSON_TOKENER_VALIDATE_UTF8},
|
||||
{"\x22\xe4\xb8\x96\xe7\x95\x8c\x22", -1, -1, json_tokener_success, 1},
|
||||
{"\x22\xe4\xb8\x96\xe7\x95\x8c\x22", -1, -1, json_tokener_success, 1, 0},
|
||||
{"\x22\xcf\x80\xcf\x86\x22", -1, -1, json_tokener_success, 1, JSON_TOKENER_VALIDATE_UTF8},
|
||||
{"\x22\xf0\xa5\x91\x95\x22", -1, -1, json_tokener_success, 1, JSON_TOKENER_VALIDATE_UTF8},
|
||||
// wrong utf-8 encoding
|
||||
{"\x22\xe6\x9d\x4e\x22", -1, 3, json_tokener_error_parse_utf8_string, 1,
|
||||
JSON_TOKENER_VALIDATE_UTF8},
|
||||
{"\x22\xe6\x9d\x4e\x22", -1, 5, json_tokener_success, 1},
|
||||
{"\x22\xe6\x9d\x4e\x22", -1, 5, json_tokener_success, 1, 0},
|
||||
// GBK encoding
|
||||
{"\x22\xc0\xee\xc5\xf4\x22", -1, 2, json_tokener_error_parse_utf8_string, 1,
|
||||
JSON_TOKENER_VALIDATE_UTF8},
|
||||
{"\x22\xc0\xee\xc5\xf4\x22", -1, 6, json_tokener_success, 1},
|
||||
{"\x22\xc0\xee\xc5\xf4\x22", -1, 6, json_tokener_success, 1, 0},
|
||||
// char after space
|
||||
{"\x20\x20\x22\xe4\xb8\x96\x22", -1, -1, json_tokener_success, 1, JSON_TOKENER_VALIDATE_UTF8},
|
||||
{"\x20\x20\x81\x22\xe4\xb8\x96\x22", -1, 2, json_tokener_error_parse_utf8_string, 1,
|
||||
@@ -542,7 +578,7 @@ struct incremental_step
|
||||
{"\x5b\x20\x81\x31\x5d", -1, 2, json_tokener_error_parse_utf8_string, 1,
|
||||
JSON_TOKENER_VALIDATE_UTF8},
|
||||
// char in state inf
|
||||
{"\x49\x6e\x66\x69\x6e\x69\x74\x79", 9, 8, json_tokener_success, 1},
|
||||
{"\x49\x6e\x66\x69\x6e\x69\x74\x79", 9, 8, json_tokener_success, 1, 0},
|
||||
{"\x49\x6e\x66\x81\x6e\x69\x74\x79", -1, 3, json_tokener_error_parse_utf8_string, 1,
|
||||
JSON_TOKENER_VALIDATE_UTF8},
|
||||
// char in escape unicode
|
||||
@@ -559,10 +595,48 @@ struct incremental_step
|
||||
{"\x7b\x22\x31\x81\x22\x3a\x31\x7d", -1, 3, json_tokener_error_parse_utf8_string, 1,
|
||||
JSON_TOKENER_VALIDATE_UTF8},
|
||||
|
||||
{NULL, -1, -1, json_tokener_success, 0},
|
||||
// Note, current asciiz APIs can't parse \x00, skip it
|
||||
{ "\"0\x01\x02\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" \
|
||||
"\x10\x11\x12\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\"",
|
||||
-1, -1, json_tokener_success, 1, 0 },
|
||||
|
||||
// Test control chars again, this time in strict mode, which should fail
|
||||
{ "\"\x01\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
|
||||
{ "\"\x02\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
|
||||
{ "\"\x03\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
|
||||
{ "\"\x04\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
|
||||
{ "\"\x05\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
|
||||
{ "\"\x06\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
|
||||
{ "\"\x07\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
|
||||
{ "\"\x08\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
|
||||
{ "\"\x09\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
|
||||
{ "\"\x0a\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
|
||||
{ "\"\x0b\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
|
||||
{ "\"\x0c\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
|
||||
{ "\"\x0d\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
|
||||
{ "\"\x0e\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
|
||||
{ "\"\x0f\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
|
||||
{ "\"\x10\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
|
||||
{ "\"\x11\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
|
||||
{ "\"\x12\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
|
||||
{ "\"\x13\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
|
||||
{ "\"\x14\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
|
||||
{ "\"\x15\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
|
||||
{ "\"\x16\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
|
||||
{ "\"\x17\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
|
||||
{ "\"\x18\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
|
||||
{ "\"\x19\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
|
||||
{ "\"\x1a\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
|
||||
{ "\"\x1b\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
|
||||
{ "\"\x1c\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
|
||||
{ "\"\x1d\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
|
||||
{ "\"\x1e\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
|
||||
{ "\"\x1f\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
|
||||
|
||||
{NULL, -1, -1, json_tokener_success, 0, 0},
|
||||
};
|
||||
|
||||
static void test_incremental_parse()
|
||||
static void test_incremental_parse(void)
|
||||
{
|
||||
json_object *new_obj;
|
||||
enum json_tokener_error jerr;
|
||||
|
||||
@@ -210,6 +210,19 @@ json_tokener_parse_ex(tok, 1234 , 5) ... OK: got object of type [int]:
|
||||
json_tokener_parse_ex(tok, Infinity9999, 8) ... OK: got correct error: continue
|
||||
json_tokener_parse_ex(tok, 1234 , 5) ... OK: got object of type [double]: Infinity
|
||||
json_tokener_parse_ex(tok, 1234 , 5) ... OK: got object of type [int]: 1234
|
||||
json_tokener_parse_ex(tok, [9223372036854775807], 22) ... OK: got object of type [array]: [ 9223372036854775807 ]
|
||||
json_tokener_parse_ex(tok, [9223372036854775808], 22) ... OK: got object of type [array]: [ 9223372036854775808 ]
|
||||
json_tokener_parse_ex(tok, [-9223372036854775808], 23) ... OK: got object of type [array]: [ -9223372036854775808 ]
|
||||
json_tokener_parse_ex(tok, [-9223372036854775809], 23) ... OK: got object of type [array]: [ -9223372036854775808 ]
|
||||
json_tokener_parse_ex(tok, [-9223372036854775809], 23) ... OK: got correct error: number expected
|
||||
json_tokener_parse_ex(tok, [18446744073709551615], 23) ... OK: got object of type [array]: [ 18446744073709551615 ]
|
||||
json_tokener_parse_ex(tok, [18446744073709551616], 23) ... OK: got object of type [array]: [ 18446744073709551615 ]
|
||||
json_tokener_parse_ex(tok, [18446744073709551616], 23) ... OK: got correct error: number expected
|
||||
json_tokener_parse_ex(tok, 18446744073709551616, 21) ... OK: got correct error: unexpected end of data
|
||||
json_tokener_parse_ex(tok, [9223372036854775808.0], 24) ... OK: got object of type [array]: [ 9223372036854775808.0 ]
|
||||
json_tokener_parse_ex(tok, [-9223372036854775809.0], 25) ... OK: got object of type [array]: [ -9223372036854775809.0 ]
|
||||
json_tokener_parse_ex(tok, [18446744073709551615.0], 25) ... OK: got object of type [array]: [ 18446744073709551615.0 ]
|
||||
json_tokener_parse_ex(tok, [18446744073709551616.0], 25) ... OK: got object of type [array]: [ 18446744073709551616.0 ]
|
||||
json_tokener_parse_ex(tok, noodle , 7) ... OK: got correct error: null expected
|
||||
json_tokener_parse_ex(tok, naodle , 7) ... OK: got correct error: null expected
|
||||
json_tokener_parse_ex(tok, track , 6) ... OK: got correct error: boolean expected
|
||||
@@ -275,5 +288,5 @@ json_tokener_parse_ex(tok, "\ud855
|
||||
json_tokener_parse_ex(tok, "\ud0031<33>" , 10) ... OK: got correct error: invalid utf-8 string
|
||||
json_tokener_parse_ex(tok, 11<31>11 , 5) ... OK: got correct error: invalid utf-8 string
|
||||
json_tokener_parse_ex(tok, {"1<>":1} , 8) ... OK: got correct error: invalid utf-8 string
|
||||
End Incremental Tests OK=185 ERROR=0
|
||||
End Incremental Tests OK=198 ERROR=0
|
||||
==================================
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
|
||||
#ifdef NDEBUG
|
||||
#undef NDEBUG
|
||||
#endif
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
@@ -31,7 +34,7 @@ void checkit_uint(const char *buf)
|
||||
* This always exits with a 0 exit value. The output should be compared
|
||||
* against previously saved expected output.
|
||||
*/
|
||||
int main()
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
char buf[100];
|
||||
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
#ifdef NDEBUG
|
||||
#undef NDEBUG
|
||||
#endif
|
||||
#include <assert.h>
|
||||
#include <limits.h>
|
||||
#include <stddef.h>
|
||||
@@ -16,7 +19,7 @@ static void test_printbuf_memset_length(void);
|
||||
#define __func__ __FUNCTION__
|
||||
#endif
|
||||
|
||||
static void test_basic_printbuf_memset()
|
||||
static void test_basic_printbuf_memset(void)
|
||||
{
|
||||
struct printbuf *pb;
|
||||
|
||||
@@ -29,7 +32,7 @@ static void test_basic_printbuf_memset()
|
||||
printf("%s: end test\n", __func__);
|
||||
}
|
||||
|
||||
static void test_printbuf_memset_length()
|
||||
static void test_printbuf_memset_length(void)
|
||||
{
|
||||
struct printbuf *pb;
|
||||
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
#ifdef NDEBUG
|
||||
#undef NDEBUG
|
||||
#endif
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
@@ -73,7 +76,8 @@ int main(int argc, char **argv)
|
||||
my_sub_object = json_object_new_double(1.0);
|
||||
json_object_object_add(my_object, "double", my_sub_object);
|
||||
printf("Check that the custom serializer does not include nul byte:\n");
|
||||
json_object_set_serializer(my_sub_object, json_object_double_to_json_string, "%125.0f,", NULL);
|
||||
#define UNCONST(a) ((void *)(uintptr_t)(const void *)(a))
|
||||
json_object_set_serializer(my_sub_object, json_object_double_to_json_string, UNCONST("%125.0f"), NULL);
|
||||
printf("my_object.to_string(custom serializer)=%s\n",
|
||||
json_object_to_json_string_ext(my_object, JSON_C_TO_STRING_NOZERO));
|
||||
|
||||
|
||||
@@ -9,4 +9,4 @@ my_object.to_string(custom serializer)=Custom Output
|
||||
Next line of output should be from the custom freeit function:
|
||||
freeit, value=123
|
||||
Check that the custom serializer does not include nul byte:
|
||||
my_object.to_string(custom serializer)={"double": 1.}
|
||||
my_object.to_string(custom serializer)={"double": 1}
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
#ifdef NDEBUG
|
||||
#undef NDEBUG
|
||||
#endif
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
@@ -68,6 +71,14 @@ int main(int argc, char **argv)
|
||||
json_object_set_string(tmp, SHORT);
|
||||
assert(strcmp(json_object_get_string(tmp), SHORT) == 0);
|
||||
assert(strcmp(json_object_to_json_string(tmp), "\"" SHORT "\"") == 0);
|
||||
|
||||
// Set an empty string a couple times to try to trigger
|
||||
// a case that used to leak memory.
|
||||
json_object_set_string(tmp, "");
|
||||
json_object_set_string(tmp, HUGE);
|
||||
json_object_set_string(tmp, "");
|
||||
json_object_set_string(tmp, HUGE);
|
||||
|
||||
json_object_put(tmp);
|
||||
printf("STRING PASSED\n");
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#ifdef NDEBUG
|
||||
#undef NDEBUG
|
||||
#endif
|
||||
#include "strerror_override.h"
|
||||
#include "strerror_override_private.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
#ifdef NDEBUG
|
||||
#undef NDEBUG
|
||||
#endif
|
||||
#include "strerror_override.h"
|
||||
#include "strerror_override_private.h"
|
||||
#ifdef WIN32
|
||||
#ifdef _WIN32
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <io.h>
|
||||
#include <windows.h>
|
||||
#endif /* defined(WIN32) */
|
||||
#endif /* defined(_WIN32) */
|
||||
#include <fcntl.h>
|
||||
#include <limits.h>
|
||||
#include <stddef.h>
|
||||
@@ -35,7 +37,7 @@ static void test_read_fd_equal(const char *testdir);
|
||||
#define PATH_MAX 256
|
||||
#endif
|
||||
|
||||
static void test_write_to_file()
|
||||
static void test_write_to_file(void)
|
||||
{
|
||||
json_object *jso;
|
||||
|
||||
@@ -90,7 +92,7 @@ static void test_write_to_file()
|
||||
static void stat_and_cat(const char *file)
|
||||
{
|
||||
struct stat sb;
|
||||
int d = open(file, O_RDONLY, 0600);
|
||||
int d = open(file, O_RDONLY);
|
||||
if (d < 0)
|
||||
{
|
||||
printf("FAIL: unable to open %s: %s\n", file, strerror(errno));
|
||||
@@ -168,7 +170,7 @@ static void test_read_valid_with_fd(const char *testdir)
|
||||
char filename[PATH_MAX];
|
||||
(void)snprintf(filename, sizeof(filename), "%s/valid.json", testdir);
|
||||
|
||||
int d = open(filename, O_RDONLY, 0);
|
||||
int d = open(filename, O_RDONLY);
|
||||
if (d < 0)
|
||||
{
|
||||
fprintf(stderr, "FAIL: unable to open %s: %s\n", filename, strerror(errno));
|
||||
@@ -193,7 +195,7 @@ static void test_read_valid_nested_with_fd(const char *testdir)
|
||||
char filename[PATH_MAX];
|
||||
(void)snprintf(filename, sizeof(filename), "%s/valid_nested.json", testdir);
|
||||
|
||||
int d = open(filename, O_RDONLY, 0);
|
||||
int d = open(filename, O_RDONLY);
|
||||
if (d < 0)
|
||||
{
|
||||
fprintf(stderr, "FAIL: unable to open %s: %s\n", filename, strerror(errno));
|
||||
@@ -231,7 +233,7 @@ static void test_read_valid_nested_with_fd(const char *testdir)
|
||||
close(d);
|
||||
}
|
||||
|
||||
static void test_read_nonexistant()
|
||||
static void test_read_nonexistant(void)
|
||||
{
|
||||
const char *filename = "./not_present.json";
|
||||
|
||||
@@ -249,10 +251,10 @@ static void test_read_nonexistant()
|
||||
}
|
||||
}
|
||||
|
||||
static void test_read_closed()
|
||||
static void test_read_closed(void)
|
||||
{
|
||||
// Test reading from a closed fd
|
||||
int d = open("/dev/null", O_RDONLY, 0);
|
||||
int d = open("/dev/null", O_RDONLY);
|
||||
if (d < 0)
|
||||
{
|
||||
puts("FAIL: unable to open");
|
||||
@@ -287,7 +289,7 @@ static void test_read_fd_equal(const char *testdir)
|
||||
|
||||
json_object *jso = json_object_from_file(filename);
|
||||
|
||||
int d = open(filename, O_RDONLY, 0);
|
||||
int d = open(filename, O_RDONLY);
|
||||
if (d < 0)
|
||||
{
|
||||
fprintf(stderr, "FAIL: unable to open %s: %s\n", filename, strerror(errno));
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
#ifdef NDEBUG
|
||||
#undef NDEBUG
|
||||
#endif
|
||||
#include <assert.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
|
||||
@@ -8,9 +8,9 @@
|
||||
|
||||
#include "snprintf_compat.h"
|
||||
|
||||
#ifndef WIN32
|
||||
#ifndef _WIN32
|
||||
#include <stdarg.h>
|
||||
#endif /* !defined(WIN32) */
|
||||
#endif /* !defined(_WIN32) */
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
@@ -18,10 +18,10 @@
|
||||
/* CAW: compliant version of vasprintf */
|
||||
static int vasprintf(char **buf, const char *fmt, va_list ap)
|
||||
{
|
||||
#ifndef WIN32
|
||||
#ifndef _WIN32
|
||||
static char _T_emptybuffer = '\0';
|
||||
va_list ap2;
|
||||
#endif /* !defined(WIN32) */
|
||||
#endif /* !defined(_WIN32) */
|
||||
int chars;
|
||||
char *b;
|
||||
|
||||
@@ -30,16 +30,16 @@ static int vasprintf(char **buf, const char *fmt, va_list ap)
|
||||
return -1;
|
||||
}
|
||||
|
||||
#ifdef WIN32
|
||||
#ifdef _WIN32
|
||||
chars = _vscprintf(fmt, ap);
|
||||
#else /* !defined(WIN32) */
|
||||
#else /* !defined(_WIN32) */
|
||||
/* CAW: RAWR! We have to hope to god here that vsnprintf doesn't overwrite
|
||||
* our buffer like on some 64bit sun systems... but hey, it's time to move on
|
||||
*/
|
||||
va_copy(ap2, ap);
|
||||
chars = vsnprintf(&_T_emptybuffer, 0, fmt, ap2);
|
||||
va_end(ap2);
|
||||
#endif /* defined(WIN32) */
|
||||
#endif /* defined(_WIN32) */
|
||||
if (chars < 0 || (size_t)chars + 1 > SIZE_MAX / sizeof(char))
|
||||
{
|
||||
return -1;
|
||||
|
||||
Reference in New Issue
Block a user