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 8.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. `json-c`
  2. ========
  3. 1. [Overview and Build Status](#overview)
  4. 2. [Building on Unix](#buildunix)
  5. * [Prerequisites](#installprereq)
  6. * [Build commands](#buildcmds)
  7. 3. [CMake options](#CMake)
  8. 4. [Testing](#testing)
  9. 5. [Building with `vcpkg`](#buildvcpkg)
  10. 6. [Linking to libjson-c](#linking)
  11. 7. [Using json-c](#using)
  12. JSON-C - A JSON implementation in C <a name="overview"></a>
  13. -----------------------------------
  14. Build Status
  15. * [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)
  16. * [Travis Build](https://travis-ci.org/json-c/json-c) ![Travis Build Status](https://travis-ci.org/json-c/json-c.svg?branch=master)
  17. Test Status
  18. * [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)
  19. JSON-C implements a reference counting object model that allows you to easily
  20. construct JSON objects in C, output them as JSON formatted strings and parse
  21. JSON formatted strings back into the C representation of JSON objects.
  22. It aims to conform to [RFC 7159](https://tools.ietf.org/html/rfc7159).
  23. Building on Unix with `git`, `gcc` and `cmake` <a name="buildunix"></a>
  24. --------------------------------------------------
  25. Home page for json-c: https://github.com/json-c/json-c/wiki
  26. ### Prerequisites: <a name="installprereq"></a>
  27. - `gcc`, `clang`, or another C compiler
  28. - cmake>=2.8, >=3.16 recommended
  29. To generate docs you'll also need:
  30. - `doxygen>=1.8.13`
  31. If you are on a relatively modern system, you'll likely be able to install
  32. the prerequisites using your OS's packaging system.
  33. ### Install using apt (e.g. Ubuntu 16.04.2 LTS)
  34. ```sh
  35. sudo apt install git
  36. sudo apt install cmake
  37. sudo apt install doxygen # optional
  38. sudo apt install valgrind # optional
  39. ```
  40. ### Build instructions: <a name="buildcmds"></a>
  41. `json-c` GitHub repo: https://github.com/json-c/json-c
  42. ```sh
  43. $ git clone https://github.com/json-c/json-c.git
  44. $ mkdir json-c-build
  45. $ cd json-c-build
  46. $ cmake ../json-c # See CMake section below for custom arguments
  47. ```
  48. Note: it's also possible to put your build directory inside the json-c
  49. source directory, or even not use a separate build directory at all, but
  50. certain things might not work quite right (notably, `make distcheck`)
  51. Then:
  52. ```sh
  53. $ make
  54. $ make test
  55. $ make USE_VALGRIND=0 test # optionally skip using valgrind
  56. $ make install
  57. ```
  58. ### Generating documentation with Doxygen:
  59. The libray documentation can be generated directly from the source codes using Doxygen tool:
  60. ```sh
  61. # in build directory
  62. make doc
  63. google-chrome doc/html/index.html
  64. ```
  65. CMake Options <a name="CMake"></a>
  66. --------------------
  67. The json-c library is built with [CMake](https://cmake.org/cmake-tutorial/),
  68. which can take a few options.
  69. Variable | Type | Description
  70. ---------------------|--------|--------------
  71. CMAKE_INSTALL_PREFIX | String | The install location.
  72. CMAKE_BUILD_TYPE | String | Defaults to "debug"
  73. BUILD_SHARED_LIBS | Bool | The default build generates a dynamic (dll/so) library. Set this to OFF to create a static library instead.
  74. BUILD_STATIC_LIBS | Bool | This build generates a static (lib/a) library.
  75. ENABLE_RDRAND | Bool | Enable RDRAND Hardware RNG Hash Seed
  76. ENABLE_THREADING | Bool | Enable partial threading support
  77. DISABLE_WERROR | Bool | Disable use of -Werror
  78. DISABLE_BSYMBOLIC | Bool | Disable use of -Bsymbolic-functions
  79. Pass these options as `-D` on CMake's command-line.
  80. ```sh
  81. # build a static library
  82. cmake -DBUILD_SHARED_LIBS=OFF ..
  83. ```
  84. Allow to build both static and shared libraries.
  85. ```sh
  86. cmake -DBUILD_STATIC_LIBS=ON ..
  87. ```
  88. ### Building with partial threading support
  89. Although json-c does not support fully multi-threaded access to
  90. object trees, it has some code to help make its use in threaded programs
  91. a bit safer. Currently, this is limited to using atomic operations for
  92. json_object_get() and json_object_put().
  93. Since this may have a performance impact, of at least 3x slower
  94. according to https://stackoverflow.com/a/11609063, it is disabled by
  95. default. You may turn it on by adjusting your cmake command with:
  96. -DENABLE_THREADING=ON
  97. Separately, the default hash function used for object field keys,
  98. lh_char_hash, uses a compare-and-swap operation to ensure the random
  99. seed is only generated once. Because this is a one-time operation, it
  100. is always compiled in when the compare-and-swap operation is available.
  101. ### cmake-configure wrapper script
  102. For those familiar with the old autoconf/autogen.sh/configure method,
  103. there is a `cmake-configure` wrapper script to ease the transition to cmake.
  104. ```sh
  105. mkdir build
  106. cd build
  107. ../cmake-configure --prefix=/some/install/path
  108. make
  109. ```
  110. cmake-configure can take a few options.
  111. | options | Description|
  112. | ---- | ---- |
  113. | prefix=PREFIX | install architecture-independent files in PREFIX |
  114. | enable-threading | Enable code to support partly multi-threaded use |
  115. | enable-rdrand | Enable RDRAND Hardware RNG Hash Seed generation on supported x86/x64 platforms. |
  116. | enable-shared | build shared libraries [default=yes] |
  117. | enable-static | build static libraries [default=yes] |
  118. | disable-Bsymbolic | Avoid linking with -Bsymbolic-function |
  119. | disable-werror | Avoid treating compiler warnings as fatal errors |
  120. Testing: <a name="testing"></a>
  121. ----------
  122. By default, if valgrind is available running tests uses it.
  123. That can slow the tests down considerably, so to disable it use:
  124. ```sh
  125. export USE_VALGRIND=0
  126. ```
  127. To run tests a separate build directory is recommended:
  128. ```sh
  129. mkdir build-test
  130. cd build-test
  131. # VALGRIND=1 causes -DVALGRIND=1 to be passed when compiling code
  132. # which uses slightly slower, but valgrind-safe code.
  133. VALGRIND=1 cmake ..
  134. make
  135. make test
  136. # By default, if valgrind is available running tests uses it.
  137. make USE_VALGRIND=0 test # optionally skip using valgrind
  138. ```
  139. If a test fails, check `Testing/Temporary/LastTest.log`,
  140. `tests/testSubDir/${testname}/${testname}.vg.out`, and other similar files.
  141. If there is insufficient output try:
  142. ```sh
  143. VERBOSE=1 make test
  144. ```
  145. or
  146. ```sh
  147. JSONC_TEST_TRACE=1 make test
  148. ```
  149. and check the log files again.
  150. Building on Unix and Windows with `vcpkg` <a name="buildvcpkg"></a>
  151. --------------------------------------------------
  152. You can download and install JSON-C using the [vcpkg](https://github.com/Microsoft/vcpkg/) dependency manager:
  153. git clone https://github.com/Microsoft/vcpkg.git
  154. cd vcpkg
  155. ./bootstrap-vcpkg.sh
  156. ./vcpkg integrate install
  157. vcpkg install json-c
  158. 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.
  159. Linking to `libjson-c` <a name="linking">
  160. ----------------------
  161. If your system has `pkgconfig`,
  162. then you can just add this to your `makefile`:
  163. ```make
  164. CFLAGS += $(shell pkg-config --cflags json-c)
  165. LDFLAGS += $(shell pkg-config --libs json-c)
  166. ```
  167. Without `pkgconfig`, you would do something like this:
  168. ```make
  169. JSON_C_DIR=/path/to/json_c/install
  170. CFLAGS += -I$(JSON_C_DIR)/include/json-c
  171. LDFLAGS+= -L$(JSON_C_DIR)/lib -ljson-c
  172. ```
  173. Using json-c <a name="using">
  174. ------------
  175. To use json-c you can either include json.h, or preferrably, one of the
  176. following more specific header files:
  177. * json_object.h - Core types and methods.
  178. * json_tokener.h - Methods for parsing and serializing json-c object trees.
  179. * json_pointer.h - JSON Pointer (RFC 6901) implementation for retrieving
  180. objects from a json-c object tree.
  181. * json_object_iterator.h - Methods for iterating over single json_object instances.
  182. * json_visit.h - Methods for walking a tree of json-c objects.
  183. * json_util.h - Miscelleanous utility functions.
  184. 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)