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

5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. Test Status
  16. * [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)
  17. JSON-C implements a reference counting object model that allows you to easily
  18. construct JSON objects in C, output them as JSON formatted strings and parse
  19. JSON formatted strings back into the C representation of JSON objects.
  20. It aims to conform to [RFC 7159](https://tools.ietf.org/html/rfc7159).
  21. Building on Unix and Windows with `vcpkg`, `gcc`/`g++`, `curl`, `unzip`, and `tar`
  22. --------------------------------------------------
  23. You can download and install JSON-C using the [vcpkg](https://github.com/Microsoft/vcpkg/) dependency manager:
  24. git clone https://github.com/Microsoft/vcpkg.git
  25. cd vcpkg
  26. ./bootstrap-vcpkg.sh
  27. ./vcpkg integrate install
  28. vcpkg install json-c
  29. 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.
  30. Building on Unix with `git`, `gcc` and `autotools` <a name="buildunix"></a>
  31. --------------------------------------------------
  32. Home page for json-c: https://github.com/json-c/json-c/wiki
  33. ### Prerequisites:
  34. See also the "Installing prerequisites" section below.
  35. - `gcc`, `clang`, or another C compiler
  36. - `libtool>=2.2.6b`
  37. If you're not using a release tarball, you'll also need:
  38. - `autoconf>=2.64` (`autoreconf`)
  39. - `automake>=1.13`
  40. Make sure you have a complete `libtool` install, including `libtoolize`.
  41. To generate docs (e.g. as part of make distcheck) you'll also need:
  42. - `doxygen>=1.8.13`
  43. ### Build instructions:
  44. `json-c` GitHub repo: https://github.com/json-c/json-c
  45. ```sh
  46. $ git clone https://github.com/json-c/json-c.git
  47. $ cd json-c
  48. $ sh autogen.sh
  49. ```
  50. followed by
  51. ```sh
  52. $ ./configure # --enable-threading
  53. $ make
  54. $ make install
  55. ```
  56. To build and run the test programs:
  57. ```sh
  58. $ make check
  59. $ make USE_VALGRIND=0 check # optionally skip using valgrind
  60. ```
  61. Install prerequisites <a name="installprereq"></a>
  62. -----------------------
  63. If you are on a relatively modern system, you'll likely be able to install
  64. the prerequisites using your OS's packaging system.
  65. ### Install using apt (e.g. Ubuntu 16.04.2 LTS)
  66. ```sh
  67. sudo apt install git
  68. sudo apt install autoconf automake libtool
  69. sudo apt install valgrind # optional
  70. ```
  71. Then start from the "git clone" command, above.
  72. ### Manually install and build autoconf, automake and libtool
  73. For older OS's that don't have up-to-date versions of the packages will
  74. require a bit more work. For example, CentOS release 5.11, etc...
  75. ```sh
  76. curl -O http://ftp.gnu.org/gnu/autoconf/autoconf-2.69.tar.gz
  77. curl -O http://ftp.gnu.org/gnu/automake/automake-1.15.tar.gz
  78. curl -O http://ftp.gnu.org/gnu/libtool/libtool-2.2.6b.tar.gz
  79. tar xzf autoconf-2.69.tar.gz
  80. tar xzf automake-1.15.tar.gz
  81. tar xzf libtool-2.2.6b.tar.gz
  82. export PATH=${HOME}/ac_install/bin:$PATH
  83. (cd autoconf-2.69 && \
  84. ./configure --prefix ${HOME}/ac_install && \
  85. make && \
  86. make install)
  87. (cd automake-1.15 && \
  88. ./configure --prefix ${HOME}/ac_install && \
  89. make && \
  90. make install)
  91. (cd libtool-2.2.6b && \
  92. ./configure --prefix ${HOME}/ac_install && \
  93. make && \
  94. make install)
  95. ```
  96. Building with partial threading support <a name="buildthreaded"></a>
  97. ----------------------------------------
  98. Although json-c does not support fully multi-threaded access to
  99. object trees, it has some code to help make its use in threaded programs
  100. a bit safer. Currently, this is limited to using atomic operations for
  101. json_object_get() and json_object_put().
  102. Since this may have a performance impact, of at least 3x slower
  103. according to https://stackoverflow.com/a/11609063, it is disabled by
  104. default. You may turn it on by adjusting your configure command with:
  105. --enable-threading
  106. Separately, the default hash function used for object field keys,
  107. lh_char_hash, uses a compare-and-swap operation to ensure the random
  108. seed is only generated once. Because this is a one-time operation, it
  109. is always compiled in when the compare-and-swap operation is available.
  110. Building with CMake <a name="CMake"></a>
  111. --------------------
  112. To use [CMake](https://cmake.org/cmake-tutorial/), build it like:
  113. ```sh
  114. mkdir build
  115. cd build
  116. cmake ../
  117. make
  118. ```
  119. CMake can take a few options.
  120. Variable | Type | Description
  121. ---------------------|--------|--------------
  122. CMAKE_INSTALL_PREFIX | String | The install location.
  123. BUILD_SHARED_LIBS | Bool | The default build generates a dynamic (dll/so) library. Set this to OFF to create a static library instead.
  124. ENABLE_RDRAND | Bool | Enable RDRAND Hardware RNG Hash Seed
  125. ENABLE_THREADING | Bool | Enable partial threading support
  126. Pass these options as `-D` on CMake's command-line.
  127. ```sh
  128. cmake -DBUILD_SHARED_LIBS=OFF ...
  129. ```
  130. **In addition, you can also use cmake-configure: Wrapper around cmake to emulate useful options.**
  131. To use cmake-configure, build it like:
  132. ```sh
  133. mkdir build
  134. cd build
  135. ../cmake-configure --disable-werror
  136. make
  137. ```
  138. cmake-configure can take a few options.
  139. | options | Description|
  140. | ---- | ---- |
  141. | prefix=PREFIX | install architecture-independent files in PREFIX |
  142. | enable-threading | Enable code to support partly multi-threaded use |
  143. | enable-rdrand | Enable RDRAND Hardware RNG Hash Seed generation on supported x86/x64 platforms. |
  144. | enable-shared | build shared libraries [default=yes] |
  145. | enable-static | build static libraries [default=yes] |
  146. | disable-Bsymbolic | Avoid linking with -Bsymbolic-function |
  147. | disable-werror | Avoid treating compiler warnings as fatal errors |
  148. Testing with cmake:
  149. By default, if valgrind is available running tests uses it.
  150. That can slow the tests down considerably, so to disable it use:
  151. ```sh
  152. export USE_VALGRIND=0
  153. ```
  154. To run tests:
  155. ```sh
  156. mkdir build-test
  157. cd build-test
  158. # VALGRIND=1 causes -DVALGRIND=1 to be included when building
  159. VALGRIND=1 cmake ..
  160. make
  161. make test
  162. # By default, if valgrind is available running tests uses it.
  163. make USE_VALGRIND=0 test # optionally skip using valgrind
  164. ```
  165. If a test fails, check `Testing/Temporary/LastTest.log`,
  166. `tests/testSubDir/${testname}/${testname}.vg.out`, and other similar files.
  167. If there is insufficient output try:
  168. ```sh
  169. VERBOSE=1 make test
  170. ```
  171. or
  172. ```sh
  173. JSONC_TEST_TRACE=1 make test
  174. ```
  175. and check the log files again.
  176. To get doxygen documentation:
  177. The libray documentation can be generated directly from the source codes using Doxygen tool:
  178. ```sh
  179. make doc
  180. google-chrome ../doc/html/index.html
  181. ```
  182. To uninstall:
  183. ```sh
  184. make uninstall
  185. ```
  186. Linking to `libjson-c` <a name="linking">
  187. ----------------------
  188. If your system has `pkgconfig`,
  189. then you can just add this to your `makefile`:
  190. ```make
  191. CFLAGS += $(shell pkg-config --cflags json-c)
  192. LDFLAGS += $(shell pkg-config --libs json-c)
  193. ```
  194. Without `pkgconfig`, you would do something like this:
  195. ```make
  196. JSON_C_DIR=/path/to/json_c/install
  197. CFLAGS += -I$(JSON_C_DIR)/include/json-c
  198. LDFLAGS+= -L$(JSON_C_DIR)/lib -ljson-c
  199. ```
  200. Using json-c <a name="using">
  201. ------------
  202. To use json-c you can either include json.h, or preferrably, one of the
  203. following more specific header files:
  204. * json_object.h - Core types and methods.
  205. * json_tokener.h - Methods for parsing and serializing json-c object trees.
  206. * json_pointer.h - JSON Pointer (RFC 6901) implementation for retrieving
  207. objects from a json-c object tree.
  208. * json_object_iterator.h - Methods for iterating over single json_object instances.
  209. * json_visit.h - Methods for walking a tree of json-c objects.
  210. * json_util.h - Miscelleanous utility functions.
  211. 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)