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.4 kB

5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. **In addition, you can also use cmake-configure: Wrapper around cmake to emulate useful options.**
  129. To use cmake-configure, build it like:
  130. ```sh
  131. mkdir build
  132. cd build
  133. ../cmake-configure --disable-werror
  134. make
  135. ```
  136. cmake-configure can take a few options.
  137. | options | Description|
  138. | ---- | ---- |
  139. | prefix=PREFIX | install architecture-independent files in PREFIX |
  140. | enable-threading | Enable code to support partly multi-threaded use |
  141. | enable-rdrand | Enable RDRAND Hardware RNG Hash Seed generation on supported x86/x64 platforms. |
  142. | enable-shared | build shared libraries [default=yes] |
  143. | enable-static | build static libraries [default=yes] |
  144. | disable-Bsymbolic | Avoid linking with -Bsymbolic-function |
  145. | disable-werror | Avoid treating compiler warnings as fatal errors |
  146. Testing with cmake:
  147. By default, if valgrind is available running tests uses it.
  148. That can slow the tests down considerably, so to disable it use:
  149. ```sh
  150. export USE_VALGRIND=0
  151. ```
  152. To run tests:
  153. ```sh
  154. mkdir build-test
  155. cd build-test
  156. # VALGRIND=1 causes -DVALGRIND=1 to be included when building
  157. VALGRIND=1 cmake ..
  158. make
  159. make test
  160. # By default, if valgrind is available running tests uses it.
  161. make USE_VALGRIND=0 test # optionally skip using valgrind
  162. ```
  163. If a test fails, check `Testing/Temporary/LastTest.log`,
  164. `tests/testSubDir/${testname}/${testname}.vg.out`, and other similar files.
  165. If there is insufficient output try:
  166. ```sh
  167. VERBOSE=1 make test
  168. ```
  169. or
  170. ```sh
  171. JSONC_TEST_TRACE=1 make test
  172. ```
  173. and check the log files again.
  174. To get doxygen documentation:
  175. The libray documentation can be generated directly from the source codes using Doxygen tool:
  176. ```sh
  177. make doc
  178. google-chrome ../doc/html/index.html
  179. ```
  180. To uninstall:
  181. ```sh
  182. make uninstall
  183. ```
  184. Linking to `libjson-c` <a name="linking">
  185. ----------------------
  186. If your system has `pkgconfig`,
  187. then you can just add this to your `makefile`:
  188. ```make
  189. CFLAGS += $(shell pkg-config --cflags json-c)
  190. LDFLAGS += $(shell pkg-config --libs json-c)
  191. ```
  192. Without `pkgconfig`, you would do something like this:
  193. ```make
  194. JSON_C_DIR=/path/to/json_c/install
  195. CFLAGS += -I$(JSON_C_DIR)/include/json-c
  196. LDFLAGS+= -L$(JSON_C_DIR)/lib -ljson-c
  197. ```
  198. Using json-c <a name="using">
  199. ------------
  200. To use json-c you can either include json.h, or preferrably, one of the
  201. following more specific header files:
  202. * json_object.h - Core types and methods.
  203. * json_tokener.h - Methods for parsing and serializing json-c object trees.
  204. * json_pointer.h - JSON Pointer (RFC 6901) implementation for retrieving
  205. objects from a json-c object tree.
  206. * json_object_iterator.h - Methods for iterating over single json_object instances.
  207. * json_visit.h - Methods for walking a tree of json-c objects.
  208. * json_util.h - Miscelleanous utility functions.
  209. 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)