1
0
Fork 0
mirror of https://github.com/json-c/json-c.git synced 2025-12-05 22:38:37 +00:00
https://github.com/json-c/json-c is the official code repository for json-c. See the wiki for release tarballs for download. API docs at http://json-c.github.io/json-c/ https://github.com/json-c/json-c/wiki
Find a file
2025-12-01 06:37:11 -05:00
.github/ISSUE_TEMPLATE Update issue templates 2022-04-04 17:10:31 -04:00
apps apps: use CMake check for getopt.h 2025-11-29 01:32:10 -06:00
bench Fix typos 2022-03-06 16:07:36 +01:00
cmake Do not use duplocale if building for libnix because it isnt supported yet 2024-12-24 10:09:50 +08:00
doc Fix typos found by codespell 2022-08-13 15:47:40 +03:00
fuzz Add fuzz tests for json_object/point/array apis 2024-11-22 07:43:32 +01:00
tests bug: Fixing runtime issue with test_util_file.c in Windows 2025-10-20 12:10:57 -06:00
.clang-format Add AfterCaseLabel to .clang-format 2021-07-04 18:28:21 -08:00
.editorconfig Adjusted URLs 2022-03-19 10:34:55 +01:00
.gitignore Clean up CMakeLists.txt and remove Amiga specific locale.h guard 2024-12-24 10:09:50 +08:00
.travis.yml Trim a few travis builds, update others to more recent toolchains. 2022-07-24 20:37:14 +00:00
abi-check.sh Update abi-check.sh to compare 0.16 and 0.18 2024-09-15 16:10:57 +00:00
Android.configure.mk Issue #369: note some useful settings when building for Android. 2017-10-19 23:30:51 -04:00
appveyor.yml Put the most recent image first in appveyor builds. 2021-11-29 18:21:05 +00:00
arraylist.c json_object: introduce json_object_array_insert_idx() API function 2023-07-31 22:17:30 -04:00
arraylist.h json_object: introduce json_object_array_insert_idx() API function 2023-07-31 22:17:30 -04:00
AUTHORS Update ChangeLog, AUTHORS and issues_closed_for_0.18.md for an upcoming 0.18 release. 2024-09-15 11:54:50 -04:00
ChangeLog Correct year in changelog for 0.18 release 2024-10-10 23:47:55 +00:00
cmake-configure Add --disable-static and --disable-dynamic options to the cmake-configure script. 2022-05-30 14:31:08 +00:00
CMakeLists.txt bug: Fixing Cmake build when using clang-cl 2025-10-20 11:39:04 -06:00
COPYING Add my copyright. 2012-07-29 20:02:00 -05:00
debug.c clang-format the files 2020-04-03 11:39:30 +08:00
debug.h Issue #709: adjust some include guards to be a bit more json-c specific. 2021-06-02 23:53:23 +00:00
INSTALL Remove multiple trailing newlines at EOF. 2020-04-11 10:35:42 +02:00
issues_closed_for_0.13.md Fix a number of things with the generated docs, including translating triple-backtick code blocks into a form doxygen understands. 2020-07-17 03:46:21 +00:00
issues_closed_for_0.14.md Fix a number of things with the generated docs, including translating triple-backtick code blocks into a form doxygen understands. 2020-07-17 03:46:21 +00:00
issues_closed_for_0.15.md Update AUTHORS, add issues_closed_for_0.15.md, tweak the release checklist slightly. 2020-07-24 03:17:13 +00:00
issues_closed_for_0.16.md Update the ChangeLog with recent changes, in preparation for a 0.16 release. 2022-04-14 01:05:38 +00:00
issues_closed_for_0.17.md Update the AUTHORS file and add issues_closed_for_0.17.md for the upcoming 0.17 release 2023-08-12 13:53:27 -04:00
issues_closed_for_0.18.md Update ChangeLog, AUTHORS and issues_closed_for_0.18.md for an upcoming 0.18 release. 2024-09-15 11:54:50 -04:00
json-c.pc.in Improve pkgconfig setting 2018-07-25 15:51:02 +02:00
json-c.sym Update the master branch to version 0.18.99 2024-09-15 13:06:06 -04:00
json.h.cmakein json_patch: add first implementation only with patch application 2023-07-31 22:18:01 -04:00
json_c_version.c clang-format the files 2020-04-03 11:39:30 +08:00
json_c_version.h Update the master branch to version 0.18.99 2024-09-15 13:06:06 -04:00
json_config.h.in Include json_inttypes.h in json_object.h since we use types like int32_t in the API and some systems need that header to compile. 2012-02-15 20:47:11 -06:00
json_config.h.win32 Updated config for vs2010/winsdk71 as well as vs2013 2015-03-04 10:45:33 +00:00
json_inttypes.h PR #679: add workaround for old compilers w/o stdint.h (i.e. VS2008 and earlier) 2023-08-05 22:11:30 -04:00
json_object.c Set error codes for truncating int conversions 2024-11-27 07:17:20 +01:00
json_object.h Set error codes for truncating int conversions 2024-11-27 07:17:20 +01:00
json_object_iterator.c Add linkhash accessor functions (lh_table_head(), lh_entry_next(), etc...) to pave the way for making the lh_table and lh_entry structure opaque in the future. 2021-11-30 03:27:55 +00:00
json_object_iterator.h Add includes and split off json_types.h to help ensure that headers can be included in any order. 2020-04-06 13:55:27 +00:00
json_object_private.h Create a json_pointer_private.h and move a few things there, fix test warnings, note array_list_insert_idx is private. 2023-07-31 22:18:03 -04:00
json_patch.c Fix json_patch_apply handling of removing the whole document (i.e. "path":""). 2023-07-31 22:18:04 -04:00
json_patch.h Adjust the behavior of the args passed to json_patch_apply to make it easier to do in place modifications, and add a struct json_patch_error to report more details on failures. 2023-07-31 22:18:04 -04:00
json_pointer.c Issue #842 - fix one particular sign conversion warning. 2023-11-30 13:59:13 +00:00
json_pointer.h json_pointer: fix comments about printf() variants of set/get() 2023-07-31 22:17:30 -04:00
json_pointer_private.h Fix an uninitialized memory access in json_pointer. 2023-07-31 22:18:03 -04:00
json_tokener.c Fix code and update tests 2025-07-30 17:40:56 -07:00
json_tokener.h Issue #881: don't allow json_tokener_new_ex() with a depth < 1 2024-11-17 22:11:24 -05:00
json_types.h To avoid target exe file export JSON functions. 2021-03-02 14:27:40 +08:00
json_util.c fix macro WIN32_LEAN_AND_MEAN redefined 2025-01-06 10:56:53 +08:00
json_util.h Apply same EINVAL handling to json_parse_uint64() as was done for json_parse_int64(). Document that overflow/underflow for these functions is not an error, but sets errno=ERANGE. 2022-10-30 03:25:32 +00:00
json_visit.c clang-format the files 2020-04-03 11:39:30 +08:00
json_visit.h Improved support for IBM operating systems 2020-05-14 15:39:35 +01:00
libjson.c clang-format the files 2020-04-03 11:39:30 +08:00
linkhash.c fix macro WIN32_LEAN_AND_MEAN redefined 2025-01-06 10:56:53 +08:00
linkhash.h fix linkhash breaking -std=c89 2024-10-07 08:16:42 +00:00
math_compat.h Revert part of PR#606 and use isnan/isinf again, but provide macro implementations of those in math_compat.h is needed, as it seems to be on AIX and IBM i systems. 2020-05-16 01:29:18 +00:00
meson.build Fix typo 2025-12-01 11:37:59 +01:00
meson_options.txt Removing werror option as meson has a built-in option for this 2025-10-24 09:48:56 -06:00
NEWS Improving README 2016-08-08 15:25:31 +02:00
printbuf.c json_object_from_fd_ex: fail if file is too large 2022-03-20 13:17:37 +01:00
printbuf.h Adjusted URLs 2022-03-19 10:34:55 +01:00
random_seed.c rename WIN32 to _WIN32 2024-02-07 18:06:55 -08:00
random_seed.h Fix compiler warnings 2018-12-18 11:30:57 -06:00
README Improving README 2016-08-08 15:25:31 +02:00
README.html Adjusted URLs 2022-03-19 10:34:55 +01:00
README.md Add support for MorphOS 2024-12-24 10:09:50 +08:00
RELEASE_CHECKLIST.txt Update openssl command to work for newer versions of openssl. 2024-09-15 13:16:48 -04:00
snprintf_compat.h rename WIN32 to _WIN32 2024-02-07 18:06:55 -08:00
strdup_compat.h clang-format the files 2020-04-03 11:39:30 +08:00
strerror_override.c Properly format errnos in _json_c_strerror 2020-08-22 11:35:50 +02:00
strerror_override.h clang-format the files 2020-04-03 11:39:30 +08:00
STYLE.txt Remove multiple trailing newlines at EOF. 2020-04-11 10:35:42 +02:00
vasprintf_compat.h rename WIN32 to _WIN32 2024-02-07 18:06:55 -08:00

