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.

index.html 18 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
  5. <meta http-equiv="X-UA-Compatible" content="IE=9"/>
  6. <title>json-c: Main Page</title>
  7. <link href="tabs.css" rel="stylesheet" type="text/css"/>
  8. <script type="text/javascript" src="jquery.js"></script>
  9. <script type="text/javascript" src="dynsections.js"></script>
  10. <link href="doxygen.css" rel="stylesheet" type="text/css" />
  11. </head>
  12. <body>
  13. <div id="top"><!-- do not remove this div, it is closed by doxygen! -->
  14. <div id="titlearea">
  15. <table cellspacing="0" cellpadding="0">
  16. <tbody>
  17. <tr style="height: 56px;">
  18. <td style="padding-left: 0.5em;">
  19. <div id="projectname">json-c
  20. &#160;<span id="projectnumber">0.16</span>
  21. </div>
  22. </td>
  23. </tr>
  24. </tbody>
  25. </table>
  26. </div>
  27. <!-- end header part -->
  28. <!-- Generated by Doxygen 1.8.2 -->
  29. <div id="navrow1" class="tabs">
  30. <ul class="tablist">
  31. <li class="current"><a href="index.html"><span>Main&#160;Page</span></a></li>
  32. <li><a href="pages.html"><span>Related&#160;Pages</span></a></li>
  33. <li><a href="annotated.html"><span>Data&#160;Structures</span></a></li>
  34. <li><a href="files.html"><span>Files</span></a></li>
  35. </ul>
  36. </div>
  37. </div><!-- top -->
  38. <div class="header">
  39. <div class="headertitle">
  40. <div class="title">json-c Documentation</div> </div>
  41. </div><!--header-->
  42. <div class="contents">
  43. <div class="textblock"><h1><code>json-c</code></h1>
  44. <ol type="1">
  45. <li><a href="#overview">Overview and Build Status</a></li>
  46. <li><a href="#buildunix">Building on Unix</a><ul>
  47. <li><a href="#installprereq">Prerequisites</a></li>
  48. <li><a href="#buildcmds">Build commands</a></li>
  49. </ul>
  50. </li>
  51. <li><a href="#CMake">CMake options</a></li>
  52. <li><a href="#testing">Testing</a></li>
  53. <li><a href="#buildvcpkg">Building with `vcpkg`</a></li>
  54. <li><a href="#linking">Linking to libjson-c</a></li>
  55. <li><a href="#using">Using json-c</a></li>
  56. </ol>
  57. <h2>JSON-C - A JSON implementation in C <a class="anchor" id="overview"></a></h2>
  58. <p>JSON-C implements a reference counting object model that allows you to easily construct JSON objects in C, output them as JSON formatted strings and parse JSON formatted strings back into the C representation of JSON objects. It aims to conform to <a href="https://tools.ietf.org/html/rfc7159">RFC 7159</a>.</p>
  59. <p>Skip down to <a href="#using">Using json-c</a> or check out the <a href="https://json-c.github.io/json-c/">API docs</a>, if you already have json-c installed and ready to use.</p>
  60. <p>Home page for json-c: <a href="https://github.com/json-c/json-c/wiki">https://github.com/json-c/json-c/wiki</a></p>
  61. <p>Build Status</p>
  62. <ul>
  63. <li><a href="https://ci.appveyor.com/project/hawicz/json-c">AppVeyor Build</a> <div class="image">
  64. <img src="https://ci.appveyor.com/api/projects/status/github/json-c/json-c?branch=master&svg=true" alt="AppVeyor Build Status"/>
  65. </div>
  66. </li>
  67. <li><a href="https://travis-ci.org/json-c/json-c">Travis Build</a> <div class="image">
  68. <img src="https://travis-ci.org/json-c/json-c.svg?branch=master" alt="Travis Build Status"/>
  69. </div>
  70. </li>
  71. </ul>
  72. <p>Test Status</p>
  73. <ul>
  74. <li><a href="https://coveralls.io/github/json-c/json-c?branch=master">Coveralls</a> <a href="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)</a></li>
  75. </ul>
  76. <h2>Building on Unix with <code>git</code>, <code>gcc</code> and <code>cmake</code> <a class="anchor" id="buildunix"></a></h2>
  77. <p>If you already have json-c installed, see <a href="#linking">Linking to `libjson-c`</a> for how to build and link your program against it.</p>
  78. <h3>Prerequisites: <a class="anchor" id="installprereq"></a></h3>
  79. <ul>
  80. <li><code>gcc</code>, <code>clang</code>, or another C compiler</li>
  81. </ul>
  82. <ul>
  83. <li><code>cmake&gt;=2.8</code>, <code>&gt;=3.16</code> recommended, <code>cmake=&gt;3.1</code> for tests</li>
  84. </ul>
  85. <p>To generate docs you'll also need:</p>
  86. <ul>
  87. <li><code>doxygen&gt;=1.8.13</code></li>
  88. </ul>
  89. <p>If you are on a relatively modern system, you'll likely be able to install the prerequisites using your OS's packaging system.</p>
  90. <h3>Install using apt (e.g. Ubuntu 16.04.2 LTS)</h3>
  91. <pre class="fragment">sudo apt install git
  92. sudo apt install cmake
  93. sudo apt install doxygen # optional
  94. sudo apt install valgrind # optional
  95. </pre><h3>Build instructions: <a class="anchor" id="buildcmds"></a></h3>
  96. <p><code>json-c</code> GitHub repo: <a href="https://github.com/json-c/json-c">https://github.com/json-c/json-c</a> </p>
  97. <pre class="fragment">$ git clone https://github.com/json-c/json-c.git
  98. $ mkdir json-c-build
  99. $ cd json-c-build
  100. $ cmake ../json-c # See CMake section below for custom arguments
  101. </pre><p>Note: it's also possible to put your build directory inside the json-c source directory, or even not use a separate build directory at all, but certain things might not work quite right (notably, <code>make distcheck</code>)</p>
  102. <p>Then: </p>
  103. <pre class="fragment">$ make
  104. $ make test
  105. $ make USE_VALGRIND=0 test # optionally skip using valgrind
  106. $ make install
  107. </pre><h3>Generating documentation with Doxygen:</h3>
  108. <p>The library documentation can be generated directly from the source code using Doxygen tool: </p>
  109. <pre class="fragment"># in build directory
  110. make doc
  111. google-chrome doc/html/index.html
  112. </pre><h2>CMake Options <a class="anchor" id="CMake"></a></h2>
  113. <p>The json-c library is built with <a href="https://cmake.org/cmake-tutorial/">CMake</a>, which can take a few options.</p>
  114. <table class="doxtable">
  115. <tr>
  116. <th>Variable </th><th>Type </th><th>Description</th></tr>
  117. <tr>
  118. <td>CMAKE_INSTALL_PREFIX </td><td>String </td><td>The install location. </td></tr>
  119. <tr>
  120. <td>CMAKE_BUILD_TYPE </td><td>String </td><td>Defaults to "debug". </td></tr>
  121. <tr>
  122. <td>BUILD_SHARED_LIBS </td><td>Bool </td><td>The default build generates a dynamic (dll/so) library. Set this to OFF to create a static library only. </td></tr>
  123. <tr>
  124. <td>BUILD_STATIC_LIBS </td><td>Bool </td><td>The default build generates a static (lib/a) library. Set this to OFF to create a shared library only. </td></tr>
  125. <tr>
  126. <td>DISABLE_STATIC_FPIC </td><td>Bool </td><td>The default builds position independent code. Set this to OFF to create a shared library only. </td></tr>
  127. <tr>
  128. <td>DISABLE_BSYMBOLIC </td><td>Bool </td><td>Disable use of -Bsymbolic-functions. </td></tr>
  129. <tr>
  130. <td>DISABLE_THREAD_LOCAL_STORAGE </td><td>Bool </td><td>Disable use of Thread-Local Storage (HAVE___THREAD). </td></tr>
  131. <tr>
  132. <td>DISABLE_WERROR </td><td>Bool </td><td>Disable use of -Werror. </td></tr>
  133. <tr>
  134. <td>ENABLE_RDRAND </td><td>Bool </td><td>Enable RDRAND Hardware RNG Hash Seed. </td></tr>
  135. <tr>
  136. <td>ENABLE_THREADING </td><td>Bool </td><td>Enable partial threading support. </td></tr>
  137. <tr>
  138. <td>OVERRIDE_GET_RANDOM_SEED </td><td>String </td><td>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. </td></tr>
  139. </table>
  140. <p>Pass these options as <code>-D</code> on CMake's command-line. </p>
  141. <pre class="fragment"># build a static library only
  142. cmake -DBUILD_SHARED_LIBS=OFF ..
  143. </pre><h3>Building with partial threading support</h3>
  144. <p>Although json-c does not support fully multi-threaded access to object trees, it has some code to help make its use in threaded programs a bit safer. Currently, this is limited to using atomic operations for <a class="el" href="json__object_8h.html#a675aa3a9cced685dbfd1c1a770a0c3e4">json_object_get()</a> and <a class="el" href="json__object_8h.html#afabf61f932cd64a4122ca8092452eed5">json_object_put()</a>.</p>
  145. <p>Since this may have a performance impact, of at least 3x slower according to <a href="https://stackoverflow.com/a/11609063,">https://stackoverflow.com/a/11609063,</a> it is disabled by default. You may turn it on by adjusting your cmake command with: -DENABLE_THREADING=ON</p>
  146. <p>Separately, the default hash function used for object field keys, lh_char_hash, uses a compare-and-swap operation to ensure the random seed is only generated once. Because this is a one-time operation, it is always compiled in when the compare-and-swap operation is available.</p>
  147. <h3>cmake-configure wrapper script</h3>
  148. <p>For those familiar with the old autoconf/autogen.sh/configure method, there is a <code>cmake-configure</code> wrapper script to ease the transition to cmake. </p>
  149. <pre class="fragment">mkdir build
  150. cd build
  151. ../cmake-configure --prefix=/some/install/path
  152. make
  153. </pre><p>cmake-configure can take a few options.</p>
  154. <table class="doxtable">
  155. <tr>
  156. <th>options </th><th>Description</th></tr>
  157. <tr>
  158. <td>prefix=PREFIX </td><td>install architecture-independent files in PREFIX </td></tr>
  159. <tr>
  160. <td>enable-threading </td><td>Enable code to support partly multi-threaded use </td></tr>
  161. <tr>
  162. <td>enable-rdrand </td><td>Enable RDRAND Hardware RNG Hash Seed generation on supported x86/x64 platforms. </td></tr>
  163. <tr>
  164. <td>enable-shared </td><td>build shared libraries [default=yes] </td></tr>
  165. <tr>
  166. <td>enable-static </td><td>build static libraries [default=yes] </td></tr>
  167. <tr>
  168. <td>disable-Bsymbolic </td><td>Avoid linking with -Bsymbolic-function </td></tr>
  169. <tr>
  170. <td>disable-werror </td><td>Avoid treating compiler warnings as fatal errors </td></tr>
  171. </table>
  172. <h2>Testing: <a class="anchor" id="testing"></a></h2>
  173. <p>By default, if valgrind is available running tests uses it. That can slow the tests down considerably, so to disable it use: </p>
  174. <pre class="fragment">export USE_VALGRIND=0
  175. </pre><p>To run tests a separate build directory is recommended: </p>
  176. <pre class="fragment">mkdir build-test
  177. cd build-test
  178. # VALGRIND=1 causes -DVALGRIND=1 to be passed when compiling code
  179. # which uses slightly slower, but valgrind-safe code.
  180. VALGRIND=1 cmake ..
  181. make
  182. make test
  183. # By default, if valgrind is available running tests uses it.
  184. make USE_VALGRIND=0 test # optionally skip using valgrind
  185. </pre><p>If a test fails, check <code>Testing/Temporary/LastTest.log</code>, <code>tests/testSubDir/${testname}/${testname}.vg.out</code>, and other similar files. If there is insufficient output try: </p>
  186. <pre class="fragment">VERBOSE=1 CTEST_OUTPUT_ON_FAILURE=1 make test
  187. </pre><p>or </p>
  188. <pre class="fragment">JSONC_TEST_TRACE=1 make test
  189. </pre><p>and check the log files again.</p>
  190. <h2>Building on Unix and Windows with <code>vcpkg</code> <a class="anchor" id="buildvcpkg"></a></h2>
  191. <p>You can download and install JSON-C using the <a href="https://github.com/Microsoft/vcpkg/">vcpkg</a> dependency manager: </p>
  192. <pre class="fragment">git clone https://github.com/Microsoft/vcpkg.git
  193. cd vcpkg
  194. ./bootstrap-vcpkg.sh
  195. ./vcpkg integrate install
  196. vcpkg install json-c
  197. </pre><p>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 <a href="https://github.com/Microsoft/vcpkg">create an issue or pull request</a> on the vcpkg repository.</p>
  198. <h2>Linking to <code>libjson-c</code> <a class="anchor" id="linking"></a></h2>
  199. <p>If your system has <code>pkgconfig</code>, then you can just add this to your <code>makefile</code>: </p>
  200. <pre class="fragment">CFLAGS += $(shell pkg-config --cflags json-c)
  201. LDFLAGS += $(shell pkg-config --libs json-c)
  202. </pre><p>Without <code>pkgconfig</code>, you might do something like this: </p>
  203. <pre class="fragment">JSON_C_DIR=/path/to/json_c/install
  204. CFLAGS += -I$(JSON_C_DIR)/include/json-c
  205. # Or to use lines like: #include &lt;json-c/json_object.h&gt;
  206. #CFLAGS += -I$(JSON_C_DIR)/include
  207. LDFLAGS+= -L$(JSON_C_DIR)/lib -ljson-c
  208. </pre><p>If your project uses cmake:</p>
  209. <ul>
  210. <li><p class="startli">Add to your CMakeLists.txt file:</p>
  211. <p class="startli">find_package(json-c CONFIG) target_link_libraries(${PROJECT_NAME} PRIVATE json-c::json-c)</p>
  212. </li>
  213. </ul>
  214. <ul>
  215. <li><p class="startli">Then you might run in your project:</p>
  216. <p class="startli">cd build cmake -DCMAKE_PREFIX_PATH=/path/to/json_c/install/lib64/cmake ..</p>
  217. </li>
  218. </ul>
  219. <h2>Using json-c <a class="anchor" id="using"></a></h2>
  220. <p>To use json-c you can either include <a class="el" href="json_8h.html" title="A convenience header that may be included instead of other individual ones.">json.h</a>, or preferably, one of the following more specific header files:</p>
  221. <ul>
  222. <li><a class="el" href="json__object_8h.html" title="Core json-c API. Start here, or with json_tokener.h.">json_object.h</a> - Core types and methods.</li>
  223. <li><a class="el" href="json__tokener_8h.html" title="Methods to parse an input string into a tree of json_object objects.">json_tokener.h</a> - Methods for parsing and serializing json-c object trees.</li>
  224. <li><a class="el" href="json__pointer_8h.html" title="JSON Pointer (RFC 6901) implementation for retrieving objects from a json-c object tree...">json_pointer.h</a> - JSON Pointer (RFC 6901) implementation for retrieving objects from a json-c object tree.</li>
  225. <li><a class="el" href="json__object__iterator_8h.html" title="An API for iterating over json_type_object objects, styled to be familiar to C++ programmers. Unlike json_object_object_foreach() and json_object_object_foreachC(), this avoids the need to expose json-c internals like lh_entry.">json_object_iterator.h</a> - Methods for iterating over single json_object instances. (See also <code><a class="el" href="json__object_8h.html#acf5f514a9e0061c10fc08055762639ee">json_object_object_foreach()</a></code> in <a class="el" href="json__object_8h.html" title="Core json-c API. Start here, or with json_tokener.h.">json_object.h</a>)</li>
  226. <li><a class="el" href="json__visit_8h.html" title="Methods for walking a tree of objects.">json_visit.h</a> - Methods for walking a tree of json-c objects.</li>
  227. <li><a class="el" href="json__util_8h.html" title="Miscllaneous utility functions and macros.">json_util.h</a> - Miscellaneous utility functions.</li>
  228. </ul>
  229. <p>For a full list of headers see <a href="https://json-c.github.io/json-c/json-c-current-release/doc/html/files.html">files.html</a></p>
  230. <p>The primary type in json-c is json_object. It describes a reference counted tree of json objects which are created by either parsing text with a <a class="el" href="structjson__tokener.html">json_tokener</a> (i.e. <code><a class="el" href="json__tokener_8h.html#a61679f178111963a9ffa3c8179553f7a">json_tokener_parse_ex()</a></code>), or by creating (with <code><a class="el" href="json__object_8h.html#a68c383f54544fca19b5f2425be397600">json_object_new_object()</a></code>, <code><a class="el" href="json__object_8h.html#ae92f0770fb4b3c884ce35de52d3d7de8">json_object_new_int()</a></code>, etc...) and adding (with <code><a class="el" href="json__object_8h.html#a27bd808a022251059a43f1f6370441cd">json_object_object_add()</a></code>, <code><a class="el" href="json__object_8h.html#a18cdd9a7455e09f36cdf6e5756b7f586">json_object_array_add()</a></code>, etc...) them individually. Typically, every object in the tree will have one reference, from its parent. When you are done with the tree of objects, you call <a class="el" href="json__object_8h.html#afabf61f932cd64a4122ca8092452eed5">json_object_put()</a> on just the root object to free it, which recurses down through any child objects calling <a class="el" href="json__object_8h.html#afabf61f932cd64a4122ca8092452eed5">json_object_put()</a> on each one of those in turn.</p>
  231. <p>You can get a reference to a single child (<code><a class="el" href="json__object_8h.html#a1a097805abb53b4c8a60d573730a8939">json_object_object_get()</a></code> or <code><a class="el" href="json__object_8h.html#a676711a76545d4ec65cc75f100f5fd19">json_object_array_get_idx()</a></code>) and use that object as long as its parent is valid. If you need a child object to live longer than its parent, you can increment the child's refcount (<code><a class="el" href="json__object_8h.html#a675aa3a9cced685dbfd1c1a770a0c3e4">json_object_get()</a></code>) to allow it to survive the parent being freed or it being removed from its parent (<code><a class="el" href="json__object_8h.html#ac6605fdafca20bd5d33c84f4f80a3bda">json_object_object_del()</a></code> or <code><a class="el" href="json__object_8h.html#a722eca9f578704d3af38b97549242c1f">json_object_array_del_idx()</a></code>)</p>
  232. <p>When parsing text, the <a class="el" href="structjson__tokener.html">json_tokener</a> object is independent from the json_object that it returns. It can be allocated (<code><a class="el" href="json__tokener_8h.html#a5ac7e2c350bc592cf2fa7b9935b00ef5">json_tokener_new()</a></code>) used one or multiple times (<code><a class="el" href="json__tokener_8h.html#a61679f178111963a9ffa3c8179553f7a">json_tokener_parse_ex()</a></code>, and freed (<code><a class="el" href="json__tokener_8h.html#a887c4661906fc6b36cc366304e522534">json_tokener_free()</a></code>) while the json_object objects live on.</p>
  233. <p>A json_object tree can be serialized back into a string with <code><a class="el" href="json__object_8h.html#a9db613127bd4ef7db42307e43a85fc1b">json_object_to_json_string_ext()</a></code>. The string that is returned is only valid until the next "to_json_string" call on that same object. Also, it is freed when the json_object is freed. </p>
  234. </div></div><!-- contents -->
  235. <!-- start footer part -->
  236. <hr class="footer"/><address class="footer"><small>
  237. Generated on Thu Apr 14 2022 01:11:24 for json-c by &#160;<a href="http://www.doxygen.org/index.html">
  238. <img class="footer" src="doxygen.png" alt="doxygen"/>
  239. </a> 1.8.2
  240. </small></address>
  241. </body>
  242. </html>

No Description