1
0
Fork 0
mirror of https://github.com/json-c/json-c.git synced 2026-09-19 17:21:24 +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
  • C 87.2%
  • CMake 5.8%
  • Shell 3.3%
  • Meson 2.5%
  • C++ 0.9%
  • Other 0.3%
Find a file
Eric Hawicz 8381d33d78
Merge pull request #967 from hilbert149/exit_with_error
Fix json_parse:exit with error when input file cannot be opened
2026-09-07 07:56:02 -05:00
.github ci: set least-privilege GITHUB_TOKEN permissions for meson workflow 2026-06-27 13:52:31 +02:00
apps Fix json_parse:exit with error when input file cannot be opened 2026-09-07 15:15:32 +08: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 Fix swapped lseek() arguments 2026-09-07 09:58:09 +08: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 Drop osx from .travis.yml since macOS builds are no longer supported. 2026-05-24 16:15:19 -04:00
abi-check.sh Update ChangeLog, abi-check.sh, AUTHORS and issues_closed list for an upcoming 0.19 release. 2026-01-19 17:46:57 -05: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 Add lh_table_delete_entry_to_tail() and array_list_set_idx() functions. 2026-05-03 20:44:08 -04:00
arraylist.h Issue #923: Avoid stack recursion in json_object_put() so heavily nested object trees can be freed. 2026-05-03 20:44:28 -04:00
AUTHORS More updates to ChangeLog for a 0.19 release 2026-06-27 09:35:37 -04:00
ChangeLog More updates to ChangeLog for a 0.19 release 2026-06-27 09:35:37 -04: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 Update the master branch to version 0.19.99 2026-06-27 09:55:42 -04: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
issues_closed_for_0.19.md More updates to ChangeLog for a 0.19 release 2026-06-27 09:35:37 -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.19.99 2026-06-27 09:55:42 -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.19.99 2026-06-27 09:55:42 -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 Support MSVC in packages that use GNU Autoconf. 2026-01-09 23:51:43 +01:00
json_object.c json_object_array_to_json_string: Hoist array length out of the loop 2026-09-07 15:23:26 +08:00
json_object.h Issue #941: fix "defined but not used" errors when building with a c++ compiler. 2026-07-11 09:28:49 -04: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 Fix typo in json_object_iterator.h 2026-09-02 17:21:36 +08:00
json_object_private.h Issue #923: Avoid stack recursion in json_object_put() so heavily nested object trees can be freed. 2026-05-03 20:44:28 -04:00
json_patch.c require string type for patch op/path/from fields 2026-07-18 21:08:17 +05:30
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 decode ~1/~0 escapes when setting an object member in json_pointer 2026-08-22 12:01:01 +05:30
json_pointer.h general callback, safer API & related tests 2026-02-09 14:01:38 +08:00
json_pointer_private.h general callback, safer API & related tests 2026-02-09 14:01:38 +08:00
json_tokener.c Check the whole RFC 8259 number grammar in strict mode 2026-08-16 20:26:15 +03: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 reject negative after whitespace in json_parse_uint64 2026-08-17 20:37:45 +05:30
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
libjson.rc Add VERSIONINFO to libjson-c.dll 2026-01-18 20:23:50 +01:00
linkhash.c Issue #923: Avoid stack recursion in json_object_put() so heavily nested object trees can be freed. 2026-05-03 20:44:28 -04:00
linkhash.h Issue #941: fix "defined but not used" errors when building with a c++ compiler. 2026-07-11 09:28:49 -04: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 Merge the two meson checks for arc4random, instead have the has_function loop check first without, then with the dependencies. 2026-08-15 10:58:04 -04:00
meson_options.txt Merge the two meson checks for arc4random, instead have the has_function loop check first without, then with the dependencies. 2026-08-15 10:58:04 -04:00
NEWS Improving README 2016-08-08 15:25:31 +02:00
printbuf.c Address printbuf review comments 2026-08-09 20:01:12 -04:00
printbuf.h Address printbuf review comments 2026-08-09 20:01:12 -04:00
random_seed.c Issue #919: Fix build on non-x86 windows. Patch thanks to @dotanbrk 2026-02-19 18:48:05 -05: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 meson: fix version numbers 2026-07-24 07:37:41 +01: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 fix out-of-bounds read for negative errno in _json_c_strerror 2026-07-13 13:46:36 +05:30
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.