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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. `json-c`
  2. ========
  3. 1. [Overview and Build Status](#overview)
  4. 2. [Building on Unix](#buildunix)
  5. 3. [Install Prerequisites](#installprereq)
  6. 4. [Building with partial threading support](#buildthreaded)
  7. 5. [Building with CMake](#CMake)
  8. 6. [Linking to libjson-c](#linking)
  9. 7. [Using json-c](#using)
  10. JSON-C - A JSON implementation in C <a name="overview"></a>
  11. -----------------------------------
  12. Build Status
  13. * [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)
  14. * [Travis Build](https://travis-ci.org/json-c/json-c) ![Travis Build Status](https://travis-ci.org/json-c/json-c.svg?branch=master)
  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. Building on Unix and Windows with `vcpkg`, `gcc`/`g++`, `curl`, `unzip`, and `tar`
  20. --------------------------------------------------
  21. You can download and install JSON-C using the [vcpkg](https://github.com/Microsoft/vcpkg/) dependency manager:
  22. git clone https://github.com/Microsoft/vcpkg.git
  23. cd vcpkg
  24. ./bootstrap-vcpkg.sh
  25. ./vcpkg integrate install
  26. vcpkg install json-c
  27. 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.
  28. Building on Unix with `git`, `gcc` and `autotools` <a name="buildunix"></a>
  29. --------------------------------------------------
  30. Home page for json-c: https://github.com/json-c/json-c/wiki
  31. ### Prerequisites:
  32. See also the "Installing prerequisites" section below.
  33. - `gcc`, `clang`, or another C compiler
  34. - `libtool>=2.2.6b`
  35. If you're not using a release tarball, you'll also need:
  36. - `autoconf>=2.64` (`autoreconf`)
  37. - `automake>=1.13`
  38. Make sure you have a complete `libtool` install, including `libtoolize`.
  39. To generate docs (e.g. as part of make distcheck) you'll also need:
  40. - `doxygen>=1.8.13`
  41. ### Build instructions:
  42. `json-c` GitHub repo: https://github.com/json-c/json-c
  43. ```sh
  44. $ git clone https://github.com/json-c/json-c.git
  45. $ cd json-c
  46. $ sh autogen.sh
  47. ```
  48. followed by
  49. ```sh
  50. $ ./configure # --enable-threading
  51. $ make
  52. $ make install
  53. ```
  54. To build and run the test programs:
  55. ```sh
  56. $ make check
  57. $ make USE_VALGRIND=0 check # optionally skip using valgrind
  58. ```
  59. Install prerequisites <a name="installprereq"></a>
  60. -----------------------
  61. If you are on a relatively modern system, you'll likely be able to install
  62. the prerequisites using your OS's packaging system.
  63. ### Install using apt (e.g. Ubuntu 16.04.2 LTS)
  64. ```sh
  65. sudo apt install git
  66. sudo apt install autoconf automake libtool
  67. sudo apt install valgrind # optional
  68. ```
  69. Then start from the "git clone" command, above.
  70. ### Manually install and build autoconf, automake and libtool
  71. For older OS's that don't have up-to-date versions of the packages will
  72. require a bit more work. For example, CentOS release 5.11, etc...
  73. ```sh
  74. curl -O http://ftp.gnu.org/gnu/autoconf/autoconf-2.69.tar.gz
  75. curl -O http://ftp.gnu.org/gnu/automake/automake-1.15.tar.gz
  76. curl -O http://ftp.gnu.org/gnu/libtool/libtool-2.2.6b.tar.gz
  77. tar xzf autoconf-2.69.tar.gz
  78. tar xzf automake-1.15.tar.gz
  79. tar xzf libtool-2.2.6b.tar.gz
  80. export PATH=${HOME}/ac_install/bin:$PATH
  81. (cd autoconf-2.69 && \
  82. ./configure --prefix ${HOME}/ac_install && \
  83. make && \
  84. make install)
  85. (cd automake-1.15 && \
  86. ./configure --prefix ${HOME}/ac_install && \
  87. make && \
  88. make install)
  89. (cd libtool-2.2.6b && \
  90. ./configure --prefix ${HOME}/ac_install && \
  91. make && \
  92. make install)
  93. ```
  94. Building with partial threading support <a name="buildthreaded"></a>
  95. ----------------------------------------
  96. Although json-c does not support fully multi-threaded access to
  97. object trees, it has some code to help make its use in threaded programs
  98. a bit safer. Currently, this is limited to using atomic operations for
  99. json_object_get() and json_object_put().
  100. Since this may have a performance impact, of at least 3x slower
  101. according to https://stackoverflow.com/a/11609063, it is disabled by
  102. default. You may turn it on by adjusting your configure command with:
  103. --enable-threading
  104. Separately, the default hash function used for object field keys,
  105. lh_char_hash, uses a compare-and-swap operation to ensure the random
  106. seed is only generated once. Because this is a one-time operation, it
  107. is always compiled in when the compare-and-swap operation is available.
  108. Building with CMake <a name="CMake"></a>
  109. --------------------
  110. To use [CMake](https://cmake.org/cmake-tutorial/), build it like:
  111. ```sh
  112. mkdir build
  113. cd build
  114. cmake ../
  115. make
  116. ```
  117. CMake can take a few options.
  118. Variable | Type | Description
  119. ---------------------|--------|--------------
  120. CMAKE_INSTALL_PREFIX | String | The install location.
  121. BUILD_SHARED_LIBS | Bool | The default build generates a dynamic (dll/so) library. Set this to OFF to create a static library instead.
  122. ENABLE_RDRAND | Bool | Enable RDRAND Hardware RNG Hash Seed
  123. ENABLE_THREADING | Bool | Enable partial threading support
  124. Pass these options as `-D` on CMake's command-line.
  125. ```sh
  126. cmake -DBUILD_SHARED_LIBS=OFF ...
  127. ```
  128. Testing with cmake:
  129. By default, if valgrind is available running tests uses it.
  130. That can slow the tests down considerably, so to disable it use:
  131. ```sh
  132. export USE_VALGRIND=0
  133. ```
  134. To run tests:
  135. ```sh
  136. mkdir build-test
  137. cd build-test
  138. # VALGRIND=1 causes -DVALGRIND=1 to be included when building
  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 make test
  150. ```
  151. or
  152. ```sh
  153. JSONC_TEST_TRACE=1 make test
  154. ```
  155. and check the log files again.
  156. Linking to `libjson-c` <a name="linking">
  157. ----------------------
  158. If your system has `pkgconfig`,
  159. then you can just add this to your `makefile`:
  160. ```make
  161. CFLAGS += $(shell pkg-config --cflags json-c)
  162. LDFLAGS += $(shell pkg-config --libs json-c)
  163. ```
  164. Without `pkgconfig`, you would do something like this:
  165. ```make
  166. JSON_C_DIR=/path/to/json_c/install
  167. CFLAGS += -I$(JSON_C_DIR)/include/json-c
  168. LDFLAGS+= -L$(JSON_C_DIR)/lib -ljson-c
  169. ```
  170. Using json-c <a name="using">
  171. ------------
  172. To use json-c you can either include json.h, or preferrably, one of the
  173. following more specific header files:
  174. * json_object.h - Core types and methods.
  175. * json_tokener.h - Methods for parsing and serializing json-c object trees.
  176. * json_pointer.h - JSON Pointer (RFC 6901) implementation for retrieving
  177. objects from a json-c object tree.
  178. * json_object_iterator.h - Methods for iterating over single json_object instances.
  179. * json_visit.h - Methods for walking a tree of json-c objects.
  180. * json_util.h - Miscelleanous utility functions.
  181. 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)