\mainpage

json-c

  1. Overview and Build Status
  2. Getting Help
  3. Building on Unix
  4. CMake options
  5. Testing
  6. Building with vcpkg
  7. Building for Android
  8. Building for Commodore Amiga or MorphOS
  9. Linking to libjson-c
  10. Using json-c

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 8259.

Skip down to Using json-c or check out the API docs, if you already have json-c installed and ready to use.

Home page for json-c: https://github.com/json-c/json-c/wiki

Getting Help

If you have questions about using json-c, please start a thread on our forums at: https://groups.google.com/forum/#!forum/json-c

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. For major new features or large changes of any kind, please first start a discussion on the forums.

Building on Unix with git, gcc and cmake

If you already have json-c installed, see Linking to libjson-c for how to build and link your program against it.

Build Status

Test Status

Prerequisites:

  • gcc, clang, or another C compiler

  • cmake>=2.8, >=3.16 recommended, cmake=>3.1 for tests

To generate docs you'll also need:

  • doxygen>=1.8.13

If you are on a relatively modern system, you'll likely be able to install the prerequisites using your OS's packaging system.

Install using apt (e.g. Ubuntu 16.04.2 LTS)

sudo apt install git
sudo apt install cmake
sudo apt install doxygen  # optional
sudo apt install valgrind # optional

