From 8a46cb7b552961e5fe0b3ca1501cd1148342c621 Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Fri, 4 Nov 2016 20:42:09 +0700 Subject: [PATCH 01/45] gitignore: ignore build directory --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index d90818b..1101bbc 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ test *.patch tags *.dylib +build/ From 9a4657fbee55ae927c7422c415d651bdd13df888 Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Fri, 4 Nov 2016 20:31:56 +0700 Subject: [PATCH 02/45] CMake: Set version and soversion --- CMakeLists.txt | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2204561..2c59f06 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,6 +3,11 @@ cmake_minimum_required(VERSION 2.8) set(PROJ_CJSON cJSON) project(${PROJ_CJSON} C) +set(CJSON_VERSION_MAJOR 1) +set(CJSON_VERSION_MINOR 0) +set(CJSON_VERSION_PATCH 0) +set(CJSON_VERSION_SO 1) +set(CJSON_VERSION "${CJSON_VERSION_MAJOR}.${CJSON_VERSION_MINOR}.${CJSON_VERSION_PATCH}") file(GLOB HEADERS cJSON.h) set(SOURCES cJSON.c) From 81e95718d574ae5b49a45e0cbb2f1af78487fde9 Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Sat, 5 Nov 2016 00:38:30 +0700 Subject: [PATCH 03/45] CMake: Regroup lines --- CMakeLists.txt | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2c59f06..efcf176 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,14 +1,15 @@ cmake_minimum_required(VERSION 2.8) -set(PROJ_CJSON cJSON) - -project(${PROJ_CJSON} C) set(CJSON_VERSION_MAJOR 1) set(CJSON_VERSION_MINOR 0) set(CJSON_VERSION_PATCH 0) set(CJSON_VERSION_SO 1) set(CJSON_VERSION "${CJSON_VERSION_MAJOR}.${CJSON_VERSION_MINOR}.${CJSON_VERSION_PATCH}") +#cJSON +set(PROJ_CJSON cJSON) +project(${PROJ_CJSON} C) + file(GLOB HEADERS cJSON.h) set(SOURCES cJSON.c) @@ -17,8 +18,11 @@ if (NOT WIN32) target_link_libraries(${PROJ_CJSON} m) endif() -set(PROJ_CJSON_UTILS cJSON_utils) +install (TARGETS ${PROJ_CJSON} DESTINATION lib${LIB_SUFFIX}) +install (FILES cJSON.h DESTINATION include/cJSON) +#cJSON_Utils +set(PROJ_CJSON_UTILS cJSON_utils) project(${PROJ_CJSON_UTILS} C) file(GLOB HEADERS_UTILS cJSON_Utils.h) @@ -27,8 +31,6 @@ set(SOURCES_UTILS cJSON_Utils.c) add_library(${PROJ_CJSON_UTILS} ${HEADERS_UTILS} ${SOURCES_UTILS}) target_link_libraries(${PROJ_CJSON_UTILS} ${PROJ_CJSON}) -install (TARGETS ${PROJ_CJSON} DESTINATION lib${LIB_SUFFIX}) -install (FILES cJSON.h DESTINATION include/cJSON) install (TARGETS ${PROJ_CJSON_UTILS} DESTINATION lib${LIB_SUFFIX}) install (FILES cJSON_Utils.h DESTINATION include/cJSON) From 34361dbb9b394051478410b292c682d261d44940 Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Sat, 5 Nov 2016 00:40:23 +0700 Subject: [PATCH 04/45] CMake: Reformat --- CMakeLists.txt | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index efcf176..c2a9e57 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,11 +15,11 @@ set(SOURCES cJSON.c) add_library(${PROJ_CJSON} ${HEADERS} ${SOURCES}) if (NOT WIN32) - target_link_libraries(${PROJ_CJSON} m) + target_link_libraries(${PROJ_CJSON} m) endif() -install (TARGETS ${PROJ_CJSON} DESTINATION lib${LIB_SUFFIX}) -install (FILES cJSON.h DESTINATION include/cJSON) +install(TARGETS ${PROJ_CJSON} DESTINATION lib${LIB_SUFFIX}) +install(FILES cJSON.h DESTINATION include/cJSON) #cJSON_Utils set(PROJ_CJSON_UTILS cJSON_utils) @@ -31,16 +31,16 @@ set(SOURCES_UTILS cJSON_Utils.c) add_library(${PROJ_CJSON_UTILS} ${HEADERS_UTILS} ${SOURCES_UTILS}) target_link_libraries(${PROJ_CJSON_UTILS} ${PROJ_CJSON}) -install (TARGETS ${PROJ_CJSON_UTILS} DESTINATION lib${LIB_SUFFIX}) -install (FILES cJSON_Utils.h DESTINATION include/cJSON) +install(TARGETS ${PROJ_CJSON_UTILS} DESTINATION lib${LIB_SUFFIX}) +install(FILES cJSON_Utils.h DESTINATION include/cJSON) option(ENABLE_CJSON_TEST "Enable building cJSON test" OFF) if(ENABLE_CJSON_TEST) - set(TEST_CJSON cJSON_test) - add_executable(${TEST_CJSON} test.c) - target_link_libraries(${TEST_CJSON} ${PROJ_CJSON}) + set(TEST_CJSON cJSON_test) + add_executable(${TEST_CJSON} test.c) + target_link_libraries(${TEST_CJSON} ${PROJ_CJSON}) - set(TEST_CJSON_UTILS cJSON_test_utils) - add_executable(${TEST_CJSON_UTILS} test_utils.c) - target_link_libraries(${TEST_CJSON_UTILS} ${PROJ_CJSON_UTILS}) + set(TEST_CJSON_UTILS cJSON_test_utils) + add_executable(${TEST_CJSON_UTILS} test_utils.c) + target_link_libraries(${TEST_CJSON_UTILS} ${PROJ_CJSON_UTILS}) endif() From 0d6ccf032d6dd6473f8bcd8870b37935614edda1 Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Sat, 5 Nov 2016 00:46:41 +0700 Subject: [PATCH 05/45] CMake: Change library names to all lowercase Library names: libcJSON -> libcjson libcJSON_utils -> libcjson_utils --- CMakeLists.txt | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c2a9e57..7ce9bd3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,39 +8,41 @@ set(CJSON_VERSION "${CJSON_VERSION_MAJOR}.${CJSON_VERSION_MINOR}.${CJSON_VERSION #cJSON set(PROJ_CJSON cJSON) +set(CJSON_LIB cjson) project(${PROJ_CJSON} C) file(GLOB HEADERS cJSON.h) set(SOURCES cJSON.c) -add_library(${PROJ_CJSON} ${HEADERS} ${SOURCES}) +add_library(${CJSON_LIB} ${HEADERS} ${SOURCES}) if (NOT WIN32) - target_link_libraries(${PROJ_CJSON} m) + target_link_libraries(${CJSON_LIB} m) endif() -install(TARGETS ${PROJ_CJSON} DESTINATION lib${LIB_SUFFIX}) -install(FILES cJSON.h DESTINATION include/cJSON) +install(TARGETS ${CJSON_LIB} DESTINATION lib${LIB_SUFFIX}) +install(FILES cJSON.h DESTINATION include/cjson) #cJSON_Utils set(PROJ_CJSON_UTILS cJSON_utils) +set(CJSON_UTILS_LIB cjson_utils) project(${PROJ_CJSON_UTILS} C) file(GLOB HEADERS_UTILS cJSON_Utils.h) set(SOURCES_UTILS cJSON_Utils.c) -add_library(${PROJ_CJSON_UTILS} ${HEADERS_UTILS} ${SOURCES_UTILS}) -target_link_libraries(${PROJ_CJSON_UTILS} ${PROJ_CJSON}) +add_library(${CJSON_UTILS_LIB} ${HEADERS_UTILS} ${SOURCES_UTILS}) +target_link_libraries(${CJSON_UTILS_LIB} ${CJSON_LIB}) -install(TARGETS ${PROJ_CJSON_UTILS} DESTINATION lib${LIB_SUFFIX}) +install(TARGETS ${CJSON_UTILS_LIB} DESTINATION lib${LIB_SUFFIX}) install(FILES cJSON_Utils.h DESTINATION include/cJSON) option(ENABLE_CJSON_TEST "Enable building cJSON test" OFF) if(ENABLE_CJSON_TEST) set(TEST_CJSON cJSON_test) add_executable(${TEST_CJSON} test.c) - target_link_libraries(${TEST_CJSON} ${PROJ_CJSON}) + target_link_libraries(${TEST_CJSON} ${CJSON_LIB}) set(TEST_CJSON_UTILS cJSON_test_utils) add_executable(${TEST_CJSON_UTILS} test_utils.c) - target_link_libraries(${TEST_CJSON_UTILS} ${PROJ_CJSON_UTILS}) + target_link_libraries(${TEST_CJSON_UTILS} ${CJSON_UTILS_LIB}) endif() From ba593394d43118a357ba30a9361326d33baeeb2b Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Sat, 5 Nov 2016 00:48:35 +0700 Subject: [PATCH 06/45] CMake: Change include directory cJSON -> cjson --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7ce9bd3..b1d54cc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -34,7 +34,7 @@ add_library(${CJSON_UTILS_LIB} ${HEADERS_UTILS} ${SOURCES_UTILS}) target_link_libraries(${CJSON_UTILS_LIB} ${CJSON_LIB}) install(TARGETS ${CJSON_UTILS_LIB} DESTINATION lib${LIB_SUFFIX}) -install(FILES cJSON_Utils.h DESTINATION include/cJSON) +install(FILES cJSON_Utils.h DESTINATION include/cjson) option(ENABLE_CJSON_TEST "Enable building cJSON test" OFF) if(ENABLE_CJSON_TEST) From ba6389291fa16d911a887da08383e5ed75e9fdb5 Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Sat, 5 Nov 2016 00:50:51 +0700 Subject: [PATCH 07/45] CMake: Set library version --- CMakeLists.txt | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index b1d54cc..3d83a03 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,6 +22,11 @@ endif() install(TARGETS ${CJSON_LIB} DESTINATION lib${LIB_SUFFIX}) install(FILES cJSON.h DESTINATION include/cjson) +set_target_properties(${CJSON_LIB} + PROPERTIES + SOVERSION ${CJSON_VERSION_SO} + VERSION ${CJSON_VERSION}) + #cJSON_Utils set(PROJ_CJSON_UTILS cJSON_utils) set(CJSON_UTILS_LIB cjson_utils) @@ -36,6 +41,11 @@ target_link_libraries(${CJSON_UTILS_LIB} ${CJSON_LIB}) install(TARGETS ${CJSON_UTILS_LIB} DESTINATION lib${LIB_SUFFIX}) install(FILES cJSON_Utils.h DESTINATION include/cjson) +set_target_properties(${CJSON_UTILS_LIB} + PROPERTIES + SOVERSION ${CJSON_VERSION_SO} + VERSION ${CJSON_VERSION}) + option(ENABLE_CJSON_TEST "Enable building cJSON test" OFF) if(ENABLE_CJSON_TEST) set(TEST_CJSON cJSON_test) From fd38fb712d133d8ab4a62bd038e076679bc1d52c Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Sat, 5 Nov 2016 00:51:31 +0700 Subject: [PATCH 08/45] CMake: Add compile options --- CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3d83a03..c7fa092 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,6 +6,8 @@ set(CJSON_VERSION_PATCH 0) set(CJSON_VERSION_SO 1) set(CJSON_VERSION "${CJSON_VERSION_MAJOR}.${CJSON_VERSION_MINOR}.${CJSON_VERSION_PATCH}") +add_compile_options(-ansi -pedantic -Wall -Wextra -Werror -Wstrict-prototypes -Wwrite-strings) + #cJSON set(PROJ_CJSON cJSON) set(CJSON_LIB cjson) From 32a9870786d9599403610c1e607b310bbfa1657a Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Sat, 5 Nov 2016 01:30:31 +0700 Subject: [PATCH 09/45] CMake: Separate so version for cJSON_Utils --- CMakeLists.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c7fa092..049f895 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,6 +4,7 @@ set(CJSON_VERSION_MAJOR 1) set(CJSON_VERSION_MINOR 0) set(CJSON_VERSION_PATCH 0) set(CJSON_VERSION_SO 1) +set(CJSON_UTILS_VERSION_SO 1) set(CJSON_VERSION "${CJSON_VERSION_MAJOR}.${CJSON_VERSION_MINOR}.${CJSON_VERSION_PATCH}") add_compile_options(-ansi -pedantic -Wall -Wextra -Werror -Wstrict-prototypes -Wwrite-strings) @@ -45,7 +46,7 @@ install(FILES cJSON_Utils.h DESTINATION include/cjson) set_target_properties(${CJSON_UTILS_LIB} PROPERTIES - SOVERSION ${CJSON_VERSION_SO} + SOVERSION ${CJSON_UTILS_VERSION_SO} VERSION ${CJSON_VERSION}) option(ENABLE_CJSON_TEST "Enable building cJSON test" OFF) From d5baeff85f9a5b6b1733d30a0e1b835b13ea458d Mon Sep 17 00:00:00 2001 From: Paulo Antonio Alvarez Date: Mon, 17 Oct 2016 22:12:12 -0200 Subject: [PATCH 10/45] Add pkg-config file support for libcjson We configure and install a pkg-config file so that our compilation and linking flags can be more easily found using pkg-config. --- CMakeLists.txt | 8 ++++++++ libcjson.pc.in | 9 +++++++++ 2 files changed, 17 insertions(+) create mode 100644 libcjson.pc.in diff --git a/CMakeLists.txt b/CMakeLists.txt index 049f895..0f0ddd3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,6 +9,10 @@ set(CJSON_VERSION "${CJSON_VERSION_MAJOR}.${CJSON_VERSION_MINOR}.${CJSON_VERSION add_compile_options(-ansi -pedantic -Wall -Wextra -Werror -Wstrict-prototypes -Wwrite-strings) +#variables for pkg-config +set(prefix ${CMAKE_INSTALL_PREFIX}) +set(libdir "${CMAKE_INSTALL_PREFIX}/lib") + #cJSON set(PROJ_CJSON cJSON) set(CJSON_LIB cjson) @@ -22,8 +26,12 @@ if (NOT WIN32) target_link_libraries(${CJSON_LIB} m) endif() +configure_file("${cJSON_SOURCE_DIR}/libcjson.pc.in" + "${cJSON_BINARY_DIR}/libcjson.pc" @ONLY) + install(TARGETS ${CJSON_LIB} DESTINATION lib${LIB_SUFFIX}) install(FILES cJSON.h DESTINATION include/cjson) +install (FILES ${cJSON_BINARY_DIR}/libcjson.pc DESTINATION lib/pkgconfig) set_target_properties(${CJSON_LIB} PROPERTIES diff --git a/libcjson.pc.in b/libcjson.pc.in new file mode 100644 index 0000000..e04d5ee --- /dev/null +++ b/libcjson.pc.in @@ -0,0 +1,9 @@ +prefix=@prefix@ +libdir=@libdir@ +includedir=${prefix}/include/cJSON + +Name: libcJSON +Version: 1.0 +Description: Ultralightweight JSON parser in ANSI C +Libs: -L${libdir} -lcJSON +Cflags: -I${includedir} From 3d546c70367c774e8065ae6956acca08ea959cdb Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Sat, 5 Nov 2016 15:50:01 +0700 Subject: [PATCH 11/45] pkg-config: Update libcjson.pc.in --- CMakeLists.txt | 1 + libcjson.pc.in | 7 ++++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0f0ddd3..9e931b2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,6 +12,7 @@ add_compile_options(-ansi -pedantic -Wall -Wextra -Werror -Wstrict-prototypes -W #variables for pkg-config set(prefix ${CMAKE_INSTALL_PREFIX}) set(libdir "${CMAKE_INSTALL_PREFIX}/lib") +set(version ${CJSON_VERSION}) #cJSON set(PROJ_CJSON cJSON) diff --git a/libcjson.pc.in b/libcjson.pc.in index e04d5ee..aac3262 100644 --- a/libcjson.pc.in +++ b/libcjson.pc.in @@ -1,9 +1,10 @@ prefix=@prefix@ libdir=@libdir@ includedir=${prefix}/include/cJSON +version=@version@ -Name: libcJSON -Version: 1.0 +Name: libcjson +Version: ${version} Description: Ultralightweight JSON parser in ANSI C -Libs: -L${libdir} -lcJSON +Libs: -L${libdir} -lcjson Cflags: -I${includedir} From aee8cb9231b5b2027ff7604faae91a33608d0433 Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Sat, 5 Nov 2016 15:57:14 +0700 Subject: [PATCH 12/45] pkg-config: Add libcjson_utils.pc --- CMakeLists.txt | 4 ++++ libcjson_utils.pc.in | 11 +++++++++++ 2 files changed, 15 insertions(+) create mode 100644 libcjson_utils.pc.in diff --git a/CMakeLists.txt b/CMakeLists.txt index 9e931b2..5450b05 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -50,8 +50,12 @@ set(SOURCES_UTILS cJSON_Utils.c) add_library(${CJSON_UTILS_LIB} ${HEADERS_UTILS} ${SOURCES_UTILS}) target_link_libraries(${CJSON_UTILS_LIB} ${CJSON_LIB}) +configure_file("${cJSON_utils_SOURCE_DIR}/libcjson_utils.pc.in" + "${cJSON_utils_BINARY_DIR}/libcjson_utils.pc" @ONLY) + install(TARGETS ${CJSON_UTILS_LIB} DESTINATION lib${LIB_SUFFIX}) install(FILES cJSON_Utils.h DESTINATION include/cjson) +install (FILES ${cJSON_utils_BINARY_DIR}/libcjson.pc DESTINATION lib/pkgconfig) set_target_properties(${CJSON_UTILS_LIB} PROPERTIES diff --git a/libcjson_utils.pc.in b/libcjson_utils.pc.in new file mode 100644 index 0000000..67effb4 --- /dev/null +++ b/libcjson_utils.pc.in @@ -0,0 +1,11 @@ +prefix=@prefix@ +libdir=@libdir@ +includedir=${prefix}/include/cJSON +version=@version@ + +Name: libcjson_utils +Version: ${version} +Description: An implementation of JSON Pointer, Patch and Merge Patch based on cJSON. +Libs: -L${libdir} -lcjson_utils +Cflags: -I${includedir} +Requires: libcjson From 9acd1c6e7ec4857591eef1a196f4d6f991d009fa Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Sat, 5 Nov 2016 16:30:31 +0700 Subject: [PATCH 13/45] Add list of contributors --- CONTRIBUTORS.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 CONTRIBUTORS.md diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md new file mode 100644 index 0000000..f0850e8 --- /dev/null +++ b/CONTRIBUTORS.md @@ -0,0 +1,22 @@ +Contributors +============ + +* [Ajay Bhargav](https://github.com/ajaybhargav) +* [Anton Sergeev](https://github.com/anton-sergeev) +* [Christian Schulze](https://github.com/ChristianSch) +* [Dave Gamble](https://github.com/DaveGamble) +* [dieyushi](https://github.com/dieyushi) +* [Dongwen Huang (黄东文)](https://github.com/DongwenHuang) +* [ecksun](https://github.com/ecksun) +* [Evan Todd](https://github.com/etodd) +* [Fabrice Fontaine](https://github.com/ffontaine) +* [IvanVoid](https://github.com/npi3pak) +* [Jonathan Fether](https://github.com/jfether) +* [Kevin Branigan](https://github.com/kbranigan) +* [Max Bruckner](https://github.com/FSMaxB) +* [Rafael Leal Dias](https://github.com/rafaeldias) +* [Rod Vagg](https://github.com/rvagg) +* [Roland Meertens](https://github.com/rmeertens) +* [Weston Schmidt](https://github.com/schmidtw) + +And more people on [SourceForge](https://sourceforge.net/p/cjson/bugs/search/?q=status%3Aclosed-rejected+or+status%3Aclosed-out-of-date+or+status%3Awont-fix+or+status%3Aclosed-fixed+or+status%3Aclosed&page=0) From c3fecc2d0729e121f8944525ecf9c9b75af06204 Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Sat, 5 Nov 2016 16:41:24 +0700 Subject: [PATCH 14/45] CMake: Build shared libraries by default This is also what other projects are doing. --- CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5450b05..450b14e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,6 +14,8 @@ set(prefix ${CMAKE_INSTALL_PREFIX}) set(libdir "${CMAKE_INSTALL_PREFIX}/lib") set(version ${CJSON_VERSION}) +option(BUILD_SHARED_LIBS "Build shared libraries" ON) + #cJSON set(PROJ_CJSON cJSON) set(CJSON_LIB cjson) From 47841ed3a7b1767959dde21059524d6fba982b4f Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Sat, 5 Nov 2016 16:40:23 +0700 Subject: [PATCH 15/45] CMake: Build test by default --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 450b14e..6cdd927 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -64,7 +64,7 @@ set_target_properties(${CJSON_UTILS_LIB} SOVERSION ${CJSON_UTILS_VERSION_SO} VERSION ${CJSON_VERSION}) -option(ENABLE_CJSON_TEST "Enable building cJSON test" OFF) +option(ENABLE_CJSON_TEST "Enable building cJSON test" ON) if(ENABLE_CJSON_TEST) set(TEST_CJSON cJSON_test) add_executable(${TEST_CJSON} test.c) From 576d9bb24f55fc5dc16549e591a871bffc653736 Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Sat, 5 Nov 2016 21:18:03 +0700 Subject: [PATCH 16/45] CMake: Only one project --- CMakeLists.txt | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6cdd927..35f4d9f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,7 @@ cmake_minimum_required(VERSION 2.8) +project(cJSON C) + set(CJSON_VERSION_MAJOR 1) set(CJSON_VERSION_MINOR 0) set(CJSON_VERSION_PATCH 0) @@ -17,9 +19,7 @@ set(version ${CJSON_VERSION}) option(BUILD_SHARED_LIBS "Build shared libraries" ON) #cJSON -set(PROJ_CJSON cJSON) set(CJSON_LIB cjson) -project(${PROJ_CJSON} C) file(GLOB HEADERS cJSON.h) set(SOURCES cJSON.c) @@ -42,9 +42,7 @@ set_target_properties(${CJSON_LIB} VERSION ${CJSON_VERSION}) #cJSON_Utils -set(PROJ_CJSON_UTILS cJSON_utils) set(CJSON_UTILS_LIB cjson_utils) -project(${PROJ_CJSON_UTILS} C) file(GLOB HEADERS_UTILS cJSON_Utils.h) set(SOURCES_UTILS cJSON_Utils.c) @@ -52,12 +50,12 @@ set(SOURCES_UTILS cJSON_Utils.c) add_library(${CJSON_UTILS_LIB} ${HEADERS_UTILS} ${SOURCES_UTILS}) target_link_libraries(${CJSON_UTILS_LIB} ${CJSON_LIB}) -configure_file("${cJSON_utils_SOURCE_DIR}/libcjson_utils.pc.in" - "${cJSON_utils_BINARY_DIR}/libcjson_utils.pc" @ONLY) +configure_file("${cJSON_SOURCE_DIR}/libcjson_utils.pc.in" + "${cJSON_BINARY_DIR}/libcjson_utils.pc" @ONLY) install(TARGETS ${CJSON_UTILS_LIB} DESTINATION lib${LIB_SUFFIX}) install(FILES cJSON_Utils.h DESTINATION include/cjson) -install (FILES ${cJSON_utils_BINARY_DIR}/libcjson.pc DESTINATION lib/pkgconfig) +install (FILES ${cJSON_BINARY_DIR}/libcjson.pc DESTINATION lib/pkgconfig) set_target_properties(${CJSON_UTILS_LIB} PROPERTIES From 302c574e0039cce2fe777deece7d9aa44bb50407 Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Sat, 5 Nov 2016 21:24:24 +0700 Subject: [PATCH 17/45] CMake: Add compiler options only for Clang and GCC --- CMakeLists.txt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 35f4d9f..6d1f083 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,7 +9,9 @@ set(CJSON_VERSION_SO 1) set(CJSON_UTILS_VERSION_SO 1) set(CJSON_VERSION "${CJSON_VERSION_MAJOR}.${CJSON_VERSION_MINOR}.${CJSON_VERSION_PATCH}") -add_compile_options(-ansi -pedantic -Wall -Wextra -Werror -Wstrict-prototypes -Wwrite-strings) +if(("${CMAKE_C_COMPILER_ID}" MATCHES "GNU") OR ("${CMAKE_C_COMPILER_ID}" MATCHES "Clang")) + add_compile_options(-ansi -pedantic -Wall -Wextra -Werror -Wstrict-prototypes -Wwrite-strings) +endif() #variables for pkg-config set(prefix ${CMAKE_INSTALL_PREFIX}) From fad10d5b1ec1e2b400eed708bf28fbed4523df1f Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Sat, 5 Nov 2016 22:01:31 +0700 Subject: [PATCH 18/45] Update list of contributors --- CONTRIBUTORS.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index f0850e8..6f2a93c 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -7,16 +7,21 @@ Contributors * [Dave Gamble](https://github.com/DaveGamble) * [dieyushi](https://github.com/dieyushi) * [Dongwen Huang (黄东文)](https://github.com/DongwenHuang) -* [ecksun](https://github.com/ecksun) +* Eswar Yaganti * [Evan Todd](https://github.com/etodd) * [Fabrice Fontaine](https://github.com/ffontaine) +* Ian Mobley +* Irwan Djadjadi * [IvanVoid](https://github.com/npi3pak) * [Jonathan Fether](https://github.com/jfether) * [Kevin Branigan](https://github.com/kbranigan) +* [Linus Wallgren](https://github.com/ecksun) * [Max Bruckner](https://github.com/FSMaxB) +* Mike Pontillo +* Paulo Antonio Alvarez * [Rafael Leal Dias](https://github.com/rafaeldias) * [Rod Vagg](https://github.com/rvagg) * [Roland Meertens](https://github.com/rmeertens) * [Weston Schmidt](https://github.com/schmidtw) -And more people on [SourceForge](https://sourceforge.net/p/cjson/bugs/search/?q=status%3Aclosed-rejected+or+status%3Aclosed-out-of-date+or+status%3Awont-fix+or+status%3Aclosed-fixed+or+status%3Aclosed&page=0) +And probably more people on [SourceForge](https://sourceforge.net/p/cjson/bugs/search/?q=status%3Aclosed-rejected+or+status%3Aclosed-out-of-date+or+status%3Awont-fix+or+status%3Aclosed-fixed+or+status%3Aclosed&page=0) From 97b255c73675f3d19f63c582d692fc980b4cc6f3 Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Sat, 5 Nov 2016 22:20:34 +0700 Subject: [PATCH 19/45] CMake: Fix installation of libcjson_utils.pc --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6d1f083..8206b73 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -57,7 +57,7 @@ configure_file("${cJSON_SOURCE_DIR}/libcjson_utils.pc.in" install(TARGETS ${CJSON_UTILS_LIB} DESTINATION lib${LIB_SUFFIX}) install(FILES cJSON_Utils.h DESTINATION include/cjson) -install (FILES ${cJSON_BINARY_DIR}/libcjson.pc DESTINATION lib/pkgconfig) +install (FILES ${cJSON_BINARY_DIR}/libcjson_utils.pc DESTINATION lib/pkgconfig) set_target_properties(${CJSON_UTILS_LIB} PROPERTIES From 3353062fc877fd5a82b62335222aa9bbac7318d1 Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Sat, 5 Nov 2016 22:20:55 +0700 Subject: [PATCH 20/45] CMake: Make building of cJSON_Utils optional Turned off by default. --- CMakeLists.txt | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8206b73..a313445 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -44,25 +44,28 @@ set_target_properties(${CJSON_LIB} VERSION ${CJSON_VERSION}) #cJSON_Utils -set(CJSON_UTILS_LIB cjson_utils) +option(ENABLE_CJSON_UTILS "Enable building the cJSON_Utils library." OFF) +if(ENABLE_CJSON_UTILS) + set(CJSON_UTILS_LIB cjson_utils) -file(GLOB HEADERS_UTILS cJSON_Utils.h) -set(SOURCES_UTILS cJSON_Utils.c) + file(GLOB HEADERS_UTILS cJSON_Utils.h) + set(SOURCES_UTILS cJSON_Utils.c) -add_library(${CJSON_UTILS_LIB} ${HEADERS_UTILS} ${SOURCES_UTILS}) -target_link_libraries(${CJSON_UTILS_LIB} ${CJSON_LIB}) + add_library(${CJSON_UTILS_LIB} ${HEADERS_UTILS} ${SOURCES_UTILS}) + target_link_libraries(${CJSON_UTILS_LIB} ${CJSON_LIB}) -configure_file("${cJSON_SOURCE_DIR}/libcjson_utils.pc.in" - "${cJSON_BINARY_DIR}/libcjson_utils.pc" @ONLY) + configure_file("${cJSON_SOURCE_DIR}/libcjson_utils.pc.in" + "${cJSON_BINARY_DIR}/libcjson_utils.pc" @ONLY) -install(TARGETS ${CJSON_UTILS_LIB} DESTINATION lib${LIB_SUFFIX}) -install(FILES cJSON_Utils.h DESTINATION include/cjson) -install (FILES ${cJSON_BINARY_DIR}/libcjson_utils.pc DESTINATION lib/pkgconfig) + install(TARGETS ${CJSON_UTILS_LIB} DESTINATION lib${LIB_SUFFIX}) + install(FILES cJSON_Utils.h DESTINATION include/cjson) + install (FILES ${cJSON_BINARY_DIR}/libcjson_utils.pc DESTINATION lib/pkgconfig) -set_target_properties(${CJSON_UTILS_LIB} - PROPERTIES - SOVERSION ${CJSON_UTILS_VERSION_SO} - VERSION ${CJSON_VERSION}) + set_target_properties(${CJSON_UTILS_LIB} + PROPERTIES + SOVERSION ${CJSON_UTILS_VERSION_SO} + VERSION ${CJSON_VERSION}) +endif() option(ENABLE_CJSON_TEST "Enable building cJSON test" ON) if(ENABLE_CJSON_TEST) @@ -70,7 +73,9 @@ if(ENABLE_CJSON_TEST) add_executable(${TEST_CJSON} test.c) target_link_libraries(${TEST_CJSON} ${CJSON_LIB}) - set(TEST_CJSON_UTILS cJSON_test_utils) - add_executable(${TEST_CJSON_UTILS} test_utils.c) - target_link_libraries(${TEST_CJSON_UTILS} ${CJSON_UTILS_LIB}) + if(ENABLE_CJSON_UTILS) + set(TEST_CJSON_UTILS cJSON_test_utils) + add_executable(${TEST_CJSON_UTILS} test_utils.c) + target_link_libraries(${TEST_CJSON_UTILS} ${CJSON_UTILS_LIB}) + endif() endif() From b2283dab873ee079c71fbb6b67148331b697c712 Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Sat, 5 Nov 2016 22:42:35 +0700 Subject: [PATCH 21/45] CMake: Use CMAKE_CURRENT_SOURCE/BINARY_DIR --- CMakeLists.txt | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a313445..852a615 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -31,12 +31,12 @@ if (NOT WIN32) target_link_libraries(${CJSON_LIB} m) endif() -configure_file("${cJSON_SOURCE_DIR}/libcjson.pc.in" - "${cJSON_BINARY_DIR}/libcjson.pc" @ONLY) +configure_file("${CMAKE_CURRENT_SOURCE_DIR}/libcjson.pc.in" + "${CMAKE_CURRENT_BINARY_DIR}/libcjson.pc" @ONLY) install(TARGETS ${CJSON_LIB} DESTINATION lib${LIB_SUFFIX}) install(FILES cJSON.h DESTINATION include/cjson) -install (FILES ${cJSON_BINARY_DIR}/libcjson.pc DESTINATION lib/pkgconfig) +install (FILES ${CMAKE_CURRENT_BINARY_DIR}/libcjson.pc DESTINATION lib/pkgconfig) set_target_properties(${CJSON_LIB} PROPERTIES @@ -54,12 +54,12 @@ if(ENABLE_CJSON_UTILS) add_library(${CJSON_UTILS_LIB} ${HEADERS_UTILS} ${SOURCES_UTILS}) target_link_libraries(${CJSON_UTILS_LIB} ${CJSON_LIB}) - configure_file("${cJSON_SOURCE_DIR}/libcjson_utils.pc.in" - "${cJSON_BINARY_DIR}/libcjson_utils.pc" @ONLY) + configure_file("${CMAKE_CURRENT_SOURCE_DIR}/libcjson_utils.pc.in" + "${CMAKE_CURRENT_BINARY_DIR}/libcjson_utils.pc" @ONLY) install(TARGETS ${CJSON_UTILS_LIB} DESTINATION lib${LIB_SUFFIX}) install(FILES cJSON_Utils.h DESTINATION include/cjson) - install (FILES ${cJSON_BINARY_DIR}/libcjson_utils.pc DESTINATION lib/pkgconfig) + install (FILES ${CMAKE_CURRENT_BINARY_DIR}/libcjson_utils.pc DESTINATION lib/pkgconfig) set_target_properties(${CJSON_UTILS_LIB} PROPERTIES From 78cdb0d6eccd4b5558625c9935c8b48db6e6c405 Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Sat, 5 Nov 2016 22:52:40 +0700 Subject: [PATCH 22/45] CMake: Quote all variables --- CMakeLists.txt | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 852a615..2ec18dd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,9 +14,9 @@ if(("${CMAKE_C_COMPILER_ID}" MATCHES "GNU") OR ("${CMAKE_C_COMPILER_ID}" MATCHES endif() #variables for pkg-config -set(prefix ${CMAKE_INSTALL_PREFIX}) +set(prefix "${CMAKE_INSTALL_PREFIX}") set(libdir "${CMAKE_INSTALL_PREFIX}/lib") -set(version ${CJSON_VERSION}) +set(version "${CJSON_VERSION}") option(BUILD_SHARED_LIBS "Build shared libraries" ON) @@ -26,22 +26,22 @@ set(CJSON_LIB cjson) file(GLOB HEADERS cJSON.h) set(SOURCES cJSON.c) -add_library(${CJSON_LIB} ${HEADERS} ${SOURCES}) +add_library("${CJSON_LIB}" "${HEADERS}" "${SOURCES}") if (NOT WIN32) - target_link_libraries(${CJSON_LIB} m) + target_link_libraries("${CJSON_LIB}" m) endif() configure_file("${CMAKE_CURRENT_SOURCE_DIR}/libcjson.pc.in" "${CMAKE_CURRENT_BINARY_DIR}/libcjson.pc" @ONLY) -install(TARGETS ${CJSON_LIB} DESTINATION lib${LIB_SUFFIX}) +install(TARGETS "${CJSON_LIB}" DESTINATION "lib${LIB_SUFFIX}") install(FILES cJSON.h DESTINATION include/cjson) -install (FILES ${CMAKE_CURRENT_BINARY_DIR}/libcjson.pc DESTINATION lib/pkgconfig) +install (FILES "${CMAKE_CURRENT_BINARY_DIR}/libcjson.pc" DESTINATION lib/pkgconfig) -set_target_properties(${CJSON_LIB} +set_target_properties("${CJSON_LIB}" PROPERTIES - SOVERSION ${CJSON_VERSION_SO} - VERSION ${CJSON_VERSION}) + SOVERSION "${CJSON_VERSION_SO}" + VERSION "${CJSON_VERSION}") #cJSON_Utils option(ENABLE_CJSON_UTILS "Enable building the cJSON_Utils library." OFF) @@ -51,31 +51,31 @@ if(ENABLE_CJSON_UTILS) file(GLOB HEADERS_UTILS cJSON_Utils.h) set(SOURCES_UTILS cJSON_Utils.c) - add_library(${CJSON_UTILS_LIB} ${HEADERS_UTILS} ${SOURCES_UTILS}) - target_link_libraries(${CJSON_UTILS_LIB} ${CJSON_LIB}) + add_library("${CJSON_UTILS_LIB}" "${HEADERS_UTILS}" "${SOURCES_UTILS}") + target_link_libraries("${CJSON_UTILS_LIB}" "${CJSON_LIB}") configure_file("${CMAKE_CURRENT_SOURCE_DIR}/libcjson_utils.pc.in" "${CMAKE_CURRENT_BINARY_DIR}/libcjson_utils.pc" @ONLY) - install(TARGETS ${CJSON_UTILS_LIB} DESTINATION lib${LIB_SUFFIX}) + install(TARGETS "${CJSON_UTILS_LIB}" DESTINATION "lib${LIB_SUFFIX}") install(FILES cJSON_Utils.h DESTINATION include/cjson) - install (FILES ${CMAKE_CURRENT_BINARY_DIR}/libcjson_utils.pc DESTINATION lib/pkgconfig) + install (FILES "${CMAKE_CURRENT_BINARY_DIR}/libcjson_utils.pc" DESTINATION lib/pkgconfig) - set_target_properties(${CJSON_UTILS_LIB} + set_target_properties("${CJSON_UTILS_LIB}" PROPERTIES - SOVERSION ${CJSON_UTILS_VERSION_SO} - VERSION ${CJSON_VERSION}) + SOVERSION "${CJSON_UTILS_VERSION_SO}" + VERSION "${CJSON_VERSION}") endif() option(ENABLE_CJSON_TEST "Enable building cJSON test" ON) if(ENABLE_CJSON_TEST) set(TEST_CJSON cJSON_test) - add_executable(${TEST_CJSON} test.c) - target_link_libraries(${TEST_CJSON} ${CJSON_LIB}) + add_executable("${TEST_CJSON}" test.c) + target_link_libraries("${TEST_CJSON}" "${CJSON_LIB}") if(ENABLE_CJSON_UTILS) set(TEST_CJSON_UTILS cJSON_test_utils) - add_executable(${TEST_CJSON_UTILS} test_utils.c) - target_link_libraries(${TEST_CJSON_UTILS} ${CJSON_UTILS_LIB}) + add_executable("${TEST_CJSON_UTILS}" test_utils.c) + target_link_libraries("${TEST_CJSON_UTILS}" "${CJSON_UTILS_LIB}") endif() endif() From 8a7f9a210120fdece0c58c8563c40cbcbff533de Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Sat, 5 Nov 2016 23:16:57 +0700 Subject: [PATCH 23/45] CMake: Use the GNUInstallDirs module --- CMakeLists.txt | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2ec18dd..17f3e4f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,7 @@ cmake_minimum_required(VERSION 2.8) +include(GNUInstallDirs) + project(cJSON C) set(CJSON_VERSION_MAJOR 1) @@ -15,7 +17,7 @@ endif() #variables for pkg-config set(prefix "${CMAKE_INSTALL_PREFIX}") -set(libdir "${CMAKE_INSTALL_PREFIX}/lib") +set(libdir "${CMAKE_INSTALL_FULL_LIBDIR}") set(version "${CJSON_VERSION}") option(BUILD_SHARED_LIBS "Build shared libraries" ON) @@ -34,9 +36,9 @@ endif() configure_file("${CMAKE_CURRENT_SOURCE_DIR}/libcjson.pc.in" "${CMAKE_CURRENT_BINARY_DIR}/libcjson.pc" @ONLY) -install(TARGETS "${CJSON_LIB}" DESTINATION "lib${LIB_SUFFIX}") -install(FILES cJSON.h DESTINATION include/cjson) -install (FILES "${CMAKE_CURRENT_BINARY_DIR}/libcjson.pc" DESTINATION lib/pkgconfig) +install(TARGETS "${CJSON_LIB}" DESTINATION "${CMAKE_INSTALL_LIBDIR}") +install(FILES cJSON.h DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/cjson") +install (FILES "${CMAKE_CURRENT_BINARY_DIR}/libcjson.pc" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig") set_target_properties("${CJSON_LIB}" PROPERTIES @@ -57,9 +59,9 @@ if(ENABLE_CJSON_UTILS) configure_file("${CMAKE_CURRENT_SOURCE_DIR}/libcjson_utils.pc.in" "${CMAKE_CURRENT_BINARY_DIR}/libcjson_utils.pc" @ONLY) - install(TARGETS "${CJSON_UTILS_LIB}" DESTINATION "lib${LIB_SUFFIX}") - install(FILES cJSON_Utils.h DESTINATION include/cjson) - install (FILES "${CMAKE_CURRENT_BINARY_DIR}/libcjson_utils.pc" DESTINATION lib/pkgconfig) + install(TARGETS "${CJSON_UTILS_LIB}" DESTINATION "${CMAKE_INSTALL_LIBDIR}") + install(FILES cJSON_Utils.h DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/cjson") + install (FILES "${CMAKE_CURRENT_BINARY_DIR}/libcjson_utils.pc" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig") set_target_properties("${CJSON_UTILS_LIB}" PROPERTIES From 251e5a5e34873a7c97c6664cedf31b94981a0479 Mon Sep 17 00:00:00 2001 From: Markus Blatt Date: Fri, 4 Nov 2016 19:59:04 +0100 Subject: [PATCH 24/45] Added rudimentary package configuration files. These files will allow cmake based projects to find the library using find_package(cJSON). If successful, they can use CJSON_LIBRARIES and CJSON_INCLUDE_DIRS. Other build systems can use a similar approach to pkg-config: cmake --find-package -DNAME=cJSON -DCOMPILER_ID=GNU -DLANGUAGE=C -DCMAKE_PREFIX_PATH= -DMODE=LINK or with MODE COMPILE or EXISTS. --- CMakeLists.txt | 22 +++++++++++++++++++++- cJSONConfig.cmake.in | 14 ++++++++++++++ cJSONConfigVersion.cmake.in | 11 +++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 cJSONConfig.cmake.in create mode 100644 cJSONConfigVersion.cmake.in diff --git a/CMakeLists.txt b/CMakeLists.txt index 17f3e4f..ea32cd1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -69,7 +69,27 @@ if(ENABLE_CJSON_UTILS) VERSION "${CJSON_VERSION}") endif() -option(ENABLE_CJSON_TEST "Enable building cJSON test" ON) +# export library information for CMake projects +export(TARGETS ${CJSON_LIB} ${CJSON_UTILS_LIB} + FILE ${PROJECT_BINARY_DIR}/cJSONConfigTargets.cmake) + +# create the other package config files +configure_file( + cJSONConfig.cmake.in + ${PROJECT_BINARY_DIR}/cJSONConfig.cmake @ONLY) +configure_file( + cJSONConfigVersion.cmake.in + ${PROJECT_BINARY_DIR}/cJSONConfigVersion.cmake @ONLY) + +# Install package config files +install(FILES ${PROJECT_BINARY_DIR}/cJSONConfig.cmake + ${PROJECT_BINARY_DIR}/cJSONConfigVersion.cmake + ${PROJECT_BINARY_DIR}/cJSONConfigTargets.cmake + DESTINATION lib/cmake/cJSON) + +option(ENABLE_CJSON_TEST "Enable building cJSON test" OFF) + + if(ENABLE_CJSON_TEST) set(TEST_CJSON cJSON_test) add_executable("${TEST_CJSON}" test.c) diff --git a/cJSONConfig.cmake.in b/cJSONConfig.cmake.in new file mode 100644 index 0000000..da8e588 --- /dev/null +++ b/cJSONConfig.cmake.in @@ -0,0 +1,14 @@ +# Whether the utils lib was build. +set(CJSON_UTILS_FOUND @ENABLE_CJSON_UTILS@) + +# All cJson libraries (including the utils lib if present +set(CJSON_LIBRARIES @CJSON_UTILS_LIB@ @CJSON_LIB@) +# The cJSON utils library +set(CJSON_UTILS_LIBRARY @CJSON_UTILS_LIB@) +# The cJSON library +set(CJSON_LIBRARY @CJSON_LIB@) +# The include directories used by cJSON +set(CJSON_INCLUDE_DIRS @CMAKE_INSTALL_PREFIX@/include) + +get_filename_component(_dir "${CMAKE_CURRENT_LIST_FILE}" PATH) +include("${_dir}/cJSONConfigTargets.cmake") diff --git a/cJSONConfigVersion.cmake.in b/cJSONConfigVersion.cmake.in new file mode 100644 index 0000000..22ffec0 --- /dev/null +++ b/cJSONConfigVersion.cmake.in @@ -0,0 +1,11 @@ +set(PACKAGE_VERSION "@PROJECT_VERSION@") + +# Check whether the requested PACKAGE_FIND_VERSION is compatible +if("${PACKAGE_VERSION}" VERSION_LESS "${PACKAGE_FIND_VERSION}") + set(PACKAGE_VERSION_COMPATIBLE FALSE) +else() + set(PACKAGE_VERSION_COMPATIBLE TRUE) + if ("${PACKAGE_VERSION}" VERSION_EQUAL "${PACKAGE_FIND_VERSION}") + set(PACKAGE_VERSION_EXACT TRUE) + endif() +endif() From f9ce93029ac906b70addb9f2212b3973d76b8fb3 Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Sun, 6 Nov 2016 14:54:43 +0700 Subject: [PATCH 25/45] Buildsystem: Fix paths Fix library and include paths in pkg-config, CMakeLists.txt and the CMake config files. --- CMakeLists.txt | 3 ++- cJSONConfig.cmake.in | 2 +- cJSONConfigVersion.cmake.in | 2 +- libcjson.pc.in | 7 +++---- libcjson_utils.pc.in | 7 +++---- 5 files changed, 10 insertions(+), 11 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ea32cd1..5ad3a0c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,8 +17,9 @@ endif() #variables for pkg-config set(prefix "${CMAKE_INSTALL_PREFIX}") -set(libdir "${CMAKE_INSTALL_FULL_LIBDIR}") +set(libdir "${CMAKE_INSTALL_LIBDIR}") set(version "${CJSON_VERSION}") +set(includedir "${CMAKE_INSTALL_INCLUDEDIR}") option(BUILD_SHARED_LIBS "Build shared libraries" ON) diff --git a/cJSONConfig.cmake.in b/cJSONConfig.cmake.in index da8e588..06696ad 100644 --- a/cJSONConfig.cmake.in +++ b/cJSONConfig.cmake.in @@ -8,7 +8,7 @@ set(CJSON_UTILS_LIBRARY @CJSON_UTILS_LIB@) # The cJSON library set(CJSON_LIBRARY @CJSON_LIB@) # The include directories used by cJSON -set(CJSON_INCLUDE_DIRS @CMAKE_INSTALL_PREFIX@/include) +set(CJSON_INCLUDE_DIRS "@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_INCLUDEDIR@") get_filename_component(_dir "${CMAKE_CURRENT_LIST_FILE}" PATH) include("${_dir}/cJSONConfigTargets.cmake") diff --git a/cJSONConfigVersion.cmake.in b/cJSONConfigVersion.cmake.in index 22ffec0..868727b 100644 --- a/cJSONConfigVersion.cmake.in +++ b/cJSONConfigVersion.cmake.in @@ -1,4 +1,4 @@ -set(PACKAGE_VERSION "@PROJECT_VERSION@") +set(PACKAGE_VERSION "@CJSON_VERSION@") # Check whether the requested PACKAGE_FIND_VERSION is compatible if("${PACKAGE_VERSION}" VERSION_LESS "${PACKAGE_FIND_VERSION}") diff --git a/libcjson.pc.in b/libcjson.pc.in index aac3262..106ac7d 100644 --- a/libcjson.pc.in +++ b/libcjson.pc.in @@ -1,10 +1,9 @@ prefix=@prefix@ -libdir=@libdir@ -includedir=${prefix}/include/cJSON -version=@version@ +libdir=${prefix}/@libdir@ +includedir=${prefix}/@includedir@ Name: libcjson -Version: ${version} +Version: @version@ Description: Ultralightweight JSON parser in ANSI C Libs: -L${libdir} -lcjson Cflags: -I${includedir} diff --git a/libcjson_utils.pc.in b/libcjson_utils.pc.in index 67effb4..679aef6 100644 --- a/libcjson_utils.pc.in +++ b/libcjson_utils.pc.in @@ -1,10 +1,9 @@ prefix=@prefix@ -libdir=@libdir@ -includedir=${prefix}/include/cJSON -version=@version@ +libdir=${prefix}/@libdir@ +includedir=${prefix}/@includedir@ Name: libcjson_utils -Version: ${version} +Version: @version@ Description: An implementation of JSON Pointer, Patch and Merge Patch based on cJSON. Libs: -L${libdir} -lcjson_utils Cflags: -I${includedir} From de992ec3c9ab13403876426e86ae0c9517585f8e Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Sun, 6 Nov 2016 14:55:47 +0700 Subject: [PATCH 26/45] pkg-config: Add URL and link math library --- libcjson.pc.in | 2 ++ libcjson_utils.pc.in | 1 + 2 files changed, 3 insertions(+) diff --git a/libcjson.pc.in b/libcjson.pc.in index 106ac7d..b3a5b40 100644 --- a/libcjson.pc.in +++ b/libcjson.pc.in @@ -5,5 +5,7 @@ includedir=${prefix}/@includedir@ Name: libcjson Version: @version@ Description: Ultralightweight JSON parser in ANSI C +URL: https://github.com/DaveGamble/cJSON Libs: -L${libdir} -lcjson +Libs.Private: -lm Cflags: -I${includedir} diff --git a/libcjson_utils.pc.in b/libcjson_utils.pc.in index 679aef6..174ab60 100644 --- a/libcjson_utils.pc.in +++ b/libcjson_utils.pc.in @@ -5,6 +5,7 @@ includedir=${prefix}/@includedir@ Name: libcjson_utils Version: @version@ Description: An implementation of JSON Pointer, Patch and Merge Patch based on cJSON. +URL: https://github.com/DaveGamble/cJSON Libs: -L${libdir} -lcjson_utils Cflags: -I${includedir} Requires: libcjson From 8a838b37e36c9d995ded47d44e4366a7fe028bc7 Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Sun, 6 Nov 2016 14:58:22 +0700 Subject: [PATCH 27/45] CMake: Use standard variables for the version --- CMakeLists.txt | 14 +++++++------- cJSONConfigVersion.cmake.in | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5ad3a0c..690ca77 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,12 +4,12 @@ include(GNUInstallDirs) project(cJSON C) -set(CJSON_VERSION_MAJOR 1) -set(CJSON_VERSION_MINOR 0) -set(CJSON_VERSION_PATCH 0) +set(PROJECT_VERSION_MAJOR 1) +set(PROJECT_VERSION_MINOR 0) +set(PROJECT_VERSION_PATCH 0) set(CJSON_VERSION_SO 1) set(CJSON_UTILS_VERSION_SO 1) -set(CJSON_VERSION "${CJSON_VERSION_MAJOR}.${CJSON_VERSION_MINOR}.${CJSON_VERSION_PATCH}") +set(PROJECT_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}") if(("${CMAKE_C_COMPILER_ID}" MATCHES "GNU") OR ("${CMAKE_C_COMPILER_ID}" MATCHES "Clang")) add_compile_options(-ansi -pedantic -Wall -Wextra -Werror -Wstrict-prototypes -Wwrite-strings) @@ -18,7 +18,7 @@ endif() #variables for pkg-config set(prefix "${CMAKE_INSTALL_PREFIX}") set(libdir "${CMAKE_INSTALL_LIBDIR}") -set(version "${CJSON_VERSION}") +set(version "${PROJECT_VERSION}") set(includedir "${CMAKE_INSTALL_INCLUDEDIR}") option(BUILD_SHARED_LIBS "Build shared libraries" ON) @@ -44,7 +44,7 @@ install (FILES "${CMAKE_CURRENT_BINARY_DIR}/libcjson.pc" DESTINATION "${CMAKE_IN set_target_properties("${CJSON_LIB}" PROPERTIES SOVERSION "${CJSON_VERSION_SO}" - VERSION "${CJSON_VERSION}") + VERSION "${PROJECT_VERSION}") #cJSON_Utils option(ENABLE_CJSON_UTILS "Enable building the cJSON_Utils library." OFF) @@ -67,7 +67,7 @@ if(ENABLE_CJSON_UTILS) set_target_properties("${CJSON_UTILS_LIB}" PROPERTIES SOVERSION "${CJSON_UTILS_VERSION_SO}" - VERSION "${CJSON_VERSION}") + VERSION "${PROJECT_VERSION}") endif() # export library information for CMake projects diff --git a/cJSONConfigVersion.cmake.in b/cJSONConfigVersion.cmake.in index 868727b..22ffec0 100644 --- a/cJSONConfigVersion.cmake.in +++ b/cJSONConfigVersion.cmake.in @@ -1,4 +1,4 @@ -set(PACKAGE_VERSION "@CJSON_VERSION@") +set(PACKAGE_VERSION "@PROJECT_VERSION@") # Check whether the requested PACKAGE_FIND_VERSION is compatible if("${PACKAGE_VERSION}" VERSION_LESS "${PACKAGE_FIND_VERSION}") From 58b76b518aa53daea3ad9bb8df8fcc10c6dfb3c1 Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Sun, 6 Nov 2016 15:35:53 +0700 Subject: [PATCH 28/45] CMake: Fix export of targets The prior approach was exporting paths from the build environment. --- CMakeLists.txt | 17 +++++++---------- cJSONConfig.cmake.in | 26 ++++++++++++++++++-------- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 690ca77..718a0b5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -37,9 +37,11 @@ endif() configure_file("${CMAKE_CURRENT_SOURCE_DIR}/libcjson.pc.in" "${CMAKE_CURRENT_BINARY_DIR}/libcjson.pc" @ONLY) -install(TARGETS "${CJSON_LIB}" DESTINATION "${CMAKE_INSTALL_LIBDIR}") install(FILES cJSON.h DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/cjson") install (FILES "${CMAKE_CURRENT_BINARY_DIR}/libcjson.pc" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig") +install(TARGETS "${CJSON_LIB}" DESTINATION "${CMAKE_INSTALL_LIBDIR}" EXPORT "${CJSON_LIB}") +# export library information for CMake projects +install(EXPORT "${CJSON_LIB}" DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/cJSON") set_target_properties("${CJSON_LIB}" PROPERTIES @@ -60,9 +62,11 @@ if(ENABLE_CJSON_UTILS) configure_file("${CMAKE_CURRENT_SOURCE_DIR}/libcjson_utils.pc.in" "${CMAKE_CURRENT_BINARY_DIR}/libcjson_utils.pc" @ONLY) - install(TARGETS "${CJSON_UTILS_LIB}" DESTINATION "${CMAKE_INSTALL_LIBDIR}") + install(TARGETS "${CJSON_UTILS_LIB}" DESTINATION "${CMAKE_INSTALL_LIBDIR}" EXPORT "${CJSON_UTILS_LIB}") install(FILES cJSON_Utils.h DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/cjson") install (FILES "${CMAKE_CURRENT_BINARY_DIR}/libcjson_utils.pc" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig") + # export library information for CMake projects + install(EXPORT "${CJSON_UTILS_LIB}" DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/cJSON") set_target_properties("${CJSON_UTILS_LIB}" PROPERTIES @@ -70,10 +74,6 @@ if(ENABLE_CJSON_UTILS) VERSION "${PROJECT_VERSION}") endif() -# export library information for CMake projects -export(TARGETS ${CJSON_LIB} ${CJSON_UTILS_LIB} - FILE ${PROJECT_BINARY_DIR}/cJSONConfigTargets.cmake) - # create the other package config files configure_file( cJSONConfig.cmake.in @@ -85,12 +85,9 @@ configure_file( # Install package config files install(FILES ${PROJECT_BINARY_DIR}/cJSONConfig.cmake ${PROJECT_BINARY_DIR}/cJSONConfigVersion.cmake - ${PROJECT_BINARY_DIR}/cJSONConfigTargets.cmake - DESTINATION lib/cmake/cJSON) + DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/cJSON") option(ENABLE_CJSON_TEST "Enable building cJSON test" OFF) - - if(ENABLE_CJSON_TEST) set(TEST_CJSON cJSON_test) add_executable("${TEST_CJSON}" test.c) diff --git a/cJSONConfig.cmake.in b/cJSONConfig.cmake.in index 06696ad..241ba4d 100644 --- a/cJSONConfig.cmake.in +++ b/cJSONConfig.cmake.in @@ -1,14 +1,24 @@ # Whether the utils lib was build. set(CJSON_UTILS_FOUND @ENABLE_CJSON_UTILS@) -# All cJson libraries (including the utils lib if present -set(CJSON_LIBRARIES @CJSON_UTILS_LIB@ @CJSON_LIB@) -# The cJSON utils library -set(CJSON_UTILS_LIBRARY @CJSON_UTILS_LIB@) -# The cJSON library -set(CJSON_LIBRARY @CJSON_LIB@) # The include directories used by cJSON -set(CJSON_INCLUDE_DIRS "@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_INCLUDEDIR@") +set(CJSON_INCLUDE_DIRS "@prefix@/@includedir@") get_filename_component(_dir "${CMAKE_CURRENT_LIST_FILE}" PATH) -include("${_dir}/cJSONConfigTargets.cmake") + +# The cJSON library +set(CJSON_LIBRARY "@CJSON_LIB@") +# Include the target +include("${_dir}/cjson.cmake") + +if(CJSON_UTILS_FOUND) + # The cJSON utils library + set(CJSON_UTILS_LIBRARY @CJSON_UTILS_LIB@) + # All cJSON libraries + set(CJSON_LIBRARIES "@CJSON_UTILS_LIB@" "@CJSON_LIB@") + # Include the target + include("${_dir}/cjson_utils.cmake") +else() + # All cJSON libraries + set(CJSON_LIBRARIES "@CJSON_LIB@") +endif() From 966646d70e1bbd91613daafdb5dde57f93032bcd Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Sun, 6 Nov 2016 15:47:33 +0700 Subject: [PATCH 29/45] CMake config: Add CJSON_INCLUDE_DIR --- cJSONConfig.cmake.in | 1 + 1 file changed, 1 insertion(+) diff --git a/cJSONConfig.cmake.in b/cJSONConfig.cmake.in index 241ba4d..0c20fe3 100644 --- a/cJSONConfig.cmake.in +++ b/cJSONConfig.cmake.in @@ -3,6 +3,7 @@ set(CJSON_UTILS_FOUND @ENABLE_CJSON_UTILS@) # The include directories used by cJSON set(CJSON_INCLUDE_DIRS "@prefix@/@includedir@") +set(CJSON_INCLUDE_DIR "@prefix@/@includedir@") get_filename_component(_dir "${CMAKE_CURRENT_LIST_FILE}" PATH) From 5838e279a51d00669f279e3d4629f7c748886334 Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Mon, 7 Nov 2016 00:08:20 +0700 Subject: [PATCH 30/45] CMake: Use CMAKE_C_FLAGS instead of add_compile_options This is for compatibility reasons with old CMake 2.8 versions. --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 718a0b5..45df2d3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,7 +12,7 @@ set(CJSON_UTILS_VERSION_SO 1) set(PROJECT_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}") if(("${CMAKE_C_COMPILER_ID}" MATCHES "GNU") OR ("${CMAKE_C_COMPILER_ID}" MATCHES "Clang")) - add_compile_options(-ansi -pedantic -Wall -Wextra -Werror -Wstrict-prototypes -Wwrite-strings) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ansi -pedantic -Wall -Wextra -Werror -Wstrict-prototypes -Wwrite-strings") endif() #variables for pkg-config From c213f2eb58c1c6227a46900f20c46cc70ad012c5 Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Mon, 7 Nov 2016 00:13:38 +0700 Subject: [PATCH 31/45] cJSON: Option to disable exporting targets This can be disabled when the exporting of the targets causes problems. --- CMakeLists.txt | 13 +++++++++---- cJSONConfig.cmake.in | 12 ++++++++---- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 45df2d3..f235e66 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,6 +22,7 @@ set(version "${PROJECT_VERSION}") set(includedir "${CMAKE_INSTALL_INCLUDEDIR}") option(BUILD_SHARED_LIBS "Build shared libraries" ON) +option(ENABLE_TARGET_EXPORT "Enable exporting of CMake targets. Disable when it causes problems!" ON) #cJSON set(CJSON_LIB cjson) @@ -40,8 +41,10 @@ configure_file("${CMAKE_CURRENT_SOURCE_DIR}/libcjson.pc.in" install(FILES cJSON.h DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/cjson") install (FILES "${CMAKE_CURRENT_BINARY_DIR}/libcjson.pc" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig") install(TARGETS "${CJSON_LIB}" DESTINATION "${CMAKE_INSTALL_LIBDIR}" EXPORT "${CJSON_LIB}") -# export library information for CMake projects -install(EXPORT "${CJSON_LIB}" DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/cJSON") +if(ENABLE_TARGET_EXPORT) + # export library information for CMake projects + install(EXPORT "${CJSON_LIB}" DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/cJSON") +endif() set_target_properties("${CJSON_LIB}" PROPERTIES @@ -65,8 +68,10 @@ if(ENABLE_CJSON_UTILS) install(TARGETS "${CJSON_UTILS_LIB}" DESTINATION "${CMAKE_INSTALL_LIBDIR}" EXPORT "${CJSON_UTILS_LIB}") install(FILES cJSON_Utils.h DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/cjson") install (FILES "${CMAKE_CURRENT_BINARY_DIR}/libcjson_utils.pc" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig") - # export library information for CMake projects - install(EXPORT "${CJSON_UTILS_LIB}" DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/cJSON") + if(ENABLE_TARGET_EXPORT) + # export library information for CMake projects + install(EXPORT "${CJSON_UTILS_LIB}" DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/cJSON") + endif() set_target_properties("${CJSON_UTILS_LIB}" PROPERTIES diff --git a/cJSONConfig.cmake.in b/cJSONConfig.cmake.in index 0c20fe3..67e8325 100644 --- a/cJSONConfig.cmake.in +++ b/cJSONConfig.cmake.in @@ -9,16 +9,20 @@ get_filename_component(_dir "${CMAKE_CURRENT_LIST_FILE}" PATH) # The cJSON library set(CJSON_LIBRARY "@CJSON_LIB@") -# Include the target -include("${_dir}/cjson.cmake") +if(@ENABLE_TARGET_EXPORT@) + # Include the target + include("${_dir}/cjson.cmake") +endif() if(CJSON_UTILS_FOUND) # The cJSON utils library set(CJSON_UTILS_LIBRARY @CJSON_UTILS_LIB@) # All cJSON libraries set(CJSON_LIBRARIES "@CJSON_UTILS_LIB@" "@CJSON_LIB@") - # Include the target - include("${_dir}/cjson_utils.cmake") + if(@ENABLE_TARGET_EXPORT@) + # Include the target + include("${_dir}/cjson_utils.cmake") + endif() else() # All cJSON libraries set(CJSON_LIBRARIES "@CJSON_LIB@") From cfd0fb83d3ca29026b1754e5f8456f0ab7343ef9 Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Fri, 11 Nov 2016 13:52:10 +0700 Subject: [PATCH 32/45] CMake: Fix warning on Cygwin --- CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index f235e66..61592f7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,3 +1,4 @@ +set(CMAKE_LEGACY_CYGWIN_WIN32 0) cmake_minimum_required(VERSION 2.8) include(GNUInstallDirs) From afd690d1c9981ee65f05915b5e1c89e8b3479cfc Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Sat, 12 Nov 2016 15:02:09 +0700 Subject: [PATCH 33/45] format fix --- cJSON.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cJSON.c b/cJSON.c index ae70fc6..06d2cb8 100644 --- a/cJSON.c +++ b/cJSON.c @@ -920,7 +920,9 @@ static const char *parse_value(cJSON *item, const char *value, const char **ep) return parse_object(item, value, ep); } - *ep=value;return 0; /* failure. */ + /* failure. */ + *ep = value; + return 0; } /* Render a value to text. */ From dc4b62915ac9357405c2297f538c6f56e491d040 Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Mon, 14 Nov 2016 19:33:03 +0700 Subject: [PATCH 34/45] fix #10, now failing on incorrect escape sequences --- cJSON.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/cJSON.c b/cJSON.c index 06d2cb8..00875c5 100644 --- a/cJSON.c +++ b/cJSON.c @@ -534,6 +534,11 @@ static const char *parse_string(cJSON *item, const char *str, const char **ep) case 't': *ptr2++ = '\t'; break; + case '\"': + case '\\': + case '/': + *ptr2++ = *ptr; + break; case 'u': /* transcode utf16 to utf8. See RFC2781 and RFC3629. */ uc = parse_hex4(ptr + 1); /* get the unicode char. */ @@ -620,8 +625,8 @@ static const char *parse_string(cJSON *item, const char *str, const char **ep) ptr2 += len; break; default: - *ptr2++ = *ptr; - break; + *ep = str; + return 0; } ptr++; } From 5cca67e3fcc1da2d38f5a648e1e409c366555da7 Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Mon, 14 Nov 2016 19:36:04 +0700 Subject: [PATCH 35/45] CMake: Build tests by default --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 61592f7..c36097a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -93,7 +93,7 @@ install(FILES ${PROJECT_BINARY_DIR}/cJSONConfig.cmake ${PROJECT_BINARY_DIR}/cJSONConfigVersion.cmake DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/cJSON") -option(ENABLE_CJSON_TEST "Enable building cJSON test" OFF) +option(ENABLE_CJSON_TEST "Enable building cJSON test" ON) if(ENABLE_CJSON_TEST) set(TEST_CJSON cJSON_test) add_executable("${TEST_CJSON}" test.c) From 46ff20c8b0499ddcbe100d772d361603db89cbcd Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Mon, 14 Nov 2016 22:25:16 +0700 Subject: [PATCH 36/45] cJSON.h: add stddef.h because of size_t --- cJSON.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cJSON.h b/cJSON.h index c2b8d57..dbbb739 100644 --- a/cJSON.h +++ b/cJSON.h @@ -28,6 +28,8 @@ extern "C" { #endif +#include + /* cJSON Types: */ #define cJSON_False (1 << 0) #define cJSON_True (1 << 1) From 778bcd1a625303564605d575064503907c63db1a Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Mon, 14 Nov 2016 22:52:44 +0700 Subject: [PATCH 37/45] Makefile: Redo with library versioning And remove special handling of SunOs --- Makefile | 157 +++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 119 insertions(+), 38 deletions(-) diff --git a/Makefile b/Makefile index 54f0d19..35d7244 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,15 @@ -OBJ = cJSON.o -LIBNAME = libcjson -TESTS = test +CJSON_OBJ = cJSON.o +UTILS_OBJ = cJSON_Utils.o +CJSON_LIBNAME = libcjson +UTILS_LIBNAME = libcjson_utils +CJSON_TEST = cJSON_test +UTILS_TEST = cJSON_test_utils + +LDLIBS = -lm + +LIBVERSION = 1.0.0 +CJSON_SOVERSION = 1 +UTILS_SOVERSION = 1 PREFIX ?= /usr/local INCLUDE_PATH ?= include/cjson @@ -11,54 +20,126 @@ INSTALL_LIBRARY_PATH = $(DESTDIR)$(PREFIX)/$(LIBRARY_PATH) INSTALL ?= cp -a -R_CFLAGS = -fpic $(CFLAGS) -Wall -Werror -Wstrict-prototypes -Wwrite-strings -D_POSIX_C_SOURCE=200112L +R_CFLAGS = -fPIC -ansi -pedantic -Wall -Werror -Wstrict-prototypes -Wwrite-strings $(CFLAGS) -uname_S := $(shell sh -c 'uname -s 2>/dev/null || echo false') +uname := $(shell sh -c 'uname -s 2>/dev/null || echo false') -## shared lib -DYLIBNAME = $(LIBNAME).so -DYLIBCMD = $(CC) -shared -o $(DYLIBNAME) +#library file extensions +SHARED = so +STATIC = a ## create dynamic (shared) library on Darwin (base OS for MacOSX and IOS) -ifeq (Darwin, $(uname_S)) - DYLIBNAME = $(LIBNAME).dylib -## create dyanmic (shared) library on SunOS -else ifeq (SunOS, $(uname_S)) - DYLIBCMD = $(CC) -G -o $(DYLIBNAME) - INSTALL = cp -r +ifeq (Darwin, $(uname)) + SHARED = dylib endif -## static lib -STLIBNAME = $(LIBNAME).a +#cJSON library names +CJSON_SHARED = $(CJSON_LIBNAME).$(SHARED) +CJSON_SHARED_VERSION = $(CJSON_LIBNAME).$(SHARED).$(LIBVERSION) +CJSON_SHARED_SO = $(CJSON_LIBNAME).$(SHARED).$(CJSON_SOVERSION) +CJSON_STATIC = $(CJSON_LIBNAME).$(STATIC) -.PHONY: all clean install +#cJSON_Utils library names +UTILS_SHARED = $(UTILS_LIBNAME).$(SHARED) +UTILS_SHARED_VERSION = $(UTILS_LIBNAME).$(SHARED).$(LIBVERSION) +UTILS_SHARED_SO = $(UTILS_LIBNAME).$(SHARED).$(UTILS_SOVERSION) +UTILS_STATIC = $(UTILS_LIBNAME).$(STATIC) -all: $(DYLIBNAME) $(STLIBNAME) $(TESTS) +SHARED_CMD = $(CC) -shared -o -$(DYLIBNAME): $(OBJ) - $(DYLIBCMD) $< $(LDFLAGS) - -$(STLIBNAME): $(OBJ) - $(AR) rcs $@ $< +.PHONY: all shared static tests clean install -$(OBJ): cJSON.c cJSON.h +all: shared static tests + +shared: $(CJSON_SHARED) $(UTILS_SHARED) + +static: $(CJSON_STATIC) $(UTILS_STATIC) + +tests: $(CJSON_TEST) $(UTILS_TEST) + +test: tests + ./$(CJSON_TEST) + ./$(UTILS_TEST) .c.o: - $(CC) -ansi -pedantic -c $(R_CFLAGS) $< + $(CC) -ansi -pedantic -c $(R_CFLAGS) $< -$(TESTS): cJSON.c cJSON.h test.c - $(CC) cJSON.c test.c -o test -lm -I. +#tests +#cJSON +$(CJSON_TEST): cJSON.c cJSON.h test.c + $(CC) $^ -o $@ $(LDLIBS) -I. +#cJSON_Utils +$(UTILS_TEST): cJSON.c cJSON.h test.c + $(CC) $^ -o $@ $(LDLIBS) -I. -install: $(DYLIBNAME) $(STLIBNAME) - mkdir -p $(INSTALL_LIBRARY_PATH) $(INSTALL_INCLUDE_PATH) - $(INSTALL) cJSON.h $(INSTALL_INCLUDE_PATH) - $(INSTALL) $(DYLIBNAME) $(INSTALL_LIBRARY_PATH) - $(INSTALL) $(STLIBNAME) $(INSTALL_LIBRARY_PATH) +#static libraries +#cJSON +$(CJSON_STATIC): $(CJSON_OBJ) + $(AR) rcs $@ $< +#cJSON_Utils +$(UTILS_STATIC): $(UTILS_OBJ) + $(AR) rcs $@ $< -uninstall: - rm -rf $(INSTALL_LIBRARY_PATH)/$(DYLIBNAME) - rm -rf $(INSTALL_LIBRARY_PATH)/$(STLIBNAME) - rm -rf $(INSTALL_INCLUDE_PATH)/cJSON.h +#shared libraries .so.1.0.0 +#cJSON +$(CJSON_SHARED_VERSION): $(CJSON_OBJ) + $(CC) -shared -o $@ $< $(LDFLAGS) +#cJSON_Utils +$(UTILS_SHARED_VERSION): $(UTILS_OBJ) + $(CC) -shared -o $@ $< $(LDFLAGS) -clean: - rm -rf $(DYLIBNAME) $(STLIBNAME) $(TESTS) *.o +#objects +#cJSON +$(CJSON_OBJ): cJSON.c cJSON.h +#cJSON_Utils +$(UTILS_OBJ): cJSON_Utils.c cJSON_Utils.h + + +#links .so -> .so.1 -> .so.1.0.0 +#cJSON +$(CJSON_SHARED_SO): $(CJSON_SHARED_VERSION) + ln -s $(CJSON_SHARED_VERSION) $(CJSON_SHARED_SO) +$(CJSON_SHARED): $(CJSON_SHARED_SO) + ln -s $(CJSON_SHARED_SO) $(CJSON_SHARED) +#cJSON_Utils +$(UTILS_SHARED_SO): $(UTILS_SHARED_VERSION) + ln -s $(UTILS_SHARED_VERSION) $(UTILS_SHARED_SO) +$(UTILS_SHARED): $(UTILS_SHARED_SO) + ln -s $(UTILS_SHARED_SO) $(UTILS_SHARED) + +#install +#cJSON +install-cjson: + mkdir -p $(INSTALL_LIBRARY_PATH) $(INSTALL_INCLUDE_PATH) + $(INSTALL) cJSON.h $(INSTALL_INCLUDE_PATH) + $(INSTALL) $(CJSON_SHARED) $(CJSON_SHARED_SO) $(CJSON_SHARED_VERSION) $(INSTALL_LIBRARY_PATH) +#cJSON_Utils +install-utils: install-cjson + $(INSTALL) cJSON_Utils.h $(INSTALL_INCLUDE_PATH) + $(INSTALL) $(UTILS_SHARED) $(UTILS_SHARED_SO) $(UTILS_SHARED_VERSION) $(INSTALL_LIBRARY_PATH) + +install: install-cjson install-utils + +#uninstall +#cJSON +uninstall-cjson: uninstall-utils + $(RM) $(INSTALL_LIBRARY_PATH)/$(CJSON_SHARED) + $(RM) $(INSTALL_LIBRARY_PATH)/$(CJSON_SHARED_VERSION) + $(RM) $(INSTALL_LIBRARY_PATH)/$(CJSON_SHARED_SO) + rmdir $(INSTALL_LIBRARY_PATH) + $(RM) $(INSTALL_INCLUDE_PATH)/cJSON.h + rmdir $(INSTALL_INCLUDE_PATH) +#cJSON_Utils +uninstall-utils: + $(RM) $(INSTALL_LIBRARY_PATH)/$(UTILS_SHARED) + $(RM) $(INSTALL_LIBRARY_PATH)/$(UTILS_SHARED_VERSION) + $(RM) $(INSTALL_LIBRARY_PATH)/$(UTILS_SHARED_SO) + $(RM) $(INSTALL_INCLUDE_PATH)/cJSON_Utils.h + +uninstall: uninstall-utils uninstall-cjson + +clean: + $(RM) $(CJSON_OBJ) $(UTILS_OBJ) #delete object files + $(RM) $(CJSON_SHARED) $(CJSON_SHARED_VERSION) $(CJSON_SHARED_SO) $(CJSON_STATIC) #delete cJSON + $(RM) $(UTILS_SHARED) $(UTILS_SHARED_VERSION) $(UTILS_SHARED_SO) $(UTILS_STATIC) #delete cJSON_Utils + $(RM) $(CJSON_TEST) $(UTILS_TEST) #delete tests From 501be026907195679930757e5d0b2d02fc8f8442 Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Tue, 15 Nov 2016 00:29:23 +0700 Subject: [PATCH 38/45] README: Reformat --- README.md | 250 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 144 insertions(+), 106 deletions(-) diff --git a/README.md b/README.md index 7bc1b12..d642b04 100644 --- a/README.md +++ b/README.md @@ -1,27 +1,40 @@ +# cJSON - Copyright (c) 2009 Dave Gamble +Ultralightweight JSON parser in ANSI C. - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - copies of the Software, and to permit persons to whom the Software is - furnished to do so, subject to the following conditions: +## Table of contents +* [License](#license) +* [Usage](#usage) + * [Welcome to cJSON](#welcome-to-cjson) + * [Some JSON](#some-json) + * [Here's the structure](#heres-the-structure) + * [Enjoy cJSON!](#enjoy-cjson) - The above copyright notice and this permission notice shall be included in - all copies or substantial portions of the Software. +## License - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - THE SOFTWARE. +> Copyright (c) 2009-2016 Dave Gamble +> +> Permission is hereby granted, free of charge, to any person obtaining a copy +> of this software and associated documentation files (the "Software"), to deal +> in the Software without restriction, including without limitation the rights +> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +> copies of the Software, and to permit persons to whom the Software is +> furnished to do so, subject to the following conditions: +> +> The above copyright notice and this permission notice shall be included in +> all copies or substantial portions of the Software. +> +> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +> THE SOFTWARE. +## Usage -Welcome to cJSON. ------------------ +### Welcome to cJSON. cJSON aims to be the dumbest possible parser that you can get your job done with. It's a single file of C, and a single header file. @@ -34,8 +47,10 @@ First up, how do I build? Add cJSON.c to your project, and put cJSON.h somewhere in the header search path. For example, to build the test app: - gcc cJSON.c test.c -o test -lm - ./test +``` +gcc cJSON.c test.c -o test -lm +./test +``` As a library, cJSON exists to take away as much legwork as it can, but not get in your way. As a point of pragmatism (i.e. ignoring the truth), I'm going to say that you can use it @@ -45,56 +60,69 @@ I lifted some JSON from this page: http://www.json.org/fatfree.html That page inspired me to write cJSON, which is a parser that tries to share the same philosophy as JSON itself. Simple, dumb, out of the way. -Some JSON: ----------- +### Some JSON: - { - "name": "Jack (\"Bee\") Nimble", - "format": { - "type": "rect", - "width": 1920, - "height": 1080, - "interlace": false, - "frame rate": 24 - } +```json +{ + "name": "Jack (\"Bee\") Nimble", + "format": { + "type": "rect", + "width": 1920, + "height": 1080, + "interlace": false, + "frame rate": 24 } +} +``` Assume that you got this from a file, a webserver, or magic JSON elves, whatever, you have a char * to it. Everything is a cJSON struct. Get it parsed: - cJSON * root = cJSON_Parse(my_json_string); +```c +cJSON * root = cJSON_Parse(my_json_string); +``` This is an object. We're in C. We don't have objects. But we do have structs. What's the framerate? - cJSON * format = cJSON_GetObjectItem(root,"format"); - int framerate = cJSON_GetObjectItem(format,"frame rate")->valueint; +```c +cJSON * format = cJSON_GetObjectItem(root,"format"); +int framerate = cJSON_GetObjectItem(format,"frame rate")->valueint; +``` Want to change the framerate? - cJSON_GetObjectItem(format,"frame rate")->valueint = 25; +```c +cJSON_GetObjectItem(format,"frame rate")->valueint = 25; +``` Back to disk? - char * rendered = cJSON_Print(root); +```c +char * rendered = cJSON_Print(root); +``` Finished? Delete the root (this takes care of everything else). - cJSON_Delete(root); +```c +cJSON_Delete(root); +``` That's AUTO mode. If you're going to use Auto mode, you really ought to check pointers before you dereference them. If you want to see how you'd build this struct in code? - cJSON *root,*fmt; - root = cJSON_CreateObject(); - cJSON_AddItemToObject(root, "name", cJSON_CreateString("Jack (\"Bee\") Nimble")); - cJSON_AddItemToObject(root, "format", fmt = cJSON_CreateObject()); - cJSON_AddStringToObject(fmt, "type", "rect"); - cJSON_AddNumberToObject(fmt, "width", 1920); - cJSON_AddNumberToObject(fmt, "height", 1080); - cJSON_AddFalseToObject (fmt, "interlace"); - cJSON_AddNumberToObject(fmt, "frame rate", 24); +```c +cJSON *root,*fmt; +root = cJSON_CreateObject(); +cJSON_AddItemToObject(root, "name", cJSON_CreateString("Jack (\"Bee\") Nimble")); +cJSON_AddItemToObject(root, "format", fmt = cJSON_CreateObject()); +cJSON_AddStringToObject(fmt, "type", "rect"); +cJSON_AddNumberToObject(fmt, "width", 1920); +cJSON_AddNumberToObject(fmt, "height", 1080); +cJSON_AddFalseToObject (fmt, "interlace"); +cJSON_AddNumberToObject(fmt, "frame rate", 24); +``` Hopefully we can agree that's not a lot of code? There's no overhead, no unnecessary setup. Look at test.c for a bunch of nice examples, mostly all ripped off the json.org site, and @@ -114,21 +142,22 @@ Sibling has type Number, name "height", value 1080, and a sibling: Sibling has type False, name "interlace", and a sibling: Sibling has type Number, name "frame rate", value 24 -Here's the structure: ---------------------- +# Here's the structure: - typedef struct cJSON { - struct cJSON *next,*prev; - struct cJSON *child; +```c +typedef struct cJSON { + struct cJSON *next,*prev; + struct cJSON *child; - int type; + int type; - char *valuestring; - int valueint; - double valuedouble; + char *valuestring; + int valueint; + double valuedouble; - char *string; - } cJSON; + char *string; +} cJSON; +``` By default all values are 0 unless set by virtue of being meaningful. @@ -152,23 +181,26 @@ the root object, and traverse the structure (which is, formally, an N-tree), and tokenise as you please. If you wanted to build a callback style parser, this is how you'd do it (just an example, since these things are very specific): - void parse_and_callback(cJSON *item,const char *prefix) - { - while (item) - { - char *newprefix = malloc(strlen(prefix) + strlen(item->name) + 2); - sprintf(newprefix,"%s/%s",prefix,item->name); - int dorecurse = callback(newprefix, item->type, item); - if (item->child && dorecurse) parse_and_callback(item->child, newprefix); - item = item->next; - free(newprefix); - } - } +```c +void parse_and_callback(cJSON *item,const char *prefix) +{ + while (item) + { + char *newprefix = malloc(strlen(prefix) + strlen(item->name) + 2); + sprintf(newprefix,"%s/%s",prefix,item->name); + int dorecurse = callback(newprefix, item->type, item); + if (item->child && dorecurse) parse_and_callback(item->child, newprefix); + item = item->next; + free(newprefix); + } +} +``` The prefix process will build you a separated list, to simplify your callback handling. The 'dorecurse' flag would let the callback decide to handle sub-arrays on it's own, or let you invoke it per-item. For the item above, your callback might look like this: +```c int callback(const char *name,int type,cJSON *item) { if (!strcmp(name,"name")) { /* populate name */ } @@ -179,33 +211,38 @@ let you invoke it per-item. For the item above, your callback might look like th else if (!strcmp(name,"format/frame rate") { /* 24 */ } return 1; } +``` Alternatively, you might like to parse iteratively. You'd use: - void parse_object(cJSON *item) - { - int i; - for (i = 0 ; i < cJSON_GetArraySize(item) ; i++) - { - cJSON * subitem = cJSON_GetArrayItem(item, i); - // handle subitem. - } - } +```c +void parse_object(cJSON *item) +{ + int i; + for (i = 0 ; i < cJSON_GetArraySize(item) ; i++) + { + cJSON * subitem = cJSON_GetArrayItem(item, i); + // handle subitem + } +} +``` Or, for PROPER manual mode: - void parse_object(cJSON * item) - { - cJSON *subitem = item->child; - while (subitem) - { - // handle subitem - if (subitem->child) parse_object(subitem->child); - - subitem = subitem->next; - } - } +```c +void parse_object(cJSON * item) +{ + cJSON *subitem = item->child; + while (subitem) + { + // handle subitem + if (subitem->child) parse_object(subitem->child); + + subitem = subitem->next; + } +} +``` Of course, this should look familiar, since this is just a stripped-down version of the callback-parser. @@ -218,22 +255,24 @@ You can, of course, hand your sub-objects to other functions to populate. Also, if you find a use for it, you can manually build the objects. For instance, suppose you wanted to build an array of objects? - cJSON * objects[24]; +```c +cJSON * objects[24]; - cJSON * Create_array_of_anything(cJSON ** items, int num) - { - int i; - cJSON * prev, * root = cJSON_CreateArray(); - for (i = 0 ; i < 24 ; i++) - { - if (!i) root->child = objects[i]; - else prev->next = objects[i], objects[i]->prev = prev; - prev = objects[i]; - } - return root; - } - -and simply: Create_array_of_anything(objects, 24); +cJSON * Create_array_of_anything(cJSON ** items, int num) +{ + int i; + cJSON * prev, * root = cJSON_CreateArray(); + for (i = 0 ; i < 24 ; i++) + { + if (!i) root->child = objects[i]; + else prev->next = objects[i], objects[i]->prev = prev; + prev = objects[i]; + } + return root; +} +``` + +and simply: `Create_array_of_anything(objects, 24);` cJSON doesn't make any assumptions about what order you create things in. You can attach the objects, as above, and later add children to each @@ -245,7 +284,6 @@ The test.c code shows how to handle a bunch of typical cases. If you uncomment the code, it'll load, parse and print a bunch of test files, also from json.org, which are more complex than I'd care to try and stash into a const char array[]. -Enjoy cJSON! ------------------------ +# Enjoy cJSON! - Dave Gamble, Aug 2009 From df1ffa1e0b15869f1b365852c90b79f5365951ba Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Tue, 15 Nov 2016 02:01:38 +0700 Subject: [PATCH 39/45] README: Building --- README.md | 65 +++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 56 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index d642b04..57f298a 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,7 @@ Ultralightweight JSON parser in ANSI C. * [License](#license) * [Usage](#usage) * [Welcome to cJSON](#welcome-to-cjson) + * [Building](#building) * [Some JSON](#some-json) * [Here's the structure](#heres-the-structure) * [Enjoy cJSON!](#enjoy-cjson) @@ -43,15 +44,6 @@ JSON is described best here: http://www.json.org/ It's like XML, but fat-free. You use it to move data around, store things, or just generally represent your program's state. -First up, how do I build? -Add cJSON.c to your project, and put cJSON.h somewhere in the header search path. -For example, to build the test app: - -``` -gcc cJSON.c test.c -o test -lm -./test -``` - As a library, cJSON exists to take away as much legwork as it can, but not get in your way. As a point of pragmatism (i.e. ignoring the truth), I'm going to say that you can use it in one of two modes: Auto and Manual. Let's have a quick run-through. @@ -60,6 +52,61 @@ I lifted some JSON from this page: http://www.json.org/fatfree.html That page inspired me to write cJSON, which is a parser that tries to share the same philosophy as JSON itself. Simple, dumb, out of the way. +### Building + +There are several ways to incorporate cJSON into your project. + +#### copying the source +Because the entire library is only one C file and one header file, you can just copy `cJSON.h` and `cJSON.c` to your projects source and start using it. + +cJSON is written in ANSI C (C89) in order to support as many platforms and compilers as possible. + +#### CMake +With CMake, cJSON supports a full blown build system. This way you get the most features. With CMake it is recommended to do an out of tree build, meaning the compiled files are put in a directory separate from the source files. So in order to build cJSON with CMake on a Unix platform, make a `build` directory and run CMake inside it. + +``` +mkdir build +cd build +cmake .. +``` + +This will create a Makefile and a bunch of other files. You can then compile it: + +``` +make +``` + +And install it with `make install` if you want. By default it installs the headers `/usr/local/include/cjson` and the libraries to `/usr/local/lib`. It also installs files for pkg-config to make it easier to detect and use an existing installation of CMake. And it installs CMake config files, that can be used by other CMake based projects to discover the library. + +You can change the build process with a list of different options that you can pass to CMake. Turn them on with `On` and off with `Off`: +* `-DENABLE_CJSON_TESTS=On`: Enable building the tests. (on by default) +* `-DENABLE_CJSON_UTILS=On`: Enable building cJSON_Utils. (off by default) +* `-DENABLE_TARGET_EXPORT=On`: Enable the export of CMake targets. Turn off if it makes problems. (on by default) +* `-DBUILD_SHARED_LIBS=On`: Build the shared libraries. (on by default) +* `-DCMAKE_INSTALL_PREFIX=/usr`: Set a prefix for the installation. + +If you are packaging cJSON for a distribution of Linux, you would probably take these steps for example: +``` +mkdir build +cd build +cmake .. -DENABLE_CJSON_UTILS=On -DENABLE_CJSON_TESTS=Off -DCMAKE_INSTALL_PREFIX=/usr +make +make DESTDIR=$pkgdir install +``` + +CMake supports a lot of different platforms, not only UNIX Makefiles, but only UNIX Makefiles have been tested. It works on GNU/Linux and has been confirmed to compile on some versions of macOS, FreeBSD, Cygwin, Solaris and OpenIndiana. + +#### Makefile +If you don't have CMake available, but still have make. You can use the makefile to build cJSON: + +Run this command in the directory with the source code and it will automatically compile static and shared libraries and a little test program. + +``` +make all +``` + +If you want, you can install the compiled library to your system using `make install`. By default it will install the headers in `/usr/local/include/cjson` and the libraries in `/usr/local/lib`. But you can change this behavior by setting the `PREFIX` and `DESTDIR` variables: `make PREFIX=/usr DESTDIR=temp install`. + ### Some JSON: ```json From a22da435782a72aa47b12ad06c3617d41bba082a Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Tue, 15 Nov 2016 02:02:14 +0700 Subject: [PATCH 40/45] README: More formatting --- README.md | 209 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 122 insertions(+), 87 deletions(-) diff --git a/README.md b/README.md index 57f298a..6353ce8 100644 --- a/README.md +++ b/README.md @@ -94,7 +94,7 @@ make make DESTDIR=$pkgdir install ``` -CMake supports a lot of different platforms, not only UNIX Makefiles, but only UNIX Makefiles have been tested. It works on GNU/Linux and has been confirmed to compile on some versions of macOS, FreeBSD, Cygwin, Solaris and OpenIndiana. +CMake supports a lot of different platforms, not only UNIX Makefiles, but only UNIX Makefiles have been tested. It works on GNU/Linux and has been confirmed to compile on some versions of macOS, Cygwin, Solaris and OpenIndiana. #### Makefile If you don't have CMake available, but still have make. You can use the makefile to build cJSON: @@ -123,7 +123,7 @@ If you want, you can install the compiled library to your system using `make ins ``` Assume that you got this from a file, a webserver, or magic JSON elves, whatever, -you have a char * to it. Everything is a cJSON struct. +you have a `char *` to it. Everything is a `cJSON` struct. Get it parsed: ```c @@ -134,20 +134,20 @@ This is an object. We're in C. We don't have objects. But we do have structs. What's the framerate? ```c -cJSON * format = cJSON_GetObjectItem(root,"format"); -int framerate = cJSON_GetObjectItem(format,"frame rate")->valueint; +cJSON *format = cJSON_GetObjectItem(root, "format"); +int framerate = cJSON_GetObjectItem(format, "frame rate")->valueint; ``` Want to change the framerate? ```c -cJSON_GetObjectItem(format,"frame rate")->valueint = 25; +cJSON_GetObjectItem(format, "frame rate")->valueint = 25; ``` Back to disk? ```c -char * rendered = cJSON_Print(root); +char *rendered = cJSON_Print(root); ``` Finished? Delete the root (this takes care of everything else). @@ -160,7 +160,8 @@ That's AUTO mode. If you're going to use Auto mode, you really ought to check po before you dereference them. If you want to see how you'd build this struct in code? ```c -cJSON *root,*fmt; +cJSON *root; +cJSON *fmt; root = cJSON_CreateObject(); cJSON_AddItemToObject(root, "name", cJSON_CreateString("Jack (\"Bee\") Nimble")); cJSON_AddItemToObject(root, "format", fmt = cJSON_CreateObject()); @@ -172,92 +173,113 @@ cJSON_AddNumberToObject(fmt, "frame rate", 24); ``` Hopefully we can agree that's not a lot of code? There's no overhead, no unnecessary setup. -Look at test.c for a bunch of nice examples, mostly all ripped off the json.org site, and +Look at `test.c` for a bunch of nice examples, mostly all ripped off the [json.org](http://json.org) site, and a few from elsewhere. What about manual mode? First up you need some detail. -Let's cover how the cJSON objects represent the JSON data. +Let's cover how the `cJSON` objects represent the JSON data. cJSON doesn't distinguish arrays from objects in handling; just type. -Each cJSON has, potentially, a child, siblings, value, a name. +Each `cJSON` has, potentially, a child, siblings, value, a name. -The root object has: Object Type and a Child -The Child has name "name", with value "Jack ("Bee") Nimble", and a sibling: -Sibling has type Object, name "format", and a child. -That child has type String, name "type", value "rect", and a sibling: -Sibling has type Number, name "width", value 1920, and a sibling: -Sibling has type Number, name "height", value 1080, and a sibling: -Sibling has type False, name "interlace", and a sibling: -Sibling has type Number, name "frame rate", value 24 +* The `root` object has: *Object* Type and a Child +* The Child has name "name", with value "Jack ("Bee") Nimble", and a sibling: +* Sibling has type *Object*, name "format", and a child. +* That child has type *String*, name "type", value "rect", and a sibling: +* Sibling has type *Number*, name "width", value 1920, and a sibling: +* Sibling has type *Number*, name "height", value 1080, and a sibling: +* Sibling has type *False*, name "interlace", and a sibling: +* Sibling has type *Number*, name "frame rate", value 24 -# Here's the structure: +### Here's the structure: ```c typedef struct cJSON { - struct cJSON *next,*prev; - struct cJSON *child; + struct cJSON *next,*prev; + struct cJSON *child; - int type; + int type; - char *valuestring; - int valueint; - double valuedouble; + char *valuestring; + int valueint; + double valuedouble; - char *string; + char *string; } cJSON; ``` By default all values are 0 unless set by virtue of being meaningful. -next/prev is a doubly linked list of siblings. next takes you to your sibling, -prev takes you back from your sibling to you. -Only objects and arrays have a "child", and it's the head of the doubly linked list. -A "child" entry will have prev==0, but next potentially points on. The last sibling has next=0. -The type expresses Null/True/False/Number/String/Array/Object, all of which are #defined in -cJSON.h +`next`/`prev` is a doubly linked list of siblings. `next` takes you to your sibling, +`prev` takes you back from your sibling to you. +Only objects and arrays have a `child`, and it's the head of the doubly linked list. +A `child` entry will have `prev == 0`, but next potentially points on. The last sibling has `next == 0`. +The type expresses *Null*/*True*/*False*/*Number*/*String*/*Array*/*Object*, all of which are `#defined` in +`cJSON.h`. -A Number has valueint and valuedouble. If you're expecting an int, read valueint, if not read -valuedouble. +A *Number* has `valueint` and `valuedouble`. If you're expecting an `int`, read `valueint`, if not read +`valuedouble`. -Any entry which is in the linked list which is the child of an object will have a "string" -which is the "name" of the entry. When I said "name" in the above example, that's "string". -"string" is the JSON name for the 'variable name' if you will. +Any entry which is in the linked list which is the child of an object will have a `string` +which is the "name" of the entry. When I said "name" in the above example, that's `string`. +`string` is the JSON name for the 'variable name' if you will. Now you can trivially walk the lists, recursively, and parse as you please. -You can invoke cJSON_Parse to get cJSON to parse for you, and then you can take +You can invoke `cJSON_Parse` to get cJSON to parse for you, and then you can take the root object, and traverse the structure (which is, formally, an N-tree), and tokenise as you please. If you wanted to build a callback style parser, this is how you'd do it (just an example, since these things are very specific): ```c -void parse_and_callback(cJSON *item,const char *prefix) +void parse_and_callback(cJSON *item, const char *prefix) { - while (item) - { - char *newprefix = malloc(strlen(prefix) + strlen(item->name) + 2); - sprintf(newprefix,"%s/%s",prefix,item->name); - int dorecurse = callback(newprefix, item->type, item); - if (item->child && dorecurse) parse_and_callback(item->child, newprefix); - item = item->next; - free(newprefix); - } + while (item) + { + char *newprefix = malloc(strlen(prefix) + strlen(item->name) + 2); + sprintf(newprefix, "%s/%s", prefix, item->name); + int dorecurse = callback(newprefix, item->type, item); + if (item->child && dorecurse) + { + parse_and_callback(item->child, newprefix); + } + item = item->next; + free(newprefix); + } } ``` -The prefix process will build you a separated list, to simplify your callback handling. -The 'dorecurse' flag would let the callback decide to handle sub-arrays on it's own, or +The `prefix` process will build you a separated list, to simplify your callback handling. +The `dorecurse` flag would let the callback decide to handle sub-arrays on it's own, or let you invoke it per-item. For the item above, your callback might look like this: ```c - int callback(const char *name,int type,cJSON *item) +int callback(const char *name, int type, cJSON *item) +{ + if (!strcmp(name, "name")) { - if (!strcmp(name,"name")) { /* populate name */ } - else if (!strcmp(name,"format/type") { /* handle "rect" */ } - else if (!strcmp(name,"format/width") { /* 800 */ } - else if (!strcmp(name,"format/height") { /* 600 */ } - else if (!strcmp(name,"format/interlace") { /* false */ } - else if (!strcmp(name,"format/frame rate") { /* 24 */ } - return 1; + /* populate name */ } + else if (!strcmp(name, "format/type") + { + /* handle "rect" */ } + else if (!strcmp(name, "format/width") + { + /* 800 */ + } + else if (!strcmp(name, "format/height") + { + /* 600 */ + } + else if (!strcmp(name, "format/interlace") + { + /* false */ + } + else if (!strcmp(name, "format/frame rate") + { + /* 24 */ + } + + return 1; +} ``` Alternatively, you might like to parse iteratively. @@ -266,28 +288,31 @@ You'd use: ```c void parse_object(cJSON *item) { - int i; - for (i = 0 ; i < cJSON_GetArraySize(item) ; i++) - { - cJSON * subitem = cJSON_GetArrayItem(item, i); - // handle subitem - } + int i; + for (i = 0; i < cJSON_GetArraySize(item); i++) + { + cJSON *subitem = cJSON_GetArrayItem(item, i); + // handle subitem + } } ``` Or, for PROPER manual mode: ```c -void parse_object(cJSON * item) +void parse_object(cJSON *item) { - cJSON *subitem = item->child; - while (subitem) - { - // handle subitem - if (subitem->child) parse_object(subitem->child); + cJSON *subitem = item->child; + while (subitem) + { + // handle subitem + if (subitem->child) + { + parse_object(subitem->child); + } - subitem = subitem->next; - } + subitem = subitem->next; + } } ``` @@ -303,19 +328,29 @@ Also, if you find a use for it, you can manually build the objects. For instance, suppose you wanted to build an array of objects? ```c -cJSON * objects[24]; +cJSON *objects[24]; -cJSON * Create_array_of_anything(cJSON ** items, int num) +cJSON *Create_array_of_anything(cJSON **items, int num) { - int i; - cJSON * prev, * root = cJSON_CreateArray(); - for (i = 0 ; i < 24 ; i++) - { - if (!i) root->child = objects[i]; - else prev->next = objects[i], objects[i]->prev = prev; - prev = objects[i]; - } - return root; + int i; + cJSON *prev; + cJSON *root = cJSON_CreateArray(); + for (i = 0; i < 24; i++) + { + if (!i) + { + root->child = objects[i]; + } + else + { + prev->next = objects[i]; + objects[i]->prev = prev; + } + + prev = objects[i]; + } + + return root; } ``` @@ -325,11 +360,11 @@ cJSON doesn't make any assumptions about what order you create things in. You can attach the objects, as above, and later add children to each of those objects. -As soon as you call cJSON_Print, it renders the structure to text. +As soon as you call `cJSON_Print`, it renders the structure to text. -The test.c code shows how to handle a bunch of typical cases. If you uncomment -the code, it'll load, parse and print a bunch of test files, also from json.org, -which are more complex than I'd care to try and stash into a const char array[]. +The `test.c` code shows how to handle a bunch of typical cases. If you uncomment +the code, it'll load, parse and print a bunch of test files, also from [json.org](http://json.org), +which are more complex than I'd care to try and stash into a `const char array[]`. # Enjoy cJSON! From 706636fb20079b941facb732d0b093333a0744de Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Tue, 15 Nov 2016 19:57:20 +0700 Subject: [PATCH 41/45] Replace -ansi with -std=c89 for better compatibility --- CMakeLists.txt | 2 +- Makefile | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c36097a..2fb7276 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,7 +13,7 @@ set(CJSON_UTILS_VERSION_SO 1) set(PROJECT_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}") if(("${CMAKE_C_COMPILER_ID}" MATCHES "GNU") OR ("${CMAKE_C_COMPILER_ID}" MATCHES "Clang")) - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ansi -pedantic -Wall -Wextra -Werror -Wstrict-prototypes -Wwrite-strings") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c89 -pedantic -Wall -Wextra -Werror -Wstrict-prototypes -Wwrite-strings") endif() #variables for pkg-config diff --git a/Makefile b/Makefile index 35d7244..59328be 100644 --- a/Makefile +++ b/Makefile @@ -20,7 +20,7 @@ INSTALL_LIBRARY_PATH = $(DESTDIR)$(PREFIX)/$(LIBRARY_PATH) INSTALL ?= cp -a -R_CFLAGS = -fPIC -ansi -pedantic -Wall -Werror -Wstrict-prototypes -Wwrite-strings $(CFLAGS) +R_CFLAGS = -fPIC -std=c89 -pedantic -Wall -Werror -Wstrict-prototypes -Wwrite-strings $(CFLAGS) uname := $(shell sh -c 'uname -s 2>/dev/null || echo false') From 1f3b95e2b1ec59c040ceec310a362052bef42e1e Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Tue, 15 Nov 2016 20:19:02 +0700 Subject: [PATCH 42/45] README: Compiles on FreeBSD --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6353ce8..bfdcd43 100644 --- a/README.md +++ b/README.md @@ -94,7 +94,7 @@ make make DESTDIR=$pkgdir install ``` -CMake supports a lot of different platforms, not only UNIX Makefiles, but only UNIX Makefiles have been tested. It works on GNU/Linux and has been confirmed to compile on some versions of macOS, Cygwin, Solaris and OpenIndiana. +CMake supports a lot of different platforms, not only UNIX Makefiles, but only UNIX Makefiles have been tested. It works on GNU/Linux and has been confirmed to compile on some versions of macOS, Cygwin, FreeBSD, Solaris and OpenIndiana. #### Makefile If you don't have CMake available, but still have make. You can use the makefile to build cJSON: From 46a3a102d5ba8e66eaf65b3da091f773a3d4040d Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Tue, 15 Nov 2016 20:21:30 +0700 Subject: [PATCH 43/45] CMake: Fix indentation --- CMakeLists.txt | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2fb7276..dc6eb3c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,8 +43,8 @@ install(FILES cJSON.h DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/cjson") install (FILES "${CMAKE_CURRENT_BINARY_DIR}/libcjson.pc" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig") install(TARGETS "${CJSON_LIB}" DESTINATION "${CMAKE_INSTALL_LIBDIR}" EXPORT "${CJSON_LIB}") if(ENABLE_TARGET_EXPORT) - # export library information for CMake projects - install(EXPORT "${CJSON_LIB}" DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/cJSON") + # export library information for CMake projects + install(EXPORT "${CJSON_LIB}" DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/cJSON") endif() set_target_properties("${CJSON_LIB}" @@ -82,16 +82,16 @@ endif() # create the other package config files configure_file( - cJSONConfig.cmake.in - ${PROJECT_BINARY_DIR}/cJSONConfig.cmake @ONLY) + cJSONConfig.cmake.in + ${PROJECT_BINARY_DIR}/cJSONConfig.cmake @ONLY) configure_file( - cJSONConfigVersion.cmake.in - ${PROJECT_BINARY_DIR}/cJSONConfigVersion.cmake @ONLY) + cJSONConfigVersion.cmake.in + ${PROJECT_BINARY_DIR}/cJSONConfigVersion.cmake @ONLY) # Install package config files install(FILES ${PROJECT_BINARY_DIR}/cJSONConfig.cmake - ${PROJECT_BINARY_DIR}/cJSONConfigVersion.cmake - DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/cJSON") + ${PROJECT_BINARY_DIR}/cJSONConfigVersion.cmake + DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/cJSON") option(ENABLE_CJSON_TEST "Enable building cJSON test" ON) if(ENABLE_CJSON_TEST) From f2cb5e4dd496f688c96efca83f5f848914cacc6d Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Tue, 15 Nov 2016 20:21:57 +0700 Subject: [PATCH 44/45] CMake: ENABLE_CUSTOM_COMPILER_FLAGS option --- CMakeLists.txt | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index dc6eb3c..3689dee 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,8 +12,11 @@ set(CJSON_VERSION_SO 1) set(CJSON_UTILS_VERSION_SO 1) set(PROJECT_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}") -if(("${CMAKE_C_COMPILER_ID}" MATCHES "GNU") OR ("${CMAKE_C_COMPILER_ID}" MATCHES "Clang")) - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c89 -pedantic -Wall -Wextra -Werror -Wstrict-prototypes -Wwrite-strings") +option(ENABLE_CUSTOM_COMPILER_FLAGS ON) +if (ENABLE_CUSTOM_COMPILER_FLAGS) + if(("${CMAKE_C_COMPILER_ID}" MATCHES "GNU") OR ("${CMAKE_C_COMPILER_ID}" MATCHES "Clang")) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c89 -pedantic -Wall -Wextra -Werror -Wstrict-prototypes -Wwrite-strings") + endif() endif() #variables for pkg-config From 3a2151d2679c164de0e1f518e45f426473800572 Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Tue, 15 Nov 2016 20:23:47 +0700 Subject: [PATCH 45/45] README: Document ENABLE_CUSTOM_COMPILER_FLAGS --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index bfdcd43..b8dd4b9 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,7 @@ You can change the build process with a list of different options that you can p * `-DENABLE_CJSON_TESTS=On`: Enable building the tests. (on by default) * `-DENABLE_CJSON_UTILS=On`: Enable building cJSON_Utils. (off by default) * `-DENABLE_TARGET_EXPORT=On`: Enable the export of CMake targets. Turn off if it makes problems. (on by default) +* `-DENABLE_CUSTOM_COMPILER_FLAGS=On`: Enable custom compiler flags (currently for Clang and GCC). Turn off if it makes problems. (on by default) * `-DBUILD_SHARED_LIBS=On`: Build the shared libraries. (on by default) * `-DCMAKE_INSTALL_PREFIX=/usr`: Set a prefix for the installation.