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

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