4 1. [Overview and Build Status](#overview)
5 2. [Building on Unix](#buildunix)
6 * [Prerequisites](#installprereq)
7 * [Build commands](#buildcmds)
8 3. [CMake options](#CMake)
10 5. [Building with `vcpkg`](#buildvcpkg)
11 6. [Linking to libjson-c](#linking)
12 7. [Using json-c](#using)
14 JSON-C - A JSON implementation in C <a name="overview"></a>
15 -----------------------------------
18 * [AppVeyor Build](https://ci.appveyor.com/project/hawicz/json-c) ![AppVeyor Build Status](https://ci.appveyor.com/api/projects/status/github/json-c/json-c?branch=master&svg=true)
19 * [Travis Build](https://travis-ci.org/json-c/json-c) ![Travis Build Status](https://travis-ci.org/json-c/json-c.svg?branch=master)
22 * [Coveralls](https://coveralls.io/github/json-c/json-c?branch=master) [![Coverage Status](https://coveralls.io/repos/github/json-c/json-c/badge.svg?branch=master)](https://coveralls.io/github/json-c/json-c?branch=master)
24 JSON-C implements a reference counting object model that allows you to easily
25 construct JSON objects in C, output them as JSON formatted strings and parse
26 JSON formatted strings back into the C representation of JSON objects.
27 It aims to conform to [RFC 7159](https://tools.ietf.org/html/rfc7159).
29 Building on Unix with `git`, `gcc` and `cmake` <a name="buildunix"></a>
30 --------------------------------------------------
32 Home page for json-c: https://github.com/json-c/json-c/wiki
34 ### Prerequisites: <a name="installprereq"></a>
36 - `gcc`, `clang`, or another C compiler
38 - cmake>=2.8, >=3.16 recommended
40 To generate docs you'll also need:
43 If you are on a relatively modern system, you'll likely be able to install
44 the prerequisites using your OS's packaging system.
46 ### Install using apt (e.g. Ubuntu 16.04.2 LTS)
49 sudo apt install cmake
50 sudo apt install doxygen # optional
51 sudo apt install valgrind # optional
54 ### Build instructions: <a name="buildcmds"></a>
56 `json-c` GitHub repo: https://github.com/json-c/json-c
59 $ git clone https://github.com/json-c/json-c.git
62 $ cmake ../json-c # See CMake section below for custom arguments
65 Note: it's also possible to put your build directory inside the json-c
66 source directory, or even not use a separate build directory at all, but
67 certain things might not work quite right (notably, `make distcheck`)
74 $ make USE_VALGRIND=0 test # optionally skip using valgrind
79 ### Generating documentation with Doxygen:
81 The libray documentation can be generated directly from the source codes using Doxygen tool:
86 google-chrome doc/html/index.html
90 CMake Options <a name="CMake"></a>
93 The json-c library is built with [CMake](https://cmake.org/cmake-tutorial/),
94 which can take a few options.
96 Variable | Type | Description
97 ---------------------|--------|--------------
98 CMAKE_INSTALL_PREFIX | String | The install location.
99 CMAKE_BUILD_TYPE | String | Defaults to "debug"
100 BUILD_SHARED_LIBS | Bool | The default build generates a dynamic (dll/so) library. Set this to OFF to create a static library instead.
101 ENABLE_RDRAND | Bool | Enable RDRAND Hardware RNG Hash Seed
102 ENABLE_THREADING | Bool | Enable partial threading support
103 DISABLE_WERROR | Bool | Disable use of -Werror
104 DISABLE_BSYMBOLIC | Bool | Disable use of -Bsymbolic-functions
106 Pass these options as `-D` on CMake's command-line.
109 cmake -DBUILD_SHARED_LIBS=OFF ...
112 ### Building with partial threading support
114 Although json-c does not support fully multi-threaded access to
115 object trees, it has some code to help make its use in threaded programs
116 a bit safer. Currently, this is limited to using atomic operations for
117 json_object_get() and json_object_put().
119 Since this may have a performance impact, of at least 3x slower
120 according to https://stackoverflow.com/a/11609063, it is disabled by
121 default. You may turn it on by adjusting your cmake command with:
122 -DENABLE_THREADING=ON
124 Separately, the default hash function used for object field keys,
125 lh_char_hash, uses a compare-and-swap operation to ensure the random
126 seed is only generated once. Because this is a one-time operation, it
127 is always compiled in when the compare-and-swap operation is available.
130 ### cmake-configure wrapper script
132 For those familiar with the old autoconf/autogen.sh/configure method,
133 there is a `cmake-configure` wrapper script to ease the transition to cmake.
138 ../cmake-configure --prefix=/some/install/path
142 cmake-configure can take a few options.
144 | options | Description|
146 | prefix=PREFIX | install architecture-independent files in PREFIX |
147 | enable-threading | Enable code to support partly multi-threaded use |
148 | enable-rdrand | Enable RDRAND Hardware RNG Hash Seed generation on supported x86/x64 platforms. |
149 | enable-shared | build shared libraries [default=yes] |
150 | enable-static | build static libraries [default=yes] |
151 | disable-Bsymbolic | Avoid linking with -Bsymbolic-function |
152 | disable-werror | Avoid treating compiler warnings as fatal errors |
155 Testing: <a name="testing"></a>
158 By default, if valgrind is available running tests uses it.
159 That can slow the tests down considerably, so to disable it use:
161 export USE_VALGRIND=0
164 To run tests a separate build directory is recommended:
168 # VALGRIND=1 causes -DVALGRIND=1 to be passed when compiling code
169 # which uses slightly slower, but valgrind-safe code.
174 # By default, if valgrind is available running tests uses it.
175 make USE_VALGRIND=0 test # optionally skip using valgrind
178 If a test fails, check `Testing/Temporary/LastTest.log`,
179 `tests/testSubDir/${testname}/${testname}.vg.out`, and other similar files.
180 If there is insufficient output try:
186 JSONC_TEST_TRACE=1 make test
188 and check the log files again.
191 Building on Unix and Windows with `vcpkg` <a name="buildvcpkg"></a>
192 --------------------------------------------------
194 You can download and install JSON-C using the [vcpkg](https://github.com/Microsoft/vcpkg/) dependency manager:
196 git clone https://github.com/Microsoft/vcpkg.git
199 ./vcpkg integrate install
202 The JSON-C port in vcpkg is kept up to date by Microsoft team members and community contributors. If the version is out of date, please [create an issue or pull request](https://github.com/Microsoft/vcpkg) on the vcpkg repository.
205 Linking to `libjson-c` <a name="linking">
206 ----------------------
208 If your system has `pkgconfig`,
209 then you can just add this to your `makefile`:
212 CFLAGS += $(shell pkg-config --cflags json-c)
213 LDFLAGS += $(shell pkg-config --libs json-c)
216 Without `pkgconfig`, you would do something like this:
219 JSON_C_DIR=/path/to/json_c/install
220 CFLAGS += -I$(JSON_C_DIR)/include/json-c
221 LDFLAGS+= -L$(JSON_C_DIR)/lib -ljson-c
225 Using json-c <a name="using">
228 To use json-c you can either include json.h, or preferrably, one of the
229 following more specific header files:
231 * json_object.h - Core types and methods.
232 * json_tokener.h - Methods for parsing and serializing json-c object trees.
233 * json_pointer.h - JSON Pointer (RFC 6901) implementation for retrieving
234 objects from a json-c object tree.
235 * json_object_iterator.h - Methods for iterating over single json_object instances.
236 * json_visit.h - Methods for walking a tree of json-c objects.
237 * json_util.h - Miscelleanous utility functions.
239 For a full list of headers see [files.html](http://json-c.github.io/json-c/json-c-0.13.1/doc/html/files.html)