components/jansson/doc/man3lib/libjansson.3lib
branchs11-update
changeset 4141 c6a303a2f8c5
equal deleted inserted replaced
4138:240dc166feab 4141:c6a303a2f8c5
       
     1 .\" Man page generated from reStructuredText.
       
     2 .
       
     3 .TH "LIBJANSSON" "3LIB" "October 28, 2014" "2.7" "Jansson"
       
     4 .SH NAME
       
     5 jansson \- Jansson Documentation
       
     6 .sp
       
     7 This is the documentation for \fI\%Jansson\fP 2.7, last updated October 28, 2014\&.
       
     8 .SH INTRODUCTION
       
     9 .sp
       
    10 \fI\%Jansson\fP is a C library for encoding, decoding and manipulating JSON
       
    11 data. Its main features and design principles are:
       
    12 .INDENT 0.0
       
    13 .IP \(bu 2
       
    14 Simple and intuitive API and data model
       
    15 .IP \(bu 2
       
    16 Comprehensive documentation
       
    17 .IP \(bu 2
       
    18 No dependencies on other libraries
       
    19 .IP \(bu 2
       
    20 Full Unicode support (UTF\-8)
       
    21 .IP \(bu 2
       
    22 Extensive test suite
       
    23 .UNINDENT
       
    24 .sp
       
    25 Jansson is licensed under the \fI\%MIT license\fP; see LICENSE in the
       
    26 source distribution for details.
       
    27 .sp
       
    28 Jansson is used in production and its API is stable. It works on
       
    29 numerous platforms, including numerous Unix like systems and Windows.
       
    30 It\(aqs suitable for use on any system, including desktop, server, and
       
    31 small embedded systems.
       
    32 .SH CONTENTS
       
    33 .SS Getting Started
       
    34 .SS Compiling and Installing Jansson
       
    35 .sp
       
    36 The Jansson source is available at
       
    37 \fI\%http://www.digip.org/jansson/releases/\fP\&.
       
    38 .SS Unix\-like systems (including MinGW)
       
    39 .sp
       
    40 Unpack the source tarball and change to the source directory:
       
    41 .INDENT 0.0
       
    42 .INDENT 3.5
       
    43 .sp
       
    44 .nf
       
    45 .ft CW
       
    46 bunzip2 \-c jansson\-2.7\&.tar.bz2 | tar xf \-
       
    47 cd jansson\-2.7
       
    48 .ft R
       
    49 .fi
       
    50 .UNINDENT
       
    51 .UNINDENT
       
    52 .sp
       
    53 The source uses GNU Autotools (\fI\%autoconf\fP, \fI\%automake\fP, \fI\%libtool\fP), so
       
    54 compiling and installing is extremely simple:
       
    55 .INDENT 0.0
       
    56 .INDENT 3.5
       
    57 .sp
       
    58 .nf
       
    59 .ft CW
       
    60 \&./configure
       
    61 make
       
    62 make check
       
    63 make install
       
    64 .ft R
       
    65 .fi
       
    66 .UNINDENT
       
    67 .UNINDENT
       
    68 .sp
       
    69 To change the destination directory (\fB/usr/local\fP by default), use
       
    70 the \fB\-\-prefix=DIR\fP argument to \fB\&./configure\fP\&. See \fB\&./configure
       
    71 \-\-help\fP for the list of all possible installation options. (There are
       
    72 no options to customize the resulting Jansson binary.)
       
    73 .sp
       
    74 The command \fBmake check\fP runs the test suite distributed with
       
    75 Jansson. This step is not strictly necessary, but it may find possible
       
    76 problems that Jansson has on your platform. If any problems are found,
       
    77 please report them.
       
    78 .sp
       
    79 If you obtained the source from a Git repository (or any other source
       
    80 control system), there\(aqs no \fB\&./configure\fP script as it\(aqs not kept in
       
    81 version control. To create the script, the build system needs to be
       
    82 bootstrapped. There are many ways to do this, but the easiest one is
       
    83 to use \fBautoreconf\fP:
       
    84 .INDENT 0.0
       
    85 .INDENT 3.5
       
    86 .sp
       
    87 .nf
       
    88 .ft CW
       
    89 autoreconf \-vi
       
    90 .ft R
       
    91 .fi
       
    92 .UNINDENT
       
    93 .UNINDENT
       
    94 .sp
       
    95 This command creates the \fB\&./configure\fP script, which can then be
       
    96 used as described above.
       
    97 .SS CMake (various platforms, including Windows)
       
    98 .sp
       
    99 Jansson can be built using \fI\%CMake\fP\&. Create a build directory for an
       
   100 out\-of\-tree build, change to that directory, and run \fBcmake\fP (or \fBccmake\fP,
       
   101 \fBcmake\-gui\fP, or similar) to configure the project.
       
   102 .sp
       
   103 See the examples below for more detailed information.
       
   104 .sp
       
   105 \fBNOTE:\fP
       
   106 .INDENT 0.0
       
   107 .INDENT 3.5
       
   108 In the below examples \fB\&..\fP is used as an argument for \fBcmake\fP\&.
       
   109 This is simply the path to the jansson project root directory.
       
   110 In the example it is assumed you\(aqve created a sub\-directory \fBbuild\fP
       
   111 and are using that. You could use any path you want.
       
   112 .UNINDENT
       
   113 .UNINDENT
       
   114 .SS Unix (Make files)
       
   115 .sp
       
   116 Generating make files on unix:
       
   117 .INDENT 0.0
       
   118 .INDENT 3.5
       
   119 .sp
       
   120 .nf
       
   121 .ft CW
       
   122 bunzip2 \-c jansson\-2.7\&.tar.bz2 | tar xf \-
       
   123 cd jansson\-2.7
       
   124 
       
   125 mkdir build
       
   126 cd build
       
   127 cmake .. # or \fBccmake ..()\fP for a GUI.
       
   128 .ft R
       
   129 .fi
       
   130 .UNINDENT
       
   131 .UNINDENT
       
   132 .sp
       
   133 Then to build:
       
   134 .INDENT 0.0
       
   135 .INDENT 3.5
       
   136 .sp
       
   137 .nf
       
   138 .ft CW
       
   139 make
       
   140 make check
       
   141 make install
       
   142 .ft R
       
   143 .fi
       
   144 .UNINDENT
       
   145 .UNINDENT
       
   146 .SS Windows (Visual Studio)
       
   147 .sp
       
   148 Creating Visual Studio project files from the command line:
       
   149 .INDENT 0.0
       
   150 .INDENT 3.5
       
   151 .sp
       
   152 .nf
       
   153 .ft CW
       
   154 <unpack>
       
   155 cd jansson\-2.7
       
   156 
       
   157 md build
       
   158 cd build
       
   159 cmake \-G "Visual Studio 10" ..
       
   160 .ft R
       
   161 .fi
       
   162 .UNINDENT
       
   163 .UNINDENT
       
   164 .sp
       
   165 You will now have a \fIVisual Studio Solution\fP in your build directory.
       
   166 To run the unit tests build the \fBRUN_TESTS\fP project.
       
   167 .sp
       
   168 If you prefer a GUI the \fBcmake\fP line in the above example can
       
   169 be replaced with:
       
   170 .INDENT 0.0
       
   171 .INDENT 3.5
       
   172 .sp
       
   173 .nf
       
   174 .ft CW
       
   175 cmake\-gui ..
       
   176 .ft R
       
   177 .fi
       
   178 .UNINDENT
       
   179 .UNINDENT
       
   180 .sp
       
   181 For command line help (including a list of available generators)
       
   182 for \fI\%CMake\fP simply run:
       
   183 .INDENT 0.0
       
   184 .INDENT 3.5
       
   185 .sp
       
   186 .nf
       
   187 .ft CW
       
   188 cmake
       
   189 .ft R
       
   190 .fi
       
   191 .UNINDENT
       
   192 .UNINDENT
       
   193 .sp
       
   194 To list available \fI\%CMake\fP settings (and what they are currently set to)
       
   195 for the project, run:
       
   196 .INDENT 0.0
       
   197 .INDENT 3.5
       
   198 .sp
       
   199 .nf
       
   200 .ft CW
       
   201 cmake \-LH ..
       
   202 .ft R
       
   203 .fi
       
   204 .UNINDENT
       
   205 .UNINDENT
       
   206 .SS Mac OSX (Xcode)
       
   207 .sp
       
   208 If you prefer using Xcode instead of make files on OSX,
       
   209 do the following. (Use the same steps as
       
   210 for \fIUnix\fP):
       
   211 .INDENT 0.0
       
   212 .INDENT 3.5
       
   213 .sp
       
   214 .nf
       
   215 .ft CW
       
   216 \&...
       
   217 cmake \-G "Xcode" ..
       
   218 .ft R
       
   219 .fi
       
   220 .UNINDENT
       
   221 .UNINDENT
       
   222 .SS Additional CMake settings
       
   223 .SS Shared library
       
   224 .sp
       
   225 By default the \fI\%CMake\fP project will generate build files for building the
       
   226 static library. To build the shared version use:
       
   227 .INDENT 0.0
       
   228 .INDENT 3.5
       
   229 .sp
       
   230 .nf
       
   231 .ft CW
       
   232 \&...
       
   233 cmake \-DJANSSON_BUILD_SHARED_LIBS=1 ..
       
   234 .ft R
       
   235 .fi
       
   236 .UNINDENT
       
   237 .UNINDENT
       
   238 .SS Changing install directory (same as autoconf \-\-prefix)
       
   239 .sp
       
   240 Just as with the \fI\%autoconf\fP project you can change the destination directory
       
   241 for \fBmake install\fP\&. The equivalent for autoconfs \fB\&./configure \-\-prefix\fP
       
   242 in \fI\%CMake\fP is:
       
   243 .INDENT 0.0
       
   244 .INDENT 3.5
       
   245 .sp
       
   246 .nf
       
   247 .ft CW
       
   248 \&...
       
   249 cmake \-DCMAKE_INSTALL_PREFIX:PATH=/some/other/path ..
       
   250 make install
       
   251 .ft R
       
   252 .fi
       
   253 .UNINDENT
       
   254 .UNINDENT
       
   255 .SS Android
       
   256 .sp
       
   257 Jansson can be built for Android platforms. Android.mk is in the
       
   258 source root directory. The configuration header file is located in the
       
   259 \fBandroid\fP directory in the source distribution.
       
   260 .SS Other Systems
       
   261 .sp
       
   262 On non Unix\-like systems, you may be unable to run the \fB\&./configure\fP
       
   263 script. In this case, follow these steps. All the files mentioned can
       
   264 be found in the \fBsrc/\fP directory.
       
   265 .INDENT 0.0
       
   266 .IP 1. 3
       
   267 Create \fBjansson_config.h\fP (which has some platform\-specific
       
   268 parameters that are normally filled in by the \fB\&./configure\fP
       
   269 script). Edit \fBjansson_config.h.in\fP, replacing all \fB@variable@\fP
       
   270 placeholders, and rename the file to \fBjansson_config.h\fP\&.
       
   271 .IP 2. 3
       
   272 Make \fBjansson.h\fP and \fBjansson_config.h\fP available to the
       
   273 compiler, so that they can be found when compiling programs that
       
   274 use Jansson.
       
   275 .IP 3. 3
       
   276 Compile all the \fB\&.c\fP files (in the \fBsrc/\fP directory) into a
       
   277 library file. Make the library available to the compiler, as in
       
   278 step 2.
       
   279 .UNINDENT
       
   280 .SS Building the Documentation
       
   281 .sp
       
   282 (This subsection describes how to build the HTML documentation you are
       
   283 currently reading, so it can be safely skipped.)
       
   284 .sp
       
   285 Documentation is in the \fBdoc/\fP subdirectory. It\(aqs written in
       
   286 \fI\%reStructuredText\fP with \fI\%Sphinx\fP annotations. To generate the HTML
       
   287 documentation, invoke:
       
   288 .INDENT 0.0
       
   289 .INDENT 3.5
       
   290 .sp
       
   291 .nf
       
   292 .ft CW
       
   293 make html
       
   294 .ft R
       
   295 .fi
       
   296 .UNINDENT
       
   297 .UNINDENT
       
   298 .sp
       
   299 and point your browser to \fBdoc/_build/html/index.html\fP\&. \fI\%Sphinx\fP 1.0
       
   300 or newer is required to generate the documentation.
       
   301 .SS Compiling Programs that Use Jansson
       
   302 .sp
       
   303 Jansson involves one C header file, \fBjansson.h\fP, so it\(aqs enough
       
   304 to put the line
       
   305 .INDENT 0.0
       
   306 .INDENT 3.5
       
   307 .sp
       
   308 .nf
       
   309 .ft CW
       
   310 #include <jansson.h>
       
   311 .ft R
       
   312 .fi
       
   313 .UNINDENT
       
   314 .UNINDENT
       
   315 .sp
       
   316 in the beginning of every source file that uses Jansson.
       
   317 .sp
       
   318 There\(aqs also just one library to link with, \fBlibjansson\fP\&. Compile and
       
   319 link the program as follows:
       
   320 .INDENT 0.0
       
   321 .INDENT 3.5
       
   322 .sp
       
   323 .nf
       
   324 .ft CW
       
   325 cc \-I/usr/include/jansson \-o prog prog.c \-ljansson
       
   326 .ft R
       
   327 .fi
       
   328 .UNINDENT
       
   329 .UNINDENT
       
   330 .sp
       
   331 Starting from version 1.2, there\(aqs also support for \fI\%pkg\-config\fP:
       
   332 .INDENT 0.0
       
   333 .INDENT 3.5
       
   334 .sp
       
   335 .nf
       
   336 .ft CW
       
   337 cc \-o prog prog.c \(gapkg\-config \-\-cflags \-\-libs jansson\(ga
       
   338 .ft R
       
   339 .fi
       
   340 .UNINDENT
       
   341 .UNINDENT
       
   342 .SS Upgrading from 1.x
       
   343 .sp
       
   344 This chapter lists the backwards incompatible changes introduced in
       
   345 Jansson 2.0, and the steps that are needed for upgrading your code.
       
   346 .sp
       
   347 \fBThe incompatibilities are not dramatic.\fP The biggest change is that
       
   348 all decoding functions now require and extra parameter. Most programs
       
   349 can be modified to work with 2.0 by adding a \fB0\fP as the second
       
   350 parameter to all calls of \fBjson_loads()\fP, \fBjson_loadf()\fP
       
   351 and \fBjson_load_file()\fP\&.
       
   352 .SS Compatibility
       
   353 .sp
       
   354 Jansson 2.0 is backwards incompatible with the Jansson 1.x releases.
       
   355 It is ABI incompatible, i.e. all programs dynamically linking to the
       
   356 Jansson library need to be recompiled. It\(aqs also API incompatible,
       
   357 i.e. the source code of programs using Jansson 1.x may need
       
   358 modifications to make them compile against Jansson 2.0.
       
   359 .sp
       
   360 All the 2.x releases are guaranteed to be backwards compatible for
       
   361 both ABI and API, so no recompilation or source changes are needed
       
   362 when upgrading from 2.x to 2.y.
       
   363 .SS List of Incompatible Changes
       
   364 .INDENT 0.0
       
   365 .TP
       
   366 .B \fBDecoding flags\fP
       
   367 For future needs, a \fBflags\fP parameter was added as the second
       
   368 parameter to all decoding functions, i.e. \fBjson_loads()\fP,
       
   369 \fBjson_loadf()\fP and \fBjson_load_file()\fP\&. All calls to
       
   370 these functions need to be changed by adding a \fB0\fP as the second
       
   371 argument. For example:
       
   372 .INDENT 7.0
       
   373 .INDENT 3.5
       
   374 .sp
       
   375 .nf
       
   376 .ft CW
       
   377 /* old code */
       
   378 json_loads(input, &error);
       
   379 
       
   380 /* new code */
       
   381 json_loads(input, 0, &error);
       
   382 .ft R
       
   383 .fi
       
   384 .UNINDENT
       
   385 .UNINDENT
       
   386 .TP
       
   387 .B \fBUnderlying type of JSON integers\fP
       
   388 The underlying C type of JSON integers has been changed from
       
   389 \fBint\fP to the widest available signed integer type, i.e.
       
   390 \fBlong long\fP or \fBlong\fP, depending on whether
       
   391 \fBlong long\fP is supported on your system or not. This makes
       
   392 the whole 64\-bit integer range available on most modern systems.
       
   393 .sp
       
   394 \fBjansson.h\fP has a typedef \fBjson_int_t\fP to the underlying
       
   395 integer type. \fBint\fP should still be used in most cases when
       
   396 dealing with smallish JSON integers, as the compiler handles
       
   397 implicit type coercion. Only when the full 64\-bit range is needed,
       
   398 \fBjson_int_t\fP should be explicitly used.
       
   399 .TP
       
   400 .B \fBMaximum encoder indentation depth\fP
       
   401 The maximum argument of the \fBJSON_INDENT()\fP macro has been
       
   402 changed from 255 to 31, to free up bits from the \fBflags\fP
       
   403 parameter of \fBjson_dumps()\fP, \fBjson_dumpf()\fP and
       
   404 \fBjson_dump_file()\fP\&. If your code uses a bigger indentation
       
   405 than 31, it needs to be changed.
       
   406 .TP
       
   407 .B \fBUnsigned integers in API functions\fP
       
   408 Version 2.0 unifies unsigned integer usage in the API. All uses of
       
   409 \fBunsigned int\fP and \fBunsigned long\fP have been replaced
       
   410 with \fBsize_t\fP\&. This includes flags, container sizes, etc.
       
   411 This should not require source code changes, as both
       
   412 \fBunsigned int\fP and \fBunsigned long\fP are usually
       
   413 compatible with \fBsize_t\fP\&.
       
   414 .UNINDENT
       
   415 .SS Tutorial
       
   416 .sp
       
   417 In this tutorial, we create a program that fetches the latest commits
       
   418 of a repository in \fI\%GitHub\fP over the web. \fI\%GitHub API\fP uses JSON, so
       
   419 the result can be parsed using Jansson.
       
   420 .sp
       
   421 To stick to the the scope of this tutorial, we will only cover the the
       
   422 parts of the program related to handling JSON data. For the best user
       
   423 experience, the full source code is available:
       
   424 \fBgithub_commits.c\fP\&. To compile it (on Unix\-like systems with
       
   425 gcc), use the following command:
       
   426 .INDENT 0.0
       
   427 .INDENT 3.5
       
   428 .sp
       
   429 .nf
       
   430 .ft CW
       
   431 gcc \-o github_commits github_commits.c \-ljansson \-lcurl
       
   432 .ft R
       
   433 .fi
       
   434 .UNINDENT
       
   435 .UNINDENT
       
   436 .sp
       
   437 \fI\%libcurl\fP is used to communicate over the web, so it is required to
       
   438 compile the program.
       
   439 .sp
       
   440 The command line syntax is:
       
   441 .INDENT 0.0
       
   442 .INDENT 3.5
       
   443 .sp
       
   444 .nf
       
   445 .ft CW
       
   446 github_commits USER REPOSITORY
       
   447 .ft R
       
   448 .fi
       
   449 .UNINDENT
       
   450 .UNINDENT
       
   451 .sp
       
   452 \fBUSER\fP is a GitHub user ID and \fBREPOSITORY\fP is the repository
       
   453 name. Please note that the GitHub API is rate limited, so if you run
       
   454 the program too many times within a short period of time, the sever
       
   455 starts to respond with an error.
       
   456 .SS The GitHub Repo Commits API
       
   457 .sp
       
   458 The \fI\%GitHub Repo Commits API\fP is used by sending HTTP requests to
       
   459 URLs like \fBhttps://api.github.com/repos/USER/REPOSITORY/commits\fP,
       
   460 where \fBUSER\fP and \fBREPOSITORY\fP are the GitHub user ID and the name
       
   461 of the repository whose commits are to be listed, respectively.
       
   462 .sp
       
   463 GitHub responds with a JSON array of the following form:
       
   464 .INDENT 0.0
       
   465 .INDENT 3.5
       
   466 .sp
       
   467 .nf
       
   468 .ft CW
       
   469 [
       
   470     {
       
   471         "sha": "<the commit ID>",
       
   472         "commit": {
       
   473             "message": "<the commit message>",
       
   474             <more fields, not important to this tutorial...>
       
   475         },
       
   476         <more fields...>
       
   477     },
       
   478     {
       
   479         "sha": "<the commit ID>",
       
   480         "commit": {
       
   481             "message": "<the commit message>",
       
   482             <more fields...>
       
   483         },
       
   484         <more fields...>
       
   485     },
       
   486     <more commits...>
       
   487 ]
       
   488 .ft R
       
   489 .fi
       
   490 .UNINDENT
       
   491 .UNINDENT
       
   492 .sp
       
   493 In our program, the HTTP request is sent using the following
       
   494 function:
       
   495 .INDENT 0.0
       
   496 .INDENT 3.5
       
   497 .sp
       
   498 .nf
       
   499 .ft CW
       
   500 static char *request(const char *url);
       
   501 .ft R
       
   502 .fi
       
   503 .UNINDENT
       
   504 .UNINDENT
       
   505 .sp
       
   506 It takes the URL as a parameter, preforms a HTTP GET request, and
       
   507 returns a newly allocated string that contains the response body. If
       
   508 the request fails, an error message is printed to stderr and the
       
   509 return value is \fINULL\fP\&. For full details, refer to \fBthe code\fP, as the actual implementation is not important
       
   510 here.
       
   511 .SS The Program
       
   512 .sp
       
   513 First the includes:
       
   514 .INDENT 0.0
       
   515 .INDENT 3.5
       
   516 .sp
       
   517 .nf
       
   518 .ft CW
       
   519 #include <string.h>
       
   520 #include <jansson.h>
       
   521 .ft R
       
   522 .fi
       
   523 .UNINDENT
       
   524 .UNINDENT
       
   525 .sp
       
   526 Like all the programs using Jansson, we need to include
       
   527 \fBjansson.h\fP\&.
       
   528 .sp
       
   529 The following definitions are used to build the GitHub API request
       
   530 URL:
       
   531 .INDENT 0.0
       
   532 .INDENT 3.5
       
   533 .sp
       
   534 .nf
       
   535 .ft CW
       
   536 #define URL_FORMAT   "https://api.github.com/repos/%s/%s/commits"
       
   537 #define URL_SIZE     256
       
   538 .ft R
       
   539 .fi
       
   540 .UNINDENT
       
   541 .UNINDENT
       
   542 .sp
       
   543 The following function is used when formatting the result to find the
       
   544 first newline in the commit message:
       
   545 .INDENT 0.0
       
   546 .INDENT 3.5
       
   547 .sp
       
   548 .nf
       
   549 .ft CW
       
   550 /* Return the offset of the first newline in text or the length of
       
   551    text if there\(aqs no newline */
       
   552 static int newline_offset(const char *text)
       
   553 {
       
   554     const char *newline = strchr(text, \(aq\en\(aq);
       
   555     if(!newline)
       
   556         return strlen(text);
       
   557     else
       
   558         return (int)(newline \- text);
       
   559 }
       
   560 .ft R
       
   561 .fi
       
   562 .UNINDENT
       
   563 .UNINDENT
       
   564 .sp
       
   565 The main function follows. In the beginning, we first declare a bunch
       
   566 of variables and check the command line parameters:
       
   567 .INDENT 0.0
       
   568 .INDENT 3.5
       
   569 .sp
       
   570 .nf
       
   571 .ft CW
       
   572 int main(int argc, char *argv[])
       
   573 {
       
   574     size_t i;
       
   575     char *text;
       
   576     char url[URL_SIZE];
       
   577 
       
   578     json_t *root;
       
   579     json_error_t error;
       
   580 
       
   581     if(argc != 3)
       
   582     {
       
   583         fprintf(stderr, "usage: %s USER REPOSITORY\en\en", argv[0]);
       
   584         fprintf(stderr, "List commits at USER\(aqs REPOSITORY.\en\en");
       
   585         return 2;
       
   586     }
       
   587 .ft R
       
   588 .fi
       
   589 .UNINDENT
       
   590 .UNINDENT
       
   591 .sp
       
   592 Then we build the request URL using the user and repository names
       
   593 given as command line parameters:
       
   594 .INDENT 0.0
       
   595 .INDENT 3.5
       
   596 .sp
       
   597 .nf
       
   598 .ft CW
       
   599 snprintf(url, URL_SIZE, URL_FORMAT, argv[1], argv[2]);
       
   600 .ft R
       
   601 .fi
       
   602 .UNINDENT
       
   603 .UNINDENT
       
   604 .sp
       
   605 This uses the \fBURL_SIZE\fP and \fBURL_FORMAT\fP constants defined above.
       
   606 Now we\(aqre ready to actually request the JSON data over the web:
       
   607 .INDENT 0.0
       
   608 .INDENT 3.5
       
   609 .sp
       
   610 .nf
       
   611 .ft CW
       
   612 text = request(url);
       
   613 if(!text)
       
   614     return 1;
       
   615 .ft R
       
   616 .fi
       
   617 .UNINDENT
       
   618 .UNINDENT
       
   619 .sp
       
   620 If an error occurs, our function \fBrequest\fP prints the error and
       
   621 returns \fINULL\fP, so it\(aqs enough to just return 1 from the main
       
   622 function.
       
   623 .sp
       
   624 Next we\(aqll call \fBjson_loads()\fP to decode the JSON text we got
       
   625 as a response:
       
   626 .INDENT 0.0
       
   627 .INDENT 3.5
       
   628 .sp
       
   629 .nf
       
   630 .ft CW
       
   631 root = json_loads(text, 0, &error);
       
   632 free(text);
       
   633 
       
   634 if(!root)
       
   635 {
       
   636     fprintf(stderr, "error: on line %d: %s\en", error.line, error.text);
       
   637     return 1;
       
   638 }
       
   639 .ft R
       
   640 .fi
       
   641 .UNINDENT
       
   642 .UNINDENT
       
   643 .sp
       
   644 We don\(aqt need the JSON text anymore, so we can free the \fBtext\fP
       
   645 variable right after decoding it. If \fBjson_loads()\fP fails, it
       
   646 returns \fINULL\fP and sets error information to the \fBjson_error_t\fP
       
   647 structure given as the second parameter. In this case, our program
       
   648 prints the error information out and returns 1 from the main function.
       
   649 .sp
       
   650 Now we\(aqre ready to extract the data out of the decoded JSON response.
       
   651 The structure of the response JSON was explained in section
       
   652 \fI\%The GitHub Repo Commits API\fP\&.
       
   653 .sp
       
   654 We check that the returned value really is an array:
       
   655 .INDENT 0.0
       
   656 .INDENT 3.5
       
   657 .sp
       
   658 .nf
       
   659 .ft CW
       
   660 if(!json_is_array(root))
       
   661 {
       
   662     fprintf(stderr, "error: root is not an array\en");
       
   663     json_decref(root);
       
   664     return 1;
       
   665 }
       
   666 .ft R
       
   667 .fi
       
   668 .UNINDENT
       
   669 .UNINDENT
       
   670 .sp
       
   671 Then we proceed to loop over all the commits in the array:
       
   672 .INDENT 0.0
       
   673 .INDENT 3.5
       
   674 .sp
       
   675 .nf
       
   676 .ft CW
       
   677 for(i = 0; i < json_array_size(root); i++)
       
   678 {
       
   679     json_t *data, *sha, *commit, *message;
       
   680     const char *message_text;
       
   681 
       
   682     data = json_array_get(root, i);
       
   683     if(!json_is_object(data))
       
   684     {
       
   685         fprintf(stderr, "error: commit data %d is not an object\en", i + 1);
       
   686         json_decref(root);
       
   687         return 1;
       
   688     }
       
   689 \&...
       
   690 .ft R
       
   691 .fi
       
   692 .UNINDENT
       
   693 .UNINDENT
       
   694 .sp
       
   695 The function \fBjson_array_size()\fP returns the size of a JSON
       
   696 array. First, we again declare some variables and then extract the
       
   697 i\(aqth element of the \fBroot\fP array using \fBjson_array_get()\fP\&.
       
   698 We also check that the resulting value is a JSON object.
       
   699 .sp
       
   700 Next we\(aqll extract the commit ID (a hexadecimal SHA\-1 sum),
       
   701 intermediate commit info object, and the commit message from that
       
   702 object. We also do proper type checks:
       
   703 .INDENT 0.0
       
   704 .INDENT 3.5
       
   705 .sp
       
   706 .nf
       
   707 .ft CW
       
   708     sha = json_object_get(data, "sha");
       
   709     if(!json_is_string(sha))
       
   710     {
       
   711         fprintf(stderr, "error: commit %d: sha is not a string\en", i + 1);
       
   712         json_decref(root);
       
   713         return 1;
       
   714     }
       
   715 
       
   716     commit = json_object_get(data, "commit");
       
   717     if(!json_is_object(commit))
       
   718     {
       
   719         fprintf(stderr, "error: commit %d: commit is not an object\en", i + 1);
       
   720         json_decref(root);
       
   721         return 1;
       
   722     }
       
   723 
       
   724     message = json_object_get(commit, "message");
       
   725     if(!json_is_string(message))
       
   726     {
       
   727         fprintf(stderr, "error: commit %d: message is not a string\en", i + 1);
       
   728         json_decref(root);
       
   729         return 1;
       
   730     }
       
   731 \&...
       
   732 .ft R
       
   733 .fi
       
   734 .UNINDENT
       
   735 .UNINDENT
       
   736 .sp
       
   737 And finally, we\(aqll print the first 8 characters of the commit ID and
       
   738 the first line of the commit message. A C\-style string is extracted
       
   739 from a JSON string using \fBjson_string_value()\fP:
       
   740 .INDENT 0.0
       
   741 .INDENT 3.5
       
   742 .sp
       
   743 .nf
       
   744 .ft CW
       
   745     message_text = json_string_value(message);
       
   746     printf("%.8s %.*s\en",
       
   747            json_string_value(id),
       
   748            newline_offset(message_text),
       
   749            message_text);
       
   750 }
       
   751 .ft R
       
   752 .fi
       
   753 .UNINDENT
       
   754 .UNINDENT
       
   755 .sp
       
   756 After sending the HTTP request, we decoded the JSON text using
       
   757 \fBjson_loads()\fP, remember? It returns a \fInew reference\fP to the
       
   758 JSON value it decodes. When we\(aqre finished with the value, we\(aqll need
       
   759 to decrease the reference count using \fBjson_decref()\fP\&. This way
       
   760 Jansson can release the resources:
       
   761 .INDENT 0.0
       
   762 .INDENT 3.5
       
   763 .sp
       
   764 .nf
       
   765 .ft CW
       
   766 json_decref(root);
       
   767 return 0;
       
   768 .ft R
       
   769 .fi
       
   770 .UNINDENT
       
   771 .UNINDENT
       
   772 .sp
       
   773 For a detailed explanation of reference counting in Jansson, see
       
   774 \fIapiref\-reference\-count\fP in \fIapiref\fP\&.
       
   775 .sp
       
   776 The program\(aqs ready, let\(aqs test it and view the latest commits in
       
   777 Jansson\(aqs repository:
       
   778 .INDENT 0.0
       
   779 .INDENT 3.5
       
   780 .sp
       
   781 .nf
       
   782 .ft CW
       
   783 $ ./github_commits akheron jansson
       
   784 1581f26a Merge branch \(aq2.3\(aq
       
   785 aabfd493 load: Change buffer_pos to be a size_t
       
   786 bd72efbd load: Avoid unexpected behaviour in macro expansion
       
   787 e8fd3e30 Document and tweak json_load_callback()
       
   788 873eddaf Merge pull request #60 from rogerz/contrib
       
   789 bd2c0c73 Ignore the binary test_load_callback
       
   790 17a51a4b Merge branch \(aq2.3\(aq
       
   791 09c39adc Add json_load_callback to the list of exported symbols
       
   792 cbb80baf Merge pull request #57 from rogerz/contrib
       
   793 040bd7b0 Add json_load_callback()
       
   794 2637faa4 Make test stripping locale independent
       
   795 <...>
       
   796 .ft R
       
   797 .fi
       
   798 .UNINDENT
       
   799 .UNINDENT
       
   800 .SS Conclusion
       
   801 .sp
       
   802 In this tutorial, we implemented a program that fetches the latest
       
   803 commits of a GitHub repository using the GitHub Repo Commits API.
       
   804 Jansson was used to decode the JSON response and to extract the commit
       
   805 data.
       
   806 .sp
       
   807 This tutorial only covered a small part of Jansson. For example, we
       
   808 did not create or manipulate JSON values at all. Proceed to
       
   809 \fIapiref\fP to explore all features of Jansson.
       
   810 .SS RFC Conformance
       
   811 .sp
       
   812 JSON is specified in \fI\%RFC 4627\fP, \fI"The application/json Media Type
       
   813 for JavaScript Object Notation (JSON)"\fP\&.
       
   814 .SS Character Encoding
       
   815 .sp
       
   816 Jansson only supports UTF\-8 encoded JSON texts. It does not support or
       
   817 auto\-detect any of the other encodings mentioned in the RFC, namely
       
   818 UTF\-16LE, UTF\-16BE, UTF\-32LE or UTF\-32BE. Pure ASCII is supported, as
       
   819 it\(aqs a subset of UTF\-8.
       
   820 .SS Strings
       
   821 .sp
       
   822 JSON strings are mapped to C\-style null\-terminated character arrays,
       
   823 and UTF\-8 encoding is used internally.
       
   824 .sp
       
   825 All Unicode codepoints U+0000 through U+10FFFF are allowed in string
       
   826 values. However, U+0000 is not allowed in object keys because of API
       
   827 restrictions.
       
   828 .sp
       
   829 Unicode normalization or any other transformation is never performed
       
   830 on any strings (string values or object keys). When checking for
       
   831 equivalence of strings or object keys, the comparison is performed
       
   832 byte by byte between the original UTF\-8 representations of the
       
   833 strings.
       
   834 .SS Numbers
       
   835 .SS Real vs. Integer
       
   836 .sp
       
   837 JSON makes no distinction between real and integer numbers; Jansson
       
   838 does. Real numbers are mapped to the \fBdouble\fP type and integers to
       
   839 the \fBjson_int_t\fP type, which is a typedef of \fBlong long\fP or
       
   840 \fBlong\fP, depending on whether \fBlong long\fP is supported by your
       
   841 compiler or not.
       
   842 .sp
       
   843 A JSON number is considered to be a real number if its lexical
       
   844 representation includes one of \fBe\fP, \fBE\fP, or \fB\&.\fP; regardless if
       
   845 its actual numeric value is a true integer (e.g., all of \fB1E6\fP,
       
   846 \fB3.0\fP, \fB400E\-2\fP, and \fB3.14E3\fP are mathematical integers, but
       
   847 will be treated as real values). With the \fBJSON_DECODE_INT_AS_REAL\fP
       
   848 decoder flag set all numbers are interpreted as real.
       
   849 .sp
       
   850 All other JSON numbers are considered integers.
       
   851 .sp
       
   852 When encoding to JSON, real values are always represented
       
   853 with a fractional part; e.g., the \fBdouble\fP value 3.0 will be
       
   854 represented in JSON as \fB3.0\fP, not \fB3\fP\&.
       
   855 .SS Overflow, Underflow & Precision
       
   856 .sp
       
   857 Real numbers whose absolute values are too small to be represented in
       
   858 a C \fBdouble\fP will be silently estimated with 0.0. Thus, depending on
       
   859 platform, JSON numbers very close to zero such as 1E\-999 may result in
       
   860 0.0.
       
   861 .sp
       
   862 Real numbers whose absolute values are too large to be represented in
       
   863 a C \fBdouble\fP will result in an overflow error (a JSON decoding
       
   864 error). Thus, depending on platform, JSON numbers like 1E+999 or
       
   865 \-1E+999 may result in a parsing error.
       
   866 .sp
       
   867 Likewise, integer numbers whose absolute values are too large to be
       
   868 represented in the \fBjson_int_t\fP type (see above) will result in an
       
   869 overflow error (a JSON decoding error). Thus, depending on platform,
       
   870 JSON numbers like 1000000000000000 may result in parsing error.
       
   871 .sp
       
   872 Parsing JSON real numbers may result in a loss of precision. As long
       
   873 as overflow does not occur (i.e. a total loss of precision), the
       
   874 rounded approximate value is silently used. Thus the JSON number
       
   875 1.000000000000000005 may, depending on platform, result in the
       
   876 \fBdouble\fP value 1.0.
       
   877 .SS Signed zeros
       
   878 .sp
       
   879 JSON makes no statement about what a number means; however Javascript
       
   880 (ECMAscript) does state that +0.0 and \-0.0 must be treated as being
       
   881 distinct values, i.e. \-0.0 ≠ 0.0. Jansson relies on the
       
   882 underlying floating point library in the C environment in which it is
       
   883 compiled. Therefore it is platform\-dependent whether 0.0 and \-0.0 will
       
   884 be distinct values. Most platforms that use the IEEE 754
       
   885 floating\-point standard will support signed zeros.
       
   886 .sp
       
   887 Note that this only applies to floating\-point; neither JSON, C, or
       
   888 IEEE support the concept of signed integer zeros.
       
   889 .SS Types
       
   890 .sp
       
   891 No support is provided in Jansson for any C numeric types other than
       
   892 \fBjson_int_t\fP and \fBdouble\fP\&. This excludes things such as unsigned
       
   893 types, \fBlong double\fP, etc. Obviously, shorter types like \fBshort\fP,
       
   894 \fBint\fP, \fBlong\fP (if \fBjson_int_t\fP is \fBlong long\fP) and \fBfloat\fP
       
   895 are implicitly handled via the ordinary C type coercion rules (subject
       
   896 to overflow semantics). Also, no support or hooks are provided for any
       
   897 supplemental "bignum" type add\-on packages.
       
   898 .SS Portability
       
   899 .SS Thread safety
       
   900 .sp
       
   901 Jansson is thread safe and has no mutable global state. The only
       
   902 exceptions are the hash function seed and memory allocation functions,
       
   903 see below.
       
   904 .sp
       
   905 There\(aqs no locking performed inside Jansson\(aqs code, so a multithreaded
       
   906 program must perform its own locking if JSON values are shared by
       
   907 multiple threads. Jansson\(aqs reference counting semantics may make this
       
   908 a bit harder than it seems, as it\(aqs possible to have a reference to a
       
   909 value that\(aqs also stored inside a list or object. Modifying the
       
   910 container (adding or removing values) may trigger concurrent access to
       
   911 such values, as containers manage the reference count of their
       
   912 contained values. Bugs involving concurrent incrementing or
       
   913 decrementing of deference counts may be hard to track.
       
   914 .sp
       
   915 The encoding functions (\fBjson_dumps()\fP and friends) track
       
   916 reference loops by modifying the internal state of objects and arrays.
       
   917 For this reason, encoding functions must not be run on the same JSON
       
   918 values in two separate threads at the same time. As already noted
       
   919 above, be especially careful if two arrays or objects share their
       
   920 contained values with another array or object.
       
   921 .sp
       
   922 If you want to make sure that two JSON value hierarchies do not
       
   923 contain shared values, use \fBjson_deep_copy()\fP to make copies.
       
   924 .SS Hash function seed
       
   925 .sp
       
   926 To prevent an attacker from intentionally causing large JSON objects
       
   927 with specially crafted keys to perform very slow, the hash function
       
   928 used by Jansson is randomized using a seed value. The seed is
       
   929 automatically generated on the first explicit or implicit call to
       
   930 \fBjson_object()\fP, if \fBjson_object_seed()\fP has not been
       
   931 called beforehand.
       
   932 .sp
       
   933 The seed is generated by using operating system\(aqs entropy sources if
       
   934 they are available (\fB/dev/urandom\fP, \fBCryptGenRandom()\fP). The
       
   935 initialization is done in as thread safe manner as possible, by using
       
   936 architecture specific lockless operations if provided by the platform
       
   937 or the compiler.
       
   938 .sp
       
   939 If you\(aqre using threads, it\(aqs recommended to autoseed the hashtable
       
   940 explicitly before spawning any threads by calling
       
   941 \fBjson_object_seed(0)\fP , especially if you\(aqre unsure whether the
       
   942 initialization is thread safe on your platform.
       
   943 .SS Memory allocation functions
       
   944 .sp
       
   945 Memory allocation functions should be set at most once, and only on
       
   946 program startup. See \fIapiref\-custom\-memory\-allocation\fP\&.
       
   947 .SS Locale
       
   948 .sp
       
   949 Jansson works fine under any locale.
       
   950 .sp
       
   951 However, if the host program is multithreaded and uses \fBsetlocale()\fP
       
   952 to switch the locale in one thread while Jansson is currently encoding
       
   953 or decoding JSON data in another thread, the result may be wrong or
       
   954 the program may even crash.
       
   955 .sp
       
   956 Jansson uses locale specific functions for certain string conversions
       
   957 in the encoder and decoder, and then converts the locale specific
       
   958 values to/from the JSON representation. This fails if the locale
       
   959 changes between the string conversion and the locale\-to\-JSON
       
   960 conversion. This can only happen in multithreaded programs that use
       
   961 \fBsetlocale()\fP, because \fBsetlocale()\fP switches the locale for all
       
   962 running threads, not only the thread that calls \fBsetlocale()\fP\&.
       
   963 .sp
       
   964 If your program uses \fBsetlocale()\fP as described above, consider
       
   965 using the thread\-safe \fBuselocale()\fP instead.
       
   966 .SS API Reference
       
   967 .SS Preliminaries
       
   968 .sp
       
   969 All declarations are in \fBjansson.h\fP, so it\(aqs enough to
       
   970 .INDENT 0.0
       
   971 .INDENT 3.5
       
   972 .sp
       
   973 .nf
       
   974 .ft CW
       
   975 #include <jansson.h>
       
   976 .ft R
       
   977 .fi
       
   978 .UNINDENT
       
   979 .UNINDENT
       
   980 .sp
       
   981 in each source file.
       
   982 .sp
       
   983 All constants are prefixed with \fBJSON_\fP (except for those describing
       
   984 the library version, prefixed with \fBJANSSON_\fP). Other identifiers
       
   985 are prefixed with \fBjson_\fP\&. Type names are suffixed with \fB_t\fP and
       
   986 \fBtypedef\fP\(aqd so that the \fBstruct\fP keyword need not be used.
       
   987 .SS Library Version
       
   988 .sp
       
   989 The Jansson version is of the form \fIA.B.C\fP, where \fIA\fP is the major
       
   990 version, \fIB\fP is the minor version and \fIC\fP is the micro version. If the
       
   991 micro version is zero, it\(aqs omitted from the version string, i.e. the
       
   992 version string is just \fIA.B\fP\&.
       
   993 .sp
       
   994 When a new release only fixes bugs and doesn\(aqt add new features or
       
   995 functionality, the micro version is incremented. When new features are
       
   996 added in a backwards compatible way, the minor version is incremented
       
   997 and the micro version is set to zero. When there are backwards
       
   998 incompatible changes, the major version is incremented and others are
       
   999 set to zero.
       
  1000 .sp
       
  1001 The following preprocessor constants specify the current version of
       
  1002 the library:
       
  1003 .INDENT 0.0
       
  1004 .TP
       
  1005 .B \fBJANSSON_MAJOR_VERSION\fP, \fBJANSSON_MINOR_VERSION\fP, \fBJANSSON_MICRO_VERSION\fP
       
  1006 Integers specifying the major, minor and micro versions,
       
  1007 respectively.
       
  1008 .TP
       
  1009 .B \fBJANSSON_VERSION\fP
       
  1010 A string representation of the current version, e.g. \fB"1.2.1"\fP or
       
  1011 \fB"1.3"\fP\&.
       
  1012 .TP
       
  1013 .B \fBJANSSON_VERSION_HEX\fP
       
  1014 A 3\-byte hexadecimal representation of the version, e.g.
       
  1015 \fB0x010201\fP for version 1.2.1 and \fB0x010300\fP for version 1.3.
       
  1016 This is useful in numeric comparisions, e.g.:
       
  1017 .INDENT 7.0
       
  1018 .INDENT 3.5
       
  1019 .sp
       
  1020 .nf
       
  1021 .ft CW
       
  1022 #if JANSSON_VERSION_HEX >= 0x010300
       
  1023 /* Code specific to version 1.3 and above */
       
  1024 #endif
       
  1025 .ft R
       
  1026 .fi
       
  1027 .UNINDENT
       
  1028 .UNINDENT
       
  1029 .UNINDENT
       
  1030 .SS Value Representation
       
  1031 .sp
       
  1032 The JSON specification (\fI\%RFC 4627\fP) defines the following data types:
       
  1033 \fIobject\fP, \fIarray\fP, \fIstring\fP, \fInumber\fP, \fIboolean\fP, and \fInull\fP\&. JSON
       
  1034 types are used dynamically; arrays and objects can hold any other data
       
  1035 type, including themselves. For this reason, Jansson\(aqs type system is
       
  1036 also dynamic in nature. There\(aqs one C type to represent all JSON
       
  1037 values, and this structure knows the type of the JSON value it holds.
       
  1038 .INDENT 0.0
       
  1039 .TP
       
  1040 .B json_t
       
  1041 This data structure is used throughout the library to represent all
       
  1042 JSON values. It always contains the type of the JSON value it holds
       
  1043 and the value\(aqs reference count. The rest depends on the type of the
       
  1044 value.
       
  1045 .UNINDENT
       
  1046 .sp
       
  1047 Objects of \fBjson_t\fP are always used through a pointer. There
       
  1048 are APIs for querying the type, manipulating the reference count, and
       
  1049 for constructing and manipulating values of different types.
       
  1050 .sp
       
  1051 Unless noted otherwise, all API functions return an error value if an
       
  1052 error occurs. Depending on the function\(aqs signature, the error value
       
  1053 is either \fINULL\fP or \-1. Invalid arguments or invalid input are
       
  1054 apparent sources for errors. Memory allocation and I/O operations may
       
  1055 also cause errors.
       
  1056 .SS Type
       
  1057 .sp
       
  1058 The type of a JSON value is queried and tested using the following
       
  1059 functions:
       
  1060 .INDENT 0.0
       
  1061 .TP
       
  1062 .B enum json_type
       
  1063 The type of a JSON value. The following members are defined:
       
  1064 .TS
       
  1065 center;
       
  1066 |l|.
       
  1067 _
       
  1068 T{
       
  1069 \fBJSON_OBJECT\fP
       
  1070 T}
       
  1071 _
       
  1072 T{
       
  1073 \fBJSON_ARRAY\fP
       
  1074 T}
       
  1075 _
       
  1076 T{
       
  1077 \fBJSON_STRING\fP
       
  1078 T}
       
  1079 _
       
  1080 T{
       
  1081 \fBJSON_INTEGER\fP
       
  1082 T}
       
  1083 _
       
  1084 T{
       
  1085 \fBJSON_REAL\fP
       
  1086 T}
       
  1087 _
       
  1088 T{
       
  1089 \fBJSON_TRUE\fP
       
  1090 T}
       
  1091 _
       
  1092 T{
       
  1093 \fBJSON_FALSE\fP
       
  1094 T}
       
  1095 _
       
  1096 T{
       
  1097 \fBJSON_NULL\fP
       
  1098 T}
       
  1099 _
       
  1100 .TE
       
  1101 .sp
       
  1102 These correspond to JSON object, array, string, number, boolean and
       
  1103 null. A number is represented by either a value of the type
       
  1104 \fBJSON_INTEGER\fP or of the type \fBJSON_REAL\fP\&. A true boolean value
       
  1105 is represented by a value of the type \fBJSON_TRUE\fP and false by a
       
  1106 value of the type \fBJSON_FALSE\fP\&.
       
  1107 .UNINDENT
       
  1108 .INDENT 0.0
       
  1109 .TP
       
  1110 .B int json_typeof(const json_t\fI\ *json\fP)
       
  1111 Return the type of the JSON value (a \fBjson_type\fP cast to
       
  1112 \fBint\fP). \fIjson\fP MUST NOT be \fINULL\fP\&. This function is actually
       
  1113 implemented as a macro for speed.
       
  1114 .UNINDENT
       
  1115 .INDENT 0.0
       
  1116 .TP
       
  1117 .B json_is_object(const json_t\fI\ *json\fP)
       
  1118 .TP
       
  1119 .B json_is_array(const json_t\fI\ *json\fP)
       
  1120 .TP
       
  1121 .B json_is_string(const json_t\fI\ *json\fP)
       
  1122 .TP
       
  1123 .B json_is_integer(const json_t\fI\ *json\fP)
       
  1124 .TP
       
  1125 .B json_is_real(const json_t\fI\ *json\fP)
       
  1126 .TP
       
  1127 .B json_is_true(const json_t\fI\ *json\fP)
       
  1128 .TP
       
  1129 .B json_is_false(const json_t\fI\ *json\fP)
       
  1130 .TP
       
  1131 .B json_is_null(const json_t\fI\ *json\fP)
       
  1132 These functions (actually macros) return true (non\-zero) for values
       
  1133 of the given type, and false (zero) for values of other types and
       
  1134 for \fINULL\fP\&.
       
  1135 .UNINDENT
       
  1136 .INDENT 0.0
       
  1137 .TP
       
  1138 .B json_is_number(const json_t\fI\ *json\fP)
       
  1139 Returns true for values of types \fBJSON_INTEGER\fP and
       
  1140 \fBJSON_REAL\fP, and false for other types and for \fINULL\fP\&.
       
  1141 .UNINDENT
       
  1142 .INDENT 0.0
       
  1143 .TP
       
  1144 .B json_is_boolean(const json_t\fI\ *json\fP)
       
  1145 Returns true for types \fBJSON_TRUE\fP and \fBJSON_FALSE\fP, and false
       
  1146 for values of other types and for \fINULL\fP\&.
       
  1147 .UNINDENT
       
  1148 .INDENT 0.0
       
  1149 .TP
       
  1150 .B json_boolean_value(const json_t\fI\ *json\fP)
       
  1151 Alias of \fBjson_is_true()\fP, i.e. returns 1 for \fBJSON_TRUE\fP
       
  1152 and 0 otherwise.
       
  1153 .sp
       
  1154 New in version 2.7.
       
  1155 
       
  1156 .UNINDENT
       
  1157 .SS Reference Count
       
  1158 .sp
       
  1159 The reference count is used to track whether a value is still in use
       
  1160 or not. When a value is created, it\(aqs reference count is set to 1. If
       
  1161 a reference to a value is kept (e.g. a value is stored somewhere for
       
  1162 later use), its reference count is incremented, and when the value is
       
  1163 no longer needed, the reference count is decremented. When the
       
  1164 reference count drops to zero, there are no references left, and the
       
  1165 value can be destroyed.
       
  1166 .sp
       
  1167 The following functions are used to manipulate the reference count.
       
  1168 .INDENT 0.0
       
  1169 .TP
       
  1170 .B json_t *json_incref(json_t\fI\ *json\fP)
       
  1171 Increment the reference count of \fIjson\fP if it\(aqs not \fINULL\fP\&.
       
  1172 Returns \fIjson\fP\&.
       
  1173 .UNINDENT
       
  1174 .INDENT 0.0
       
  1175 .TP
       
  1176 .B void json_decref(json_t\fI\ *json\fP)
       
  1177 Decrement the reference count of \fIjson\fP\&. As soon as a call to
       
  1178 \fBjson_decref()\fP drops the reference count to zero, the value
       
  1179 is destroyed and it can no longer be used.
       
  1180 .UNINDENT
       
  1181 .sp
       
  1182 Functions creating new JSON values set the reference count to 1. These
       
  1183 functions are said to return a \fBnew reference\fP\&. Other functions
       
  1184 returning (existing) JSON values do not normally increase the
       
  1185 reference count. These functions are said to return a \fBborrowed
       
  1186 reference\fP\&. So, if the user will hold a reference to a value returned
       
  1187 as a borrowed reference, he must call \fBjson_incref()\fP\&. As soon as
       
  1188 the value is no longer needed, \fBjson_decref()\fP should be called
       
  1189 to release the reference.
       
  1190 .sp
       
  1191 Normally, all functions accepting a JSON value as an argument will
       
  1192 manage the reference, i.e. increase and decrease the reference count
       
  1193 as needed. However, some functions \fBsteal\fP the reference, i.e. they
       
  1194 have the same result as if the user called \fBjson_decref()\fP on
       
  1195 the argument right after calling the function. These functions are
       
  1196 suffixed with \fB_new\fP or have \fB_new_\fP somewhere in their name.
       
  1197 .sp
       
  1198 For example, the following code creates a new JSON array and appends
       
  1199 an integer to it:
       
  1200 .INDENT 0.0
       
  1201 .INDENT 3.5
       
  1202 .sp
       
  1203 .nf
       
  1204 .ft CW
       
  1205 json_t *array, *integer;
       
  1206 
       
  1207 array = json_array();
       
  1208 integer = json_integer(42);
       
  1209 
       
  1210 json_array_append(array, integer);
       
  1211 json_decref(integer);
       
  1212 .ft R
       
  1213 .fi
       
  1214 .UNINDENT
       
  1215 .UNINDENT
       
  1216 .sp
       
  1217 Note how the caller has to release the reference to the integer value
       
  1218 by calling \fBjson_decref()\fP\&. By using a reference stealing
       
  1219 function \fBjson_array_append_new()\fP instead of
       
  1220 \fBjson_array_append()\fP, the code becomes much simpler:
       
  1221 .INDENT 0.0
       
  1222 .INDENT 3.5
       
  1223 .sp
       
  1224 .nf
       
  1225 .ft CW
       
  1226 json_t *array = json_array();
       
  1227 json_array_append_new(array, json_integer(42));
       
  1228 .ft R
       
  1229 .fi
       
  1230 .UNINDENT
       
  1231 .UNINDENT
       
  1232 .sp
       
  1233 In this case, the user doesn\(aqt have to explicitly release the
       
  1234 reference to the integer value, as \fBjson_array_append_new()\fP
       
  1235 steals the reference when appending the value to the array.
       
  1236 .sp
       
  1237 In the following sections it is clearly documented whether a function
       
  1238 will return a new or borrowed reference or steal a reference to its
       
  1239 argument.
       
  1240 .SS Circular References
       
  1241 .sp
       
  1242 A circular reference is created when an object or an array is,
       
  1243 directly or indirectly, inserted inside itself. The direct case is
       
  1244 simple:
       
  1245 .INDENT 0.0
       
  1246 .INDENT 3.5
       
  1247 .sp
       
  1248 .nf
       
  1249 .ft CW
       
  1250 json_t *obj = json_object();
       
  1251 json_object_set(obj, "foo", obj);
       
  1252 .ft R
       
  1253 .fi
       
  1254 .UNINDENT
       
  1255 .UNINDENT
       
  1256 .sp
       
  1257 Jansson will refuse to do this, and \fBjson_object_set()\fP (and
       
  1258 all the other such functions for objects and arrays) will return with
       
  1259 an error status. The indirect case is the dangerous one:
       
  1260 .INDENT 0.0
       
  1261 .INDENT 3.5
       
  1262 .sp
       
  1263 .nf
       
  1264 .ft CW
       
  1265 json_t *arr1 = json_array(), *arr2 = json_array();
       
  1266 json_array_append(arr1, arr2);
       
  1267 json_array_append(arr2, arr1);
       
  1268 .ft R
       
  1269 .fi
       
  1270 .UNINDENT
       
  1271 .UNINDENT
       
  1272 .sp
       
  1273 In this example, the array \fBarr2\fP is contained in the array
       
  1274 \fBarr1\fP, and vice versa. Jansson cannot check for this kind of
       
  1275 indirect circular references without a performance hit, so it\(aqs up to
       
  1276 the user to avoid them.
       
  1277 .sp
       
  1278 If a circular reference is created, the memory consumed by the values
       
  1279 cannot be freed by \fBjson_decref()\fP\&. The reference counts never
       
  1280 drops to zero because the values are keeping the references to each
       
  1281 other. Moreover, trying to encode the values with any of the encoding
       
  1282 functions will fail. The encoder detects circular references and
       
  1283 returns an error status.
       
  1284 .SS True, False and Null
       
  1285 .sp
       
  1286 These three values are implemented as singletons, so the returned
       
  1287 pointers won\(aqt change between invocations of these functions.
       
  1288 .INDENT 0.0
       
  1289 .TP
       
  1290 .B json_t *json_true(void)
       
  1291 Return value: New reference.
       
  1292 .sp
       
  1293 Returns the JSON true value.
       
  1294 .UNINDENT
       
  1295 .INDENT 0.0
       
  1296 .TP
       
  1297 .B json_t *json_false(void)
       
  1298 Return value: New reference.
       
  1299 .sp
       
  1300 Returns the JSON false value.
       
  1301 .UNINDENT
       
  1302 .INDENT 0.0
       
  1303 .TP
       
  1304 .B json_t *json_boolean(val)
       
  1305 Return value: New reference.
       
  1306 .sp
       
  1307 Returns JSON false if \fBval\fP is zero, and JSON true otherwise.
       
  1308 This is a macro, and equivalent to \fBval ? json_true() :
       
  1309 json_false()\fP\&.
       
  1310 .sp
       
  1311 New in version 2.4.
       
  1312 
       
  1313 .UNINDENT
       
  1314 .INDENT 0.0
       
  1315 .TP
       
  1316 .B json_t *json_null(void)
       
  1317 Return value: New reference.
       
  1318 .sp
       
  1319 Returns the JSON null value.
       
  1320 .UNINDENT
       
  1321 .SS String
       
  1322 .sp
       
  1323 Jansson uses UTF\-8 as the character encoding. All JSON strings must be
       
  1324 valid UTF\-8 (or ASCII, as it\(aqs a subset of UTF\-8). All Unicode
       
  1325 codepoints U+0000 through U+10FFFF are allowed, but you must use
       
  1326 length\-aware functions if you wish to embed NUL bytes in strings.
       
  1327 .INDENT 0.0
       
  1328 .TP
       
  1329 .B json_t *json_string(const char\fI\ *value\fP)
       
  1330 Return value: New reference.
       
  1331 .sp
       
  1332 Returns a new JSON string, or \fINULL\fP on error. \fIvalue\fP must be a
       
  1333 valid null terminated UTF\-8 encoded Unicode string.
       
  1334 .UNINDENT
       
  1335 .INDENT 0.0
       
  1336 .TP
       
  1337 .B json_t *json_stringn(const char\fI\ *value\fP, size_t\fI\ len\fP)
       
  1338 Return value: New reference.
       
  1339 .sp
       
  1340 Like \fBjson_string()\fP, but with explicit length, so \fIvalue\fP may
       
  1341 contain null characters or not be null terminated.
       
  1342 .UNINDENT
       
  1343 .INDENT 0.0
       
  1344 .TP
       
  1345 .B json_t *json_string_nocheck(const char\fI\ *value\fP)
       
  1346 Return value: New reference.
       
  1347 .sp
       
  1348 Like \fBjson_string()\fP, but doesn\(aqt check that \fIvalue\fP is valid
       
  1349 UTF\-8. Use this function only if you are certain that this really
       
  1350 is the case (e.g. you have already checked it by other means).
       
  1351 .UNINDENT
       
  1352 .INDENT 0.0
       
  1353 .TP
       
  1354 .B json_t *json_stringn_nocheck(const char\fI\ *value\fP, size_t\fI\ len\fP)
       
  1355 Return value: New reference.
       
  1356 .sp
       
  1357 Like \fBjson_string_nocheck()\fP, but with explicit length, so
       
  1358 \fIvalue\fP may contain null characters or not be null terminated.
       
  1359 .UNINDENT
       
  1360 .INDENT 0.0
       
  1361 .TP
       
  1362 .B const char *json_string_value(const json_t\fI\ *string\fP)
       
  1363 Returns the associated value of \fIstring\fP as a null terminated UTF\-8
       
  1364 encoded string, or \fINULL\fP if \fIstring\fP is not a JSON string.
       
  1365 .sp
       
  1366 The retuned value is read\-only and must not be modified or freed by
       
  1367 the user. It is valid as long as \fIstring\fP exists, i.e. as long as
       
  1368 its reference count has not dropped to zero.
       
  1369 .UNINDENT
       
  1370 .INDENT 0.0
       
  1371 .TP
       
  1372 .B size_t json_string_length(const json_t\fI\ *string\fP)
       
  1373 Returns the length of \fIstring\fP in its UTF\-8 presentation, or zero
       
  1374 if \fIstring\fP is not a JSON string.
       
  1375 .UNINDENT
       
  1376 .INDENT 0.0
       
  1377 .TP
       
  1378 .B int json_string_set(const json_t\fI\ *string\fP, const char\fI\ *value\fP)
       
  1379 Sets the associated value of \fIstring\fP to \fIvalue\fP\&. \fIvalue\fP must be a
       
  1380 valid UTF\-8 encoded Unicode string. Returns 0 on success and \-1 on
       
  1381 error.
       
  1382 .UNINDENT
       
  1383 .INDENT 0.0
       
  1384 .TP
       
  1385 .B int json_string_setn(json_t\fI\ *string\fP, const char\fI\ *value\fP, size_t\fI\ len\fP)
       
  1386 Like \fBjson_string_set()\fP, but with explicit length, so \fIvalue\fP
       
  1387 may contain null characters or not be null terminated.
       
  1388 .UNINDENT
       
  1389 .INDENT 0.0
       
  1390 .TP
       
  1391 .B int json_string_set_nocheck(const json_t\fI\ *string\fP, const char\fI\ *value\fP)
       
  1392 Like \fBjson_string_set()\fP, but doesn\(aqt check that \fIvalue\fP is
       
  1393 valid UTF\-8. Use this function only if you are certain that this
       
  1394 really is the case (e.g. you have already checked it by other
       
  1395 means).
       
  1396 .UNINDENT
       
  1397 .INDENT 0.0
       
  1398 .TP
       
  1399 .B int json_string_setn_nocheck(json_t\fI\ *string\fP, const char\fI\ *value\fP, size_t\fI\ len\fP)
       
  1400 Like \fBjson_string_set_nocheck()\fP, but with explicit length,
       
  1401 so \fIvalue\fP may contain null characters or not be null terminated.
       
  1402 .UNINDENT
       
  1403 .SS Number
       
  1404 .sp
       
  1405 The JSON specification only contains one numeric type, "number". The C
       
  1406 programming language has distinct types for integer and floating\-point
       
  1407 numbers, so for practical reasons Jansson also has distinct types for
       
  1408 the two. They are called "integer" and "real", respectively. For more
       
  1409 information, see \fIrfc\-conformance\fP\&.
       
  1410 .INDENT 0.0
       
  1411 .TP
       
  1412 .B json_int_t
       
  1413 This is the C type that is used to store JSON integer values. It
       
  1414 represents the widest integer type available on your system. In
       
  1415 practice it\(aqs just a typedef of \fBlong long\fP if your compiler
       
  1416 supports it, otherwise \fBlong\fP\&.
       
  1417 .sp
       
  1418 Usually, you can safely use plain \fBint\fP in place of
       
  1419 \fBjson_int_t\fP, and the implicit C integer conversion handles the
       
  1420 rest. Only when you know that you need the full 64\-bit range, you
       
  1421 should use \fBjson_int_t\fP explicitly.
       
  1422 .UNINDENT
       
  1423 .INDENT 0.0
       
  1424 .TP
       
  1425 .B \fBJSON_INTEGER_IS_LONG_LONG\fP
       
  1426 This is a preprocessor variable that holds the value 1 if
       
  1427 \fBjson_int_t\fP is \fBlong long\fP, and 0 if it\(aqs \fBlong\fP\&. It
       
  1428 can be used as follows:
       
  1429 .INDENT 7.0
       
  1430 .INDENT 3.5
       
  1431 .sp
       
  1432 .nf
       
  1433 .ft CW
       
  1434 #if JSON_INTEGER_IS_LONG_LONG
       
  1435 /* Code specific for long long */
       
  1436 #else
       
  1437 /* Code specific for long */
       
  1438 #endif
       
  1439 .ft R
       
  1440 .fi
       
  1441 .UNINDENT
       
  1442 .UNINDENT
       
  1443 .TP
       
  1444 .B \fBJSON_INTEGER_FORMAT\fP
       
  1445 This is a macro that expands to a \fBprintf()\fP conversion
       
  1446 specifier that corresponds to \fBjson_int_t\fP, without the
       
  1447 leading \fB%\fP sign, i.e. either \fB"lld"\fP or \fB"ld"\fP\&. This macro
       
  1448 is required because the actual type of \fBjson_int_t\fP can be
       
  1449 either \fBlong\fP or \fBlong long\fP, and \fBprintf()\fP reuiqres
       
  1450 different length modifiers for the two.
       
  1451 .sp
       
  1452 Example:
       
  1453 .INDENT 7.0
       
  1454 .INDENT 3.5
       
  1455 .sp
       
  1456 .nf
       
  1457 .ft CW
       
  1458 json_int_t x = 123123123;
       
  1459 printf("x is %" JSON_INTEGER_FORMAT "\en", x);
       
  1460 .ft R
       
  1461 .fi
       
  1462 .UNINDENT
       
  1463 .UNINDENT
       
  1464 .UNINDENT
       
  1465 .INDENT 0.0
       
  1466 .TP
       
  1467 .B json_t *json_integer(json_int_t\fI\ value\fP)
       
  1468 Return value: New reference.
       
  1469 .sp
       
  1470 Returns a new JSON integer, or \fINULL\fP on error.
       
  1471 .UNINDENT
       
  1472 .INDENT 0.0
       
  1473 .TP
       
  1474 .B json_int_t json_integer_value(const json_t\fI\ *integer\fP)
       
  1475 Returns the associated value of \fIinteger\fP, or 0 if \fIjson\fP is not a
       
  1476 JSON integer.
       
  1477 .UNINDENT
       
  1478 .INDENT 0.0
       
  1479 .TP
       
  1480 .B int json_integer_set(const json_t\fI\ *integer\fP, json_int_t\fI\ value\fP)
       
  1481 Sets the associated value of \fIinteger\fP to \fIvalue\fP\&. Returns 0 on
       
  1482 success and \-1 if \fIinteger\fP is not a JSON integer.
       
  1483 .UNINDENT
       
  1484 .INDENT 0.0
       
  1485 .TP
       
  1486 .B json_t *json_real(double\fI\ value\fP)
       
  1487 Return value: New reference.
       
  1488 .sp
       
  1489 Returns a new JSON real, or \fINULL\fP on error.
       
  1490 .UNINDENT
       
  1491 .INDENT 0.0
       
  1492 .TP
       
  1493 .B double json_real_value(const json_t\fI\ *real\fP)
       
  1494 Returns the associated value of \fIreal\fP, or 0.0 if \fIreal\fP is not a
       
  1495 JSON real.
       
  1496 .UNINDENT
       
  1497 .INDENT 0.0
       
  1498 .TP
       
  1499 .B int json_real_set(const json_t\fI\ *real\fP, double\fI\ value\fP)
       
  1500 Sets the associated value of \fIreal\fP to \fIvalue\fP\&. Returns 0 on
       
  1501 success and \-1 if \fIreal\fP is not a JSON real.
       
  1502 .UNINDENT
       
  1503 .sp
       
  1504 In addition to the functions above, there\(aqs a common query function
       
  1505 for integers and reals:
       
  1506 .INDENT 0.0
       
  1507 .TP
       
  1508 .B double json_number_value(const json_t\fI\ *json\fP)
       
  1509 Returns the associated value of the JSON integer or JSON real
       
  1510 \fIjson\fP, cast to double regardless of the actual type. If \fIjson\fP is
       
  1511 neither JSON real nor JSON integer, 0.0 is returned.
       
  1512 .UNINDENT
       
  1513 .SS Array
       
  1514 .sp
       
  1515 A JSON array is an ordered collection of other JSON values.
       
  1516 .INDENT 0.0
       
  1517 .TP
       
  1518 .B json_t *json_array(void)
       
  1519 Return value: New reference.
       
  1520 .sp
       
  1521 Returns a new JSON array, or \fINULL\fP on error. Initially, the array
       
  1522 is empty.
       
  1523 .UNINDENT
       
  1524 .INDENT 0.0
       
  1525 .TP
       
  1526 .B size_t json_array_size(const json_t\fI\ *array\fP)
       
  1527 Returns the number of elements in \fIarray\fP, or 0 if \fIarray\fP is NULL
       
  1528 or not a JSON array.
       
  1529 .UNINDENT
       
  1530 .INDENT 0.0
       
  1531 .TP
       
  1532 .B json_t *json_array_get(const json_t\fI\ *array\fP, size_t\fI\ index\fP)
       
  1533 Return value: Borrowed reference.
       
  1534 .sp
       
  1535 Returns the element in \fIarray\fP at position \fIindex\fP\&. The valid range
       
  1536 for \fIindex\fP is from 0 to the return value of
       
  1537 \fBjson_array_size()\fP minus 1. If \fIarray\fP is not a JSON array,
       
  1538 if \fIarray\fP is \fINULL\fP, or if \fIindex\fP is out of range, \fINULL\fP is
       
  1539 returned.
       
  1540 .UNINDENT
       
  1541 .INDENT 0.0
       
  1542 .TP
       
  1543 .B int json_array_set(json_t\fI\ *array\fP, size_t\fI\ index\fP, json_t\fI\ *value\fP)
       
  1544 Replaces the element in \fIarray\fP at position \fIindex\fP with \fIvalue\fP\&.
       
  1545 The valid range for \fIindex\fP is from 0 to the return value of
       
  1546 \fBjson_array_size()\fP minus 1. Returns 0 on success and \-1 on
       
  1547 error.
       
  1548 .UNINDENT
       
  1549 .INDENT 0.0
       
  1550 .TP
       
  1551 .B int json_array_set_new(json_t\fI\ *array\fP, size_t\fI\ index\fP, json_t\fI\ *value\fP)
       
  1552 Like \fBjson_array_set()\fP but steals the reference to \fIvalue\fP\&.
       
  1553 This is useful when \fIvalue\fP is newly created and not used after
       
  1554 the call.
       
  1555 .UNINDENT
       
  1556 .INDENT 0.0
       
  1557 .TP
       
  1558 .B int json_array_append(json_t\fI\ *array\fP, json_t\fI\ *value\fP)
       
  1559 Appends \fIvalue\fP to the end of \fIarray\fP, growing the size of \fIarray\fP
       
  1560 by 1. Returns 0 on success and \-1 on error.
       
  1561 .UNINDENT
       
  1562 .INDENT 0.0
       
  1563 .TP
       
  1564 .B int json_array_append_new(json_t\fI\ *array\fP, json_t\fI\ *value\fP)
       
  1565 Like \fBjson_array_append()\fP but steals the reference to
       
  1566 \fIvalue\fP\&. This is useful when \fIvalue\fP is newly created and not used
       
  1567 after the call.
       
  1568 .UNINDENT
       
  1569 .INDENT 0.0
       
  1570 .TP
       
  1571 .B int json_array_insert(json_t\fI\ *array\fP, size_t\fI\ index\fP, json_t\fI\ *value\fP)
       
  1572 Inserts \fIvalue\fP to \fIarray\fP at position \fIindex\fP, shifting the
       
  1573 elements at \fIindex\fP and after it one position towards the end of
       
  1574 the array. Returns 0 on success and \-1 on error.
       
  1575 .UNINDENT
       
  1576 .INDENT 0.0
       
  1577 .TP
       
  1578 .B int json_array_insert_new(json_t\fI\ *array\fP, size_t\fI\ index\fP, json_t\fI\ *value\fP)
       
  1579 Like \fBjson_array_insert()\fP but steals the reference to
       
  1580 \fIvalue\fP\&. This is useful when \fIvalue\fP is newly created and not used
       
  1581 after the call.
       
  1582 .UNINDENT
       
  1583 .INDENT 0.0
       
  1584 .TP
       
  1585 .B int json_array_remove(json_t\fI\ *array\fP, size_t\fI\ index\fP)
       
  1586 Removes the element in \fIarray\fP at position \fIindex\fP, shifting the
       
  1587 elements after \fIindex\fP one position towards the start of the array.
       
  1588 Returns 0 on success and \-1 on error. The reference count of the
       
  1589 removed value is decremented.
       
  1590 .UNINDENT
       
  1591 .INDENT 0.0
       
  1592 .TP
       
  1593 .B int json_array_clear(json_t\fI\ *array\fP)
       
  1594 Removes all elements from \fIarray\fP\&. Returns 0 on sucess and \-1 on
       
  1595 error. The reference count of all removed values are decremented.
       
  1596 .UNINDENT
       
  1597 .INDENT 0.0
       
  1598 .TP
       
  1599 .B int json_array_extend(json_t\fI\ *array\fP, json_t\fI\ *other_array\fP)
       
  1600 Appends all elements in \fIother_array\fP to the end of \fIarray\fP\&.
       
  1601 Returns 0 on success and \-1 on error.
       
  1602 .UNINDENT
       
  1603 .sp
       
  1604 The following macro can be used to iterate through all elements
       
  1605 in an array.
       
  1606 .INDENT 0.0
       
  1607 .TP
       
  1608 .B json_array_foreach(array, index, value)
       
  1609 Iterate over every element of \fBarray\fP, running the block
       
  1610 of code that follows each time with the proper values set to
       
  1611 variables \fBindex\fP and \fBvalue\fP, of types \fBsize_t\fP and
       
  1612 \fBjson_t *\fP respectively. Example:
       
  1613 .INDENT 7.0
       
  1614 .INDENT 3.5
       
  1615 .sp
       
  1616 .nf
       
  1617 .ft CW
       
  1618 /* array is a JSON array */
       
  1619 size_t index;
       
  1620 json_t *value;
       
  1621 
       
  1622 json_array_foreach(array, index, value) {
       
  1623     /* block of code that uses index and value */
       
  1624 }
       
  1625 .ft R
       
  1626 .fi
       
  1627 .UNINDENT
       
  1628 .UNINDENT
       
  1629 .sp
       
  1630 The items are returned in increasing index order.
       
  1631 .sp
       
  1632 This macro expands to an ordinary \fBfor\fP statement upon
       
  1633 preprocessing, so its performance is equivalent to that of
       
  1634 hand\-written code using the array access functions.
       
  1635 The main advantage of this macro is that it abstracts
       
  1636 away the complexity, and makes for shorter, more
       
  1637 concise code.
       
  1638 .sp
       
  1639 New in version 2.5.
       
  1640 
       
  1641 .UNINDENT
       
  1642 .SS Object
       
  1643 .sp
       
  1644 A JSON object is a dictionary of key\-value pairs, where the key is a
       
  1645 Unicode string and the value is any JSON value.
       
  1646 .sp
       
  1647 Even though NUL bytes are allowed in string values, they are not
       
  1648 allowed in object keys.
       
  1649 .INDENT 0.0
       
  1650 .TP
       
  1651 .B json_t *json_object(void)
       
  1652 Return value: New reference.
       
  1653 .sp
       
  1654 Returns a new JSON object, or \fINULL\fP on error. Initially, the
       
  1655 object is empty.
       
  1656 .UNINDENT
       
  1657 .INDENT 0.0
       
  1658 .TP
       
  1659 .B size_t json_object_size(const json_t\fI\ *object\fP)
       
  1660 Returns the number of elements in \fIobject\fP, or 0 if \fIobject\fP is not
       
  1661 a JSON object.
       
  1662 .UNINDENT
       
  1663 .INDENT 0.0
       
  1664 .TP
       
  1665 .B json_t *json_object_get(const json_t\fI\ *object\fP, const char\fI\ *key\fP)
       
  1666 Return value: Borrowed reference.
       
  1667 .sp
       
  1668 Get a value corresponding to \fIkey\fP from \fIobject\fP\&. Returns \fINULL\fP if
       
  1669 \fIkey\fP is not found and on error.
       
  1670 .UNINDENT
       
  1671 .INDENT 0.0
       
  1672 .TP
       
  1673 .B int json_object_set(json_t\fI\ *object\fP, const char\fI\ *key\fP, json_t\fI\ *value\fP)
       
  1674 Set the value of \fIkey\fP to \fIvalue\fP in \fIobject\fP\&. \fIkey\fP must be a
       
  1675 valid null terminated UTF\-8 encoded Unicode string. If there
       
  1676 already is a value for \fIkey\fP, it is replaced by the new value.
       
  1677 Returns 0 on success and \-1 on error.
       
  1678 .UNINDENT
       
  1679 .INDENT 0.0
       
  1680 .TP
       
  1681 .B int json_object_set_nocheck(json_t\fI\ *object\fP, const char\fI\ *key\fP, json_t\fI\ *value\fP)
       
  1682 Like \fBjson_object_set()\fP, but doesn\(aqt check that \fIkey\fP is
       
  1683 valid UTF\-8. Use this function only if you are certain that this
       
  1684 really is the case (e.g. you have already checked it by other
       
  1685 means).
       
  1686 .UNINDENT
       
  1687 .INDENT 0.0
       
  1688 .TP
       
  1689 .B int json_object_set_new(json_t\fI\ *object\fP, const char\fI\ *key\fP, json_t\fI\ *value\fP)
       
  1690 Like \fBjson_object_set()\fP but steals the reference to
       
  1691 \fIvalue\fP\&. This is useful when \fIvalue\fP is newly created and not used
       
  1692 after the call.
       
  1693 .UNINDENT
       
  1694 .INDENT 0.0
       
  1695 .TP
       
  1696 .B int json_object_set_new_nocheck(json_t\fI\ *object\fP, const char\fI\ *key\fP, json_t\fI\ *value\fP)
       
  1697 Like \fBjson_object_set_new()\fP, but doesn\(aqt check that \fIkey\fP is
       
  1698 valid UTF\-8. Use this function only if you are certain that this
       
  1699 really is the case (e.g. you have already checked it by other
       
  1700 means).
       
  1701 .UNINDENT
       
  1702 .INDENT 0.0
       
  1703 .TP
       
  1704 .B int json_object_del(json_t\fI\ *object\fP, const char\fI\ *key\fP)
       
  1705 Delete \fIkey\fP from \fIobject\fP if it exists. Returns 0 on success, or
       
  1706 \-1 if \fIkey\fP was not found. The reference count of the removed value
       
  1707 is decremented.
       
  1708 .UNINDENT
       
  1709 .INDENT 0.0
       
  1710 .TP
       
  1711 .B int json_object_clear(json_t\fI\ *object\fP)
       
  1712 Remove all elements from \fIobject\fP\&. Returns 0 on success and \-1 if
       
  1713 \fIobject\fP is not a JSON object. The reference count of all removed
       
  1714 values are decremented.
       
  1715 .UNINDENT
       
  1716 .INDENT 0.0
       
  1717 .TP
       
  1718 .B int json_object_update(json_t\fI\ *object\fP, json_t\fI\ *other\fP)
       
  1719 Update \fIobject\fP with the key\-value pairs from \fIother\fP, overwriting
       
  1720 existing keys. Returns 0 on success or \-1 on error.
       
  1721 .UNINDENT
       
  1722 .INDENT 0.0
       
  1723 .TP
       
  1724 .B int json_object_update_existing(json_t\fI\ *object\fP, json_t\fI\ *other\fP)
       
  1725 Like \fBjson_object_update()\fP, but only the values of existing
       
  1726 keys are updated. No new keys are created. Returns 0 on success or
       
  1727 \-1 on error.
       
  1728 .sp
       
  1729 New in version 2.3.
       
  1730 
       
  1731 .UNINDENT
       
  1732 .INDENT 0.0
       
  1733 .TP
       
  1734 .B int json_object_update_missing(json_t\fI\ *object\fP, json_t\fI\ *other\fP)
       
  1735 Like \fBjson_object_update()\fP, but only new keys are created.
       
  1736 The value of any existing key is not changed. Returns 0 on success
       
  1737 or \-1 on error.
       
  1738 .sp
       
  1739 New in version 2.3.
       
  1740 
       
  1741 .UNINDENT
       
  1742 .sp
       
  1743 The following macro can be used to iterate through all key\-value pairs
       
  1744 in an object.
       
  1745 .INDENT 0.0
       
  1746 .TP
       
  1747 .B json_object_foreach(object, key, value)
       
  1748 Iterate over every key\-value pair of \fBobject\fP, running the block
       
  1749 of code that follows each time with the proper values set to
       
  1750 variables \fBkey\fP and \fBvalue\fP, of types \fBconst char *\fP and
       
  1751 \fBjson_t *\fP respectively. Example:
       
  1752 .INDENT 7.0
       
  1753 .INDENT 3.5
       
  1754 .sp
       
  1755 .nf
       
  1756 .ft CW
       
  1757 /* obj is a JSON object */
       
  1758 const char *key;
       
  1759 json_t *value;
       
  1760 
       
  1761 json_object_foreach(obj, key, value) {
       
  1762     /* block of code that uses key and value */
       
  1763 }
       
  1764 .ft R
       
  1765 .fi
       
  1766 .UNINDENT
       
  1767 .UNINDENT
       
  1768 .sp
       
  1769 The items are not returned in any particular order.
       
  1770 .sp
       
  1771 This macro expands to an ordinary \fBfor\fP statement upon
       
  1772 preprocessing, so its performance is equivalent to that of
       
  1773 hand\-written iteration code using the object iteration protocol
       
  1774 (see below). The main advantage of this macro is that it abstracts
       
  1775 away the complexity behind iteration, and makes for shorter, more
       
  1776 concise code.
       
  1777 .sp
       
  1778 New in version 2.3.
       
  1779 
       
  1780 .UNINDENT
       
  1781 .sp
       
  1782 The following functions implement an iteration protocol for objects,
       
  1783 allowing to iterate through all key\-value pairs in an object. The
       
  1784 items are not returned in any particular order, as this would require
       
  1785 sorting due to the internal hashtable implementation.
       
  1786 .INDENT 0.0
       
  1787 .TP
       
  1788 .B void *json_object_iter(json_t\fI\ *object\fP)
       
  1789 Returns an opaque iterator which can be used to iterate over all
       
  1790 key\-value pairs in \fIobject\fP, or \fINULL\fP if \fIobject\fP is empty.
       
  1791 .UNINDENT
       
  1792 .INDENT 0.0
       
  1793 .TP
       
  1794 .B void *json_object_iter_at(json_t\fI\ *object\fP, const char\fI\ *key\fP)
       
  1795 Like \fBjson_object_iter()\fP, but returns an iterator to the
       
  1796 key\-value pair in \fIobject\fP whose key is equal to \fIkey\fP, or NULL if
       
  1797 \fIkey\fP is not found in \fIobject\fP\&. Iterating forward to the end of
       
  1798 \fIobject\fP only yields all key\-value pairs of the object if \fIkey\fP
       
  1799 happens to be the first key in the underlying hash table.
       
  1800 .UNINDENT
       
  1801 .INDENT 0.0
       
  1802 .TP
       
  1803 .B void *json_object_iter_next(json_t\fI\ *object\fP, void\fI\ *iter\fP)
       
  1804 Returns an iterator pointing to the next key\-value pair in \fIobject\fP
       
  1805 after \fIiter\fP, or \fINULL\fP if the whole object has been iterated
       
  1806 through.
       
  1807 .UNINDENT
       
  1808 .INDENT 0.0
       
  1809 .TP
       
  1810 .B const char *json_object_iter_key(void\fI\ *iter\fP)
       
  1811 Extract the associated key from \fIiter\fP\&.
       
  1812 .UNINDENT
       
  1813 .INDENT 0.0
       
  1814 .TP
       
  1815 .B json_t *json_object_iter_value(void\fI\ *iter\fP)
       
  1816 Return value: Borrowed reference.
       
  1817 .sp
       
  1818 Extract the associated value from \fIiter\fP\&.
       
  1819 .UNINDENT
       
  1820 .INDENT 0.0
       
  1821 .TP
       
  1822 .B int json_object_iter_set(json_t\fI\ *object\fP, void\fI\ *iter\fP, json_t\fI\ *value\fP)
       
  1823 Set the value of the key\-value pair in \fIobject\fP, that is pointed to
       
  1824 by \fIiter\fP, to \fIvalue\fP\&.
       
  1825 .UNINDENT
       
  1826 .INDENT 0.0
       
  1827 .TP
       
  1828 .B int json_object_iter_set_new(json_t\fI\ *object\fP, void\fI\ *iter\fP, json_t\fI\ *value\fP)
       
  1829 Like \fBjson_object_iter_set()\fP, but steals the reference to
       
  1830 \fIvalue\fP\&. This is useful when \fIvalue\fP is newly created and not used
       
  1831 after the call.
       
  1832 .UNINDENT
       
  1833 .INDENT 0.0
       
  1834 .TP
       
  1835 .B void *json_object_key_to_iter(const char\fI\ *key\fP)
       
  1836 Like \fBjson_object_iter_at()\fP, but much faster. Only works for
       
  1837 values returned by \fBjson_object_iter_key()\fP\&. Using other keys
       
  1838 will lead to segfaults. This function is used internally to
       
  1839 implement \fBjson_object_foreach()\fP\&.
       
  1840 .sp
       
  1841 New in version 2.3.
       
  1842 
       
  1843 .UNINDENT
       
  1844 .sp
       
  1845 The iteration protocol can be used for example as follows:
       
  1846 .INDENT 0.0
       
  1847 .INDENT 3.5
       
  1848 .sp
       
  1849 .nf
       
  1850 .ft CW
       
  1851 /* obj is a JSON object */
       
  1852 const char *key;
       
  1853 json_t *value;
       
  1854 
       
  1855 void *iter = json_object_iter(obj);
       
  1856 while(iter)
       
  1857 {
       
  1858     key = json_object_iter_key(iter);
       
  1859     value = json_object_iter_value(iter);
       
  1860     /* use key and value ... */
       
  1861     iter = json_object_iter_next(obj, iter);
       
  1862 }
       
  1863 .ft R
       
  1864 .fi
       
  1865 .UNINDENT
       
  1866 .UNINDENT
       
  1867 .INDENT 0.0
       
  1868 .TP
       
  1869 .B void json_object_seed(size_t\fI\ seed\fP)
       
  1870 Seed the hash function used in Jansson\(aqs hashtable implementation.
       
  1871 The seed is used to randomize the hash function so that an
       
  1872 attacker cannot control its output.
       
  1873 .sp
       
  1874 If \fIseed\fP is 0, Jansson generates the seed itselfy by reading
       
  1875 random data from the operating system\(aqs entropy sources. If no
       
  1876 entropy sources are available, falls back to using a combination
       
  1877 of the current timestamp (with microsecond precision if possible)
       
  1878 and the process ID.
       
  1879 .sp
       
  1880 If called at all, this function must be called before any calls to
       
  1881 \fBjson_object()\fP, either explicit or implicit. If this
       
  1882 function is not called by the user, the first call to
       
  1883 \fBjson_object()\fP (either explicit or implicit) seeds the hash
       
  1884 function. See \fIportability\-thread\-safety\fP for notes on thread
       
  1885 safety.
       
  1886 .sp
       
  1887 If repeatable results are required, for e.g. unit tests, the hash
       
  1888 function can be "unrandomized" by calling \fBjson_object_seed()\fP
       
  1889 with a constant value on program startup, e.g.
       
  1890 \fBjson_object_seed(1)\fP\&.
       
  1891 .sp
       
  1892 New in version 2.6.
       
  1893 
       
  1894 .UNINDENT
       
  1895 .SS Error reporting
       
  1896 .sp
       
  1897 Jansson uses a single struct type to pass error information to the
       
  1898 user. See sections \fIapiref\-decoding\fP, \fIapiref\-pack\fP and
       
  1899 \fIapiref\-unpack\fP for functions that pass error information using
       
  1900 this struct.
       
  1901 .INDENT 0.0
       
  1902 .TP
       
  1903 .B json_error_t
       
  1904 .INDENT 7.0
       
  1905 .TP
       
  1906 .B char text[]
       
  1907 The error message (in UTF\-8), or an empty string if a message is
       
  1908 not available.
       
  1909 .UNINDENT
       
  1910 .INDENT 7.0
       
  1911 .TP
       
  1912 .B char source[]
       
  1913 Source of the error. This can be (a part of) the file name or a
       
  1914 special identifier in angle brackers (e.g. \fB<string>\fP).
       
  1915 .UNINDENT
       
  1916 .INDENT 7.0
       
  1917 .TP
       
  1918 .B int line
       
  1919 The line number on which the error occurred.
       
  1920 .UNINDENT
       
  1921 .INDENT 7.0
       
  1922 .TP
       
  1923 .B int column
       
  1924 The column on which the error occurred. Note that this is the
       
  1925 \fIcharacter column\fP, not the byte column, i.e. a multibyte UTF\-8
       
  1926 character counts as one column.
       
  1927 .UNINDENT
       
  1928 .INDENT 7.0
       
  1929 .TP
       
  1930 .B size_t position
       
  1931 The position in bytes from the start of the input. This is
       
  1932 useful for debugging Unicode encoding problems.
       
  1933 .UNINDENT
       
  1934 .UNINDENT
       
  1935 .sp
       
  1936 The normal use of \fBjson_error_t\fP is to allocate it on the stack,
       
  1937 and pass a pointer to a function. Example:
       
  1938 .INDENT 0.0
       
  1939 .INDENT 3.5
       
  1940 .sp
       
  1941 .nf
       
  1942 .ft CW
       
  1943 int main() {
       
  1944     json_t *json;
       
  1945     json_error_t error;
       
  1946 
       
  1947     json = json_load_file("/path/to/file.json", 0, &error);
       
  1948     if(!json) {
       
  1949         /* the error variable contains error information */
       
  1950     }
       
  1951     ...
       
  1952 }
       
  1953 .ft R
       
  1954 .fi
       
  1955 .UNINDENT
       
  1956 .UNINDENT
       
  1957 .sp
       
  1958 Also note that if the call succeeded (\fBjson != NULL\fP in the above
       
  1959 example), the contents of \fBerror\fP are generally left unspecified.
       
  1960 The decoding functions write to the \fBposition\fP member also on
       
  1961 success. See \fIapiref\-decoding\fP for more info.
       
  1962 .sp
       
  1963 All functions also accept \fINULL\fP as the \fBjson_error_t\fP pointer,
       
  1964 in which case no error information is returned to the caller.
       
  1965 .SS Encoding
       
  1966 .sp
       
  1967 This sections describes the functions that can be used to encode
       
  1968 values to JSON. By default, only objects and arrays can be encoded
       
  1969 directly, since they are the only valid \fIroot\fP values of a JSON text.
       
  1970 To encode any JSON value, use the \fBJSON_ENCODE_ANY\fP flag (see
       
  1971 below).
       
  1972 .sp
       
  1973 By default, the output has no newlines, and spaces are used between
       
  1974 array and object elements for a readable output. This behavior can be
       
  1975 altered by using the \fBJSON_INDENT\fP and \fBJSON_COMPACT\fP flags
       
  1976 described below. A newline is never appended to the end of the encoded
       
  1977 JSON data.
       
  1978 .sp
       
  1979 Each function takes a \fIflags\fP parameter that controls some aspects of
       
  1980 how the data is encoded. Its default value is 0. The following macros
       
  1981 can be ORed together to obtain \fIflags\fP\&.
       
  1982 .INDENT 0.0
       
  1983 .TP
       
  1984 .B \fBJSON_INDENT(n)\fP
       
  1985 Pretty\-print the result, using newlines between array and object
       
  1986 items, and indenting with \fIn\fP spaces. The valid range for \fIn\fP is
       
  1987 between 0 and 31 (inclusive), other values result in an undefined
       
  1988 output. If \fBJSON_INDENT\fP is not used or \fIn\fP is 0, no newlines are
       
  1989 inserted between array and object items.
       
  1990 .sp
       
  1991 The \fBJSON_MAX_INDENT\fP constant defines the maximum indentation
       
  1992 that can be used, and its value is 31.
       
  1993 .sp
       
  1994 Changed in version 2.7: Added \fBJSON_MAX_INDENT\fP\&.
       
  1995 
       
  1996 .TP
       
  1997 .B \fBJSON_COMPACT\fP
       
  1998 This flag enables a compact representation, i.e. sets the separator
       
  1999 between array and object items to \fB","\fP and between object keys
       
  2000 and values to \fB":"\fP\&. Without this flag, the corresponding
       
  2001 separators are \fB", "\fP and \fB": "\fP for more readable output.
       
  2002 .TP
       
  2003 .B \fBJSON_ENSURE_ASCII\fP
       
  2004 If this flag is used, the output is guaranteed to consist only of
       
  2005 ASCII characters. This is achived by escaping all Unicode
       
  2006 characters outside the ASCII range.
       
  2007 .TP
       
  2008 .B \fBJSON_SORT_KEYS\fP
       
  2009 If this flag is used, all the objects in output are sorted by key.
       
  2010 This is useful e.g. if two JSON texts are diffed or visually
       
  2011 compared.
       
  2012 .TP
       
  2013 .B \fBJSON_PRESERVE_ORDER\fP
       
  2014 If this flag is used, object keys in the output are sorted into the
       
  2015 same order in which they were first inserted to the object. For
       
  2016 example, decoding a JSON text and then encoding with this flag
       
  2017 preserves the order of object keys.
       
  2018 .TP
       
  2019 .B \fBJSON_ENCODE_ANY\fP
       
  2020 Specifying this flag makes it possible to encode any JSON value on
       
  2021 its own. Without it, only objects and arrays can be passed as the
       
  2022 \fIroot\fP value to the encoding functions.
       
  2023 .sp
       
  2024 \fBNote:\fP Encoding any value may be useful in some scenarios, but
       
  2025 it\(aqs generally discouraged as it violates strict compatiblity with
       
  2026 \fI\%RFC 4627\fP\&. If you use this flag, don\(aqt expect interoperatibility
       
  2027 with other JSON systems.
       
  2028 .sp
       
  2029 New in version 2.1.
       
  2030 
       
  2031 .TP
       
  2032 .B \fBJSON_ESCAPE_SLASH\fP
       
  2033 Escape the \fB/\fP characters in strings with \fB\e/\fP\&.
       
  2034 .sp
       
  2035 New in version 2.4.
       
  2036 
       
  2037 .TP
       
  2038 .B \fBJSON_REAL_PRECISION(n)\fP
       
  2039 Output all real numbers with at most \fIn\fP digits of precision. The
       
  2040 valid range for \fIn\fP is between 0 and 31 (inclusive), and other
       
  2041 values result in an undefined behavior.
       
  2042 .sp
       
  2043 By default, the precision is 17, to correctly and losslessly encode
       
  2044 all IEEE 754 double precision floating point numbers.
       
  2045 .sp
       
  2046 New in version 2.7.
       
  2047 
       
  2048 .UNINDENT
       
  2049 .sp
       
  2050 The following functions perform the actual JSON encoding. The result
       
  2051 is in UTF\-8.
       
  2052 .INDENT 0.0
       
  2053 .TP
       
  2054 .B char *json_dumps(const json_t\fI\ *root\fP, size_t\fI\ flags\fP)
       
  2055 Returns the JSON representation of \fIroot\fP as a string, or \fINULL\fP on
       
  2056 error. \fIflags\fP is described above. The return value must be freed
       
  2057 by the caller using \fBfree()\fP\&.
       
  2058 .UNINDENT
       
  2059 .INDENT 0.0
       
  2060 .TP
       
  2061 .B int json_dumpf(const json_t\fI\ *root\fP, FILE\fI\ *output\fP, size_t\fI\ flags\fP)
       
  2062 Write the JSON representation of \fIroot\fP to the stream \fIoutput\fP\&.
       
  2063 \fIflags\fP is described above. Returns 0 on success and \-1 on error.
       
  2064 If an error occurs, something may have already been written to
       
  2065 \fIoutput\fP\&. In this case, the output is undefined and most likely not
       
  2066 valid JSON.
       
  2067 .UNINDENT
       
  2068 .INDENT 0.0
       
  2069 .TP
       
  2070 .B int json_dump_file(const json_t\fI\ *json\fP, const char\fI\ *path\fP, size_t\fI\ flags\fP)
       
  2071 Write the JSON representation of \fIroot\fP to the file \fIpath\fP\&. If
       
  2072 \fIpath\fP already exists, it is overwritten. \fIflags\fP is described
       
  2073 above. Returns 0 on success and \-1 on error.
       
  2074 .UNINDENT
       
  2075 .INDENT 0.0
       
  2076 .TP
       
  2077 .B json_dump_callback_t
       
  2078 A typedef for a function that\(aqs called by
       
  2079 \fBjson_dump_callback()\fP:
       
  2080 .INDENT 7.0
       
  2081 .INDENT 3.5
       
  2082 .sp
       
  2083 .nf
       
  2084 .ft CW
       
  2085 typedef int (*json_dump_callback_t)(const char *buffer, size_t size, void *data);
       
  2086 .ft R
       
  2087 .fi
       
  2088 .UNINDENT
       
  2089 .UNINDENT
       
  2090 .sp
       
  2091 \fIbuffer\fP points to a buffer containing a chunk of output, \fIsize\fP is
       
  2092 the length of the buffer, and \fIdata\fP is the corresponding
       
  2093 \fBjson_dump_callback()\fP argument passed through.
       
  2094 .sp
       
  2095 On error, the function should return \-1 to stop the encoding
       
  2096 process. On success, it should return 0.
       
  2097 .sp
       
  2098 New in version 2.2.
       
  2099 
       
  2100 .UNINDENT
       
  2101 .INDENT 0.0
       
  2102 .TP
       
  2103 .B int json_dump_callback(const json_t\fI\ *json\fP, json_dump_callback_t\fI\ callback\fP, void\fI\ *data\fP, size_t\fI\ flags\fP)
       
  2104 Call \fIcallback\fP repeatedly, passing a chunk of the JSON
       
  2105 representation of \fIroot\fP each time. \fIflags\fP is described above.
       
  2106 Returns 0 on success and \-1 on error.
       
  2107 .sp
       
  2108 New in version 2.2.
       
  2109 
       
  2110 .UNINDENT
       
  2111 .SS Decoding
       
  2112 .sp
       
  2113 This sections describes the functions that can be used to decode JSON
       
  2114 text to the Jansson representation of JSON data. The JSON
       
  2115 specification requires that a JSON text is either a serialized array
       
  2116 or object, and this requirement is also enforced with the following
       
  2117 functions. In other words, the top level value in the JSON text being
       
  2118 decoded must be either array or object. To decode any JSON value, use
       
  2119 the \fBJSON_DECODE_ANY\fP flag (see below).
       
  2120 .sp
       
  2121 See \fIrfc\-conformance\fP for a discussion on Jansson\(aqs conformance
       
  2122 to the JSON specification. It explains many design decisions that
       
  2123 affect especially the behavior of the decoder.
       
  2124 .sp
       
  2125 Each function takes a \fIflags\fP parameter that can be used to control
       
  2126 the behavior of the decoder. Its default value is 0. The following
       
  2127 macros can be ORed together to obtain \fIflags\fP\&.
       
  2128 .INDENT 0.0
       
  2129 .TP
       
  2130 .B \fBJSON_REJECT_DUPLICATES\fP
       
  2131 Issue a decoding error if any JSON object in the input text
       
  2132 contains duplicate keys. Without this flag, the value of the last
       
  2133 occurence of each key ends up in the result. Key equivalence is
       
  2134 checked byte\-by\-byte, without special Unicode comparison
       
  2135 algorithms.
       
  2136 .sp
       
  2137 New in version 2.1.
       
  2138 
       
  2139 .TP
       
  2140 .B \fBJSON_DECODE_ANY\fP
       
  2141 By default, the decoder expects an array or object as the input.
       
  2142 With this flag enabled, the decoder accepts any valid JSON value.
       
  2143 .sp
       
  2144 \fBNote:\fP Decoding any value may be useful in some scenarios, but
       
  2145 it\(aqs generally discouraged as it violates strict compatiblity with
       
  2146 \fI\%RFC 4627\fP\&. If you use this flag, don\(aqt expect interoperatibility
       
  2147 with other JSON systems.
       
  2148 .sp
       
  2149 New in version 2.3.
       
  2150 
       
  2151 .TP
       
  2152 .B \fBJSON_DISABLE_EOF_CHECK\fP
       
  2153 By default, the decoder expects that its whole input constitutes a
       
  2154 valid JSON text, and issues an error if there\(aqs extra data after
       
  2155 the otherwise valid JSON input. With this flag enabled, the decoder
       
  2156 stops after decoding a valid JSON array or object, and thus allows
       
  2157 extra data after the JSON text.
       
  2158 .sp
       
  2159 Normally, reading will stop when the last \fB]\fP or \fB}\fP in the
       
  2160 JSON input is encountered. If both \fBJSON_DISABLE_EOF_CHECK\fP and
       
  2161 \fBJSON_DECODE_ANY\fP flags are used, the decoder may read one extra
       
  2162 UTF\-8 code unit (up to 4 bytes of input). For example, decoding
       
  2163 \fB4true\fP correctly decodes the integer 4, but also reads the
       
  2164 \fBt\fP\&. For this reason, if reading multiple consecutive values that
       
  2165 are not arrays or objects, they should be separated by at least one
       
  2166 whitespace character.
       
  2167 .sp
       
  2168 New in version 2.1.
       
  2169 
       
  2170 .TP
       
  2171 .B \fBJSON_DECODE_INT_AS_REAL\fP
       
  2172 JSON defines only one number type. Jansson distinguishes between
       
  2173 ints and reals. For more information see \fIreal\-vs\-integer\fP\&.
       
  2174 With this flag enabled the decoder interprets all numbers as real
       
  2175 values. Integers that do not have an exact double representation
       
  2176 will silently result in a loss of precision. Integers that cause
       
  2177 a double overflow will cause an error.
       
  2178 .sp
       
  2179 New in version 2.5.
       
  2180 
       
  2181 .TP
       
  2182 .B \fBJSON_ALLOW_NUL\fP
       
  2183 Allow \fB\eu0000\fP escape inside string values. This is a safety
       
  2184 measure; If you know your input can contain NUL bytes, use this
       
  2185 flag. If you don\(aqt use this flag, you don\(aqt have to worry about NUL
       
  2186 bytes inside strings unless you explicitly create themselves by
       
  2187 using e.g. \fBjson_stringn()\fP or \fBs#\fP format specifier for
       
  2188 \fBjson_pack()\fP\&.
       
  2189 .sp
       
  2190 Object keys cannot have embedded NUL bytes even if this flag is
       
  2191 used.
       
  2192 .sp
       
  2193 New in version 2.6.
       
  2194 
       
  2195 .UNINDENT
       
  2196 .sp
       
  2197 Each function also takes an optional \fBjson_error_t\fP parameter
       
  2198 that is filled with error information if decoding fails. It\(aqs also
       
  2199 updated on success; the number of bytes of input read is written to
       
  2200 its \fBposition\fP field. This is especially useful when using
       
  2201 \fBJSON_DISABLE_EOF_CHECK\fP to read multiple consecutive JSON texts.
       
  2202 .sp
       
  2203 New in version 2.3: Number of bytes of input read is written to the \fBposition\fP field
       
  2204 of the \fBjson_error_t\fP structure.
       
  2205 
       
  2206 .sp
       
  2207 If no error or position information is needed, you can pass \fINULL\fP\&.
       
  2208 .sp
       
  2209 The following functions perform the actual JSON decoding.
       
  2210 .INDENT 0.0
       
  2211 .TP
       
  2212 .B json_t *json_loads(const char\fI\ *input\fP, size_t\fI\ flags\fP, json_error_t\fI\ *error\fP)
       
  2213 Return value: New reference.
       
  2214 .sp
       
  2215 Decodes the JSON string \fIinput\fP and returns the array or object it
       
  2216 contains, or \fINULL\fP on error, in which case \fIerror\fP is filled with
       
  2217 information about the error. \fIflags\fP is described above.
       
  2218 .UNINDENT
       
  2219 .INDENT 0.0
       
  2220 .TP
       
  2221 .B json_t *json_loadb(const char\fI\ *buffer\fP, size_t\fI\ buflen\fP, size_t\fI\ flags\fP, json_error_t\fI\ *error\fP)
       
  2222 Return value: New reference.
       
  2223 .sp
       
  2224 Decodes the JSON string \fIbuffer\fP, whose length is \fIbuflen\fP, and
       
  2225 returns the array or object it contains, or \fINULL\fP on error, in
       
  2226 which case \fIerror\fP is filled with information about the error. This
       
  2227 is similar to \fBjson_loads()\fP except that the string doesn\(aqt
       
  2228 need to be null\-terminated. \fIflags\fP is described above.
       
  2229 .sp
       
  2230 New in version 2.1.
       
  2231 
       
  2232 .UNINDENT
       
  2233 .INDENT 0.0
       
  2234 .TP
       
  2235 .B json_t *json_loadf(FILE\fI\ *input\fP, size_t\fI\ flags\fP, json_error_t\fI\ *error\fP)
       
  2236 Return value: New reference.
       
  2237 .sp
       
  2238 Decodes the JSON text in stream \fIinput\fP and returns the array or
       
  2239 object it contains, or \fINULL\fP on error, in which case \fIerror\fP is
       
  2240 filled with information about the error. \fIflags\fP is described
       
  2241 above.
       
  2242 .sp
       
  2243 This function will start reading the input from whatever position
       
  2244 the input file was, without attempting to seek first. If an error
       
  2245 occurs, the file position will be left indeterminate. On success,
       
  2246 the file position will be at EOF, unless \fBJSON_DISABLE_EOF_CHECK\fP
       
  2247 flag was used. In this case, the file position will be at the first
       
  2248 character after the last \fB]\fP or \fB}\fP in the JSON input. This
       
  2249 allows calling \fBjson_loadf()\fP on the same \fBFILE\fP object
       
  2250 multiple times, if the input consists of consecutive JSON texts,
       
  2251 possibly separated by whitespace.
       
  2252 .UNINDENT
       
  2253 .INDENT 0.0
       
  2254 .TP
       
  2255 .B json_t *json_load_file(const char\fI\ *path\fP, size_t\fI\ flags\fP, json_error_t\fI\ *error\fP)
       
  2256 Return value: New reference.
       
  2257 .sp
       
  2258 Decodes the JSON text in file \fIpath\fP and returns the array or
       
  2259 object it contains, or \fINULL\fP on error, in which case \fIerror\fP is
       
  2260 filled with information about the error. \fIflags\fP is described
       
  2261 above.
       
  2262 .UNINDENT
       
  2263 .INDENT 0.0
       
  2264 .TP
       
  2265 .B json_load_callback_t
       
  2266 A typedef for a function that\(aqs called by
       
  2267 \fBjson_load_callback()\fP to read a chunk of input data:
       
  2268 .INDENT 7.0
       
  2269 .INDENT 3.5
       
  2270 .sp
       
  2271 .nf
       
  2272 .ft CW
       
  2273 typedef size_t (*json_load_callback_t)(void *buffer, size_t buflen, void *data);
       
  2274 .ft R
       
  2275 .fi
       
  2276 .UNINDENT
       
  2277 .UNINDENT
       
  2278 .sp
       
  2279 \fIbuffer\fP points to a buffer of \fIbuflen\fP bytes, and \fIdata\fP is the
       
  2280 corresponding \fBjson_load_callback()\fP argument passed through.
       
  2281 .sp
       
  2282 On success, the function should return the number of bytes read; a
       
  2283 returned value of 0 indicates that no data was read and that the
       
  2284 end of file has been reached. On error, the function should return
       
  2285 \fB(size_t)\-1\fP to abort the decoding process.
       
  2286 .sp
       
  2287 New in version 2.4.
       
  2288 
       
  2289 .UNINDENT
       
  2290 .INDENT 0.0
       
  2291 .TP
       
  2292 .B json_t *json_load_callback(json_load_callback_t\fI\ callback\fP, void\fI\ *data\fP, size_t\fI\ flags\fP, json_error_t\fI\ *error\fP)
       
  2293 Return value: New reference.
       
  2294 .sp
       
  2295 Decodes the JSON text produced by repeated calls to \fIcallback\fP, and
       
  2296 returns the array or object it contains, or \fINULL\fP on error, in
       
  2297 which case \fIerror\fP is filled with information about the error.
       
  2298 \fIdata\fP is passed through to \fIcallback\fP on each call. \fIflags\fP is
       
  2299 described above.
       
  2300 .sp
       
  2301 New in version 2.4.
       
  2302 
       
  2303 .UNINDENT
       
  2304 .SS Building Values
       
  2305 .sp
       
  2306 This section describes functions that help to create, or \fIpack\fP,
       
  2307 complex JSON values, especially nested objects and arrays. Value
       
  2308 building is based on a \fIformat string\fP that is used to tell the
       
  2309 functions about the expected arguments.
       
  2310 .sp
       
  2311 For example, the format string \fB"i"\fP specifies a single integer
       
  2312 value, while the format string \fB"[ssb]"\fP or the equivalent \fB"[s, s,
       
  2313 b]"\fP specifies an array value with two strings and a boolean as its
       
  2314 items:
       
  2315 .INDENT 0.0
       
  2316 .INDENT 3.5
       
  2317 .sp
       
  2318 .nf
       
  2319 .ft CW
       
  2320 /* Create the JSON integer 42 */
       
  2321 json_pack("i", 42);
       
  2322 
       
  2323 /* Create the JSON array ["foo", "bar", true] */
       
  2324 json_pack("[ssb]", "foo", "bar", 1);
       
  2325 .ft R
       
  2326 .fi
       
  2327 .UNINDENT
       
  2328 .UNINDENT
       
  2329 .sp
       
  2330 Here\(aqs the full list of format specifiers. The type in parentheses
       
  2331 denotes the resulting JSON type, and the type in brackets (if any)
       
  2332 denotes the C type that is expected as the corresponding argument or
       
  2333 arguments.
       
  2334 .INDENT 0.0
       
  2335 .TP
       
  2336 .B \fBs\fP (string) [const char *]
       
  2337 Convert a NULL terminated UTF\-8 string to a JSON string.
       
  2338 .TP
       
  2339 .B \fBs#\fP (string) [const char *, int]
       
  2340 Convert a UTF\-8 buffer of a given length to a JSON string.
       
  2341 .sp
       
  2342 New in version 2.5.
       
  2343 
       
  2344 .TP
       
  2345 .B \fBs%\fP (string) [const char *, size_t]
       
  2346 Like \fBs#\fP but the length argument is of type \fBsize_t\fP\&.
       
  2347 .sp
       
  2348 New in version 2.6.
       
  2349 
       
  2350 .TP
       
  2351 .B \fB+\fP [const char *]
       
  2352 Like \fBs\fP, but concatenate to the previous string. Only valid
       
  2353 after \fBs\fP, \fBs#\fP, \fB+\fP or \fB+#\fP\&.
       
  2354 .sp
       
  2355 New in version 2.5.
       
  2356 
       
  2357 .TP
       
  2358 .B \fB+#\fP [const char *, int]
       
  2359 Like \fBs#\fP, but concatenate to the previous string. Only valid
       
  2360 after \fBs\fP, \fBs#\fP, \fB+\fP or \fB+#\fP\&.
       
  2361 .sp
       
  2362 New in version 2.5.
       
  2363 
       
  2364 .TP
       
  2365 .B \fB+%\fP (string) [const char *, size_t]
       
  2366 Like \fB+#\fP but the length argument is of type \fBsize_t\fP\&.
       
  2367 .sp
       
  2368 New in version 2.6.
       
  2369 
       
  2370 .TP
       
  2371 .B \fBn\fP (null)
       
  2372 Output a JSON null value. No argument is consumed.
       
  2373 .TP
       
  2374 .B \fBb\fP (boolean) [int]
       
  2375 Convert a C \fBint\fP to JSON boolean value. Zero is converted
       
  2376 to \fBfalse\fP and non\-zero to \fBtrue\fP\&.
       
  2377 .TP
       
  2378 .B \fBi\fP (integer) [int]
       
  2379 Convert a C \fBint\fP to JSON integer.
       
  2380 .TP
       
  2381 .B \fBI\fP (integer) [json_int_t]
       
  2382 Convert a C \fBjson_int_t\fP to JSON integer.
       
  2383 .TP
       
  2384 .B \fBf\fP (real) [double]
       
  2385 Convert a C \fBdouble\fP to JSON real.
       
  2386 .TP
       
  2387 .B \fBo\fP (any value) [json_t *]
       
  2388 Output any given JSON value as\-is. If the value is added to an
       
  2389 array or object, the reference to the value passed to \fBo\fP is
       
  2390 stolen by the container.
       
  2391 .TP
       
  2392 .B \fBO\fP (any value) [json_t *]
       
  2393 Like \fBo\fP, but the argument\(aqs reference count is incremented.
       
  2394 This is useful if you pack into an array or object and want to
       
  2395 keep the reference for the JSON value consumed by \fBO\fP to
       
  2396 yourself.
       
  2397 .TP
       
  2398 .B \fB[fmt]\fP (array)
       
  2399 Build an array with contents from the inner format string. \fBfmt\fP
       
  2400 may contain objects and arrays, i.e. recursive value building is
       
  2401 supported.
       
  2402 .TP
       
  2403 .B \fB{fmt}\fP (object)
       
  2404 Build an object with contents from the inner format string
       
  2405 \fBfmt\fP\&. The first, third, etc. format specifier represent a key,
       
  2406 and must be a string (see \fBs\fP, \fBs#\fP, \fB+\fP and \fB+#\fP above),
       
  2407 as object keys are always strings. The second, fourth, etc. format
       
  2408 specifier represent a value. Any value may be an object or array,
       
  2409 i.e. recursive value building is supported.
       
  2410 .UNINDENT
       
  2411 .sp
       
  2412 Whitespace, \fB:\fP and \fB,\fP are ignored.
       
  2413 .sp
       
  2414 The following functions compose the value building API:
       
  2415 .INDENT 0.0
       
  2416 .TP
       
  2417 .B json_t *json_pack(const char\fI\ *fmt\fP, \&...)
       
  2418 Return value: New reference.
       
  2419 .sp
       
  2420 Build a new JSON value according to the format string \fIfmt\fP\&. For
       
  2421 each format specifier (except for \fB{}[]n\fP), one or more arguments
       
  2422 are consumed and used to build the corresponding value. Returns
       
  2423 \fINULL\fP on error.
       
  2424 .UNINDENT
       
  2425 .INDENT 0.0
       
  2426 .TP
       
  2427 .B json_t *json_pack_ex(json_error_t\fI\ *error\fP, size_t\fI\ flags\fP, const char\fI\ *fmt\fP, \&...)
       
  2428 .TP
       
  2429 .B json_t *json_vpack_ex(json_error_t\fI\ *error\fP, size_t\fI\ flags\fP, const char\fI\ *fmt\fP, va_list\fI\ ap\fP)
       
  2430 Return value: New reference.
       
  2431 .sp
       
  2432 Like \fBjson_pack()\fP, but an in the case of an error, an error
       
  2433 message is written to \fIerror\fP, if it\(aqs not \fINULL\fP\&. The \fIflags\fP
       
  2434 parameter is currently unused and should be set to 0.
       
  2435 .sp
       
  2436 As only the errors in format string (and out\-of\-memory errors) can
       
  2437 be caught by the packer, these two functions are most likely only
       
  2438 useful for debugging format strings.
       
  2439 .UNINDENT
       
  2440 .sp
       
  2441 More examples:
       
  2442 .INDENT 0.0
       
  2443 .INDENT 3.5
       
  2444 .sp
       
  2445 .nf
       
  2446 .ft CW
       
  2447 /* Build an empty JSON object */
       
  2448 json_pack("{}");
       
  2449 
       
  2450 /* Build the JSON object {"foo": 42, "bar": 7} */
       
  2451 json_pack("{sisi}", "foo", 42, "bar", 7);
       
  2452 
       
  2453 /* Like above, \(aq:\(aq, \(aq,\(aq and whitespace are ignored */
       
  2454 json_pack("{s:i, s:i}", "foo", 42, "bar", 7);
       
  2455 
       
  2456 /* Build the JSON array [[1, 2], {"cool": true}] */
       
  2457 json_pack("[[i,i],{s:b}]", 1, 2, "cool", 1);
       
  2458 
       
  2459 /* Build a string from a non\-NUL terminated buffer */
       
  2460 char buffer[4] = {\(aqt\(aq, \(aqe\(aq, \(aqs\(aq, \(aqt\(aq};
       
  2461 json_pack("s#", buffer, 4);
       
  2462 
       
  2463 /* Concatentate strings together to build the JSON string "foobarbaz" */
       
  2464 json_pack("s++", "foo", "bar", "baz");
       
  2465 .ft R
       
  2466 .fi
       
  2467 .UNINDENT
       
  2468 .UNINDENT
       
  2469 .SS Parsing and Validating Values
       
  2470 .sp
       
  2471 This section describes functions that help to validate complex values
       
  2472 and extract, or \fIunpack\fP, data from them. Like \fIbuilding values\fP, this is also based on format strings.
       
  2473 .sp
       
  2474 While a JSON value is unpacked, the type specified in the format
       
  2475 string is checked to match that of the JSON value. This is the
       
  2476 validation part of the process. In addition to this, the unpacking
       
  2477 functions can also check that all items of arrays and objects are
       
  2478 unpacked. This check be enabled with the format specifier \fB!\fP or by
       
  2479 using the flag \fBJSON_STRICT\fP\&. See below for details.
       
  2480 .sp
       
  2481 Here\(aqs the full list of format specifiers. The type in parentheses
       
  2482 denotes the JSON type, and the type in brackets (if any) denotes the C
       
  2483 type whose address should be passed.
       
  2484 .INDENT 0.0
       
  2485 .TP
       
  2486 .B \fBs\fP (string) [const char *]
       
  2487 Convert a JSON string to a pointer to a NULL terminated UTF\-8
       
  2488 string. The resulting string is extracted by using
       
  2489 \fBjson_string_value()\fP internally, so it exists as long as
       
  2490 there are still references to the corresponding JSON string.
       
  2491 .TP
       
  2492 .B \fBs%\fP (string) [const char *, size_t *]
       
  2493 Convert a JSON string to a pointer to a NULL terminated UTF\-8
       
  2494 string and its length.
       
  2495 .sp
       
  2496 New in version 2.6.
       
  2497 
       
  2498 .TP
       
  2499 .B \fBn\fP (null)
       
  2500 Expect a JSON null value. Nothing is extracted.
       
  2501 .TP
       
  2502 .B \fBb\fP (boolean) [int]
       
  2503 Convert a JSON boolean value to a C \fBint\fP, so that \fBtrue\fP
       
  2504 is converted to 1 and \fBfalse\fP to 0.
       
  2505 .TP
       
  2506 .B \fBi\fP (integer) [int]
       
  2507 Convert a JSON integer to C \fBint\fP\&.
       
  2508 .TP
       
  2509 .B \fBI\fP (integer) [json_int_t]
       
  2510 Convert a JSON integer to C \fBjson_int_t\fP\&.
       
  2511 .TP
       
  2512 .B \fBf\fP (real) [double]
       
  2513 Convert a JSON real to C \fBdouble\fP\&.
       
  2514 .TP
       
  2515 .B \fBF\fP (integer or real) [double]
       
  2516 Convert a JSON number (integer or real) to C \fBdouble\fP\&.
       
  2517 .TP
       
  2518 .B \fBo\fP (any value) [json_t *]
       
  2519 Store a JSON value with no conversion to a \fBjson_t\fP pointer.
       
  2520 .TP
       
  2521 .B \fBO\fP (any value) [json_t *]
       
  2522 Like \fBO\fP, but the JSON value\(aqs reference count is incremented.
       
  2523 .TP
       
  2524 .B \fB[fmt]\fP (array)
       
  2525 Convert each item in the JSON array according to the inner format
       
  2526 string. \fBfmt\fP may contain objects and arrays, i.e. recursive
       
  2527 value extraction is supporetd.
       
  2528 .TP
       
  2529 .B \fB{fmt}\fP (object)
       
  2530 Convert each item in the JSON object according to the inner format
       
  2531 string \fBfmt\fP\&. The first, third, etc. format specifier represent
       
  2532 a key, and must be \fBs\fP\&. The corresponding argument to unpack
       
  2533 functions is read as the object key. The second fourth, etc.
       
  2534 format specifier represent a value and is written to the address
       
  2535 given as the corresponding argument. \fBNote\fP that every other
       
  2536 argument is read from and every other is written to.
       
  2537 .sp
       
  2538 \fBfmt\fP may contain objects and arrays as values, i.e. recursive
       
  2539 value extraction is supporetd.
       
  2540 .sp
       
  2541 New in version 2.3: Any \fBs\fP representing a key may be suffixed with a \fB?\fP to
       
  2542 make the key optional. If the key is not found, nothing is
       
  2543 extracted. See below for an example.
       
  2544 
       
  2545 .TP
       
  2546 .B \fB!\fP
       
  2547 This special format specifier is used to enable the check that
       
  2548 all object and array items are accessed, on a per\-value basis. It
       
  2549 must appear inside an array or object as the last format specifier
       
  2550 before the closing bracket or brace. To enable the check globally,
       
  2551 use the \fBJSON_STRICT\fP unpacking flag.
       
  2552 .TP
       
  2553 .B \fB*\fP
       
  2554 This special format specifier is the opposite of \fB!\fP\&. If the
       
  2555 \fBJSON_STRICT\fP flag is used, \fB*\fP can be used to disable the
       
  2556 strict check on a per\-value basis. It must appear inside an array
       
  2557 or object as the last format specifier before the closing bracket
       
  2558 or brace.
       
  2559 .UNINDENT
       
  2560 .sp
       
  2561 Whitespace, \fB:\fP and \fB,\fP are ignored.
       
  2562 .sp
       
  2563 The following functions compose the parsing and validation API:
       
  2564 .INDENT 0.0
       
  2565 .TP
       
  2566 .B int json_unpack(json_t\fI\ *root\fP, const char\fI\ *fmt\fP, \&...)
       
  2567 Validate and unpack the JSON value \fIroot\fP according to the format
       
  2568 string \fIfmt\fP\&. Returns 0 on success and \-1 on failure.
       
  2569 .UNINDENT
       
  2570 .INDENT 0.0
       
  2571 .TP
       
  2572 .B int json_unpack_ex(json_t\fI\ *root\fP, json_error_t\fI\ *error\fP, size_t\fI\ flags\fP, const char\fI\ *fmt\fP, \&...)
       
  2573 .TP
       
  2574 .B int json_vunpack_ex(json_t\fI\ *root\fP, json_error_t\fI\ *error\fP, size_t\fI\ flags\fP, const char\fI\ *fmt\fP, va_list\fI\ ap\fP)
       
  2575 Validate and unpack the JSON value \fIroot\fP according to the format
       
  2576 string \fIfmt\fP\&. If an error occurs and \fIerror\fP is not \fINULL\fP, write
       
  2577 error information to \fIerror\fP\&. \fIflags\fP can be used to control the
       
  2578 behaviour of the unpacker, see below for the flags. Returns 0 on
       
  2579 success and \-1 on failure.
       
  2580 .UNINDENT
       
  2581 .sp
       
  2582 \fBNOTE:\fP
       
  2583 .INDENT 0.0
       
  2584 .INDENT 3.5
       
  2585 The first argument of all unpack functions is \fBjson_t *root\fP
       
  2586 instead of \fBconst json_t *root\fP, because the use of \fBO\fP format
       
  2587 specifier causes the reference count of \fBroot\fP, or some value
       
  2588 reachable from \fBroot\fP, to be increased. Furthermore, the \fBo\fP
       
  2589 format specifier may be used to extract a value as\-is, which allows
       
  2590 modifying the structure or contents of a value reachable from
       
  2591 \fBroot\fP\&.
       
  2592 .sp
       
  2593 If the \fBO\fP and \fBo\fP format specifiers are not used, it\(aqs
       
  2594 perfectly safe to cast a \fBconst json_t *\fP variable to plain
       
  2595 \fBjson_t *\fP when used with these functions.
       
  2596 .UNINDENT
       
  2597 .UNINDENT
       
  2598 .sp
       
  2599 The following unpacking flags are available:
       
  2600 .INDENT 0.0
       
  2601 .TP
       
  2602 .B \fBJSON_STRICT\fP
       
  2603 Enable the extra validation step checking that all object and
       
  2604 array items are unpacked. This is equivalent to appending the
       
  2605 format specifier \fB!\fP to the end of every array and object in the
       
  2606 format string.
       
  2607 .TP
       
  2608 .B \fBJSON_VALIDATE_ONLY\fP
       
  2609 Don\(aqt extract any data, just validate the JSON value against the
       
  2610 given format string. Note that object keys must still be specified
       
  2611 after the format string.
       
  2612 .UNINDENT
       
  2613 .sp
       
  2614 Examples:
       
  2615 .INDENT 0.0
       
  2616 .INDENT 3.5
       
  2617 .sp
       
  2618 .nf
       
  2619 .ft CW
       
  2620 /* root is the JSON integer 42 */
       
  2621 int myint;
       
  2622 json_unpack(root, "i", &myint);
       
  2623 assert(myint == 42);
       
  2624 
       
  2625 /* root is the JSON object {"foo": "bar", "quux": true} */
       
  2626 const char *str;
       
  2627 int boolean;
       
  2628 json_unpack(root, "{s:s, s:b}", "foo", &str, "quux", &boolean);
       
  2629 assert(strcmp(str, "bar") == 0 && boolean == 1);
       
  2630 
       
  2631 /* root is the JSON array [[1, 2], {"baz": null} */
       
  2632 json_error_t error;
       
  2633 json_unpack_ex(root, &error, JSON_VALIDATE_ONLY, "[[i,i], {s:n}]", "baz");
       
  2634 /* returns 0 for validation success, nothing is extracted */
       
  2635 
       
  2636 /* root is the JSON array [1, 2, 3, 4, 5] */
       
  2637 int myint1, myint2;
       
  2638 json_unpack(root, "[ii!]", &myint1, &myint2);
       
  2639 /* returns \-1 for failed validation */
       
  2640 
       
  2641 /* root is an empty JSON object */
       
  2642 int myint = 0, myint2 = 0;
       
  2643 json_unpack(root, "{s?i, s?[ii]}",
       
  2644             "foo", &myint1,
       
  2645             "bar", &myint2, &myint3);
       
  2646 /* myint1, myint2 or myint3 is no touched as "foo" and "bar" don\(aqt exist */
       
  2647 .ft R
       
  2648 .fi
       
  2649 .UNINDENT
       
  2650 .UNINDENT
       
  2651 .SS Equality
       
  2652 .sp
       
  2653 Testing for equality of two JSON values cannot, in general, be
       
  2654 achieved using the \fB==\fP operator. Equality in the terms of the
       
  2655 \fB==\fP operator states that the two \fBjson_t\fP pointers point to
       
  2656 exactly the same JSON value. However, two JSON values can be equal not
       
  2657 only if they are exactly the same value, but also if they have equal
       
  2658 "contents":
       
  2659 .INDENT 0.0
       
  2660 .IP \(bu 2
       
  2661 Two integer or real values are equal if their contained numeric
       
  2662 values are equal. An integer value is never equal to a real value,
       
  2663 though.
       
  2664 .IP \(bu 2
       
  2665 Two strings are equal if their contained UTF\-8 strings are equal,
       
  2666 byte by byte. Unicode comparison algorithms are not implemented.
       
  2667 .IP \(bu 2
       
  2668 Two arrays are equal if they have the same number of elements and
       
  2669 each element in the first array is equal to the corresponding
       
  2670 element in the second array.
       
  2671 .IP \(bu 2
       
  2672 Two objects are equal if they have exactly the same keys and the
       
  2673 value for each key in the first object is equal to the value of the
       
  2674 corresponding key in the second object.
       
  2675 .IP \(bu 2
       
  2676 Two true, false or null values have no "contents", so they are equal
       
  2677 if their types are equal. (Because these values are singletons,
       
  2678 their equality can actually be tested with \fB==\fP\&.)
       
  2679 .UNINDENT
       
  2680 .sp
       
  2681 The following function can be used to test whether two JSON values are
       
  2682 equal.
       
  2683 .INDENT 0.0
       
  2684 .TP
       
  2685 .B int json_equal(json_t\fI\ *value1\fP, json_t\fI\ *value2\fP)
       
  2686 Returns 1 if \fIvalue1\fP and \fIvalue2\fP are equal, as defined above.
       
  2687 Returns 0 if they are inequal or one or both of the pointers are
       
  2688 \fINULL\fP\&.
       
  2689 .UNINDENT
       
  2690 .SS Copying
       
  2691 .sp
       
  2692 Because of reference counting, passing JSON values around doesn\(aqt
       
  2693 require copying them. But sometimes a fresh copy of a JSON value is
       
  2694 needed. For example, if you need to modify an array, but still want to
       
  2695 use the original afterwards, you should take a copy of it first.
       
  2696 .sp
       
  2697 Jansson supports two kinds of copying: shallow and deep. There is a
       
  2698 difference between these methods only for arrays and objects. Shallow
       
  2699 copying only copies the first level value (array or object) and uses
       
  2700 the same child values in the copied value. Deep copying makes a fresh
       
  2701 copy of the child values, too. Moreover, all the child values are deep
       
  2702 copied in a recursive fashion.
       
  2703 .INDENT 0.0
       
  2704 .TP
       
  2705 .B json_t *json_copy(json_t\fI\ *value\fP)
       
  2706 Return value: New reference.
       
  2707 .sp
       
  2708 Returns a shallow copy of \fIvalue\fP, or \fINULL\fP on error.
       
  2709 .UNINDENT
       
  2710 .INDENT 0.0
       
  2711 .TP
       
  2712 .B json_t *json_deep_copy(const json_t\fI\ *value\fP)
       
  2713 Return value: New reference.
       
  2714 .sp
       
  2715 Returns a deep copy of \fIvalue\fP, or \fINULL\fP on error.
       
  2716 .UNINDENT
       
  2717 .SS Custom Memory Allocation
       
  2718 .sp
       
  2719 By default, Jansson uses \fBmalloc()\fP and \fBfree()\fP for
       
  2720 memory allocation. These functions can be overridden if custom
       
  2721 behavior is needed.
       
  2722 .INDENT 0.0
       
  2723 .TP
       
  2724 .B json_malloc_t
       
  2725 A typedef for a function pointer with \fBmalloc()\fP\(aqs
       
  2726 signature:
       
  2727 .INDENT 7.0
       
  2728 .INDENT 3.5
       
  2729 .sp
       
  2730 .nf
       
  2731 .ft CW
       
  2732 typedef void *(*json_malloc_t)(size_t);
       
  2733 .ft R
       
  2734 .fi
       
  2735 .UNINDENT
       
  2736 .UNINDENT
       
  2737 .UNINDENT
       
  2738 .INDENT 0.0
       
  2739 .TP
       
  2740 .B json_free_t
       
  2741 A typedef for a function pointer with \fBfree()\fP\(aqs
       
  2742 signature:
       
  2743 .INDENT 7.0
       
  2744 .INDENT 3.5
       
  2745 .sp
       
  2746 .nf
       
  2747 .ft CW
       
  2748 typedef void (*json_free_t)(void *);
       
  2749 .ft R
       
  2750 .fi
       
  2751 .UNINDENT
       
  2752 .UNINDENT
       
  2753 .UNINDENT
       
  2754 .INDENT 0.0
       
  2755 .TP
       
  2756 .B void json_set_alloc_funcs(json_malloc_t\fI\ malloc_fn\fP, json_free_t\fI\ free_fn\fP)
       
  2757 Use \fImalloc_fn\fP instead of \fBmalloc()\fP and \fIfree_fn\fP instead
       
  2758 of \fBfree()\fP\&. This function has to be called before any other
       
  2759 Jansson\(aqs API functions to ensure that all memory operations use
       
  2760 the same functions.
       
  2761 .UNINDENT
       
  2762 .sp
       
  2763 \fBExamples:\fP
       
  2764 .sp
       
  2765 Circumvent problems with different CRT heaps on Windows by using
       
  2766 application\(aqs \fBmalloc()\fP and \fBfree()\fP:
       
  2767 .INDENT 0.0
       
  2768 .INDENT 3.5
       
  2769 .sp
       
  2770 .nf
       
  2771 .ft CW
       
  2772 json_set_alloc_funcs(malloc, free);
       
  2773 .ft R
       
  2774 .fi
       
  2775 .UNINDENT
       
  2776 .UNINDENT
       
  2777 .sp
       
  2778 Use the \fI\%Boehm\(aqs conservative garbage collector\fP for memory
       
  2779 operations:
       
  2780 .INDENT 0.0
       
  2781 .INDENT 3.5
       
  2782 .sp
       
  2783 .nf
       
  2784 .ft CW
       
  2785 json_set_alloc_funcs(GC_malloc, GC_free);
       
  2786 .ft R
       
  2787 .fi
       
  2788 .UNINDENT
       
  2789 .UNINDENT
       
  2790 .sp
       
  2791 Allow storing sensitive data (e.g. passwords or encryption keys) in
       
  2792 JSON structures by zeroing all memory when freed:
       
  2793 .INDENT 0.0
       
  2794 .INDENT 3.5
       
  2795 .sp
       
  2796 .nf
       
  2797 .ft CW
       
  2798 static void *secure_malloc(size_t size)
       
  2799 {
       
  2800     /* Store the memory area size in the beginning of the block */
       
  2801     void *ptr = malloc(size + 8);
       
  2802     *((size_t *)ptr) = size;
       
  2803     return ptr + 8;
       
  2804 }
       
  2805 
       
  2806 static void secure_free(void *ptr)
       
  2807 {
       
  2808     size_t size;
       
  2809 
       
  2810     ptr \-= 8;
       
  2811     size = *((size_t *)ptr);
       
  2812 
       
  2813     guaranteed_memset(ptr, 0, size + 8);
       
  2814     free(ptr);
       
  2815 }
       
  2816 
       
  2817 int main()
       
  2818 {
       
  2819     json_set_alloc_funcs(secure_malloc, secure_free);
       
  2820     /* ... */
       
  2821 }
       
  2822 .ft R
       
  2823 .fi
       
  2824 .UNINDENT
       
  2825 .UNINDENT
       
  2826 .sp
       
  2827 For more information about the issues of storing sensitive data in
       
  2828 memory, see
       
  2829 \fI\%http://www.dwheeler.com/secure\-programs/Secure\-Programs\-HOWTO/protect\-secrets.html\fP\&.
       
  2830 The page also explains the \fBguaranteed_memset()\fP function used
       
  2831 in the example and gives a sample implementation for it.
       
  2832 .SS Changes in Jansson
       
  2833 .SS Version 2.7
       
  2834 .sp
       
  2835 Released 2014\-10\-02
       
  2836 .INDENT 0.0
       
  2837 .IP \(bu 2
       
  2838 New features:
       
  2839 .INDENT 2.0
       
  2840 .IP \(bu 2
       
  2841 \fBjson_pack()\fP and friends: Add format specifiers \fBs%\fP and \fB+%\fP
       
  2842 for a size_t string length (#141).
       
  2843 .IP \(bu 2
       
  2844 \fBjson_unpack()\fP and friends: Add format specifier \fBs%\fP for
       
  2845 unpacking the string length along with the string itself (#141).
       
  2846 .IP \(bu 2
       
  2847 Add length\-aware string constructors \fBjson_stringn()\fP and
       
  2848 \fBjson_stringn_nocheck()\fP, length\-aware string mutators
       
  2849 \fBjson_string_setn()\fP and \fBjson_string_setn_nocheck()\fP, and a
       
  2850 function for getting string\(aqs length \fBjson_string_length()\fP (#141,
       
  2851 #143).
       
  2852 .IP \(bu 2
       
  2853 Support \fB\eu0000\fP escapes in the decoder. The support can be
       
  2854 enabled by using the \fBJSON_ALLOW_NUL\fP decoding flag (#141).
       
  2855 .IP \(bu 2
       
  2856 Add \fBjson_boolean_value()\fP as an alias for \fBjson_is_true()\fP
       
  2857 (#146).
       
  2858 .IP \(bu 2
       
  2859 Add JSON_REAL_PRECISION encoding flag/macro for controlling real
       
  2860 number precision (#178).
       
  2861 .IP \(bu 2
       
  2862 Define the maximum indentation as JSON_MAX_INDENT (#191).
       
  2863 .UNINDENT
       
  2864 .IP \(bu 2
       
  2865 Bug fixes:
       
  2866 .INDENT 2.0
       
  2867 .IP \(bu 2
       
  2868 Some malformed \fB\euNNNN\fP escapes could crash the decoder with an
       
  2869 assertion failure.
       
  2870 .IP \(bu 2
       
  2871 Avoid integer overflows with very long strings in UTF\-8 decoder and
       
  2872 hashtable.
       
  2873 .IP \(bu 2
       
  2874 Check for \fINULL\fP key in \fBjson_object_get()\fP and
       
  2875 \fBjson_object_del()\fP (#151).
       
  2876 .IP \(bu 2
       
  2877 Enhance hashtable seeding on Windows (#162).
       
  2878 .IP \(bu 2
       
  2879 \fBjson_unpack()\fP: Allow mixing JSON_STRICT with optional keys
       
  2880 (#162, #163).
       
  2881 .IP \(bu 2
       
  2882 Fix int/int32 mismatch (#142).
       
  2883 .IP \(bu 2
       
  2884 Parse subnormal numbers correctly (#202).
       
  2885 .UNINDENT
       
  2886 .IP \(bu 2
       
  2887 Build:
       
  2888 .INDENT 2.0
       
  2889 .IP \(bu 2
       
  2890 Remove VS2010 build files. CMake should be used on Windows instead
       
  2891 (#165).
       
  2892 .IP \(bu 2
       
  2893 Fix CMake build flags for MinGW (#193).
       
  2894 .IP \(bu 2
       
  2895 Add CMake config files for find_package. Rename config.h to
       
  2896 jansson_private_config.h (#157, #159).
       
  2897 .IP \(bu 2
       
  2898 Make Valgrind checks work with CMake (#160).
       
  2899 .IP \(bu 2
       
  2900 Fix feature checks to use correct __ATOMIC flags.
       
  2901 .IP \(bu 2
       
  2902 Fix CMake checks for uint16_t and uint8_t support (#177).
       
  2903 .IP \(bu 2
       
  2904 Make Jansson build on SmartOS/Solaris (#171).
       
  2905 .IP \(bu 2
       
  2906 Work around a GCC bug on Solaris (#175).
       
  2907 .IP \(bu 2
       
  2908 Fix autoreconf on Debian (#182).
       
  2909 .IP \(bu 2
       
  2910 Don\(aqt use GNU make specific export for global AM_CFLAGS (#203,
       
  2911 #204).
       
  2912 .IP \(bu 2
       
  2913 Fix building on Android using the supplied Android.mk (#166,
       
  2914 #174).
       
  2915 .IP \(bu 2
       
  2916 Android.mk: Add \-DHAVE_STDINT_H to LOCAL_CFLAGS (#200).
       
  2917 .UNINDENT
       
  2918 .IP \(bu 2
       
  2919 Documentation:
       
  2920 .INDENT 2.0
       
  2921 .IP \(bu 2
       
  2922 Document JANSSON_BUILD_SHARED_LIBS CMake option (#187).
       
  2923 .UNINDENT
       
  2924 .IP \(bu 2
       
  2925 Tests:
       
  2926 .INDENT 2.0
       
  2927 .IP \(bu 2
       
  2928 Close file handles correctly (#198).
       
  2929 .UNINDENT
       
  2930 .IP \(bu 2
       
  2931 Other changes:
       
  2932 .INDENT 2.0
       
  2933 .IP \(bu 2
       
  2934 \fB\euNNNN\fP escapes are now encoded in upper case for better
       
  2935 readability.
       
  2936 .IP \(bu 2
       
  2937 Enable usage of AddressSanitizer (#180).
       
  2938 .UNINDENT
       
  2939 .UNINDENT
       
  2940 .SS Version 2.6
       
  2941 .sp
       
  2942 Released 2014\-02\-11
       
  2943 .INDENT 0.0
       
  2944 .IP \(bu 2
       
  2945 Security:
       
  2946 .INDENT 2.0
       
  2947 .IP \(bu 2
       
  2948 CVE\-2013\-6401: The hash function used by the hashtable
       
  2949 implementation has been changed, and is automatically seeded with
       
  2950 random data when the first JSON object is created. This prevents
       
  2951 an attacker from causing large JSON objects with specially crafted
       
  2952 keys perform poorly.
       
  2953 .UNINDENT
       
  2954 .IP \(bu 2
       
  2955 New features:
       
  2956 .INDENT 2.0
       
  2957 .IP \(bu 2
       
  2958 \fBjson_object_seed()\fP: Set the seed value of the hash function.
       
  2959 .UNINDENT
       
  2960 .IP \(bu 2
       
  2961 Bug fixes:
       
  2962 .INDENT 2.0
       
  2963 .IP \(bu 2
       
  2964 Include CMake specific files in the release tarball.
       
  2965 .UNINDENT
       
  2966 .IP \(bu 2
       
  2967 Documentation:
       
  2968 .INDENT 2.0
       
  2969 .IP \(bu 2
       
  2970 Fix tutorial source to send a User\-Agent header, which is now
       
  2971 required by the GitHub API.
       
  2972 .IP \(bu 2
       
  2973 Set all memory to zero in secure_free() example.
       
  2974 .UNINDENT
       
  2975 .UNINDENT
       
  2976 .SS Version 2.5
       
  2977 .sp
       
  2978 Released 2013\-09\-19
       
  2979 .INDENT 0.0
       
  2980 .IP \(bu 2
       
  2981 New features:
       
  2982 .INDENT 2.0
       
  2983 .IP \(bu 2
       
  2984 \fBjson_pack()\fP and friends: Add format specifiers \fBs#\fP, \fB+\fP and
       
  2985 \fB+#\fP\&.
       
  2986 .IP \(bu 2
       
  2987 Add \fBJSON_DECODE_INT_AS_REAL\fP decoding flag to treat all numbers
       
  2988 as real in the decoder (#123).
       
  2989 .IP \(bu 2
       
  2990 Add \fBjson_array_foreach()\fP, paralleling \fBjson_object_foreach()\fP
       
  2991 (#118).
       
  2992 .UNINDENT
       
  2993 .IP \(bu 2
       
  2994 Bug fixes:
       
  2995 .INDENT 2.0
       
  2996 .IP \(bu 2
       
  2997 \fBjson_dumps()\fP and friends: Don\(aqt crash if json is \fINULL\fP and
       
  2998 \fBJSON_ENCODE_ANY\fP is set.
       
  2999 .IP \(bu 2
       
  3000 Fix a theoretical integer overflow in \fBjsonp_strdup()\fP\&.
       
  3001 .IP \(bu 2
       
  3002 Fix \fBl_isxdigit()\fP macro (#97).
       
  3003 .IP \(bu 2
       
  3004 Fix an off\-by\-one error in \fBjson_array_remove()\fP\&.
       
  3005 .UNINDENT
       
  3006 .IP \(bu 2
       
  3007 Build:
       
  3008 .INDENT 2.0
       
  3009 .IP \(bu 2
       
  3010 Support CMake in addition to GNU Autotools (#106, #107, #112,
       
  3011 #115, #120, #127).
       
  3012 .IP \(bu 2
       
  3013 Support building for Android (#109).
       
  3014 .IP \(bu 2
       
  3015 Don\(aqt use \fB\-Werror\fP by default.
       
  3016 .IP \(bu 2
       
  3017 Support building and testing with VPATH (#93).
       
  3018 .IP \(bu 2
       
  3019 Fix compilation when \fBNDEBUG\fP is defined (#128)
       
  3020 .UNINDENT
       
  3021 .IP \(bu 2
       
  3022 Tests:
       
  3023 .INDENT 2.0
       
  3024 .IP \(bu 2
       
  3025 Fix a refleak in \fBtest/bin/json_process.c\fP\&.
       
  3026 .UNINDENT
       
  3027 .IP \(bu 2
       
  3028 Documentation:
       
  3029 .INDENT 2.0
       
  3030 .IP \(bu 2
       
  3031 Clarify the return value of \fBjson_load_callback_t()\fP\&.
       
  3032 .IP \(bu 2
       
  3033 Document how to circumvent problems with separate heaps on Windows.
       
  3034 .IP \(bu 2
       
  3035 Fix memory leaks and warnings in \fBgithub_commits.c\fP\&.
       
  3036 .IP \(bu 2
       
  3037 Use \fBjson_decref()\fP properly in tutorial.
       
  3038 .UNINDENT
       
  3039 .IP \(bu 2
       
  3040 Other:
       
  3041 .INDENT 2.0
       
  3042 .IP \(bu 2
       
  3043 Make it possible to forward declare \fBstruct json_t\fP\&.
       
  3044 .UNINDENT
       
  3045 .UNINDENT
       
  3046 .SS Version 2.4
       
  3047 .sp
       
  3048 Released 2012\-09\-23
       
  3049 .INDENT 0.0
       
  3050 .IP \(bu 2
       
  3051 New features:
       
  3052 .INDENT 2.0
       
  3053 .IP \(bu 2
       
  3054 Add \fBjson_boolean()\fP macro that returns the JSON true or false
       
  3055 value based on its argument (#86).
       
  3056 .IP \(bu 2
       
  3057 Add \fBjson_load_callback()\fP that calls a callback function
       
  3058 repeatedly to read the JSON input (#57).
       
  3059 .IP \(bu 2
       
  3060 Add JSON_ESCAPE_SLASH encoding flag to escape all occurences of
       
  3061 \fB/\fP with \fB\e/\fP\&.
       
  3062 .UNINDENT
       
  3063 .IP \(bu 2
       
  3064 Bug fixes:
       
  3065 .INDENT 2.0
       
  3066 .IP \(bu 2
       
  3067 Check for and reject NaN and Inf values for reals. Encoding these
       
  3068 values resulted in invalid JSON.
       
  3069 .IP \(bu 2
       
  3070 Fix \fBjson_real_set()\fP to return \-1 on error.
       
  3071 .UNINDENT
       
  3072 .IP \(bu 2
       
  3073 Build:
       
  3074 .INDENT 2.0
       
  3075 .IP \(bu 2
       
  3076 Jansson now builds on Windows with Visual Studio 2010, and
       
  3077 includes solution and project files in \fBwin32/vs2010/\fP
       
  3078 directory.
       
  3079 .IP \(bu 2
       
  3080 Fix build warnings (#77, #78).
       
  3081 .IP \(bu 2
       
  3082 Add \fB\-no\-undefined\fP to LDFLAGS (#90).
       
  3083 .UNINDENT
       
  3084 .IP \(bu 2
       
  3085 Tests:
       
  3086 .INDENT 2.0
       
  3087 .IP \(bu 2
       
  3088 Fix the symbol exports test on Linux/PPC64 (#88).
       
  3089 .UNINDENT
       
  3090 .IP \(bu 2
       
  3091 Documentation:
       
  3092 .INDENT 2.0
       
  3093 .IP \(bu 2
       
  3094 Fix typos (#73, #84).
       
  3095 .UNINDENT
       
  3096 .UNINDENT
       
  3097 .SS Version 2.3.1
       
  3098 .sp
       
  3099 Released 2012\-04\-20
       
  3100 .INDENT 0.0
       
  3101 .IP \(bu 2
       
  3102 Build issues:
       
  3103 .INDENT 2.0
       
  3104 .IP \(bu 2
       
  3105 Only use \fBlong long\fP if \fBstrtoll()\fP is also available.
       
  3106 .UNINDENT
       
  3107 .IP \(bu 2
       
  3108 Documentation:
       
  3109 .INDENT 2.0
       
  3110 .IP \(bu 2
       
  3111 Fix the names of library version constants in documentation. (#52)
       
  3112 .IP \(bu 2
       
  3113 Change the tutorial to use GitHub API v3. (#65)
       
  3114 .UNINDENT
       
  3115 .IP \(bu 2
       
  3116 Tests:
       
  3117 .INDENT 2.0
       
  3118 .IP \(bu 2
       
  3119 Make some tests locale independent. (#51)
       
  3120 .IP \(bu 2
       
  3121 Distribute the library exports test in the tarball.
       
  3122 .IP \(bu 2
       
  3123 Make test run on shells that don\(aqt support the \fBexport FOO=bar\fP
       
  3124 syntax.
       
  3125 .UNINDENT
       
  3126 .UNINDENT
       
  3127 .SS Version 2.3
       
  3128 .sp
       
  3129 Released 2012\-01\-27
       
  3130 .INDENT 0.0
       
  3131 .IP \(bu 2
       
  3132 New features:
       
  3133 .INDENT 2.0
       
  3134 .IP \(bu 2
       
  3135 \fBjson_unpack()\fP and friends: Add support for optional object keys
       
  3136 with the \fB{s?o}\fP syntax.
       
  3137 .IP \(bu 2
       
  3138 Add \fBjson_object_update_existing()\fP and
       
  3139 \fBjson_object_update_missing()\fP, for updating only existing keys or
       
  3140 only adding missing keys to an object. (#37)
       
  3141 .IP \(bu 2
       
  3142 Add \fBjson_object_foreach()\fP for more convenient iteration over
       
  3143 objects. (#45, #46)
       
  3144 .IP \(bu 2
       
  3145 When decoding JSON, write the number of bytes that were read from
       
  3146 input to \fBerror.position\fP also on success. This is handy with
       
  3147 \fBJSON_DISABLE_EOF_CHECK\fP\&.
       
  3148 .IP \(bu 2
       
  3149 Add support for decoding any JSON value, not just arrays or
       
  3150 objects. The support is enabled with the new \fBJSON_DECODE_ANY\fP
       
  3151 flag. Patch by Andrea Marchesini. (#4)
       
  3152 .UNINDENT
       
  3153 .IP \(bu 2
       
  3154 Bug fixes
       
  3155 .INDENT 2.0
       
  3156 .IP \(bu 2
       
  3157 Avoid problems with object\(aqs serial number growing too big. (#40,
       
  3158 #41)
       
  3159 .IP \(bu 2
       
  3160 Decoding functions now return NULL if the first argument is NULL.
       
  3161 Patch by Andrea Marchesini.
       
  3162 .IP \(bu 2
       
  3163 Include \fBjansson_config.h.win32\fP in the distribution tarball.
       
  3164 .IP \(bu 2
       
  3165 Remove \fB+\fP and leading zeros from exponents in the encoder.
       
  3166 (#39)
       
  3167 .IP \(bu 2
       
  3168 Make Jansson build and work on MinGW. (#39, #38)
       
  3169 .UNINDENT
       
  3170 .IP \(bu 2
       
  3171 Documentation
       
  3172 .INDENT 2.0
       
  3173 .IP \(bu 2
       
  3174 Note that the same JSON values must not be encoded in parallel by
       
  3175 separate threads. (#42)
       
  3176 .IP \(bu 2
       
  3177 Document MinGW support.
       
  3178 .UNINDENT
       
  3179 .UNINDENT
       
  3180 .SS Version 2.2.1
       
  3181 .sp
       
  3182 Released 2011\-10\-06
       
  3183 .INDENT 0.0
       
  3184 .IP \(bu 2
       
  3185 Bug fixes:
       
  3186 .INDENT 2.0
       
  3187 .IP \(bu 2
       
  3188 Fix real number encoding and decoding under non\-C locales. (#32)
       
  3189 .IP \(bu 2
       
  3190 Fix identifier decoding under non\-UTF\-8 locales. (#35)
       
  3191 .IP \(bu 2
       
  3192 \fBjson_load_file()\fP: Open the input file in binary mode for maximum
       
  3193 compatiblity.
       
  3194 .UNINDENT
       
  3195 .IP \(bu 2
       
  3196 Documentation:
       
  3197 .INDENT 2.0
       
  3198 .IP \(bu 2
       
  3199 Clarify the lifecycle of the result of the \fBs\fP fromat of
       
  3200 \fBjson_unpack()\fP\&. (#31)
       
  3201 .IP \(bu 2
       
  3202 Add some portability info. (#36)
       
  3203 .IP \(bu 2
       
  3204 Little clarifications here and there.
       
  3205 .UNINDENT
       
  3206 .IP \(bu 2
       
  3207 Other:
       
  3208 .INDENT 2.0
       
  3209 .IP \(bu 2
       
  3210 Some style fixes, issues detected by static analyzers.
       
  3211 .UNINDENT
       
  3212 .UNINDENT
       
  3213 .SS Version 2.2
       
  3214 .sp
       
  3215 Released 2011\-09\-03
       
  3216 .INDENT 0.0
       
  3217 .IP \(bu 2
       
  3218 New features:
       
  3219 .INDENT 2.0
       
  3220 .IP \(bu 2
       
  3221 \fBjson_dump_callback()\fP: Pass the encoder output to a callback
       
  3222 function in chunks.
       
  3223 .UNINDENT
       
  3224 .IP \(bu 2
       
  3225 Bug fixes:
       
  3226 .INDENT 2.0
       
  3227 .IP \(bu 2
       
  3228 \fBjson_string_set()\fP: Check that target is a string and value is
       
  3229 not NULL.
       
  3230 .UNINDENT
       
  3231 .IP \(bu 2
       
  3232 Other:
       
  3233 .INDENT 2.0
       
  3234 .IP \(bu 2
       
  3235 Documentation typo fixes and clarifications.
       
  3236 .UNINDENT
       
  3237 .UNINDENT
       
  3238 .SS Version 2.1
       
  3239 .sp
       
  3240 Released 2011\-06\-10
       
  3241 .INDENT 0.0
       
  3242 .IP \(bu 2
       
  3243 New features:
       
  3244 .INDENT 2.0
       
  3245 .IP \(bu 2
       
  3246 \fBjson_loadb()\fP: Decode a string with a given size, useful if the
       
  3247 string is not null terminated.
       
  3248 .IP \(bu 2
       
  3249 Add \fBJSON_ENCODE_ANY\fP encoding flag to allow encoding any JSON
       
  3250 value. By default, only arrays and objects can be encoded. (#19)
       
  3251 .IP \(bu 2
       
  3252 Add \fBJSON_REJECT_DUPLICATES\fP decoding flag to issue a decoding
       
  3253 error if any JSON object in the input contins duplicate keys. (#3)
       
  3254 .IP \(bu 2
       
  3255 Add \fBJSON_DISABLE_EOF_CHECK\fP decoding flag to stop decoding after a
       
  3256 valid JSON input. This allows other data after the JSON data.
       
  3257 .UNINDENT
       
  3258 .IP \(bu 2
       
  3259 Bug fixes:
       
  3260 .INDENT 2.0
       
  3261 .IP \(bu 2
       
  3262 Fix an additional memory leak when memory allocation fails in
       
  3263 \fBjson_object_set()\fP and friends.
       
  3264 .IP \(bu 2
       
  3265 Clear errno before calling \fBstrtod()\fP for better portability. (#27)
       
  3266 .UNINDENT
       
  3267 .IP \(bu 2
       
  3268 Building:
       
  3269 .INDENT 2.0
       
  3270 .IP \(bu 2
       
  3271 Avoid set\-but\-not\-used warning/error in a test. (#20)
       
  3272 .UNINDENT
       
  3273 .IP \(bu 2
       
  3274 Other:
       
  3275 .INDENT 2.0
       
  3276 .IP \(bu 2
       
  3277 Minor clarifications to documentation.
       
  3278 .UNINDENT
       
  3279 .UNINDENT
       
  3280 .SS Version 2.0.1
       
  3281 .sp
       
  3282 Released 2011\-03\-31
       
  3283 .INDENT 0.0
       
  3284 .IP \(bu 2
       
  3285 Bug fixes:
       
  3286 .INDENT 2.0
       
  3287 .IP \(bu 2
       
  3288 Replace a few \fBmalloc()\fP and \fBfree()\fP calls with their
       
  3289 counterparts that support custom memory management.
       
  3290 .IP \(bu 2
       
  3291 Fix object key hashing in json_unpack() strict checking mode.
       
  3292 .IP \(bu 2
       
  3293 Fix the parentheses in \fBJANSSON_VERSION_HEX\fP macro.
       
  3294 .IP \(bu 2
       
  3295 Fix \fBjson_object_size()\fP return value.
       
  3296 .IP \(bu 2
       
  3297 Fix a few compilation issues.
       
  3298 .UNINDENT
       
  3299 .IP \(bu 2
       
  3300 Portability:
       
  3301 .INDENT 2.0
       
  3302 .IP \(bu 2
       
  3303 Enhance portability of \fBva_copy()\fP\&.
       
  3304 .IP \(bu 2
       
  3305 Test framework portability enhancements.
       
  3306 .UNINDENT
       
  3307 .IP \(bu 2
       
  3308 Documentation:
       
  3309 .INDENT 2.0
       
  3310 .IP \(bu 2
       
  3311 Distribute \fBdoc/upgrading.rst\fP with the source tarball.
       
  3312 .IP \(bu 2
       
  3313 Build documentation in strict mode in \fBmake distcheck\fP\&.
       
  3314 .UNINDENT
       
  3315 .UNINDENT
       
  3316 .SS Version 2.0
       
  3317 .sp
       
  3318 Released 2011\-02\-28
       
  3319 .sp
       
  3320 This release is backwards incompatible with the 1.x release series.
       
  3321 See the chapter "Upgrading from older versions" in documentation for
       
  3322 details.
       
  3323 .INDENT 0.0
       
  3324 .IP \(bu 2
       
  3325 Backwards incompatible changes:
       
  3326 .INDENT 2.0
       
  3327 .IP \(bu 2
       
  3328 Unify unsigned integer usage in the API: All occurences of
       
  3329 unsigned int and unsigned long have been replaced with size_t.
       
  3330 .IP \(bu 2
       
  3331 Change JSON integer\(aqs underlying type to the widest signed integer
       
  3332 type available, i.e. long long if it\(aqs supported, otherwise long.
       
  3333 Add a typedef json_int_t that defines the type.
       
  3334 .IP \(bu 2
       
  3335 Change the maximum indentation depth to 31 spaces in encoder. This
       
  3336 frees up bits from the flags parameter of encoding functions
       
  3337 \fBjson_dumpf()\fP, \fBjson_dumps()\fP and \fBjson_dump_file()\fP\&.
       
  3338 .IP \(bu 2
       
  3339 For future needs, add a flags parameter to all decoding functions
       
  3340 \fBjson_loadf()\fP, \fBjson_loads()\fP and \fBjson_load_file()\fP\&.
       
  3341 .UNINDENT
       
  3342 .IP \(bu 2
       
  3343 New features
       
  3344 .INDENT 2.0
       
  3345 .IP \(bu 2
       
  3346 \fBjson_pack()\fP, \fBjson_pack_ex()\fP, \fBjson_vpack_ex()\fP: Create JSON
       
  3347 values based on a format string.
       
  3348 .IP \(bu 2
       
  3349 \fBjson_unpack()\fP, \fBjson_unpack_ex()\fP, \fBjson_vunpack_ex()\fP: Simple
       
  3350 value extraction and validation functionality based on a format
       
  3351 string.
       
  3352 .IP \(bu 2
       
  3353 Add column, position and source fields to the \fBjson_error_t\fP
       
  3354 struct.
       
  3355 .IP \(bu 2
       
  3356 Enhance error reporting in the decoder.
       
  3357 .IP \(bu 2
       
  3358 \fBJANSSON_VERSION\fP et al.: Preprocessor constants that define the
       
  3359 library version.
       
  3360 .IP \(bu 2
       
  3361 \fBjson_set_alloc_funcs()\fP: Set custom memory allocation functions.
       
  3362 .UNINDENT
       
  3363 .IP \(bu 2
       
  3364 Fix many portability issues, especially on Windows.
       
  3365 .IP \(bu 2
       
  3366 Configuration
       
  3367 .INDENT 2.0
       
  3368 .IP \(bu 2
       
  3369 Add file \fBjansson_config.h\fP that contains site specific
       
  3370 configuration. It\(aqs created automatically by the configure script,
       
  3371 or can be created by hand if the configure script cannot be used.
       
  3372 The file \fBjansson_config.h.win32\fP can be used without
       
  3373 modifications on Windows systems.
       
  3374 .IP \(bu 2
       
  3375 Add a section to documentation describing how to build Jansson on
       
  3376 Windows.
       
  3377 .IP \(bu 2
       
  3378 Documentation now requires Sphinx 1.0 or newer.
       
  3379 .UNINDENT
       
  3380 .UNINDENT
       
  3381 .SS Version 1.3
       
  3382 .sp
       
  3383 Released 2010\-06\-13
       
  3384 .INDENT 0.0
       
  3385 .IP \(bu 2
       
  3386 New functions:
       
  3387 .INDENT 2.0
       
  3388 .IP \(bu 2
       
  3389 \fBjson_object_iter_set()\fP, \fBjson_object_iter_set_new()\fP: Change
       
  3390 object contents while iterating over it.
       
  3391 .IP \(bu 2
       
  3392 \fBjson_object_iter_at()\fP: Return an iterator that points to a
       
  3393 specific object item.
       
  3394 .UNINDENT
       
  3395 .IP \(bu 2
       
  3396 New encoding flags:
       
  3397 .INDENT 2.0
       
  3398 .IP \(bu 2
       
  3399 \fBJSON_PRESERVE_ORDER\fP: Preserve the insertion order of object
       
  3400 keys.
       
  3401 .UNINDENT
       
  3402 .IP \(bu 2
       
  3403 Bug fixes:
       
  3404 .INDENT 2.0
       
  3405 .IP \(bu 2
       
  3406 Fix an error that occured when an array or object was first
       
  3407 encoded as empty, then populated with some data, and then
       
  3408 re\-encoded
       
  3409 .IP \(bu 2
       
  3410 Fix the situation like above, but when the first encoding resulted
       
  3411 in an error
       
  3412 .UNINDENT
       
  3413 .IP \(bu 2
       
  3414 Documentation:
       
  3415 .INDENT 2.0
       
  3416 .IP \(bu 2
       
  3417 Clarify the documentation on reference stealing, providing an
       
  3418 example usage pattern
       
  3419 .UNINDENT
       
  3420 .UNINDENT
       
  3421 .SS Version 1.2.1
       
  3422 .sp
       
  3423 Released 2010\-04\-03
       
  3424 .INDENT 0.0
       
  3425 .IP \(bu 2
       
  3426 Bug fixes:
       
  3427 .INDENT 2.0
       
  3428 .IP \(bu 2
       
  3429 Fix reference counting on \fBtrue\fP, \fBfalse\fP and \fBnull\fP
       
  3430 .IP \(bu 2
       
  3431 Estimate real number underflows in decoder with 0.0 instead of
       
  3432 issuing an error
       
  3433 .UNINDENT
       
  3434 .IP \(bu 2
       
  3435 Portability:
       
  3436 .INDENT 2.0
       
  3437 .IP \(bu 2
       
  3438 Make \fBint32_t\fP available on all systems
       
  3439 .IP \(bu 2
       
  3440 Support compilers that don\(aqt have the \fBinline\fP keyword
       
  3441 .IP \(bu 2
       
  3442 Require Autoconf 2.60 (for \fBint32_t\fP)
       
  3443 .UNINDENT
       
  3444 .IP \(bu 2
       
  3445 Tests:
       
  3446 .INDENT 2.0
       
  3447 .IP \(bu 2
       
  3448 Print test names correctly when \fBVERBOSE=1\fP
       
  3449 .IP \(bu 2
       
  3450 \fBtest/suites/api\fP: Fail when a test fails
       
  3451 .IP \(bu 2
       
  3452 Enhance tests for iterators
       
  3453 .IP \(bu 2
       
  3454 Enhance tests for decoding texts that contain null bytes
       
  3455 .UNINDENT
       
  3456 .IP \(bu 2
       
  3457 Documentation:
       
  3458 .INDENT 2.0
       
  3459 .IP \(bu 2
       
  3460 Don\(aqt remove \fBchanges.rst\fP in \fBmake clean\fP
       
  3461 .IP \(bu 2
       
  3462 Add a chapter on RFC conformance
       
  3463 .UNINDENT
       
  3464 .UNINDENT
       
  3465 .SS Version 1.2
       
  3466 .sp
       
  3467 Released 2010\-01\-21
       
  3468 .INDENT 0.0
       
  3469 .IP \(bu 2
       
  3470 New functions:
       
  3471 .INDENT 2.0
       
  3472 .IP \(bu 2
       
  3473 \fBjson_equal()\fP: Test whether two JSON values are equal
       
  3474 .IP \(bu 2
       
  3475 \fBjson_copy()\fP and \fBjson_deep_copy()\fP: Make shallow and deep copies
       
  3476 of JSON values
       
  3477 .IP \(bu 2
       
  3478 Add a version of all functions taking a string argument that
       
  3479 doesn\(aqt check for valid UTF\-8: \fBjson_string_nocheck()\fP,
       
  3480 \fBjson_string_set_nocheck()\fP, \fBjson_object_set_nocheck()\fP,
       
  3481 \fBjson_object_set_new_nocheck()\fP
       
  3482 .UNINDENT
       
  3483 .IP \(bu 2
       
  3484 New encoding flags:
       
  3485 .INDENT 2.0
       
  3486 .IP \(bu 2
       
  3487 \fBJSON_SORT_KEYS\fP: Sort objects by key
       
  3488 .IP \(bu 2
       
  3489 \fBJSON_ENSURE_ASCII\fP: Escape all non\-ASCII Unicode characters
       
  3490 .IP \(bu 2
       
  3491 \fBJSON_COMPACT\fP: Use a compact representation with all unneeded
       
  3492 whitespace stripped
       
  3493 .UNINDENT
       
  3494 .IP \(bu 2
       
  3495 Bug fixes:
       
  3496 .INDENT 2.0
       
  3497 .IP \(bu 2
       
  3498 Revise and unify whitespace usage in encoder: Add spaces between
       
  3499 array and object items, never append newline to output.
       
  3500 .IP \(bu 2
       
  3501 Remove const qualifier from the \fBjson_t\fP parameter in
       
  3502 \fBjson_string_set()\fP, \fBjson_integer_set()\fP and \fBjson_real_set()\fP\&.
       
  3503 .IP \(bu 2
       
  3504 Use \fBint32_t\fP internally for representing Unicode code points
       
  3505 (int is not enough on all platforms)
       
  3506 .UNINDENT
       
  3507 .IP \(bu 2
       
  3508 Other changes:
       
  3509 .INDENT 2.0
       
  3510 .IP \(bu 2
       
  3511 Convert \fBCHANGES\fP (this file) to reStructured text and add it to
       
  3512 HTML documentation
       
  3513 .IP \(bu 2
       
  3514 The test system has been refactored. Python is no longer required
       
  3515 to run the tests.
       
  3516 .IP \(bu 2
       
  3517 Documentation can now be built by invoking \fBmake html\fP
       
  3518 .IP \(bu 2
       
  3519 Support for pkg\-config
       
  3520 .UNINDENT
       
  3521 .UNINDENT
       
  3522 .SS Version 1.1.3
       
  3523 .sp
       
  3524 Released 2009\-12\-18
       
  3525 .INDENT 0.0
       
  3526 .IP \(bu 2
       
  3527 Encode reals correctly, so that first encoding and then decoding a
       
  3528 real always produces the same value
       
  3529 .IP \(bu 2
       
  3530 Don\(aqt export private symbols in \fBlibjansson.so\fP
       
  3531 .UNINDENT
       
  3532 .SS Version 1.1.2
       
  3533 .sp
       
  3534 Released 2009\-11\-08
       
  3535 .INDENT 0.0
       
  3536 .IP \(bu 2
       
  3537 Fix a bug where an error message was not produced if the input file
       
  3538 could not be opened in \fBjson_load_file()\fP
       
  3539 .IP \(bu 2
       
  3540 Fix an assertion failure in decoder caused by a minus sign without a
       
  3541 digit after it
       
  3542 .IP \(bu 2
       
  3543 Remove an unneeded include of \fBstdint.h\fP in \fBjansson.h\fP
       
  3544 .UNINDENT
       
  3545 .SS Version 1.1.1
       
  3546 .sp
       
  3547 Released 2009\-10\-26
       
  3548 .INDENT 0.0
       
  3549 .IP \(bu 2
       
  3550 All documentation files were not distributed with v1.1; build
       
  3551 documentation in make distcheck to prevent this in the future
       
  3552 .IP \(bu 2
       
  3553 Fix v1.1 release date in \fBCHANGES\fP
       
  3554 .UNINDENT
       
  3555 .SS Version 1.1
       
  3556 .sp
       
  3557 Released 2009\-10\-20
       
  3558 .INDENT 0.0
       
  3559 .IP \(bu 2
       
  3560 API additions and improvements:
       
  3561 .INDENT 2.0
       
  3562 .IP \(bu 2
       
  3563 Extend array and object APIs
       
  3564 .IP \(bu 2
       
  3565 Add functions to modify integer, real and string values
       
  3566 .IP \(bu 2
       
  3567 Improve argument validation
       
  3568 .IP \(bu 2
       
  3569 Use unsigned int instead of \fBuint32_t\fP for encoding flags
       
  3570 .UNINDENT
       
  3571 .IP \(bu 2
       
  3572 Enhance documentation
       
  3573 .INDENT 2.0
       
  3574 .IP \(bu 2
       
  3575 Add getting started guide and tutorial
       
  3576 .IP \(bu 2
       
  3577 Fix some typos
       
  3578 .IP \(bu 2
       
  3579 General clarifications and cleanup
       
  3580 .UNINDENT
       
  3581 .IP \(bu 2
       
  3582 Check for integer and real overflows and underflows in decoder
       
  3583 .IP \(bu 2
       
  3584 Make singleton values thread\-safe (\fBtrue\fP, \fBfalse\fP and \fBnull\fP)
       
  3585 .IP \(bu 2
       
  3586 Enhance circular reference handling
       
  3587 .IP \(bu 2
       
  3588 Don\(aqt define \fB\-std=c99\fP in \fBAM_CFLAGS\fP
       
  3589 .IP \(bu 2
       
  3590 Add C++ guards to \fBjansson.h\fP
       
  3591 .IP \(bu 2
       
  3592 Minor performance and portability improvements
       
  3593 .IP \(bu 2
       
  3594 Expand test coverage
       
  3595 .UNINDENT
       
  3596 .SS Version 1.0.4
       
  3597 .sp
       
  3598 Released 2009\-10\-11
       
  3599 .INDENT 0.0
       
  3600 .IP \(bu 2
       
  3601 Relax Autoconf version requirement to 2.59
       
  3602 .IP \(bu 2
       
  3603 Make Jansson compile on platforms where plain \fBchar\fP is unsigned
       
  3604 .IP \(bu 2
       
  3605 Fix API tests for object
       
  3606 .UNINDENT
       
  3607 .SS Version 1.0.3
       
  3608 .sp
       
  3609 Released 2009\-09\-14
       
  3610 .INDENT 0.0
       
  3611 .IP \(bu 2
       
  3612 Check for integer and real overflows and underflows in decoder
       
  3613 .IP \(bu 2
       
  3614 Use the Python json module for tests, or simplejson if the json
       
  3615 module is not found
       
  3616 .IP \(bu 2
       
  3617 Distribute changelog (this file)
       
  3618 .UNINDENT
       
  3619 .SS Version 1.0.2
       
  3620 .sp
       
  3621 Released 2009\-09\-08
       
  3622 .INDENT 0.0
       
  3623 .IP \(bu 2
       
  3624 Handle EOF correctly in decoder
       
  3625 .UNINDENT
       
  3626 .SS Version 1.0.1
       
  3627 .sp
       
  3628 Released 2009\-09\-04
       
  3629 .INDENT 0.0
       
  3630 .IP \(bu 2
       
  3631 Fixed broken \fBjson_is_boolean()\fP
       
  3632 .UNINDENT
       
  3633 .SS Version 1.0
       
  3634 .sp
       
  3635 Released 2009\-08\-25
       
  3636 .INDENT 0.0
       
  3637 .IP \(bu 2
       
  3638 Initial release
       
  3639 .UNINDENT
       
  3640 .INDENT 0.0
       
  3641 .IP \(bu 2
       
  3642 \fIgenindex\fP
       
  3643 .IP \(bu 2
       
  3644 \fIsearch\fP
       
  3645 .UNINDENT
       
  3646 .sp
       
  3647 .SH AUTHOR
       
  3648 Petri Lehtinen
       
  3649 .SH COPYRIGHT
       
  3650 2009-2014, Petri Lehtinen
       
  3651 .\" Generated by docutils manpage writer.
       
  3652 .