You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 11 kB

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