Imported Upstream version 0.14
[platform/upstream/json-c.git] / README.md
1 `json-c`
2 ========
3
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)
9 4. [Testing](#testing)
10 5. [Building with `vcpkg`](#buildvcpkg)
11 6. [Linking to libjson-c](#linking)
12 7. [Using json-c](#using)
13
14 JSON-C - A JSON implementation in C <a name="overview"></a>
15 -----------------------------------
16
17 Build Status
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)
20
21 Test Status
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)
23
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).
28
29 Building on Unix with `git`, `gcc` and `cmake` <a name="buildunix"></a>
30 --------------------------------------------------
31
32 Home page for json-c: https://github.com/json-c/json-c/wiki
33
34 ### Prerequisites: <a name="installprereq"></a>
35
36  - `gcc`, `clang`, or another C compiler
37
38  - cmake>=2.8, >=3.16 recommended
39
40 To generate docs you'll also need:
41  - `doxygen>=1.8.13`
42
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.
45
46 ### Install using apt (e.g. Ubuntu 16.04.2 LTS)
47 ```sh
48 sudo apt install git
49 sudo apt install cmake
50 sudo apt install doxygen  # optional
51 sudo apt install valgrind # optional
52 ```
53
54 ### Build instructions:  <a name="buildcmds"></a>
55
56 `json-c` GitHub repo: https://github.com/json-c/json-c
57
58 ```sh
59 $ git clone https://github.com/json-c/json-c.git
60 $ mkdir json-c-build
61 $ cd json-c-build
62 $ cmake ../json-c   # See CMake section below for custom arguments
63 ```
64
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`)
68
69 Then:
70
71 ```sh
72 $ make
73 $ make test
74 $ make USE_VALGRIND=0 test   # optionally skip using valgrind
75 $ make install
76 ```
77
78
79 ### Generating documentation with Doxygen:
80
81 The libray documentation can be generated directly from the source codes using Doxygen tool:
82
83 ```sh
84 # in build directory
85 make doc
86 google-chrome doc/html/index.html
87 ```
88
89
90 CMake Options <a name="CMake"></a>
91 --------------------
92
93 The json-c library is built with [CMake](https://cmake.org/cmake-tutorial/),
94 which can take a few options.
95
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
105
106 Pass these options as `-D` on CMake's command-line.
107
108 ```sh
109 cmake -DBUILD_SHARED_LIBS=OFF ...
110 ```
111
112 ### Building with partial threading support
113
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().
118
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
123
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.
128
129
130 ### cmake-configure wrapper script
131
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.
134
135 ```sh
136 mkdir build
137 cd build
138 ../cmake-configure --prefix=/some/install/path
139 make
140 ```
141
142 cmake-configure can take a few options.
143
144 | options | Description|
145 | ---- | ---- |
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 |
153
154
155 Testing:  <a name="testing"></a>
156 ----------
157
158 By default, if valgrind is available running tests uses it.
159 That can slow the tests down considerably, so to disable it use:
160 ```sh
161 export USE_VALGRIND=0
162 ```
163
164 To run tests a separate build directory is recommended:
165 ```sh
166 mkdir build-test
167 cd build-test
168 # VALGRIND=1 causes -DVALGRIND=1 to be passed when compiling code
169 # which uses slightly slower, but valgrind-safe code.
170 VALGRIND=1 cmake ..
171 make
172
173 make test
174 # By default, if valgrind is available running tests uses it.
175 make USE_VALGRIND=0 test   # optionally skip using valgrind
176 ```
177
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:
181 ```sh
182 VERBOSE=1 make test
183 ```
184 or
185 ```sh
186 JSONC_TEST_TRACE=1 make test
187 ```
188 and check the log files again.
189
190
191 Building on Unix and Windows with `vcpkg` <a name="buildvcpkg"></a>
192 --------------------------------------------------
193
194 You can download and install JSON-C using the [vcpkg](https://github.com/Microsoft/vcpkg/) dependency manager:
195
196     git clone https://github.com/Microsoft/vcpkg.git
197     cd vcpkg
198     ./bootstrap-vcpkg.sh
199     ./vcpkg integrate install
200     vcpkg install json-c
201
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.
203
204
205 Linking to `libjson-c` <a name="linking">
206 ----------------------
207
208 If your system has `pkgconfig`,
209 then you can just add this to your `makefile`:
210
211 ```make
212 CFLAGS += $(shell pkg-config --cflags json-c)
213 LDFLAGS += $(shell pkg-config --libs json-c)
214 ```
215
216 Without `pkgconfig`, you would do something like this:
217
218 ```make
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
222 ```
223
224
225 Using json-c <a name="using">
226 ------------
227
228 To use json-c you can either include json.h, or preferrably, one of the
229 following more specific header files:
230
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.
238
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)