Build instructions:

json-c GitHub repo: https://github.com/json-c/json-c

$ git clone https://github.com/json-c/json-c.git
$ mkdir json-c-build
$ cd json-c-build
$ cmake ../json-c   # See CMake section below for custom arguments

Note: it's also possible to put your build directory inside the json-c source directory, or even not use a separate build directory at all, but certain things might not work quite right (notably, make distcheck)

Then:

$ make
$ make test
$ make USE_VALGRIND=0 test   # optionally skip using valgrind
$ sudo make install          # it could be necessary to execute make install

Generating documentation with Doxygen:

The library documentation can be generated directly from the source code using Doxygen tool:

# in build directory
make doc
google-chrome doc/html/index.html

CMake Options

The json-c library is built with CMake, which can take a few options.

Variable Type Description
CMAKE_INSTALL_PREFIX String The install location.
CMAKE_BUILD_TYPE String Defaults to "debug".
BUILD_SHARED_LIBS Bool The default build generates a dynamic (dll/so) library. Set this to OFF to create a static library only.
BUILD_STATIC_LIBS Bool The default build generates a static (lib/a) library. Set this to OFF to create a shared library only.
DISABLE_STATIC_FPIC Bool The default builds position independent code. Set this to OFF to create a shared library only.
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.

Pass these options as -D on CMake's command-line.

# build a static library only
cmake -DBUILD_SHARED_LIBS=OFF ..

Building with partial threading support

Although json-c does not support fully multi-threaded access to object trees, it has some code to help make its use in threaded programs a bit safer. Currently, this is limited to using atomic operations for json_object_get() and json_object_put().

Since this may have a performance impact, of at least 3x slower according to https://stackoverflow.com/a/11609063, it is disabled by default. You may turn it on by adjusting your cmake command with: -DENABLE_THREADING=ON

Separately, the default hash function used for object field keys, lh_char_hash, uses a compare-and-swap operation to ensure the random seed is only generated once. Because this is a one-time operation, it is always compiled in when the compare-and-swap operation is available.

cmake-configure wrapper script

For those familiar with the old autoconf/autogen.sh/configure method, there is a cmake-configure wrapper script to ease the transition to cmake.

mkdir build
cd build
../cmake-configure --prefix=/some/install/path
make

cmake-configure can take a few options.

options Description
prefix=PREFIX install architecture-independent files in PREFIX
enable-threading Enable code to support partly multi-threaded use
enable-rdrand Enable RDRAND Hardware RNG Hash Seed generation on supported x86/x64 platforms.
enable-shared build shared libraries [default=yes]
enable-static build static libraries [default=yes]
disable-Bsymbolic Avoid linking with -Bsymbolic-function
disable-werror Avoid treating compiler warnings as fatal errors

Testing:

By default, if valgrind is available running tests uses it. That can slow the tests down considerably, so to disable it use:

export USE_VALGRIND=0

To run tests a separate build directory is recommended:

mkdir build-test
cd build-test
# VALGRIND=1 causes -DVALGRIND=1 to be passed when compiling code
# which uses slightly slower, but valgrind-safe code.
VALGRIND=1 cmake ..
make

make test
# By default, if valgrind is available running tests uses it.
make USE_VALGRIND=0 test   # optionally skip using valgrind

If a test fails, check Testing/Temporary/LastTest.log, tests/testSubDir/${testname}/${testname}.vg.out, and other similar files. If there is insufficient output try:

VERBOSE=1 CTEST_OUTPUT_ON_FAILURE=1 make test

or

JSONC_TEST_TRACE=1 make test

and check the log files again.

