1\mainpage 2 3`json-c` 4======== 5 61. [Overview and Build Status](#overview) 72. [Building on Unix](#buildunix) 8 * [Prerequisites](#installprereq) 9 * [Build commands](#buildcmds) 103. [CMake options](#CMake) 114. [Testing](#testing) 125. [Building with `vcpkg`](#buildvcpkg) 136. [Linking to libjson-c](#linking) 147. [Using json-c](#using) 15 16JSON-C - A JSON implementation in C <a name="overview"></a> 17----------------------------------- 18 19Build Status 20* [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) 21* [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 23Test Status 24* [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) 25 26JSON-C implements a reference counting object model that allows you to easily 27construct JSON objects in C, output them as JSON formatted strings and parse 28JSON formatted strings back into the C representation of JSON objects. 29It aims to conform to [RFC 7159](https://tools.ietf.org/html/rfc7159). 30 31Building on Unix with `git`, `gcc` and `cmake` <a name="buildunix"></a> 32-------------------------------------------------- 33 34Home page for json-c: https://github.com/json-c/json-c/wiki 35 36### Prerequisites: <a name="installprereq"></a> 37 38 - `gcc`, `clang`, or another C compiler 39 40 - cmake>=2.8, >=3.16 recommended 41 42To generate docs you'll also need: 43 - `doxygen>=1.8.13` 44 45If you are on a relatively modern system, you'll likely be able to install 46the prerequisites using your OS's packaging system. 47 48### Install using apt (e.g. Ubuntu 16.04.2 LTS) 49```sh 50sudo apt install git 51sudo apt install cmake 52sudo apt install doxygen # optional 53sudo apt install valgrind # optional 54``` 55 56### Build instructions: <a name="buildcmds"></a> 57 58`json-c` GitHub repo: https://github.com/json-c/json-c 59 60```sh 61$ git clone https://github.com/json-c/json-c.git 62$ mkdir json-c-build 63$ cd json-c-build 64$ cmake ../json-c # See CMake section below for custom arguments 65``` 66 67Note: it's also possible to put your build directory inside the json-c 68source directory, or even not use a separate build directory at all, but 69certain things might not work quite right (notably, `make distcheck`) 70 71Then: 72 73```sh 74$ make 75$ make test 76$ make USE_VALGRIND=0 test # optionally skip using valgrind 77$ make install 78``` 79 80 81### Generating documentation with Doxygen: 82 83The libray documentation can be generated directly from the source codes using Doxygen tool: 84 85```sh 86# in build directory 87make doc 88google-chrome doc/html/index.html 89``` 90 91 92CMake Options <a name="CMake"></a> 93-------------------- 94 95The json-c library is built with [CMake](https://cmake.org/cmake-tutorial/), 96which can take a few options. 97 98Variable | Type | Description 99-----------------------------|--------|-------------- 100CMAKE_INSTALL_PREFIX | String | The install location. 101CMAKE_BUILD_TYPE | String | Defaults to "debug". 102BUILD_SHARED_LIBS | Bool | The default build generates a dynamic (dll/so) library. Set this to OFF to create a static library only. 103BUILD_STATIC_LIBS | Bool | The default build generates a static (lib/a) library. Set this to OFF to create a shared library only. 104DISABLE_STATIC_FPIC | Bool | The default builds position independent code. Set this to OFF to create a shared library only. 105DISABLE_BSYMBOLIC | Bool | Disable use of -Bsymbolic-functions. 106DISABLE_THREAD_LOCAL_STORAGE | Bool | Disable use of Thread-Local Storage (HAVE___THREAD). 107DISABLE_WERROR | Bool | Disable use of -Werror. 108ENABLE_RDRAND | Bool | Enable RDRAND Hardware RNG Hash Seed. 109ENABLE_THREADING | Bool | Enable partial threading support. 110OVERRIDE_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. 111 112Pass these options as `-D` on CMake's command-line. 113 114```sh 115# build a static library only 116cmake -DBUILD_SHARED_LIBS=OFF .. 117``` 118 119### Building with partial threading support 120 121Although json-c does not support fully multi-threaded access to 122object trees, it has some code to help make its use in threaded programs 123a bit safer. Currently, this is limited to using atomic operations for 124json_object_get() and json_object_put(). 125 126Since this may have a performance impact, of at least 3x slower 127according to https://stackoverflow.com/a/11609063, it is disabled by 128default. You may turn it on by adjusting your cmake command with: 129 -DENABLE_THREADING=ON 130 131Separately, the default hash function used for object field keys, 132lh_char_hash, uses a compare-and-swap operation to ensure the random 133seed is only generated once. Because this is a one-time operation, it 134is always compiled in when the compare-and-swap operation is available. 135 136 137### cmake-configure wrapper script 138 139For those familiar with the old autoconf/autogen.sh/configure method, 140there is a `cmake-configure` wrapper script to ease the transition to cmake. 141 142```sh 143mkdir build 144cd build 145../cmake-configure --prefix=/some/install/path 146make 147``` 148 149cmake-configure can take a few options. 150 151| options | Description| 152| ---- | ---- | 153| prefix=PREFIX | install architecture-independent files in PREFIX | 154| enable-threading | Enable code to support partly multi-threaded use | 155| enable-rdrand | Enable RDRAND Hardware RNG Hash Seed generation on supported x86/x64 platforms. | 156| enable-shared | build shared libraries [default=yes] | 157| enable-static | build static libraries [default=yes] | 158| disable-Bsymbolic | Avoid linking with -Bsymbolic-function | 159| disable-werror | Avoid treating compiler warnings as fatal errors | 160 161 162Testing: <a name="testing"></a> 163---------- 164 165By default, if valgrind is available running tests uses it. 166That can slow the tests down considerably, so to disable it use: 167```sh 168export USE_VALGRIND=0 169``` 170 171To run tests a separate build directory is recommended: 172```sh 173mkdir build-test 174cd build-test 175# VALGRIND=1 causes -DVALGRIND=1 to be passed when compiling code 176# which uses slightly slower, but valgrind-safe code. 177VALGRIND=1 cmake .. 178make 179 180make test 181# By default, if valgrind is available running tests uses it. 182make USE_VALGRIND=0 test # optionally skip using valgrind 183``` 184 185If a test fails, check `Testing/Temporary/LastTest.log`, 186`tests/testSubDir/${testname}/${testname}.vg.out`, and other similar files. 187If there is insufficient output try: 188```sh 189VERBOSE=1 make test 190``` 191or 192```sh 193JSONC_TEST_TRACE=1 make test 194``` 195and check the log files again. 196 197 198Building on Unix and Windows with `vcpkg` <a name="buildvcpkg"></a> 199-------------------------------------------------- 200 201You can download and install JSON-C using the [vcpkg](https://github.com/Microsoft/vcpkg/) dependency manager: 202 203 git clone https://github.com/Microsoft/vcpkg.git 204 cd vcpkg 205 ./bootstrap-vcpkg.sh 206 ./vcpkg integrate install 207 vcpkg install json-c 208 209The 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. 210 211 212Linking to `libjson-c` <a name="linking"> 213---------------------- 214 215If your system has `pkgconfig`, 216then you can just add this to your `makefile`: 217 218```make 219CFLAGS += $(shell pkg-config --cflags json-c) 220LDFLAGS += $(shell pkg-config --libs json-c) 221``` 222 223Without `pkgconfig`, you would do something like this: 224 225```make 226JSON_C_DIR=/path/to/json_c/install 227CFLAGS += -I$(JSON_C_DIR)/include/json-c 228LDFLAGS+= -L$(JSON_C_DIR)/lib -ljson-c 229``` 230 231 232Using json-c <a name="using"> 233------------ 234 235To use json-c you can either include json.h, or preferrably, one of the 236following more specific header files: 237 238* json_object.h - Core types and methods. 239* json_tokener.h - Methods for parsing and serializing json-c object trees. 240* json_pointer.h - JSON Pointer (RFC 6901) implementation for retrieving 241 objects from a json-c object tree. 242* json_object_iterator.h - Methods for iterating over single json_object instances. (See also `json_object_object_foreach()` in json_object.h) 243* json_visit.h - Methods for walking a tree of json-c objects. 244* json_util.h - Miscelleanous utility functions. 245 246For a full list of headers see [files.html](http://json-c.github.io/json-c/json-c-current-release/doc/html/files.html) 247 248The primary type in json-c is json_object. It describes a reference counted 249tree of json objects which are created by either parsing text with a 250json_tokener (i.e. `json_tokener_parse_ex()`), or by creating 251(with `json_object_new_object()`, `json_object_new_int()`, etc...) and adding 252(with `json_object_object_add()`, `json_object_array_add()`, etc...) them 253individually. 254Typically, every object in the tree will have one reference, from it's parent. 255When you are done with the tree of objects, you call json_object_put() on just 256the root object to free it, which recurses down through any child objects 257calling json_object_put() on each one of those in turn. 258 259You can get a reference to a single child 260(`json_object_object_get()` or `json_object_array_get_idx()`) 261and use that object as long as its parent is valid. 262If you need a child object to live longer than its parent, you can 263increment the child's refcount (`json_object_get()`) to allow it to survive 264the parent being freed or it being removed from its parent 265(`json_object_object_del()` or `json_object_array_del_idx()`) 266 267When parsing text, the json_tokener object is independent from the json_object 268that it returns. It can be allocated (`json_tokener_new()`) 269used ones or multiple times (`json_tokener_parse_ex()`, and 270freed (`json_tokener_free()`) while the json_object objects live on. 271 272A json_object tree can be serialized back into a string with 273`json_object_to_json_string_ext()`. The string that is returned 274is only valid until the next "to_json_string" call on that same object. 275Also, it is freed when the json_object is freed. 276 277