• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/json-c/README.md

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

This page was automatically generated by LXR 0.3.1.  •  OpenWrt