Building on Unix and Windows with vcpkg

You can download and install JSON-C using the vcpkg dependency manager:

git clone https://github.com/Microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.sh
./vcpkg integrate install
vcpkg install json-c

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 on the vcpkg repository.

Building for Android

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

Building for Commodore Amiga or MorphOS

Building for Commodore Amiga is supported for both Motorola 68k (AmigaOS 3) and PowerPC (AmigaOS 4) architectures. MorphOS on compatible PowerPC hardware is also supported. You can set up a cross compiler locally, however it is much easier to use the already preconfigured Amiga development environment wtthin a Docker container.

Install Docker on your machine if you don't already have it. You can download Docker Desktop for Windows/macOS/Linux here.

To build for Motorola 68k Amiga:

mkdir build
docker run --rm \
    -v ${PWD}:/work \
    -e USER=$( id -u ) -e GROUP=$( id -g ) \
    -it  sacredbanana/amiga-compiler:m68k-amigaos bash
cd build
cmake -DM68K_CRT=newlib ..
make

libjson-c.a will get created in the build directory.

You can change newlib to nix20, nix13, ixemul or clib2 if you would like to build the library suited for libnix or clib2 instead. Newlib is default.

To build for PowerPC Amiga:

mkdir build
docker run --rm \
    -v ${PWD}:/work \
    -e USER=$( id -u ) -e GROUP=$( id -g ) \
    -it  sacredbanana/amiga-compiler:ppc-amigaos bash
cd build
cmake ..
make

libjson-c.a will get created in the build directory.

To build for PowerPC MorphOS:

mkdir build
docker run --rm \
    -v ${PWD}:/work \
    -e USER=$( id -u ) -e GROUP=$( id -g ) \
    -it  sacredbanana/amiga-compiler:ppc-morphos bash
cd build
cmake -DNOIXEMUL=1 ..
make

If you are making an application that absolutely requires ixemul, then remove the -DNOIXEMUL=1.

libjson-c.a will get created in the build directory.

Linking to libjson-c

If your system has pkgconfig, then you can just add this to your makefile:

CFLAGS += $(shell pkg-config --cflags json-c)
LDFLAGS += $(shell pkg-config --libs json-c)

Without pkgconfig, you might do something like this:

JSON_C_DIR=/path/to/json_c/install
CFLAGS += -I$(JSON_C_DIR)/include/json-c
# Or to use lines like: #include <json-c/json_object.h>
#CFLAGS += -I$(JSON_C_DIR)/include
LDFLAGS+= -L$(JSON_C_DIR)/lib -ljson-c

If your project uses cmake:

  • Add to your CMakeLists.txt file:
find_package(json-c CONFIG)
target_link_libraries(${PROJECT_NAME} PRIVATE json-c::json-c)
  • Then you might run in your project:
cd build
cmake -DCMAKE_PREFIX_PATH=/path/to/json_c/install/lib64/cmake ..

Using json-c

To use json-c you can either include json.h, or preferably, one of the following more specific header files:

  • json_object.h - Core types and methods.
  • json_tokener.h - Methods for parsing and serializing json-c object trees.
  • json_pointer.h - JSON Pointer (RFC 6901) implementation for retrieving objects from a json-c object tree.
  • json_object_iterator.h - Methods for iterating over single json_object instances. (See also json_object_object_foreach() in json_object.h)
  • json_visit.h - Methods for walking a tree of json-c objects.
  • json_util.h - Miscellaneous utility functions.

For a full list of headers see files.html

The primary type in json-c is json_object. It describes a reference counted tree of json objects which are created by either parsing text with a json_tokener (i.e. json_tokener_parse_ex()), or by creating (with json_object_new_object(), json_object_new_int(), etc...) and adding (with json_object_object_add(), json_object_array_add(), etc...) them individually. Typically, every object in the tree will have one reference, from its parent. When you are done with the tree of objects, you call json_object_put() on just the root object to free it, which recurses down through any child objects calling json_object_put() on each one of those in turn.

You can get a reference to a single child (json_object_object_get() or json_object_array_get_idx()) and use that object as long as its parent is valid.
If you need a child object to live longer than its parent, you can increment the child's refcount (json_object_get()) to allow it to survive the parent being freed or it being removed from its parent (json_object_object_del() or json_object_array_del_idx())

When parsing text, the json_tokener object is independent from the json_object that it returns. It can be allocated (json_tokener_new()) used one or multiple times (json_tokener_parse_ex(), and freed (json_tokener_free()) while the json_object objects live on.

A json_object tree can be serialized back into a string with json_object_to_json_string_ext(). The string that is returned is only valid until the next "to_json_string" call on that same object. Also, it is freed when the json_object is freed.