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

3 years ago
5 years ago
1 year ago
1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. \mainpage
  2. json-c
  3. ========
  4. 1. [Overview and Build Status](#overview)
  5. 2. [Getting Help](#gettinghelp)
  6. 3. [Building on Unix](#buildunix)
  7. * [Prerequisites](#installprereq)
  8. * [Build commands](#buildcmds)
  9. 4. [CMake options](#CMake)
  10. 5. [Testing](#testing)
  11. 6. [Building with `vcpkg`](#buildvcpkg)
  12. 7. [Building for Android](#android)
  13. 8. [Building for Commodore Amiga](#amiga)
  14. 9. [Linking to libjson-c](#linking)
  15. 10. [Using json-c](#using)
  16. <a name="overview"></a>
  17. JSON-C - A JSON implementation in C
  18. -----------------------------------
  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 8259](https://www.rfc-editor.org/rfc/rfc8259).
  23. Skip down to [Using json-c](#using)
  24. or check out the [API docs](https://json-c.github.io/json-c/),
  25. if you already have json-c installed and ready to use.
  26. Home page for json-c: https://github.com/json-c/json-c/wiki
  27. <a name="gettinghelp"></a>
  28. Getting Help
  29. ------------
  30. If you have questions about using json-c, please start a thread on
  31. our forums at: https://groups.google.com/forum/#!forum/json-c
  32. If you believe you've discovered a bug, report it at
  33. (https://github.com/json-c/json-c/issues). Please be sure to include
  34. the version of json-c you're using, the OS you're running on, and any
  35. other relevant details. Fully reproducible test cases and/or patches
  36. to fix problems are greatly appreciated.
  37. Fixes for bugs, or small new features can be directly submitted as a
  38. [pull request](https://github.com/json-c/json-c/pulls). For major new
  39. features or large changes of any kind, please first start a discussion
  40. on the [forums](https://groups.google.com/forum/#!forum/json-c).
  41. <a name="buildunix"></a>
  42. Building on Unix with `git`, `gcc` and `cmake`
  43. --------------------------------------------------
  44. If you already have json-c installed, see [Linking to `libjson-c`](#linking)
  45. for how to build and link your program against it.
  46. Build Status
  47. * [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)
  48. * [Travis Build](https://app.travis-ci.com/github/json-c/json-c) ![Travis Build Status](https://api.travis-ci.com/json-c/json-c.svg?branch=master)
  49. Test Status
  50. * [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)
  51. <a name="installprereq"></a>
  52. ### Prerequisites:
  53. - `gcc`, `clang`, or another C compiler
  54. - `cmake>=2.8`, `>=3.16` recommended, `cmake=>3.1` for tests
  55. To generate docs you'll also need:
  56. - `doxygen>=1.8.13`
  57. If you are on a relatively modern system, you'll likely be able to install
  58. the prerequisites using your OS's packaging system.
  59. ### Install using apt (e.g. Ubuntu 16.04.2 LTS)
  60. ```sh
  61. sudo apt install git
  62. sudo apt install cmake
  63. sudo apt install doxygen # optional
  64. sudo apt install valgrind # optional
  65. ```
  66. <a name="buildcmds"></a>
  67. ### Build instructions:
  68. `json-c` GitHub repo: https://github.com/json-c/json-c
  69. ```sh
  70. $ git clone https://github.com/json-c/json-c.git
  71. $ mkdir json-c-build
  72. $ cd json-c-build
  73. $ cmake ../json-c # See CMake section below for custom arguments
  74. ```
  75. Note: it's also possible to put your build directory inside the json-c
  76. source directory, or even not use a separate build directory at all, but
  77. certain things might not work quite right (notably, `make distcheck`)
  78. Then:
  79. ```sh
  80. $ make
  81. $ make test
  82. $ make USE_VALGRIND=0 test # optionally skip using valgrind
  83. $ sudo make install # it could be necessary to execute make install
  84. ```
  85. ### Generating documentation with Doxygen:
  86. The library documentation can be generated directly from the source code using Doxygen tool:
  87. ```sh
  88. # in build directory
  89. make doc
  90. google-chrome doc/html/index.html
  91. ```
  92. <a name="CMake"></a>
  93. CMake Options
  94. --------------------
  95. The json-c library is built with [CMake](https://cmake.org/cmake-tutorial/),
  96. which can take a few options.
  97. Variable | Type | Description
  98. -----------------------------|--------|--------------
  99. CMAKE_INSTALL_PREFIX | String | The install location.
  100. CMAKE_BUILD_TYPE | String | Defaults to "debug".
  101. BUILD_SHARED_LIBS | Bool | The default build generates a dynamic (dll/so) library. Set this to OFF to create a static library only.
  102. BUILD_STATIC_LIBS | Bool | The default build generates a static (lib/a) library. Set this to OFF to create a shared library only.
  103. DISABLE_STATIC_FPIC | Bool | The default builds position independent code. Set this to OFF to create a shared library only.
  104. DISABLE_BSYMBOLIC | Bool | Disable use of -Bsymbolic-functions.
  105. DISABLE_THREAD_LOCAL_STORAGE | Bool | Disable use of Thread-Local Storage (HAVE___THREAD).
  106. DISABLE_WERROR | Bool | Disable use of -Werror.
  107. DISABLE_EXTRA_LIBS | Bool | Disable use of extra libraries, libbsd
  108. DISABLE_JSON_POINTER | Bool | Omit json_pointer support from the build.
  109. ENABLE_RDRAND | Bool | Enable RDRAND Hardware RNG Hash Seed.
  110. ENABLE_THREADING | Bool | Enable partial threading support.
  111. 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.
  112. Pass these options as `-D` on CMake's command-line.
  113. ```sh
  114. # build a static library only
  115. cmake -DBUILD_SHARED_LIBS=OFF ..
  116. ```
  117. ### Building with partial threading support
  118. Although json-c does not support fully multi-threaded access to
  119. object trees, it has some code to help make its use in threaded programs
  120. a bit safer. Currently, this is limited to using atomic operations for
  121. json_object_get() and json_object_put().
  122. Since this may have a performance impact, of at least 3x slower
  123. according to https://stackoverflow.com/a/11609063, it is disabled by
  124. default. You may turn it on by adjusting your cmake command with:
  125. -DENABLE_THREADING=ON
  126. Separately, the default hash function used for object field keys,
  127. lh_char_hash, uses a compare-and-swap operation to ensure the random
  128. seed is only generated once. Because this is a one-time operation, it
  129. is always compiled in when the compare-and-swap operation is available.
  130. ### cmake-configure wrapper script
  131. For those familiar with the old autoconf/autogen.sh/configure method,
  132. there is a `cmake-configure` wrapper script to ease the transition to cmake.
  133. ```sh
  134. mkdir build
  135. cd build
  136. ../cmake-configure --prefix=/some/install/path
  137. make
  138. ```
  139. cmake-configure can take a few options.
  140. | options | Description|
  141. | ---- | ---- |
  142. | prefix=PREFIX | install architecture-independent files in PREFIX |
  143. | enable-threading | Enable code to support partly multi-threaded use |
  144. | enable-rdrand | Enable RDRAND Hardware RNG Hash Seed generation on supported x86/x64 platforms. |
  145. | enable-shared | build shared libraries [default=yes] |
  146. | enable-static | build static libraries [default=yes] |
  147. | disable-Bsymbolic | Avoid linking with -Bsymbolic-function |
  148. | disable-werror | Avoid treating compiler warnings as fatal errors |
  149. <a name="testing"></a>
  150. Testing:
  151. ----------
  152. By default, if valgrind is available running tests uses it.
  153. That can slow the tests down considerably, so to disable it use:
  154. ```sh
  155. export USE_VALGRIND=0
  156. ```
  157. To run tests a separate build directory is recommended:
  158. ```sh
  159. mkdir build-test
  160. cd build-test
  161. # VALGRIND=1 causes -DVALGRIND=1 to be passed when compiling code
  162. # which uses slightly slower, but valgrind-safe code.
  163. VALGRIND=1 cmake ..
  164. make
  165. make test
  166. # By default, if valgrind is available running tests uses it.
  167. make USE_VALGRIND=0 test # optionally skip using valgrind
  168. ```
  169. If a test fails, check `Testing/Temporary/LastTest.log`,
  170. `tests/testSubDir/${testname}/${testname}.vg.out`, and other similar files.
  171. If there is insufficient output try:
  172. ```sh
  173. VERBOSE=1 CTEST_OUTPUT_ON_FAILURE=1 make test
  174. ```
  175. or
  176. ```sh
  177. JSONC_TEST_TRACE=1 make test
  178. ```
  179. and check the log files again.
  180. <a name="buildvcpkg"></a>
  181. Building on Unix and Windows with `vcpkg`
  182. --------------------------------------------------
  183. You can download and install JSON-C using the [vcpkg](https://github.com/Microsoft/vcpkg/) dependency manager:
  184. git clone https://github.com/Microsoft/vcpkg.git
  185. cd vcpkg
  186. ./bootstrap-vcpkg.sh
  187. ./vcpkg integrate install
  188. vcpkg install json-c
  189. 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.
  190. <a name="android"></a>
  191. Building for Android
  192. ----------------------
  193. Building on Android is now particularly well supported, but there
  194. have been some reports of success using
  195. https://developer.android.com/ndk/guides/cmake
  196. ```
  197. mkdir json-c-build
  198. cd json-c-build/
  199. export NDK_HOME=~/Library/Android/sdk/ndk/22.1.7171670/
  200. cmake \
  201. --toolchain=$NDK_HOME/build/cmake/android.toolchain.cmake \
  202. -DANDROID_STL=none \
  203. -DANDROID_ABI=arm64-v8a \
  204. -DANDROID_PLATFORM=android-29 \
  205. -DANDROID_LD=lld \
  206. -DCMAKE_BUILD_TYPE=MinSizeRel \
  207. -DCMAKE_INSTALL_PREFIX=<install prefix> \
  208. -DENABLE_THREADING=true \
  209. ..
  210. make install
  211. ```
  212. <a name="amiga"></a>
  213. Building for Commodore Amiga
  214. ----------------------
  215. Building for Commodore Amiga is supported for both Motorola 68k (AmigaOS 3) and PowerPC (AmigaOS 4) architectures. You can set up a cross compiler locally, however it is much easier to use the already preconfigured Amiga development environment wtthin a Docker container.
  216. Install Docker on your machine if you don't already have it. You can download Docker Desktop for Windows/macOS/Linux [here](https://www.docker.com/products/docker-desktop/).
  217. ### To build for Motorola 68k Amiga:
  218. ```
  219. mkdir json-c-build
  220. docker run --rm \
  221. -v ${PWD}:/work \
  222. -e USER=$( id -u ) -e GROUP=$( id -g ) \
  223. -it sacredbanana/amiga-compiler:m68k-amigaos bash
  224. cd json-c-build
  225. cmake -DM68K_CRT=newlib ..
  226. make
  227. ```
  228. libjson-c.a will get created in the json-c-build directory.
  229. You can change newlib to nix20, nix13 or clib2 if you would like to build the library suited for libnix or clib2 instead. Newlib is default.
  230. ### To build for PowerPC Amiga:
  231. ```
  232. mkdir json-c-build
  233. docker run --rm \
  234. -v ${PWD}:/work \
  235. -e USER=$( id -u ) -e GROUP=$( id -g ) \
  236. -it sacredbanana/amiga-compiler:ppc-amigaos bash
  237. cd json-c-build
  238. cmake ..
  239. make
  240. ```
  241. libjson-c.a will get created in the json-c-build directory.
  242. <a name="linking"></a>
  243. Linking to `libjson-c`
  244. ----------------------
  245. If your system has `pkgconfig`,
  246. then you can just add this to your `makefile`:
  247. ```make
  248. CFLAGS += $(shell pkg-config --cflags json-c)
  249. LDFLAGS += $(shell pkg-config --libs json-c)
  250. ```
  251. Without `pkgconfig`, you might do something like this:
  252. ```make
  253. JSON_C_DIR=/path/to/json_c/install
  254. CFLAGS += -I$(JSON_C_DIR)/include/json-c
  255. # Or to use lines like: #include <json-c/json_object.h>
  256. #CFLAGS += -I$(JSON_C_DIR)/include
  257. LDFLAGS+= -L$(JSON_C_DIR)/lib -ljson-c
  258. ```
  259. If your project uses cmake:
  260. * Add to your CMakeLists.txt file:
  261. ```cmake
  262. find_package(json-c CONFIG)
  263. target_link_libraries(${PROJECT_NAME} PRIVATE json-c::json-c)
  264. ```
  265. * Then you might run in your project:
  266. ```sh
  267. cd build
  268. cmake -DCMAKE_PREFIX_PATH=/path/to/json_c/install/lib64/cmake ..
  269. ```
  270. <a name="using"></a>
  271. Using json-c
  272. ------------
  273. To use json-c you can either include json.h, or preferably, one of the
  274. following more specific header files:
  275. * json_object.h - Core types and methods.
  276. * json_tokener.h - Methods for parsing and serializing json-c object trees.
  277. * json_pointer.h - JSON Pointer (RFC 6901) implementation for retrieving
  278. objects from a json-c object tree.
  279. * json_object_iterator.h - Methods for iterating over single json_object instances. (See also `json_object_object_foreach()` in json_object.h)
  280. * json_visit.h - Methods for walking a tree of json-c objects.
  281. * json_util.h - Miscellaneous utility functions.
  282. For a full list of headers see [files.html](https://json-c.github.io/json-c/json-c-current-release/doc/html/files.html)
  283. The primary type in json-c is json_object. It describes a reference counted
  284. tree of json objects which are created by either parsing text with a
  285. json_tokener (i.e. `json_tokener_parse_ex()`), or by creating
  286. (with `json_object_new_object()`, `json_object_new_int()`, etc...) and adding
  287. (with `json_object_object_add()`, `json_object_array_add()`, etc...) them
  288. individually.
  289. Typically, every object in the tree will have one reference, from its parent.
  290. When you are done with the tree of objects, you call json_object_put() on just
  291. the root object to free it, which recurses down through any child objects
  292. calling json_object_put() on each one of those in turn.
  293. You can get a reference to a single child
  294. (`json_object_object_get()` or `json_object_array_get_idx()`)
  295. and use that object as long as its parent is valid.
  296. If you need a child object to live longer than its parent, you can
  297. increment the child's refcount (`json_object_get()`) to allow it to survive
  298. the parent being freed or it being removed from its parent
  299. (`json_object_object_del()` or `json_object_array_del_idx()`)
  300. When parsing text, the json_tokener object is independent from the json_object
  301. that it returns. It can be allocated (`json_tokener_new()`)
  302. used one or multiple times (`json_tokener_parse_ex()`, and
  303. freed (`json_tokener_free()`) while the json_object objects live on.
  304. A json_object tree can be serialized back into a string with
  305. `json_object_to_json_string_ext()`. The string that is returned
  306. is only valid until the next "to_json_string" call on that same object.
  307. Also, it is freed when the json_object is freed.