/usr/share/doc/libjansson-dev/html/apiref.html is in libjansson-doc 2.11-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>API Reference — Jansson 2.11 documentation</title>
<link rel="stylesheet" href="_static/alabaster.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '2.11',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true,
SOURCELINK_SUFFIX: '.txt'
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="Changes in Jansson" href="changes.html" />
<link rel="prev" title="Portability" href="portability.html" />
<link rel="stylesheet" href="_static/custom.css" type="text/css" />
<meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />
</head>
<body>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="api-reference">
<span id="apiref"></span><h1>API Reference<a class="headerlink" href="#api-reference" title="Permalink to this headline">¶</a></h1>
<div class="section" id="preliminaries">
<h2>Preliminaries<a class="headerlink" href="#preliminaries" title="Permalink to this headline">¶</a></h2>
<p>All declarations are in <code class="file docutils literal"><span class="pre">jansson.h</span></code>, so it’s enough to</p>
<div class="highlight-c"><div class="highlight"><pre><span></span><span class="cp">#include</span> <span class="cpf"><jansson.h></span><span class="cp"></span>
</pre></div>
</div>
<p>in each source file.</p>
<p>All constants are prefixed with <code class="docutils literal"><span class="pre">JSON_</span></code> (except for those describing
the library version, prefixed with <code class="docutils literal"><span class="pre">JANSSON_</span></code>). Other identifiers
are prefixed with <code class="docutils literal"><span class="pre">json_</span></code>. Type names are suffixed with <code class="docutils literal"><span class="pre">_t</span></code> and
<code class="docutils literal"><span class="pre">typedef</span></code>’d so that the <code class="docutils literal"><span class="pre">struct</span></code> keyword need not be used.</p>
</div>
<div class="section" id="library-version">
<h2>Library Version<a class="headerlink" href="#library-version" title="Permalink to this headline">¶</a></h2>
<p>The Jansson version is of the form <em>A.B.C</em>, where <em>A</em> is the major
version, <em>B</em> is the minor version and <em>C</em> is the micro version. If the
micro version is zero, it’s omitted from the version string, i.e. the
version string is just <em>A.B</em>.</p>
<p>When a new release only fixes bugs and doesn’t add new features or
functionality, the micro version is incremented. When new features are
added in a backwards compatible way, the minor version is incremented
and the micro version is set to zero. When there are backwards
incompatible changes, the major version is incremented and others are
set to zero.</p>
<p>The following preprocessor constants specify the current version of
the library:</p>
<dl class="docutils">
<dt><code class="docutils literal"><span class="pre">JANSSON_MAJOR_VERSION</span></code>, <code class="docutils literal"><span class="pre">JANSSON_MINOR_VERSION</span></code>, <code class="docutils literal"><span class="pre">JANSSON_MICRO_VERSION</span></code></dt>
<dd>Integers specifying the major, minor and micro versions,
respectively.</dd>
<dt><code class="docutils literal"><span class="pre">JANSSON_VERSION</span></code></dt>
<dd>A string representation of the current version, e.g. <code class="docutils literal"><span class="pre">"1.2.1"</span></code> or
<code class="docutils literal"><span class="pre">"1.3"</span></code>.</dd>
<dt><code class="docutils literal"><span class="pre">JANSSON_VERSION_HEX</span></code></dt>
<dd><p class="first">A 3-byte hexadecimal representation of the version, e.g.
<code class="docutils literal"><span class="pre">0x010201</span></code> for version 1.2.1 and <code class="docutils literal"><span class="pre">0x010300</span></code> for version 1.3.
This is useful in numeric comparisons, e.g.:</p>
<div class="last highlight-c"><div class="highlight"><pre><span></span><span class="cp">#if JANSSON_VERSION_HEX >= 0x010300</span>
<span class="cm">/* Code specific to version 1.3 and above */</span>
<span class="cp">#endif</span>
</pre></div>
</div>
</dd>
<dt><code class="docutils literal"><span class="pre">JANSSON_THREAD_SAFE_REFCOUNT</span></code></dt>
<dd>If this value is defined all read-only operations and reference counting in
Jansson are thread safe. This value is not defined for versions older than
<code class="docutils literal"><span class="pre">2.11</span></code> or when the compiler does not provide built-in atomic functions.</dd>
</dl>
</div>
<div class="section" id="value-representation">
<h2>Value Representation<a class="headerlink" href="#value-representation" title="Permalink to this headline">¶</a></h2>
<p>The JSON specification (<span class="target" id="index-0"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc4627.html"><strong>RFC 4627</strong></a>) defines the following data types:
<em>object</em>, <em>array</em>, <em>string</em>, <em>number</em>, <em>boolean</em>, and <em>null</em>. JSON
types are used dynamically; arrays and objects can hold any other data
type, including themselves. For this reason, Jansson’s type system is
also dynamic in nature. There’s one C type to represent all JSON
values, and this structure knows the type of the JSON value it holds.</p>
<dl class="type">
<dt id="c.json_t">
<code class="descname">json_t</code><a class="headerlink" href="#c.json_t" title="Permalink to this definition">¶</a></dt>
<dd><p>This data structure is used throughout the library to represent all
JSON values. It always contains the type of the JSON value it holds
and the value’s reference count. The rest depends on the type of the
value.</p>
</dd></dl>
<p>Objects of <a class="reference internal" href="#c.json_t" title="json_t"><code class="xref c c-type docutils literal"><span class="pre">json_t</span></code></a> are always used through a pointer. There
are APIs for querying the type, manipulating the reference count, and
for constructing and manipulating values of different types.</p>
<p>Unless noted otherwise, all API functions return an error value if an
error occurs. Depending on the function’s signature, the error value
is either <em>NULL</em> or -1. Invalid arguments or invalid input are
apparent sources for errors. Memory allocation and I/O operations may
also cause errors.</p>
<div class="section" id="type">
<h3>Type<a class="headerlink" href="#type" title="Permalink to this headline">¶</a></h3>
<dl class="type">
<dt id="c.json_type">
enum <code class="descname">json_type</code><a class="headerlink" href="#c.json_type" title="Permalink to this definition">¶</a></dt>
<dd><p>The type of a JSON value. The following members are defined:</p>
<table border="1" class="docutils">
<colgroup>
<col width="100%" />
</colgroup>
<tbody valign="top">
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">JSON_OBJECT</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal"><span class="pre">JSON_ARRAY</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">JSON_STRING</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal"><span class="pre">JSON_INTEGER</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">JSON_REAL</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal"><span class="pre">JSON_TRUE</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">JSON_FALSE</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal"><span class="pre">JSON_NULL</span></code></td>
</tr>
</tbody>
</table>
<p>These correspond to JSON object, array, string, number, boolean and
null. A number is represented by either a value of the type
<code class="docutils literal"><span class="pre">JSON_INTEGER</span></code> or of the type <code class="docutils literal"><span class="pre">JSON_REAL</span></code>. A true boolean value
is represented by a value of the type <code class="docutils literal"><span class="pre">JSON_TRUE</span></code> and false by a
value of the type <code class="docutils literal"><span class="pre">JSON_FALSE</span></code>.</p>
</dd></dl>
<dl class="function">
<dt id="c.json_typeof">
int <code class="descname">json_typeof</code><span class="sig-paren">(</span>const <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *json</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_typeof" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the type of the JSON value (a <a class="reference internal" href="#c.json_type" title="json_type"><code class="xref c c-type docutils literal"><span class="pre">json_type</span></code></a> cast to
<code class="xref c c-type docutils literal"><span class="pre">int</span></code>). <em>json</em> MUST NOT be <em>NULL</em>. This function is actually
implemented as a macro for speed.</p>
</dd></dl>
<dl class="function">
<dt id="c.json_is_object">
<code class="descname">json_is_object</code><span class="sig-paren">(</span>const <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *json</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_is_object" title="Permalink to this definition">¶</a></dt>
<dt id="c.json_is_array">
<code class="descname">json_is_array</code><span class="sig-paren">(</span>const <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *json</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_is_array" title="Permalink to this definition">¶</a></dt>
<dt id="c.json_is_string">
<code class="descname">json_is_string</code><span class="sig-paren">(</span>const <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *json</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_is_string" title="Permalink to this definition">¶</a></dt>
<dt id="c.json_is_integer">
<code class="descname">json_is_integer</code><span class="sig-paren">(</span>const <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *json</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_is_integer" title="Permalink to this definition">¶</a></dt>
<dt id="c.json_is_real">
<code class="descname">json_is_real</code><span class="sig-paren">(</span>const <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *json</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_is_real" title="Permalink to this definition">¶</a></dt>
<dt id="c.json_is_true">
<code class="descname">json_is_true</code><span class="sig-paren">(</span>const <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *json</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_is_true" title="Permalink to this definition">¶</a></dt>
<dt id="c.json_is_false">
<code class="descname">json_is_false</code><span class="sig-paren">(</span>const <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *json</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_is_false" title="Permalink to this definition">¶</a></dt>
<dt id="c.json_is_null">
<code class="descname">json_is_null</code><span class="sig-paren">(</span>const <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *json</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_is_null" title="Permalink to this definition">¶</a></dt>
<dd><p>These functions (actually macros) return true (non-zero) for values
of the given type, and false (zero) for values of other types and
for <em>NULL</em>.</p>
</dd></dl>
<dl class="function">
<dt id="c.json_is_number">
<code class="descname">json_is_number</code><span class="sig-paren">(</span>const <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *json</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_is_number" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns true for values of types <code class="docutils literal"><span class="pre">JSON_INTEGER</span></code> and
<code class="docutils literal"><span class="pre">JSON_REAL</span></code>, and false for other types and for <em>NULL</em>.</p>
</dd></dl>
<dl class="function">
<dt id="c.json_is_boolean">
<code class="descname">json_is_boolean</code><span class="sig-paren">(</span>const <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *json</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_is_boolean" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns true for types <code class="docutils literal"><span class="pre">JSON_TRUE</span></code> and <code class="docutils literal"><span class="pre">JSON_FALSE</span></code>, and false
for values of other types and for <em>NULL</em>.</p>
</dd></dl>
<dl class="function">
<dt id="c.json_boolean_value">
<code class="descname">json_boolean_value</code><span class="sig-paren">(</span>const <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *json</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_boolean_value" title="Permalink to this definition">¶</a></dt>
<dd><p>Alias of <a class="reference internal" href="#c.json_is_true" title="json_is_true"><code class="xref c c-func docutils literal"><span class="pre">json_is_true()</span></code></a>, i.e. returns 1 for <code class="docutils literal"><span class="pre">JSON_TRUE</span></code>
and 0 otherwise.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.7.</span></p>
</div>
</dd></dl>
</div>
<div class="section" id="reference-count">
<span id="apiref-reference-count"></span><h3>Reference Count<a class="headerlink" href="#reference-count" title="Permalink to this headline">¶</a></h3>
<p>The reference count is used to track whether a value is still in use
or not. When a value is created, it’s reference count is set to 1. If
a reference to a value is kept (e.g. a value is stored somewhere for
later use), its reference count is incremented, and when the value is
no longer needed, the reference count is decremented. When the
reference count drops to zero, there are no references left, and the
value can be destroyed.</p>
<dl class="function">
<dt id="c.json_incref">
<a class="reference internal" href="#c.json_t" title="json_t">json_t</a> *<code class="descname">json_incref</code><span class="sig-paren">(</span><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *json</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_incref" title="Permalink to this definition">¶</a></dt>
<dd><p>Increment the reference count of <em>json</em> if it’s not <em>NULL</em>.
Returns <em>json</em>.</p>
</dd></dl>
<dl class="function">
<dt id="c.json_decref">
void <code class="descname">json_decref</code><span class="sig-paren">(</span><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *json</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_decref" title="Permalink to this definition">¶</a></dt>
<dd><p>Decrement the reference count of <em>json</em>. As soon as a call to
<a class="reference internal" href="#c.json_decref" title="json_decref"><code class="xref c c-func docutils literal"><span class="pre">json_decref()</span></code></a> drops the reference count to zero, the value
is destroyed and it can no longer be used.</p>
</dd></dl>
<p>Functions creating new JSON values set the reference count to 1. These
functions are said to return a <strong>new reference</strong>. Other functions
returning (existing) JSON values do not normally increase the
reference count. These functions are said to return a <strong>borrowed
reference</strong>. So, if the user will hold a reference to a value returned
as a borrowed reference, he must call <a class="reference internal" href="#c.json_incref" title="json_incref"><code class="xref c c-func docutils literal"><span class="pre">json_incref()</span></code></a>. As soon as
the value is no longer needed, <a class="reference internal" href="#c.json_decref" title="json_decref"><code class="xref c c-func docutils literal"><span class="pre">json_decref()</span></code></a> should be called
to release the reference.</p>
<p>Normally, all functions accepting a JSON value as an argument will
manage the reference, i.e. increase and decrease the reference count
as needed. However, some functions <strong>steal</strong> the reference, i.e. they
have the same result as if the user called <a class="reference internal" href="#c.json_decref" title="json_decref"><code class="xref c c-func docutils literal"><span class="pre">json_decref()</span></code></a> on
the argument right after calling the function. These functions are
suffixed with <code class="docutils literal"><span class="pre">_new</span></code> or have <code class="docutils literal"><span class="pre">_new_</span></code> somewhere in their name.</p>
<p>For example, the following code creates a new JSON array and appends
an integer to it:</p>
<div class="highlight-c"><div class="highlight"><pre><span></span><span class="n">json_t</span> <span class="o">*</span><span class="n">array</span><span class="p">,</span> <span class="o">*</span><span class="n">integer</span><span class="p">;</span>
<span class="n">array</span> <span class="o">=</span> <span class="n">json_array</span><span class="p">();</span>
<span class="n">integer</span> <span class="o">=</span> <span class="n">json_integer</span><span class="p">(</span><span class="mi">42</span><span class="p">);</span>
<span class="n">json_array_append</span><span class="p">(</span><span class="n">array</span><span class="p">,</span> <span class="n">integer</span><span class="p">);</span>
<span class="n">json_decref</span><span class="p">(</span><span class="n">integer</span><span class="p">);</span>
</pre></div>
</div>
<p>Note how the caller has to release the reference to the integer value
by calling <a class="reference internal" href="#c.json_decref" title="json_decref"><code class="xref c c-func docutils literal"><span class="pre">json_decref()</span></code></a>. By using a reference stealing
function <a class="reference internal" href="#c.json_array_append_new" title="json_array_append_new"><code class="xref c c-func docutils literal"><span class="pre">json_array_append_new()</span></code></a> instead of
<a class="reference internal" href="#c.json_array_append" title="json_array_append"><code class="xref c c-func docutils literal"><span class="pre">json_array_append()</span></code></a>, the code becomes much simpler:</p>
<div class="highlight-c"><div class="highlight"><pre><span></span><span class="n">json_t</span> <span class="o">*</span><span class="n">array</span> <span class="o">=</span> <span class="n">json_array</span><span class="p">();</span>
<span class="n">json_array_append_new</span><span class="p">(</span><span class="n">array</span><span class="p">,</span> <span class="n">json_integer</span><span class="p">(</span><span class="mi">42</span><span class="p">));</span>
</pre></div>
</div>
<p>In this case, the user doesn’t have to explicitly release the
reference to the integer value, as <a class="reference internal" href="#c.json_array_append_new" title="json_array_append_new"><code class="xref c c-func docutils literal"><span class="pre">json_array_append_new()</span></code></a>
steals the reference when appending the value to the array.</p>
<p>In the following sections it is clearly documented whether a function
will return a new or borrowed reference or steal a reference to its
argument.</p>
</div>
<div class="section" id="circular-references">
<h3>Circular References<a class="headerlink" href="#circular-references" title="Permalink to this headline">¶</a></h3>
<p>A circular reference is created when an object or an array is,
directly or indirectly, inserted inside itself. The direct case is
simple:</p>
<div class="highlight-c"><div class="highlight"><pre><span></span><span class="n">json_t</span> <span class="o">*</span><span class="n">obj</span> <span class="o">=</span> <span class="n">json_object</span><span class="p">();</span>
<span class="n">json_object_set</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="s">"foo"</span><span class="p">,</span> <span class="n">obj</span><span class="p">);</span>
</pre></div>
</div>
<p>Jansson will refuse to do this, and <a class="reference internal" href="#c.json_object_set" title="json_object_set"><code class="xref c c-func docutils literal"><span class="pre">json_object_set()</span></code></a> (and
all the other such functions for objects and arrays) will return with
an error status. The indirect case is the dangerous one:</p>
<div class="highlight-c"><div class="highlight"><pre><span></span><span class="n">json_t</span> <span class="o">*</span><span class="n">arr1</span> <span class="o">=</span> <span class="n">json_array</span><span class="p">(),</span> <span class="o">*</span><span class="n">arr2</span> <span class="o">=</span> <span class="n">json_array</span><span class="p">();</span>
<span class="n">json_array_append</span><span class="p">(</span><span class="n">arr1</span><span class="p">,</span> <span class="n">arr2</span><span class="p">);</span>
<span class="n">json_array_append</span><span class="p">(</span><span class="n">arr2</span><span class="p">,</span> <span class="n">arr1</span><span class="p">);</span>
</pre></div>
</div>
<p>In this example, the array <code class="docutils literal"><span class="pre">arr2</span></code> is contained in the array
<code class="docutils literal"><span class="pre">arr1</span></code>, and vice versa. Jansson cannot check for this kind of
indirect circular references without a performance hit, so it’s up to
the user to avoid them.</p>
<p>If a circular reference is created, the memory consumed by the values
cannot be freed by <a class="reference internal" href="#c.json_decref" title="json_decref"><code class="xref c c-func docutils literal"><span class="pre">json_decref()</span></code></a>. The reference counts never
drops to zero because the values are keeping the references to each
other. Moreover, trying to encode the values with any of the encoding
functions will fail. The encoder detects circular references and
returns an error status.</p>
</div>
<div class="section" id="scope-dereferencing">
<h3>Scope Dereferencing<a class="headerlink" href="#scope-dereferencing" title="Permalink to this headline">¶</a></h3>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.9.</span></p>
</div>
<p>It is possible to use the <code class="docutils literal"><span class="pre">json_auto_t</span></code> type to automatically
dereference a value at the end of a scope. For example:</p>
<div class="highlight-c"><div class="highlight"><pre><span></span><span class="kt">void</span> <span class="nf">function</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="p">{</span>
<span class="n">json_auto_t</span> <span class="o">*</span><span class="n">value</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
<span class="n">value</span> <span class="o">=</span> <span class="n">json_string</span><span class="p">(</span><span class="s">"foo"</span><span class="p">);</span>
<span class="cm">/* json_decref(value) is automatically called. */</span>
<span class="p">}</span>
</pre></div>
</div>
<p>This feature is only available on GCC and Clang. So if your project
has a portability requirement for other compilers, you should avoid
this feature.</p>
<p>Additionally, as always, care should be taken when passing values to
functions that steal references.</p>
</div>
</div>
<div class="section" id="true-false-and-null">
<h2>True, False and Null<a class="headerlink" href="#true-false-and-null" title="Permalink to this headline">¶</a></h2>
<p>These three values are implemented as singletons, so the returned
pointers won’t change between invocations of these functions.</p>
<dl class="function">
<dt id="c.json_true">
<a class="reference internal" href="#c.json_t" title="json_t">json_t</a> *<code class="descname">json_true</code><span class="sig-paren">(</span>void<span class="sig-paren">)</span><a class="headerlink" href="#c.json_true" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Returns the JSON true value.</p>
</dd></dl>
<dl class="function">
<dt id="c.json_false">
<a class="reference internal" href="#c.json_t" title="json_t">json_t</a> *<code class="descname">json_false</code><span class="sig-paren">(</span>void<span class="sig-paren">)</span><a class="headerlink" href="#c.json_false" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Returns the JSON false value.</p>
</dd></dl>
<dl class="function">
<dt id="c.json_boolean">
<a class="reference internal" href="#c.json_t" title="json_t">json_t</a> *<code class="descname">json_boolean</code><span class="sig-paren">(</span>val<span class="sig-paren">)</span><a class="headerlink" href="#c.json_boolean" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Returns JSON false if <code class="docutils literal"><span class="pre">val</span></code> is zero, and JSON true otherwise.
This is a macro, and equivalent to <code class="docutils literal"><span class="pre">val</span> <span class="pre">?</span> <span class="pre">json_true()</span> <span class="pre">:</span>
<span class="pre">json_false()</span></code>.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.4.</span></p>
</div>
</dd></dl>
<dl class="function">
<dt id="c.json_null">
<a class="reference internal" href="#c.json_t" title="json_t">json_t</a> *<code class="descname">json_null</code><span class="sig-paren">(</span>void<span class="sig-paren">)</span><a class="headerlink" href="#c.json_null" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Returns the JSON null value.</p>
</dd></dl>
</div>
<div class="section" id="string">
<h2>String<a class="headerlink" href="#string" title="Permalink to this headline">¶</a></h2>
<p>Jansson uses UTF-8 as the character encoding. All JSON strings must be
valid UTF-8 (or ASCII, as it’s a subset of UTF-8). All Unicode
codepoints U+0000 through U+10FFFF are allowed, but you must use
length-aware functions if you wish to embed null bytes in strings.</p>
<dl class="function">
<dt id="c.json_string">
<a class="reference internal" href="#c.json_t" title="json_t">json_t</a> *<code class="descname">json_string</code><span class="sig-paren">(</span>const char<em> *value</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_string" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Returns a new JSON string, or <em>NULL</em> on error. <em>value</em> must be a
valid null terminated UTF-8 encoded Unicode string.</p>
</dd></dl>
<dl class="function">
<dt id="c.json_stringn">
<a class="reference internal" href="#c.json_t" title="json_t">json_t</a> *<code class="descname">json_stringn</code><span class="sig-paren">(</span>const char<em> *value</em>, size_t<em> len</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_stringn" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Like <a class="reference internal" href="#c.json_string" title="json_string"><code class="xref c c-func docutils literal"><span class="pre">json_string()</span></code></a>, but with explicit length, so <em>value</em> may
contain null characters or not be null terminated.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.7.</span></p>
</div>
</dd></dl>
<dl class="function">
<dt id="c.json_string_nocheck">
<a class="reference internal" href="#c.json_t" title="json_t">json_t</a> *<code class="descname">json_string_nocheck</code><span class="sig-paren">(</span>const char<em> *value</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_string_nocheck" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Like <a class="reference internal" href="#c.json_string" title="json_string"><code class="xref c c-func docutils literal"><span class="pre">json_string()</span></code></a>, but doesn’t check that <em>value</em> is valid
UTF-8. Use this function only if you are certain that this really
is the case (e.g. you have already checked it by other means).</p>
</dd></dl>
<dl class="function">
<dt id="c.json_stringn_nocheck">
<a class="reference internal" href="#c.json_t" title="json_t">json_t</a> *<code class="descname">json_stringn_nocheck</code><span class="sig-paren">(</span>const char<em> *value</em>, size_t<em> len</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_stringn_nocheck" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Like <a class="reference internal" href="#c.json_string_nocheck" title="json_string_nocheck"><code class="xref c c-func docutils literal"><span class="pre">json_string_nocheck()</span></code></a>, but with explicit length, so
<em>value</em> may contain null characters or not be null terminated.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.7.</span></p>
</div>
</dd></dl>
<dl class="function">
<dt id="c.json_string_value">
const char *<code class="descname">json_string_value</code><span class="sig-paren">(</span>const <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *string</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_string_value" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the associated value of <em>string</em> as a null terminated UTF-8
encoded string, or <em>NULL</em> if <em>string</em> is not a JSON string.</p>
<p>The returned value is read-only and must not be modified or freed by
the user. It is valid as long as <em>string</em> exists, i.e. as long as
its reference count has not dropped to zero.</p>
</dd></dl>
<dl class="function">
<dt id="c.json_string_length">
size_t <code class="descname">json_string_length</code><span class="sig-paren">(</span>const <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *string</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_string_length" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the length of <em>string</em> in its UTF-8 presentation, or zero
if <em>string</em> is not a JSON string.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.7.</span></p>
</div>
</dd></dl>
<dl class="function">
<dt id="c.json_string_set">
int <code class="descname">json_string_set</code><span class="sig-paren">(</span><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *string</em>, const char<em> *value</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_string_set" title="Permalink to this definition">¶</a></dt>
<dd><p>Sets the associated value of <em>string</em> to <em>value</em>. <em>value</em> must be a
valid UTF-8 encoded Unicode string. Returns 0 on success and -1 on
error.</p>
</dd></dl>
<dl class="function">
<dt id="c.json_string_setn">
int <code class="descname">json_string_setn</code><span class="sig-paren">(</span><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *string</em>, const char<em> *value</em>, size_t<em> len</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_string_setn" title="Permalink to this definition">¶</a></dt>
<dd><p>Like <a class="reference internal" href="#c.json_string_set" title="json_string_set"><code class="xref c c-func docutils literal"><span class="pre">json_string_set()</span></code></a>, but with explicit length, so <em>value</em>
may contain null characters or not be null terminated.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.7.</span></p>
</div>
</dd></dl>
<dl class="function">
<dt id="c.json_string_set_nocheck">
int <code class="descname">json_string_set_nocheck</code><span class="sig-paren">(</span><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *string</em>, const char<em> *value</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_string_set_nocheck" title="Permalink to this definition">¶</a></dt>
<dd><p>Like <a class="reference internal" href="#c.json_string_set" title="json_string_set"><code class="xref c c-func docutils literal"><span class="pre">json_string_set()</span></code></a>, but doesn’t check that <em>value</em> is
valid UTF-8. Use this function only if you are certain that this
really is the case (e.g. you have already checked it by other
means).</p>
</dd></dl>
<dl class="function">
<dt id="c.json_string_setn_nocheck">
int <code class="descname">json_string_setn_nocheck</code><span class="sig-paren">(</span><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *string</em>, const char<em> *value</em>, size_t<em> len</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_string_setn_nocheck" title="Permalink to this definition">¶</a></dt>
<dd><p>Like <a class="reference internal" href="#c.json_string_set_nocheck" title="json_string_set_nocheck"><code class="xref c c-func docutils literal"><span class="pre">json_string_set_nocheck()</span></code></a>, but with explicit length,
so <em>value</em> may contain null characters or not be null terminated.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.7.</span></p>
</div>
</dd></dl>
<dl class="function">
<dt id="c.json_sprintf">
<a class="reference internal" href="#c.json_t" title="json_t">json_t</a> *<code class="descname">json_sprintf</code><span class="sig-paren">(</span>const char<em> *format</em>, ...<span class="sig-paren">)</span><a class="headerlink" href="#c.json_sprintf" title="Permalink to this definition">¶</a></dt>
<dt id="c.json_vsprintf">
<a class="reference internal" href="#c.json_t" title="json_t">json_t</a> *<code class="descname">json_vsprintf</code><span class="sig-paren">(</span>const char<em> *format</em>, va_list<em> ap</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_vsprintf" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Construct a JSON string from a format string and varargs, just like
<code class="xref c c-func docutils literal"><span class="pre">printf()</span></code>.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.11.</span></p>
</div>
</dd></dl>
</div>
<div class="section" id="number">
<h2>Number<a class="headerlink" href="#number" title="Permalink to this headline">¶</a></h2>
<p>The JSON specification only contains one numeric type, “number”. The C
programming language has distinct types for integer and floating-point
numbers, so for practical reasons Jansson also has distinct types for
the two. They are called “integer” and “real”, respectively. For more
information, see <a class="reference internal" href="conformance.html#rfc-conformance"><span class="std std-ref">RFC Conformance</span></a>.</p>
<dl class="type">
<dt id="c.json_int_t">
<code class="descname">json_int_t</code><a class="headerlink" href="#c.json_int_t" title="Permalink to this definition">¶</a></dt>
<dd><p>This is the C type that is used to store JSON integer values. It
represents the widest integer type available on your system. In
practice it’s just a typedef of <code class="docutils literal"><span class="pre">long</span> <span class="pre">long</span></code> if your compiler
supports it, otherwise <code class="docutils literal"><span class="pre">long</span></code>.</p>
<p>Usually, you can safely use plain <code class="docutils literal"><span class="pre">int</span></code> in place of
<code class="docutils literal"><span class="pre">json_int_t</span></code>, and the implicit C integer conversion handles the
rest. Only when you know that you need the full 64-bit range, you
should use <code class="docutils literal"><span class="pre">json_int_t</span></code> explicitly.</p>
</dd></dl>
<dl class="docutils">
<dt><code class="docutils literal"><span class="pre">JSON_INTEGER_IS_LONG_LONG</span></code></dt>
<dd><p class="first">This is a preprocessor variable that holds the value 1 if
<a class="reference internal" href="#c.json_int_t" title="json_int_t"><code class="xref c c-type docutils literal"><span class="pre">json_int_t</span></code></a> is <code class="docutils literal"><span class="pre">long</span> <span class="pre">long</span></code>, and 0 if it’s <code class="docutils literal"><span class="pre">long</span></code>. It
can be used as follows:</p>
<div class="last highlight-c"><div class="highlight"><pre><span></span><span class="cp">#if JSON_INTEGER_IS_LONG_LONG</span>
<span class="cm">/* Code specific for long long */</span>
<span class="cp">#else</span>
<span class="cm">/* Code specific for long */</span>
<span class="cp">#endif</span>
</pre></div>
</div>
</dd>
<dt><code class="docutils literal"><span class="pre">JSON_INTEGER_FORMAT</span></code></dt>
<dd><p class="first">This is a macro that expands to a <code class="xref c c-func docutils literal"><span class="pre">printf()</span></code> conversion
specifier that corresponds to <a class="reference internal" href="#c.json_int_t" title="json_int_t"><code class="xref c c-type docutils literal"><span class="pre">json_int_t</span></code></a>, without the
leading <code class="docutils literal"><span class="pre">%</span></code> sign, i.e. either <code class="docutils literal"><span class="pre">"lld"</span></code> or <code class="docutils literal"><span class="pre">"ld"</span></code>. This macro
is required because the actual type of <a class="reference internal" href="#c.json_int_t" title="json_int_t"><code class="xref c c-type docutils literal"><span class="pre">json_int_t</span></code></a> can be
either <code class="docutils literal"><span class="pre">long</span></code> or <code class="docutils literal"><span class="pre">long</span> <span class="pre">long</span></code>, and <code class="xref c c-func docutils literal"><span class="pre">printf()</span></code> requires
different length modifiers for the two.</p>
<p>Example:</p>
<div class="last highlight-c"><div class="highlight"><pre><span></span><span class="n">json_int_t</span> <span class="n">x</span> <span class="o">=</span> <span class="mi">123123123</span><span class="p">;</span>
<span class="n">printf</span><span class="p">(</span><span class="s">"x is %"</span> <span class="n">JSON_INTEGER_FORMAT</span> <span class="s">"</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">x</span><span class="p">);</span>
</pre></div>
</div>
</dd>
</dl>
<dl class="function">
<dt id="c.json_integer">
<a class="reference internal" href="#c.json_t" title="json_t">json_t</a> *<code class="descname">json_integer</code><span class="sig-paren">(</span><a class="reference internal" href="#c.json_int_t" title="json_int_t">json_int_t</a><em> value</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_integer" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Returns a new JSON integer, or <em>NULL</em> on error.</p>
</dd></dl>
<dl class="function">
<dt id="c.json_integer_value">
<a class="reference internal" href="#c.json_int_t" title="json_int_t">json_int_t</a> <code class="descname">json_integer_value</code><span class="sig-paren">(</span>const <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *integer</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_integer_value" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the associated value of <em>integer</em>, or 0 if <em>json</em> is not a
JSON integer.</p>
</dd></dl>
<dl class="function">
<dt id="c.json_integer_set">
int <code class="descname">json_integer_set</code><span class="sig-paren">(</span>const <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *integer</em>, <a class="reference internal" href="#c.json_int_t" title="json_int_t">json_int_t</a><em> value</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_integer_set" title="Permalink to this definition">¶</a></dt>
<dd><p>Sets the associated value of <em>integer</em> to <em>value</em>. Returns 0 on
success and -1 if <em>integer</em> is not a JSON integer.</p>
</dd></dl>
<dl class="function">
<dt id="c.json_real">
<a class="reference internal" href="#c.json_t" title="json_t">json_t</a> *<code class="descname">json_real</code><span class="sig-paren">(</span>double<em> value</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_real" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Returns a new JSON real, or <em>NULL</em> on error.</p>
</dd></dl>
<dl class="function">
<dt id="c.json_real_value">
double <code class="descname">json_real_value</code><span class="sig-paren">(</span>const <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *real</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_real_value" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the associated value of <em>real</em>, or 0.0 if <em>real</em> is not a
JSON real.</p>
</dd></dl>
<dl class="function">
<dt id="c.json_real_set">
int <code class="descname">json_real_set</code><span class="sig-paren">(</span>const <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *real</em>, double<em> value</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_real_set" title="Permalink to this definition">¶</a></dt>
<dd><p>Sets the associated value of <em>real</em> to <em>value</em>. Returns 0 on
success and -1 if <em>real</em> is not a JSON real.</p>
</dd></dl>
<dl class="function">
<dt id="c.json_number_value">
double <code class="descname">json_number_value</code><span class="sig-paren">(</span>const <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *json</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_number_value" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the associated value of the JSON integer or JSON real
<em>json</em>, cast to double regardless of the actual type. If <em>json</em> is
neither JSON real nor JSON integer, 0.0 is returned.</p>
</dd></dl>
</div>
<div class="section" id="array">
<h2>Array<a class="headerlink" href="#array" title="Permalink to this headline">¶</a></h2>
<p>A JSON array is an ordered collection of other JSON values.</p>
<dl class="function">
<dt id="c.json_array">
<a class="reference internal" href="#c.json_t" title="json_t">json_t</a> *<code class="descname">json_array</code><span class="sig-paren">(</span>void<span class="sig-paren">)</span><a class="headerlink" href="#c.json_array" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Returns a new JSON array, or <em>NULL</em> on error. Initially, the array
is empty.</p>
</dd></dl>
<dl class="function">
<dt id="c.json_array_size">
size_t <code class="descname">json_array_size</code><span class="sig-paren">(</span>const <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *array</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_array_size" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the number of elements in <em>array</em>, or 0 if <em>array</em> is NULL
or not a JSON array.</p>
</dd></dl>
<dl class="function">
<dt id="c.json_array_get">
<a class="reference internal" href="#c.json_t" title="json_t">json_t</a> *<code class="descname">json_array_get</code><span class="sig-paren">(</span>const <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *array</em>, size_t<em> index</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_array_get" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: Borrowed reference.</em><p>Returns the element in <em>array</em> at position <em>index</em>. The valid range
for <em>index</em> is from 0 to the return value of
<a class="reference internal" href="#c.json_array_size" title="json_array_size"><code class="xref c c-func docutils literal"><span class="pre">json_array_size()</span></code></a> minus 1. If <em>array</em> is not a JSON array,
if <em>array</em> is <em>NULL</em>, or if <em>index</em> is out of range, <em>NULL</em> is
returned.</p>
</dd></dl>
<dl class="function">
<dt id="c.json_array_set">
int <code class="descname">json_array_set</code><span class="sig-paren">(</span><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *array</em>, size_t<em> index</em>, <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *value</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_array_set" title="Permalink to this definition">¶</a></dt>
<dd><p>Replaces the element in <em>array</em> at position <em>index</em> with <em>value</em>.
The valid range for <em>index</em> is from 0 to the return value of
<a class="reference internal" href="#c.json_array_size" title="json_array_size"><code class="xref c c-func docutils literal"><span class="pre">json_array_size()</span></code></a> minus 1. Returns 0 on success and -1 on
error.</p>
</dd></dl>
<dl class="function">
<dt id="c.json_array_set_new">
int <code class="descname">json_array_set_new</code><span class="sig-paren">(</span><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *array</em>, size_t<em> index</em>, <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *value</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_array_set_new" title="Permalink to this definition">¶</a></dt>
<dd><p>Like <a class="reference internal" href="#c.json_array_set" title="json_array_set"><code class="xref c c-func docutils literal"><span class="pre">json_array_set()</span></code></a> but steals the reference to <em>value</em>.
This is useful when <em>value</em> is newly created and not used after
the call.</p>
</dd></dl>
<dl class="function">
<dt id="c.json_array_append">
int <code class="descname">json_array_append</code><span class="sig-paren">(</span><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *array</em>, <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *value</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_array_append" title="Permalink to this definition">¶</a></dt>
<dd><p>Appends <em>value</em> to the end of <em>array</em>, growing the size of <em>array</em>
by 1. Returns 0 on success and -1 on error.</p>
</dd></dl>
<dl class="function">
<dt id="c.json_array_append_new">
int <code class="descname">json_array_append_new</code><span class="sig-paren">(</span><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *array</em>, <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *value</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_array_append_new" title="Permalink to this definition">¶</a></dt>
<dd><p>Like <a class="reference internal" href="#c.json_array_append" title="json_array_append"><code class="xref c c-func docutils literal"><span class="pre">json_array_append()</span></code></a> but steals the reference to
<em>value</em>. This is useful when <em>value</em> is newly created and not used
after the call.</p>
</dd></dl>
<dl class="function">
<dt id="c.json_array_insert">
int <code class="descname">json_array_insert</code><span class="sig-paren">(</span><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *array</em>, size_t<em> index</em>, <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *value</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_array_insert" title="Permalink to this definition">¶</a></dt>
<dd><p>Inserts <em>value</em> to <em>array</em> at position <em>index</em>, shifting the
elements at <em>index</em> and after it one position towards the end of
the array. Returns 0 on success and -1 on error.</p>
</dd></dl>
<dl class="function">
<dt id="c.json_array_insert_new">
int <code class="descname">json_array_insert_new</code><span class="sig-paren">(</span><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *array</em>, size_t<em> index</em>, <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *value</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_array_insert_new" title="Permalink to this definition">¶</a></dt>
<dd><p>Like <a class="reference internal" href="#c.json_array_insert" title="json_array_insert"><code class="xref c c-func docutils literal"><span class="pre">json_array_insert()</span></code></a> but steals the reference to
<em>value</em>. This is useful when <em>value</em> is newly created and not used
after the call.</p>
</dd></dl>
<dl class="function">
<dt id="c.json_array_remove">
int <code class="descname">json_array_remove</code><span class="sig-paren">(</span><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *array</em>, size_t<em> index</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_array_remove" title="Permalink to this definition">¶</a></dt>
<dd><p>Removes the element in <em>array</em> at position <em>index</em>, shifting the
elements after <em>index</em> one position towards the start of the array.
Returns 0 on success and -1 on error. The reference count of the
removed value is decremented.</p>
</dd></dl>
<dl class="function">
<dt id="c.json_array_clear">
int <code class="descname">json_array_clear</code><span class="sig-paren">(</span><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *array</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_array_clear" title="Permalink to this definition">¶</a></dt>
<dd><p>Removes all elements from <em>array</em>. Returns 0 on success and -1 on
error. The reference count of all removed values are decremented.</p>
</dd></dl>
<dl class="function">
<dt id="c.json_array_extend">
int <code class="descname">json_array_extend</code><span class="sig-paren">(</span><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *array</em>, <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *other_array</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_array_extend" title="Permalink to this definition">¶</a></dt>
<dd><p>Appends all elements in <em>other_array</em> to the end of <em>array</em>.
Returns 0 on success and -1 on error.</p>
</dd></dl>
<dl class="function">
<dt id="c.json_array_foreach">
<code class="descname">json_array_foreach</code><span class="sig-paren">(</span>array, index, value<span class="sig-paren">)</span><a class="headerlink" href="#c.json_array_foreach" title="Permalink to this definition">¶</a></dt>
<dd><p>Iterate over every element of <code class="docutils literal"><span class="pre">array</span></code>, running the block
of code that follows each time with the proper values set to
variables <code class="docutils literal"><span class="pre">index</span></code> and <code class="docutils literal"><span class="pre">value</span></code>, of types <code class="xref c c-type docutils literal"><span class="pre">size_t</span></code> and
<a class="reference internal" href="#c.json_t" title="json_t"><code class="xref c c-type docutils literal"><span class="pre">json_t</span> <span class="pre">*</span></code></a> respectively. Example:</p>
<div class="highlight-c"><div class="highlight"><pre><span></span><span class="cm">/* array is a JSON array */</span>
<span class="kt">size_t</span> <span class="n">index</span><span class="p">;</span>
<span class="n">json_t</span> <span class="o">*</span><span class="n">value</span><span class="p">;</span>
<span class="n">json_array_foreach</span><span class="p">(</span><span class="n">array</span><span class="p">,</span> <span class="n">index</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span> <span class="p">{</span>
<span class="cm">/* block of code that uses index and value */</span>
<span class="p">}</span>
</pre></div>
</div>
<p>The items are returned in increasing index order.</p>
<p>This macro expands to an ordinary <code class="docutils literal"><span class="pre">for</span></code> statement upon
preprocessing, so its performance is equivalent to that of
hand-written code using the array access functions.
The main advantage of this macro is that it abstracts
away the complexity, and makes for more concise and readable code.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.5.</span></p>
</div>
</dd></dl>
</div>
<div class="section" id="object">
<h2>Object<a class="headerlink" href="#object" title="Permalink to this headline">¶</a></h2>
<p>A JSON object is a dictionary of key-value pairs, where the key is a
Unicode string and the value is any JSON value.</p>
<p>Even though null bytes are allowed in string values, they are not
allowed in object keys.</p>
<dl class="function">
<dt id="c.json_object">
<a class="reference internal" href="#c.json_t" title="json_t">json_t</a> *<code class="descname">json_object</code><span class="sig-paren">(</span>void<span class="sig-paren">)</span><a class="headerlink" href="#c.json_object" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Returns a new JSON object, or <em>NULL</em> on error. Initially, the
object is empty.</p>
</dd></dl>
<dl class="function">
<dt id="c.json_object_size">
size_t <code class="descname">json_object_size</code><span class="sig-paren">(</span>const <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *object</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_object_size" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the number of elements in <em>object</em>, or 0 if <em>object</em> is not
a JSON object.</p>
</dd></dl>
<dl class="function">
<dt id="c.json_object_get">
<a class="reference internal" href="#c.json_t" title="json_t">json_t</a> *<code class="descname">json_object_get</code><span class="sig-paren">(</span>const <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *object</em>, const char<em> *key</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_object_get" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: Borrowed reference.</em><p>Get a value corresponding to <em>key</em> from <em>object</em>. Returns <em>NULL</em> if
<em>key</em> is not found and on error.</p>
</dd></dl>
<dl class="function">
<dt id="c.json_object_set">
int <code class="descname">json_object_set</code><span class="sig-paren">(</span><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *object</em>, const char<em> *key</em>, <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *value</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_object_set" title="Permalink to this definition">¶</a></dt>
<dd><p>Set the value of <em>key</em> to <em>value</em> in <em>object</em>. <em>key</em> must be a
valid null terminated UTF-8 encoded Unicode string. If there
already is a value for <em>key</em>, it is replaced by the new value.
Returns 0 on success and -1 on error.</p>
</dd></dl>
<dl class="function">
<dt id="c.json_object_set_nocheck">
int <code class="descname">json_object_set_nocheck</code><span class="sig-paren">(</span><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *object</em>, const char<em> *key</em>, <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *value</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_object_set_nocheck" title="Permalink to this definition">¶</a></dt>
<dd><p>Like <a class="reference internal" href="#c.json_object_set" title="json_object_set"><code class="xref c c-func docutils literal"><span class="pre">json_object_set()</span></code></a>, but doesn’t check that <em>key</em> is
valid UTF-8. Use this function only if you are certain that this
really is the case (e.g. you have already checked it by other
means).</p>
</dd></dl>
<dl class="function">
<dt id="c.json_object_set_new">
int <code class="descname">json_object_set_new</code><span class="sig-paren">(</span><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *object</em>, const char<em> *key</em>, <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *value</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_object_set_new" title="Permalink to this definition">¶</a></dt>
<dd><p>Like <a class="reference internal" href="#c.json_object_set" title="json_object_set"><code class="xref c c-func docutils literal"><span class="pre">json_object_set()</span></code></a> but steals the reference to
<em>value</em>. This is useful when <em>value</em> is newly created and not used
after the call.</p>
</dd></dl>
<dl class="function">
<dt id="c.json_object_set_new_nocheck">
int <code class="descname">json_object_set_new_nocheck</code><span class="sig-paren">(</span><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *object</em>, const char<em> *key</em>, <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *value</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_object_set_new_nocheck" title="Permalink to this definition">¶</a></dt>
<dd><p>Like <a class="reference internal" href="#c.json_object_set_new" title="json_object_set_new"><code class="xref c c-func docutils literal"><span class="pre">json_object_set_new()</span></code></a>, but doesn’t check that <em>key</em> is
valid UTF-8. Use this function only if you are certain that this
really is the case (e.g. you have already checked it by other
means).</p>
</dd></dl>
<dl class="function">
<dt id="c.json_object_del">
int <code class="descname">json_object_del</code><span class="sig-paren">(</span><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *object</em>, const char<em> *key</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_object_del" title="Permalink to this definition">¶</a></dt>
<dd><p>Delete <em>key</em> from <em>object</em> if it exists. Returns 0 on success, or
-1 if <em>key</em> was not found. The reference count of the removed value
is decremented.</p>
</dd></dl>
<dl class="function">
<dt id="c.json_object_clear">
int <code class="descname">json_object_clear</code><span class="sig-paren">(</span><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *object</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_object_clear" title="Permalink to this definition">¶</a></dt>
<dd><p>Remove all elements from <em>object</em>. Returns 0 on success and -1 if
<em>object</em> is not a JSON object. The reference count of all removed
values are decremented.</p>
</dd></dl>
<dl class="function">
<dt id="c.json_object_update">
int <code class="descname">json_object_update</code><span class="sig-paren">(</span><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *object</em>, <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *other</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_object_update" title="Permalink to this definition">¶</a></dt>
<dd><p>Update <em>object</em> with the key-value pairs from <em>other</em>, overwriting
existing keys. Returns 0 on success or -1 on error.</p>
</dd></dl>
<dl class="function">
<dt id="c.json_object_update_existing">
int <code class="descname">json_object_update_existing</code><span class="sig-paren">(</span><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *object</em>, <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *other</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_object_update_existing" title="Permalink to this definition">¶</a></dt>
<dd><p>Like <a class="reference internal" href="#c.json_object_update" title="json_object_update"><code class="xref c c-func docutils literal"><span class="pre">json_object_update()</span></code></a>, but only the values of existing
keys are updated. No new keys are created. Returns 0 on success or
-1 on error.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.3.</span></p>
</div>
</dd></dl>
<dl class="function">
<dt id="c.json_object_update_missing">
int <code class="descname">json_object_update_missing</code><span class="sig-paren">(</span><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *object</em>, <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *other</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_object_update_missing" title="Permalink to this definition">¶</a></dt>
<dd><p>Like <a class="reference internal" href="#c.json_object_update" title="json_object_update"><code class="xref c c-func docutils literal"><span class="pre">json_object_update()</span></code></a>, but only new keys are created.
The value of any existing key is not changed. Returns 0 on success
or -1 on error.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.3.</span></p>
</div>
</dd></dl>
<dl class="function">
<dt id="c.json_object_foreach">
<code class="descname">json_object_foreach</code><span class="sig-paren">(</span>object, key, value<span class="sig-paren">)</span><a class="headerlink" href="#c.json_object_foreach" title="Permalink to this definition">¶</a></dt>
<dd><p>Iterate over every key-value pair of <code class="docutils literal"><span class="pre">object</span></code>, running the block
of code that follows each time with the proper values set to
variables <code class="docutils literal"><span class="pre">key</span></code> and <code class="docutils literal"><span class="pre">value</span></code>, of types <code class="xref c c-type docutils literal"><span class="pre">const</span> <span class="pre">char</span> <span class="pre">*</span></code> and
<a class="reference internal" href="#c.json_t" title="json_t"><code class="xref c c-type docutils literal"><span class="pre">json_t</span> <span class="pre">*</span></code></a> respectively. Example:</p>
<div class="highlight-c"><div class="highlight"><pre><span></span><span class="cm">/* obj is a JSON object */</span>
<span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">key</span><span class="p">;</span>
<span class="n">json_t</span> <span class="o">*</span><span class="n">value</span><span class="p">;</span>
<span class="n">json_object_foreach</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span> <span class="p">{</span>
<span class="cm">/* block of code that uses key and value */</span>
<span class="p">}</span>
</pre></div>
</div>
<p>The items are returned in the order they were inserted to the
object.</p>
<p><strong>Note:</strong> It’s not safe to call <code class="docutils literal"><span class="pre">json_object_del(object,</span> <span class="pre">key)</span></code>
during iteration. If you need to, use
<a class="reference internal" href="#c.json_object_foreach_safe" title="json_object_foreach_safe"><code class="xref c c-func docutils literal"><span class="pre">json_object_foreach_safe()</span></code></a> instead.</p>
<p>This macro expands to an ordinary <code class="docutils literal"><span class="pre">for</span></code> statement upon
preprocessing, so its performance is equivalent to that of
hand-written iteration code using the object iteration protocol
(see below). The main advantage of this macro is that it abstracts
away the complexity behind iteration, and makes for more concise and
readable code.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.3.</span></p>
</div>
</dd></dl>
<dl class="function">
<dt id="c.json_object_foreach_safe">
<code class="descname">json_object_foreach_safe</code><span class="sig-paren">(</span>object, tmp, key, value<span class="sig-paren">)</span><a class="headerlink" href="#c.json_object_foreach_safe" title="Permalink to this definition">¶</a></dt>
<dd><p>Like <a class="reference internal" href="#c.json_object_foreach" title="json_object_foreach"><code class="xref c c-func docutils literal"><span class="pre">json_object_foreach()</span></code></a>, but it’s safe to call
<code class="docutils literal"><span class="pre">json_object_del(object,</span> <span class="pre">key)</span></code> during iteration. You need to pass
an extra <code class="docutils literal"><span class="pre">void</span> <span class="pre">*</span></code> parameter <code class="docutils literal"><span class="pre">tmp</span></code> that is used for temporary storage.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.8.</span></p>
</div>
</dd></dl>
<p>The following functions can be used to iterate through all key-value
pairs in an object. The items are returned in the order they were
inserted to the object.</p>
<dl class="function">
<dt id="c.json_object_iter">
void *<code class="descname">json_object_iter</code><span class="sig-paren">(</span><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *object</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_object_iter" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns an opaque iterator which can be used to iterate over all
key-value pairs in <em>object</em>, or <em>NULL</em> if <em>object</em> is empty.</p>
</dd></dl>
<dl class="function">
<dt id="c.json_object_iter_at">
void *<code class="descname">json_object_iter_at</code><span class="sig-paren">(</span><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *object</em>, const char<em> *key</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_object_iter_at" title="Permalink to this definition">¶</a></dt>
<dd><p>Like <a class="reference internal" href="#c.json_object_iter" title="json_object_iter"><code class="xref c c-func docutils literal"><span class="pre">json_object_iter()</span></code></a>, but returns an iterator to the
key-value pair in <em>object</em> whose key is equal to <em>key</em>, or NULL if
<em>key</em> is not found in <em>object</em>. Iterating forward to the end of
<em>object</em> only yields all key-value pairs of the object if <em>key</em>
happens to be the first key in the underlying hash table.</p>
</dd></dl>
<dl class="function">
<dt id="c.json_object_iter_next">
void *<code class="descname">json_object_iter_next</code><span class="sig-paren">(</span><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *object</em>, void<em> *iter</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_object_iter_next" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns an iterator pointing to the next key-value pair in <em>object</em>
after <em>iter</em>, or <em>NULL</em> if the whole object has been iterated
through.</p>
</dd></dl>
<dl class="function">
<dt id="c.json_object_iter_key">
const char *<code class="descname">json_object_iter_key</code><span class="sig-paren">(</span>void<em> *iter</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_object_iter_key" title="Permalink to this definition">¶</a></dt>
<dd><p>Extract the associated key from <em>iter</em>.</p>
</dd></dl>
<dl class="function">
<dt id="c.json_object_iter_value">
<a class="reference internal" href="#c.json_t" title="json_t">json_t</a> *<code class="descname">json_object_iter_value</code><span class="sig-paren">(</span>void<em> *iter</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_object_iter_value" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: Borrowed reference.</em><p>Extract the associated value from <em>iter</em>.</p>
</dd></dl>
<dl class="function">
<dt id="c.json_object_iter_set">
int <code class="descname">json_object_iter_set</code><span class="sig-paren">(</span><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *object</em>, void<em> *iter</em>, <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *value</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_object_iter_set" title="Permalink to this definition">¶</a></dt>
<dd><p>Set the value of the key-value pair in <em>object</em>, that is pointed to
by <em>iter</em>, to <em>value</em>.</p>
</dd></dl>
<dl class="function">
<dt id="c.json_object_iter_set_new">
int <code class="descname">json_object_iter_set_new</code><span class="sig-paren">(</span><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *object</em>, void<em> *iter</em>, <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *value</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_object_iter_set_new" title="Permalink to this definition">¶</a></dt>
<dd><p>Like <a class="reference internal" href="#c.json_object_iter_set" title="json_object_iter_set"><code class="xref c c-func docutils literal"><span class="pre">json_object_iter_set()</span></code></a>, but steals the reference to
<em>value</em>. This is useful when <em>value</em> is newly created and not used
after the call.</p>
</dd></dl>
<dl class="function">
<dt id="c.json_object_key_to_iter">
void *<code class="descname">json_object_key_to_iter</code><span class="sig-paren">(</span>const char<em> *key</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_object_key_to_iter" title="Permalink to this definition">¶</a></dt>
<dd><p>Like <a class="reference internal" href="#c.json_object_iter_at" title="json_object_iter_at"><code class="xref c c-func docutils literal"><span class="pre">json_object_iter_at()</span></code></a>, but much faster. Only works for
values returned by <a class="reference internal" href="#c.json_object_iter_key" title="json_object_iter_key"><code class="xref c c-func docutils literal"><span class="pre">json_object_iter_key()</span></code></a>. Using other keys
will lead to segfaults. This function is used internally to
implement <a class="reference internal" href="#c.json_object_foreach" title="json_object_foreach"><code class="xref c c-func docutils literal"><span class="pre">json_object_foreach()</span></code></a>. Example:</p>
<div class="highlight-c"><div class="highlight"><pre><span></span><span class="cm">/* obj is a JSON object */</span>
<span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">key</span><span class="p">;</span>
<span class="n">json_t</span> <span class="o">*</span><span class="n">value</span><span class="p">;</span>
<span class="kt">void</span> <span class="o">*</span><span class="n">iter</span> <span class="o">=</span> <span class="n">json_object_iter</span><span class="p">(</span><span class="n">obj</span><span class="p">);</span>
<span class="k">while</span><span class="p">(</span><span class="n">iter</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">key</span> <span class="o">=</span> <span class="n">json_object_iter_key</span><span class="p">(</span><span class="n">iter</span><span class="p">);</span>
<span class="n">value</span> <span class="o">=</span> <span class="n">json_object_iter_value</span><span class="p">(</span><span class="n">iter</span><span class="p">);</span>
<span class="cm">/* use key and value ... */</span>
<span class="n">iter</span> <span class="o">=</span> <span class="n">json_object_iter_next</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">iter</span><span class="p">);</span>
<span class="p">}</span>
</pre></div>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.3.</span></p>
</div>
</dd></dl>
<dl class="function">
<dt id="c.json_object_seed">
void <code class="descname">json_object_seed</code><span class="sig-paren">(</span>size_t<em> seed</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_object_seed" title="Permalink to this definition">¶</a></dt>
<dd><p>Seed the hash function used in Jansson’s hashtable implementation.
The seed is used to randomize the hash function so that an
attacker cannot control its output.</p>
<p>If <em>seed</em> is 0, Jansson generates the seed itself by reading
random data from the operating system’s entropy sources. If no
entropy sources are available, falls back to using a combination
of the current timestamp (with microsecond precision if possible)
and the process ID.</p>
<p>If called at all, this function must be called before any calls to
<a class="reference internal" href="#c.json_object" title="json_object"><code class="xref c c-func docutils literal"><span class="pre">json_object()</span></code></a>, either explicit or implicit. If this
function is not called by the user, the first call to
<a class="reference internal" href="#c.json_object" title="json_object"><code class="xref c c-func docutils literal"><span class="pre">json_object()</span></code></a> (either explicit or implicit) seeds the hash
function. See <a class="reference internal" href="portability.html#portability-thread-safety"><span class="std std-ref">Thread safety</span></a> for notes on thread
safety.</p>
<p>If repeatable results are required, for e.g. unit tests, the hash
function can be “unrandomized” by calling <a class="reference internal" href="#c.json_object_seed" title="json_object_seed"><code class="xref c c-func docutils literal"><span class="pre">json_object_seed()</span></code></a>
with a constant value on program startup, e.g.
<code class="docutils literal"><span class="pre">json_object_seed(1)</span></code>.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.6.</span></p>
</div>
</dd></dl>
</div>
<div class="section" id="error-reporting">
<h2>Error reporting<a class="headerlink" href="#error-reporting" title="Permalink to this headline">¶</a></h2>
<p>Jansson uses a single struct type to pass error information to the
user. See sections <a class="reference internal" href="#apiref-decoding"><span class="std std-ref">Decoding</span></a>, <a class="reference internal" href="#apiref-pack"><span class="std std-ref">Building Values</span></a> and
<a class="reference internal" href="#apiref-unpack"><span class="std std-ref">Parsing and Validating Values</span></a> for functions that pass error information using
this struct.</p>
<dl class="type">
<dt id="c.json_error_t">
<code class="descname">json_error_t</code><a class="headerlink" href="#c.json_error_t" title="Permalink to this definition">¶</a></dt>
<dd><dl class="member">
<dt>
<code class="descname">char text[]</code></dt>
<dd><p>The error message (in UTF-8), or an empty string if a message is
not available.</p>
<p>The last byte of this array contains a numeric error code. Use
<a class="reference internal" href="#c.json_error_code" title="json_error_code"><code class="xref c c-func docutils literal"><span class="pre">json_error_code()</span></code></a> to extract this code.</p>
</dd></dl>
<dl class="member">
<dt>
<code class="descname">char source[]</code></dt>
<dd><p>Source of the error. This can be (a part of) the file name or a
special identifier in angle brackets (e.g. <code class="docutils literal"><span class="pre"><string></span></code>).</p>
</dd></dl>
<dl class="member">
<dt id="c.json_error_t.line">
int <code class="descname">line</code><a class="headerlink" href="#c.json_error_t.line" title="Permalink to this definition">¶</a></dt>
<dd><p>The line number on which the error occurred.</p>
</dd></dl>
<dl class="member">
<dt id="c.json_error_t.column">
int <code class="descname">column</code><a class="headerlink" href="#c.json_error_t.column" title="Permalink to this definition">¶</a></dt>
<dd><p>The column on which the error occurred. Note that this is the
<em>character column</em>, not the byte column, i.e. a multibyte UTF-8
character counts as one column.</p>
</dd></dl>
<dl class="member">
<dt id="c.json_error_t.position">
int <code class="descname">position</code><a class="headerlink" href="#c.json_error_t.position" title="Permalink to this definition">¶</a></dt>
<dd><p>The position in bytes from the start of the input. This is
useful for debugging Unicode encoding problems.</p>
</dd></dl>
</dd></dl>
<p>The normal use of <a class="reference internal" href="#c.json_error_t" title="json_error_t"><code class="xref c c-type docutils literal"><span class="pre">json_error_t</span></code></a> is to allocate it on the stack,
and pass a pointer to a function. Example:</p>
<div class="highlight-c"><div class="highlight"><pre><span></span><span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
<span class="n">json_t</span> <span class="o">*</span><span class="n">json</span><span class="p">;</span>
<span class="n">json_error_t</span> <span class="n">error</span><span class="p">;</span>
<span class="n">json</span> <span class="o">=</span> <span class="n">json_load_file</span><span class="p">(</span><span class="s">"/path/to/file.json"</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="o">&</span><span class="n">error</span><span class="p">);</span>
<span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">json</span><span class="p">)</span> <span class="p">{</span>
<span class="cm">/* the error variable contains error information */</span>
<span class="p">}</span>
<span class="p">...</span>
<span class="p">}</span>
</pre></div>
</div>
<p>Also note that if the call succeeded (<code class="docutils literal"><span class="pre">json</span> <span class="pre">!=</span> <span class="pre">NULL</span></code> in the above
example), the contents of <code class="docutils literal"><span class="pre">error</span></code> are generally left unspecified.
The decoding functions write to the <code class="docutils literal"><span class="pre">position</span></code> member also on
success. See <a class="reference internal" href="#apiref-decoding"><span class="std std-ref">Decoding</span></a> for more info.</p>
<p>All functions also accept <em>NULL</em> as the <a class="reference internal" href="#c.json_error_t" title="json_error_t"><code class="xref c c-type docutils literal"><span class="pre">json_error_t</span></code></a> pointer,
in which case no error information is returned to the caller.</p>
<dl class="type">
<dt id="c.json_error_code">
enum <code class="descname">json_error_code</code><a class="headerlink" href="#c.json_error_code" title="Permalink to this definition">¶</a></dt>
<dd><p>An enumeration containing numeric error codes. The following errors are
currently defined:</p>
<p><code class="docutils literal"><span class="pre">json_error_unknown</span></code></p>
<blockquote>
<div>Unknown error. This should only be returned for non-errorneous
<a class="reference internal" href="#c.json_error_t" title="json_error_t"><code class="xref c c-type docutils literal"><span class="pre">json_error_t</span></code></a> structures.</div></blockquote>
<p><code class="docutils literal"><span class="pre">json_error_out_of_memory</span></code></p>
<blockquote>
<div>The library couldn’t allocate any heap memory.</div></blockquote>
<p><code class="docutils literal"><span class="pre">json_error_stack_overflow</span></code></p>
<blockquote>
<div>Nesting too deep.</div></blockquote>
<p><code class="docutils literal"><span class="pre">json_error_cannot_open_file</span></code></p>
<blockquote>
<div>Couldn’t open input file.</div></blockquote>
<p><code class="docutils literal"><span class="pre">json_error_invalid_argument</span></code></p>
<blockquote>
<div>A function argument was invalid.</div></blockquote>
<p><code class="docutils literal"><span class="pre">json_error_invalid_utf8</span></code></p>
<blockquote>
<div>The input string isn’t valid UTF-8.</div></blockquote>
<p><code class="docutils literal"><span class="pre">json_error_premature_end_of_input</span></code></p>
<blockquote>
<div>The input ended in the middle of a JSON value.</div></blockquote>
<p><code class="docutils literal"><span class="pre">json_error_end_of_input_expected</span></code></p>
<blockquote>
<div>There was some text after the end of a JSON value. See the
<code class="docutils literal"><span class="pre">JSON_DISABLE_EOF_CHECK</span></code> flag.</div></blockquote>
<p><code class="docutils literal"><span class="pre">json_error_invalid_syntax</span></code></p>
<blockquote>
<div>JSON syntax error.</div></blockquote>
<p><code class="docutils literal"><span class="pre">json_error_invalid_format</span></code></p>
<blockquote>
<div>Invalid format string for packing or unpacking.</div></blockquote>
<p><code class="docutils literal"><span class="pre">json_error_wrong_type</span></code></p>
<blockquote>
<div>When packing or unpacking, the actual type of a value differed from the
one specified in the format string.</div></blockquote>
<p><code class="docutils literal"><span class="pre">json_error_null_character</span></code></p>
<blockquote>
<div>A null character was detected in a JSON string. See the
<code class="docutils literal"><span class="pre">JSON_ALLOW_NUL</span></code> flag.</div></blockquote>
<p><code class="docutils literal"><span class="pre">json_error_null_value</span></code></p>
<blockquote>
<div>When packing or unpacking, some key or value was <code class="docutils literal"><span class="pre">NULL</span></code>.</div></blockquote>
<p><code class="docutils literal"><span class="pre">json_error_null_byte_in_key</span></code></p>
<blockquote>
<div>An object key would contain a null byte. Jansson can’t represent such
keys; see <a class="reference internal" href="conformance.html#rfc-conformance"><span class="std std-ref">RFC Conformance</span></a>.</div></blockquote>
<p><code class="docutils literal"><span class="pre">json_error_duplicate_key</span></code></p>
<blockquote>
<div>Duplicate key in object. See the <code class="docutils literal"><span class="pre">JSON_REJECT_DUPLICATES</span></code> flag.</div></blockquote>
<p><code class="docutils literal"><span class="pre">json_error_numeric_overflow</span></code></p>
<blockquote>
<div>When converting a JSON number to a C numeric type, a numeric overflow
was detected.</div></blockquote>
<p><code class="docutils literal"><span class="pre">json_error_item_not_found</span></code></p>
<blockquote>
<div>Key in object not found.</div></blockquote>
<p><code class="docutils literal"><span class="pre">json_error_index_out_of_range</span></code></p>
<blockquote>
<div>Array index is out of range.</div></blockquote>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.11.</span></p>
</div>
</dd></dl>
<dl class="function">
<dt>
enum <a class="reference internal" href="#c.json_error_code" title="json_error_code">json_error_code</a> <code class="descname">json_error_code</code><span class="sig-paren">(</span>const <a class="reference internal" href="#c.json_error_t" title="json_error_t">json_error_t</a><em> *error</em><span class="sig-paren">)</span></dt>
<dd><p>Returns the error code embedded in <code class="docutils literal"><span class="pre">error->text</span></code>.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.11.</span></p>
</div>
</dd></dl>
</div>
<div class="section" id="encoding">
<h2>Encoding<a class="headerlink" href="#encoding" title="Permalink to this headline">¶</a></h2>
<p>This sections describes the functions that can be used to encode
values to JSON. By default, only objects and arrays can be encoded
directly, since they are the only valid <em>root</em> values of a JSON text.
To encode any JSON value, use the <code class="docutils literal"><span class="pre">JSON_ENCODE_ANY</span></code> flag (see
below).</p>
<p>By default, the output has no newlines, and spaces are used between
array and object elements for a readable output. This behavior can be
altered by using the <code class="docutils literal"><span class="pre">JSON_INDENT</span></code> and <code class="docutils literal"><span class="pre">JSON_COMPACT</span></code> flags
described below. A newline is never appended to the end of the encoded
JSON data.</p>
<p>Each function takes a <em>flags</em> parameter that controls some aspects of
how the data is encoded. Its default value is 0. The following macros
can be ORed together to obtain <em>flags</em>.</p>
<dl class="docutils">
<dt><code class="docutils literal"><span class="pre">JSON_INDENT(n)</span></code></dt>
<dd><p class="first">Pretty-print the result, using newlines between array and object
items, and indenting with <em>n</em> spaces. The valid range for <em>n</em> is
between 0 and 31 (inclusive), other values result in an undefined
output. If <code class="docutils literal"><span class="pre">JSON_INDENT</span></code> is not used or <em>n</em> is 0, no newlines are
inserted between array and object items.</p>
<p>The <code class="docutils literal"><span class="pre">JSON_MAX_INDENT</span></code> constant defines the maximum indentation
that can be used, and its value is 31.</p>
<div class="last versionchanged">
<p><span class="versionmodified">Changed in version 2.7: </span>Added <code class="docutils literal"><span class="pre">JSON_MAX_INDENT</span></code>.</p>
</div>
</dd>
<dt><code class="docutils literal"><span class="pre">JSON_COMPACT</span></code></dt>
<dd>This flag enables a compact representation, i.e. sets the separator
between array and object items to <code class="docutils literal"><span class="pre">","</span></code> and between object keys
and values to <code class="docutils literal"><span class="pre">":"</span></code>. Without this flag, the corresponding
separators are <code class="docutils literal"><span class="pre">",</span> <span class="pre">"</span></code> and <code class="docutils literal"><span class="pre">":</span> <span class="pre">"</span></code> for more readable output.</dd>
<dt><code class="docutils literal"><span class="pre">JSON_ENSURE_ASCII</span></code></dt>
<dd>If this flag is used, the output is guaranteed to consist only of
ASCII characters. This is achieved by escaping all Unicode
characters outside the ASCII range.</dd>
<dt><code class="docutils literal"><span class="pre">JSON_SORT_KEYS</span></code></dt>
<dd>If this flag is used, all the objects in output are sorted by key.
This is useful e.g. if two JSON texts are diffed or visually
compared.</dd>
<dt><code class="docutils literal"><span class="pre">JSON_PRESERVE_ORDER</span></code></dt>
<dd><p class="first"><strong>Deprecated since version 2.8:</strong> Order of object keys
is always preserved.</p>
<p class="last">Prior to version 2.8: If this flag is used, object keys in the
output are sorted into the same order in which they were first
inserted to the object. For example, decoding a JSON text and then
encoding with this flag preserves the order of object keys.</p>
</dd>
<dt><code class="docutils literal"><span class="pre">JSON_ENCODE_ANY</span></code></dt>
<dd><p class="first">Specifying this flag makes it possible to encode any JSON value on
its own. Without it, only objects and arrays can be passed as the
<em>json</em> value to the encoding functions.</p>
<p><strong>Note:</strong> Encoding any value may be useful in some scenarios, but
it’s generally discouraged as it violates strict compatibility with
<span class="target" id="index-1"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc4627.html"><strong>RFC 4627</strong></a>. If you use this flag, don’t expect interoperability
with other JSON systems.</p>
<div class="last versionadded">
<p><span class="versionmodified">New in version 2.1.</span></p>
</div>
</dd>
<dt><code class="docutils literal"><span class="pre">JSON_ESCAPE_SLASH</span></code></dt>
<dd><p class="first">Escape the <code class="docutils literal"><span class="pre">/</span></code> characters in strings with <code class="docutils literal"><span class="pre">\/</span></code>.</p>
<div class="last versionadded">
<p><span class="versionmodified">New in version 2.4.</span></p>
</div>
</dd>
<dt><code class="docutils literal"><span class="pre">JSON_REAL_PRECISION(n)</span></code></dt>
<dd><p class="first">Output all real numbers with at most <em>n</em> digits of precision. The
valid range for <em>n</em> is between 0 and 31 (inclusive), and other
values result in an undefined behavior.</p>
<p>By default, the precision is 17, to correctly and losslessly encode
all IEEE 754 double precision floating point numbers.</p>
<div class="last versionadded">
<p><span class="versionmodified">New in version 2.7.</span></p>
</div>
</dd>
<dt><code class="docutils literal"><span class="pre">JSON_EMBED</span></code></dt>
<dd><p class="first">If this flag is used, the opening and closing characters of the top-level
array (‘[‘, ‘]’) or object (‘{‘, ‘}’) are omitted during encoding. This
flag is useful when concatenating multiple arrays or objects into a stream.</p>
<div class="last versionadded">
<p><span class="versionmodified">New in version 2.10.</span></p>
</div>
</dd>
</dl>
<p>These functions output UTF-8:</p>
<dl class="function">
<dt id="c.json_dumps">
char *<code class="descname">json_dumps</code><span class="sig-paren">(</span>const <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *json</em>, size_t<em> flags</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_dumps" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the JSON representation of <em>json</em> as a string, or <em>NULL</em> on
error. <em>flags</em> is described above. The return value must be freed
by the caller using <code class="xref c c-func docutils literal"><span class="pre">free()</span></code>.</p>
</dd></dl>
<dl class="function">
<dt id="c.json_dumpb">
size_t <code class="descname">json_dumpb</code><span class="sig-paren">(</span>const <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *json</em>, char<em> *buffer</em>, size_t<em> size</em>, size_t<em> flags</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_dumpb" title="Permalink to this definition">¶</a></dt>
<dd><p>Writes the JSON representation of <em>json</em> to the <em>buffer</em> of
<em>size</em> bytes. Returns the number of bytes that would be written
or 0 on error. <em>flags</em> is described above. <em>buffer</em> is not
null-terminated.</p>
<p>This function never writes more than <em>size</em> bytes. If the return
value is greater than <em>size</em>, the contents of the <em>buffer</em> are
undefined. This behavior enables you to specify a NULL <em>buffer</em>
to determine the length of the encoding. For example:</p>
<div class="highlight-c"><div class="highlight"><pre><span></span><span class="kt">size_t</span> <span class="n">size</span> <span class="o">=</span> <span class="n">json_dumpb</span><span class="p">(</span><span class="n">json</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="n">size</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span>
<span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>
<span class="kt">char</span> <span class="o">*</span><span class="n">buf</span> <span class="o">=</span> <span class="n">alloca</span><span class="p">(</span><span class="n">size</span><span class="p">);</span>
<span class="n">size</span> <span class="o">=</span> <span class="n">json_dumpb</span><span class="p">(</span><span class="n">json</span><span class="p">,</span> <span class="n">buf</span><span class="p">,</span> <span class="n">size</span><span class="p">,</span> <span class="mi">0</span><span class="p">);</span>
</pre></div>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.10.</span></p>
</div>
</dd></dl>
<dl class="function">
<dt id="c.json_dumpf">
int <code class="descname">json_dumpf</code><span class="sig-paren">(</span>const <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *json</em>, FILE<em> *output</em>, size_t<em> flags</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_dumpf" title="Permalink to this definition">¶</a></dt>
<dd><p>Write the JSON representation of <em>json</em> to the stream <em>output</em>.
<em>flags</em> is described above. Returns 0 on success and -1 on error.
If an error occurs, something may have already been written to
<em>output</em>. In this case, the output is undefined and most likely not
valid JSON.</p>
</dd></dl>
<dl class="function">
<dt id="c.json_dumpfd">
int <code class="descname">json_dumpfd</code><span class="sig-paren">(</span>const <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *json</em>, int<em> output</em>, size_t<em> flags</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_dumpfd" title="Permalink to this definition">¶</a></dt>
<dd><p>Write the JSON representation of <em>json</em> to the stream <em>output</em>.
<em>flags</em> is described above. Returns 0 on success and -1 on error.
If an error occurs, something may have already been written to
<em>output</em>. In this case, the output is undefined and most likely not
valid JSON.</p>
<p>It is important to note that this function can only succeed on stream
file descriptors (such as SOCK_STREAM). Using this function on a
non-stream file descriptor will result in undefined behavior. For
non-stream file descriptors, see instead <a class="reference internal" href="#c.json_dumpb" title="json_dumpb"><code class="xref c c-func docutils literal"><span class="pre">json_dumpb()</span></code></a>.</p>
<p>This function requires POSIX and fails on all non-POSIX systems.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.10.</span></p>
</div>
</dd></dl>
<dl class="function">
<dt id="c.json_dump_file">
int <code class="descname">json_dump_file</code><span class="sig-paren">(</span>const <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *json</em>, const char<em> *path</em>, size_t<em> flags</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_dump_file" title="Permalink to this definition">¶</a></dt>
<dd><p>Write the JSON representation of <em>json</em> to the file <em>path</em>. If
<em>path</em> already exists, it is overwritten. <em>flags</em> is described
above. Returns 0 on success and -1 on error.</p>
</dd></dl>
<dl class="type">
<dt id="c.json_dump_callback_t">
<code class="descname">json_dump_callback_t</code><a class="headerlink" href="#c.json_dump_callback_t" title="Permalink to this definition">¶</a></dt>
<dd><p>A typedef for a function that’s called by
<a class="reference internal" href="#c.json_dump_callback" title="json_dump_callback"><code class="xref c c-func docutils literal"><span class="pre">json_dump_callback()</span></code></a>:</p>
<div class="highlight-c"><div class="highlight"><pre><span></span><span class="k">typedef</span> <span class="nf">int</span> <span class="p">(</span><span class="o">*</span><span class="n">json_dump_callback_t</span><span class="p">)(</span><span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">buffer</span><span class="p">,</span> <span class="kt">size_t</span> <span class="n">size</span><span class="p">,</span> <span class="kt">void</span> <span class="o">*</span><span class="n">data</span><span class="p">);</span>
</pre></div>
</div>
<p><em>buffer</em> points to a buffer containing a chunk of output, <em>size</em> is
the length of the buffer, and <em>data</em> is the corresponding
<a class="reference internal" href="#c.json_dump_callback" title="json_dump_callback"><code class="xref c c-func docutils literal"><span class="pre">json_dump_callback()</span></code></a> argument passed through.</p>
<p><em>buffer</em> is guaranteed to be a valid UTF-8 string (i.e. multi-byte
code unit sequences are preserved). <em>buffer</em> never contains
embedded null bytes.</p>
<p>On error, the function should return -1 to stop the encoding
process. On success, it should return 0.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.2.</span></p>
</div>
</dd></dl>
<dl class="function">
<dt id="c.json_dump_callback">
int <code class="descname">json_dump_callback</code><span class="sig-paren">(</span>const <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *json</em>, <a class="reference internal" href="#c.json_dump_callback_t" title="json_dump_callback_t">json_dump_callback_t</a><em> callback</em>, void<em> *data</em>, size_t<em> flags</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_dump_callback" title="Permalink to this definition">¶</a></dt>
<dd><p>Call <em>callback</em> repeatedly, passing a chunk of the JSON
representation of <em>json</em> each time. <em>flags</em> is described above.
Returns 0 on success and -1 on error.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.2.</span></p>
</div>
</dd></dl>
</div>
<div class="section" id="decoding">
<span id="apiref-decoding"></span><h2>Decoding<a class="headerlink" href="#decoding" title="Permalink to this headline">¶</a></h2>
<p>This sections describes the functions that can be used to decode JSON
text to the Jansson representation of JSON data. The JSON
specification requires that a JSON text is either a serialized array
or object, and this requirement is also enforced with the following
functions. In other words, the top level value in the JSON text being
decoded must be either array or object. To decode any JSON value, use
the <code class="docutils literal"><span class="pre">JSON_DECODE_ANY</span></code> flag (see below).</p>
<p>See <a class="reference internal" href="conformance.html#rfc-conformance"><span class="std std-ref">RFC Conformance</span></a> for a discussion on Jansson’s conformance
to the JSON specification. It explains many design decisions that
affect especially the behavior of the decoder.</p>
<p>Each function takes a <em>flags</em> parameter that can be used to control
the behavior of the decoder. Its default value is 0. The following
macros can be ORed together to obtain <em>flags</em>.</p>
<dl class="docutils">
<dt><code class="docutils literal"><span class="pre">JSON_REJECT_DUPLICATES</span></code></dt>
<dd><p class="first">Issue a decoding error if any JSON object in the input text
contains duplicate keys. Without this flag, the value of the last
occurrence of each key ends up in the result. Key equivalence is
checked byte-by-byte, without special Unicode comparison
algorithms.</p>
<div class="last versionadded">
<p><span class="versionmodified">New in version 2.1.</span></p>
</div>
</dd>
<dt><code class="docutils literal"><span class="pre">JSON_DECODE_ANY</span></code></dt>
<dd><p class="first">By default, the decoder expects an array or object as the input.
With this flag enabled, the decoder accepts any valid JSON value.</p>
<p><strong>Note:</strong> Decoding any value may be useful in some scenarios, but
it’s generally discouraged as it violates strict compatibility with
<span class="target" id="index-2"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc4627.html"><strong>RFC 4627</strong></a>. If you use this flag, don’t expect interoperability
with other JSON systems.</p>
<div class="last versionadded">
<p><span class="versionmodified">New in version 2.3.</span></p>
</div>
</dd>
<dt><code class="docutils literal"><span class="pre">JSON_DISABLE_EOF_CHECK</span></code></dt>
<dd><p class="first">By default, the decoder expects that its whole input constitutes a
valid JSON text, and issues an error if there’s extra data after
the otherwise valid JSON input. With this flag enabled, the decoder
stops after decoding a valid JSON array or object, and thus allows
extra data after the JSON text.</p>
<p>Normally, reading will stop when the last <code class="docutils literal"><span class="pre">]</span></code> or <code class="docutils literal"><span class="pre">}</span></code> in the
JSON input is encountered. If both <code class="docutils literal"><span class="pre">JSON_DISABLE_EOF_CHECK</span></code> and
<code class="docutils literal"><span class="pre">JSON_DECODE_ANY</span></code> flags are used, the decoder may read one extra
UTF-8 code unit (up to 4 bytes of input). For example, decoding
<code class="docutils literal"><span class="pre">4true</span></code> correctly decodes the integer 4, but also reads the
<code class="docutils literal"><span class="pre">t</span></code>. For this reason, if reading multiple consecutive values that
are not arrays or objects, they should be separated by at least one
whitespace character.</p>
<div class="last versionadded">
<p><span class="versionmodified">New in version 2.1.</span></p>
</div>
</dd>
<dt><code class="docutils literal"><span class="pre">JSON_DECODE_INT_AS_REAL</span></code></dt>
<dd><p class="first">JSON defines only one number type. Jansson distinguishes between
ints and reals. For more information see <a class="reference internal" href="conformance.html#real-vs-integer"><span class="std std-ref">Real vs. Integer</span></a>.
With this flag enabled the decoder interprets all numbers as real
values. Integers that do not have an exact double representation
will silently result in a loss of precision. Integers that cause
a double overflow will cause an error.</p>
<div class="last versionadded">
<p><span class="versionmodified">New in version 2.5.</span></p>
</div>
</dd>
<dt><code class="docutils literal"><span class="pre">JSON_ALLOW_NUL</span></code></dt>
<dd><p class="first">Allow <code class="docutils literal"><span class="pre">\u0000</span></code> escape inside string values. This is a safety
measure; If you know your input can contain null bytes, use this
flag. If you don’t use this flag, you don’t have to worry about null
bytes inside strings unless you explicitly create themselves by
using e.g. <a class="reference internal" href="#c.json_stringn" title="json_stringn"><code class="xref c c-func docutils literal"><span class="pre">json_stringn()</span></code></a> or <code class="docutils literal"><span class="pre">s#</span></code> format specifier for
<a class="reference internal" href="#c.json_pack" title="json_pack"><code class="xref c c-func docutils literal"><span class="pre">json_pack()</span></code></a>.</p>
<p>Object keys cannot have embedded null bytes even if this flag is
used.</p>
<div class="last versionadded">
<p><span class="versionmodified">New in version 2.6.</span></p>
</div>
</dd>
</dl>
<p>Each function also takes an optional <a class="reference internal" href="#c.json_error_t" title="json_error_t"><code class="xref c c-type docutils literal"><span class="pre">json_error_t</span></code></a> parameter
that is filled with error information if decoding fails. It’s also
updated on success; the number of bytes of input read is written to
its <code class="docutils literal"><span class="pre">position</span></code> field. This is especially useful when using
<code class="docutils literal"><span class="pre">JSON_DISABLE_EOF_CHECK</span></code> to read multiple consecutive JSON texts.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.3: </span>Number of bytes of input read is written to the <code class="docutils literal"><span class="pre">position</span></code> field
of the <a class="reference internal" href="#c.json_error_t" title="json_error_t"><code class="xref c c-type docutils literal"><span class="pre">json_error_t</span></code></a> structure.</p>
</div>
<p>If no error or position information is needed, you can pass <em>NULL</em>.</p>
<dl class="function">
<dt id="c.json_loads">
<a class="reference internal" href="#c.json_t" title="json_t">json_t</a> *<code class="descname">json_loads</code><span class="sig-paren">(</span>const char<em> *input</em>, size_t<em> flags</em>, <a class="reference internal" href="#c.json_error_t" title="json_error_t">json_error_t</a><em> *error</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_loads" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Decodes the JSON string <em>input</em> and returns the array or object it
contains, or <em>NULL</em> on error, in which case <em>error</em> is filled with
information about the error. <em>flags</em> is described above.</p>
</dd></dl>
<dl class="function">
<dt id="c.json_loadb">
<a class="reference internal" href="#c.json_t" title="json_t">json_t</a> *<code class="descname">json_loadb</code><span class="sig-paren">(</span>const char<em> *buffer</em>, size_t<em> buflen</em>, size_t<em> flags</em>, <a class="reference internal" href="#c.json_error_t" title="json_error_t">json_error_t</a><em> *error</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_loadb" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Decodes the JSON string <em>buffer</em>, whose length is <em>buflen</em>, and
returns the array or object it contains, or <em>NULL</em> on error, in
which case <em>error</em> is filled with information about the error. This
is similar to <a class="reference internal" href="#c.json_loads" title="json_loads"><code class="xref c c-func docutils literal"><span class="pre">json_loads()</span></code></a> except that the string doesn’t
need to be null-terminated. <em>flags</em> is described above.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.1.</span></p>
</div>
</dd></dl>
<dl class="function">
<dt id="c.json_loadf">
<a class="reference internal" href="#c.json_t" title="json_t">json_t</a> *<code class="descname">json_loadf</code><span class="sig-paren">(</span>FILE<em> *input</em>, size_t<em> flags</em>, <a class="reference internal" href="#c.json_error_t" title="json_error_t">json_error_t</a><em> *error</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_loadf" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Decodes the JSON text in stream <em>input</em> and returns the array or
object it contains, or <em>NULL</em> on error, in which case <em>error</em> is
filled with information about the error. <em>flags</em> is described
above.</p>
<p>This function will start reading the input from whatever position
the input file was in, without attempting to seek first. If an error
occurs, the file position will be left indeterminate. On success,
the file position will be at EOF, unless <code class="docutils literal"><span class="pre">JSON_DISABLE_EOF_CHECK</span></code>
flag was used. In this case, the file position will be at the first
character after the last <code class="docutils literal"><span class="pre">]</span></code> or <code class="docutils literal"><span class="pre">}</span></code> in the JSON input. This
allows calling <a class="reference internal" href="#c.json_loadf" title="json_loadf"><code class="xref c c-func docutils literal"><span class="pre">json_loadf()</span></code></a> on the same <code class="docutils literal"><span class="pre">FILE</span></code> object
multiple times, if the input consists of consecutive JSON texts,
possibly separated by whitespace.</p>
</dd></dl>
<dl class="function">
<dt id="c.json_loadfd">
<a class="reference internal" href="#c.json_t" title="json_t">json_t</a> *<code class="descname">json_loadfd</code><span class="sig-paren">(</span>int<em> input</em>, size_t<em> flags</em>, <a class="reference internal" href="#c.json_error_t" title="json_error_t">json_error_t</a><em> *error</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_loadfd" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Decodes the JSON text in stream <em>input</em> and returns the array or
object it contains, or <em>NULL</em> on error, in which case <em>error</em> is
filled with information about the error. <em>flags</em> is described
above.</p>
<p>This function will start reading the input from whatever position
the input file descriptor was in, without attempting to seek first.
If an error occurs, the file position will be left indeterminate.
On success, the file position will be at EOF, unless
<code class="docutils literal"><span class="pre">JSON_DISABLE_EOF_CHECK</span></code> flag was used. In this case, the file
descriptor’s position will be at the first character after the last
<code class="docutils literal"><span class="pre">]</span></code> or <code class="docutils literal"><span class="pre">}</span></code> in the JSON input. This allows calling
<a class="reference internal" href="#c.json_loadfd" title="json_loadfd"><code class="xref c c-func docutils literal"><span class="pre">json_loadfd()</span></code></a> on the same file descriptor multiple times,
if the input consists of consecutive JSON texts, possibly separated
by whitespace.</p>
<p>It is important to note that this function can only succeed on stream
file descriptors (such as SOCK_STREAM). Using this function on a
non-stream file descriptor will result in undefined behavior. For
non-stream file descriptors, see instead <a class="reference internal" href="#c.json_loadb" title="json_loadb"><code class="xref c c-func docutils literal"><span class="pre">json_loadb()</span></code></a>.</p>
<p>This function requires POSIX and fails on all non-POSIX systems.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.10.</span></p>
</div>
</dd></dl>
<dl class="function">
<dt id="c.json_load_file">
<a class="reference internal" href="#c.json_t" title="json_t">json_t</a> *<code class="descname">json_load_file</code><span class="sig-paren">(</span>const char<em> *path</em>, size_t<em> flags</em>, <a class="reference internal" href="#c.json_error_t" title="json_error_t">json_error_t</a><em> *error</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_load_file" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Decodes the JSON text in file <em>path</em> and returns the array or
object it contains, or <em>NULL</em> on error, in which case <em>error</em> is
filled with information about the error. <em>flags</em> is described
above.</p>
</dd></dl>
<dl class="type">
<dt id="c.json_load_callback_t">
<code class="descname">json_load_callback_t</code><a class="headerlink" href="#c.json_load_callback_t" title="Permalink to this definition">¶</a></dt>
<dd><p>A typedef for a function that’s called by
<a class="reference internal" href="#c.json_load_callback" title="json_load_callback"><code class="xref c c-func docutils literal"><span class="pre">json_load_callback()</span></code></a> to read a chunk of input data:</p>
<div class="highlight-c"><div class="highlight"><pre><span></span><span class="k">typedef</span> <span class="nf">size_t</span> <span class="p">(</span><span class="o">*</span><span class="n">json_load_callback_t</span><span class="p">)(</span><span class="kt">void</span> <span class="o">*</span><span class="n">buffer</span><span class="p">,</span> <span class="kt">size_t</span> <span class="n">buflen</span><span class="p">,</span> <span class="kt">void</span> <span class="o">*</span><span class="n">data</span><span class="p">);</span>
</pre></div>
</div>
<p><em>buffer</em> points to a buffer of <em>buflen</em> bytes, and <em>data</em> is the
corresponding <a class="reference internal" href="#c.json_load_callback" title="json_load_callback"><code class="xref c c-func docutils literal"><span class="pre">json_load_callback()</span></code></a> argument passed through.</p>
<p>On success, the function should write at most <em>buflen</em> bytes to
<em>buffer</em>, and return the number of bytes written; a returned value
of 0 indicates that no data was produced and that the end of file
has been reached. On error, the function should return
<code class="docutils literal"><span class="pre">(size_t)-1</span></code> to abort the decoding process.</p>
<p>In UTF-8, some code points are encoded as multi-byte sequences. The
callback function doesn’t need to worry about this, as Jansson
handles it at a higher level. For example, you can safely read a
fixed number of bytes from a network connection without having to
care about code unit sequences broken apart by the chunk
boundaries.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.4.</span></p>
</div>
</dd></dl>
<dl class="function">
<dt id="c.json_load_callback">
<a class="reference internal" href="#c.json_t" title="json_t">json_t</a> *<code class="descname">json_load_callback</code><span class="sig-paren">(</span><a class="reference internal" href="#c.json_load_callback_t" title="json_load_callback_t">json_load_callback_t</a><em> callback</em>, void<em> *data</em>, size_t<em> flags</em>, <a class="reference internal" href="#c.json_error_t" title="json_error_t">json_error_t</a><em> *error</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_load_callback" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Decodes the JSON text produced by repeated calls to <em>callback</em>, and
returns the array or object it contains, or <em>NULL</em> on error, in
which case <em>error</em> is filled with information about the error.
<em>data</em> is passed through to <em>callback</em> on each call. <em>flags</em> is
described above.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.4.</span></p>
</div>
</dd></dl>
</div>
<div class="section" id="building-values">
<span id="apiref-pack"></span><h2>Building Values<a class="headerlink" href="#building-values" title="Permalink to this headline">¶</a></h2>
<p>This section describes functions that help to create, or <em>pack</em>,
complex JSON values, especially nested objects and arrays. Value
building is based on a <em>format string</em> that is used to tell the
functions about the expected arguments.</p>
<p>For example, the format string <code class="docutils literal"><span class="pre">"i"</span></code> specifies a single integer
value, while the format string <code class="docutils literal"><span class="pre">"[ssb]"</span></code> or the equivalent <code class="docutils literal"><span class="pre">"[s,</span> <span class="pre">s,</span>
<span class="pre">b]"</span></code> specifies an array value with two strings and a boolean as its
items:</p>
<div class="highlight-c"><div class="highlight"><pre><span></span><span class="cm">/* Create the JSON integer 42 */</span>
<span class="n">json_pack</span><span class="p">(</span><span class="s">"i"</span><span class="p">,</span> <span class="mi">42</span><span class="p">);</span>
<span class="cm">/* Create the JSON array ["foo", "bar", true] */</span>
<span class="n">json_pack</span><span class="p">(</span><span class="s">"[ssb]"</span><span class="p">,</span> <span class="s">"foo"</span><span class="p">,</span> <span class="s">"bar"</span><span class="p">,</span> <span class="mi">1</span><span class="p">);</span>
</pre></div>
</div>
<p>Here’s the full list of format specifiers. The type in parentheses
denotes the resulting JSON type, and the type in brackets (if any)
denotes the C type that is expected as the corresponding argument or
arguments.</p>
<dl class="docutils">
<dt><code class="docutils literal"><span class="pre">s</span></code> (string) [const char *]</dt>
<dd>Convert a null terminated UTF-8 string to a JSON string.</dd>
<dt><code class="docutils literal"><span class="pre">s?</span></code> (string) [const char *]</dt>
<dd><p class="first">Like <code class="docutils literal"><span class="pre">s</span></code>, but if the argument is <em>NULL</em>, output a JSON null
value.</p>
<div class="last versionadded">
<p><span class="versionmodified">New in version 2.8.</span></p>
</div>
</dd>
<dt><code class="docutils literal"><span class="pre">s*</span></code> (string) [const char *]</dt>
<dd><p class="first">Like <code class="docutils literal"><span class="pre">s</span></code>, but if the argument is <em>NULL</em>, do not output any value.
This format can only be used inside an object or an array. If used
inside an object, the corresponding key is additionally suppressed
when the value is omitted. See below for an example.</p>
<div class="last versionadded">
<p><span class="versionmodified">New in version 2.11.</span></p>
</div>
</dd>
<dt><code class="docutils literal"><span class="pre">s#</span></code> (string) [const char *, int]</dt>
<dd><p class="first">Convert a UTF-8 buffer of a given length to a JSON string.</p>
<div class="last versionadded">
<p><span class="versionmodified">New in version 2.5.</span></p>
</div>
</dd>
<dt><code class="docutils literal"><span class="pre">s%</span></code> (string) [const char *, size_t]</dt>
<dd><p class="first">Like <code class="docutils literal"><span class="pre">s#</span></code> but the length argument is of type <code class="xref c c-type docutils literal"><span class="pre">size_t</span></code>.</p>
<div class="last versionadded">
<p><span class="versionmodified">New in version 2.6.</span></p>
</div>
</dd>
<dt><code class="docutils literal"><span class="pre">+</span></code> [const char *]</dt>
<dd><p class="first">Like <code class="docutils literal"><span class="pre">s</span></code>, but concatenate to the previous string. Only valid
after <code class="docutils literal"><span class="pre">s</span></code>, <code class="docutils literal"><span class="pre">s#</span></code>, <code class="docutils literal"><span class="pre">+</span></code> or <code class="docutils literal"><span class="pre">+#</span></code>.</p>
<div class="last versionadded">
<p><span class="versionmodified">New in version 2.5.</span></p>
</div>
</dd>
<dt><code class="docutils literal"><span class="pre">+#</span></code> [const char *, int]</dt>
<dd><p class="first">Like <code class="docutils literal"><span class="pre">s#</span></code>, but concatenate to the previous string. Only valid
after <code class="docutils literal"><span class="pre">s</span></code>, <code class="docutils literal"><span class="pre">s#</span></code>, <code class="docutils literal"><span class="pre">+</span></code> or <code class="docutils literal"><span class="pre">+#</span></code>.</p>
<div class="last versionadded">
<p><span class="versionmodified">New in version 2.5.</span></p>
</div>
</dd>
<dt><code class="docutils literal"><span class="pre">+%</span></code> (string) [const char *, size_t]</dt>
<dd><p class="first">Like <code class="docutils literal"><span class="pre">+#</span></code> but the length argument is of type <code class="xref c c-type docutils literal"><span class="pre">size_t</span></code>.</p>
<div class="last versionadded">
<p><span class="versionmodified">New in version 2.6.</span></p>
</div>
</dd>
<dt><code class="docutils literal"><span class="pre">n</span></code> (null)</dt>
<dd>Output a JSON null value. No argument is consumed.</dd>
<dt><code class="docutils literal"><span class="pre">b</span></code> (boolean) [int]</dt>
<dd>Convert a C <code class="xref c c-type docutils literal"><span class="pre">int</span></code> to JSON boolean value. Zero is converted
to <code class="docutils literal"><span class="pre">false</span></code> and non-zero to <code class="docutils literal"><span class="pre">true</span></code>.</dd>
<dt><code class="docutils literal"><span class="pre">i</span></code> (integer) [int]</dt>
<dd>Convert a C <code class="xref c c-type docutils literal"><span class="pre">int</span></code> to JSON integer.</dd>
<dt><code class="docutils literal"><span class="pre">I</span></code> (integer) [json_int_t]</dt>
<dd>Convert a C <a class="reference internal" href="#c.json_int_t" title="json_int_t"><code class="xref c c-type docutils literal"><span class="pre">json_int_t</span></code></a> to JSON integer.</dd>
<dt><code class="docutils literal"><span class="pre">f</span></code> (real) [double]</dt>
<dd>Convert a C <code class="xref c c-type docutils literal"><span class="pre">double</span></code> to JSON real.</dd>
<dt><code class="docutils literal"><span class="pre">o</span></code> (any value) [json_t *]</dt>
<dd>Output any given JSON value as-is. If the value is added to an
array or object, the reference to the value passed to <code class="docutils literal"><span class="pre">o</span></code> is
stolen by the container.</dd>
<dt><code class="docutils literal"><span class="pre">O</span></code> (any value) [json_t *]</dt>
<dd>Like <code class="docutils literal"><span class="pre">o</span></code>, but the argument’s reference count is incremented.
This is useful if you pack into an array or object and want to
keep the reference for the JSON value consumed by <code class="docutils literal"><span class="pre">O</span></code> to
yourself.</dd>
<dt><code class="docutils literal"><span class="pre">o?</span></code>, <code class="docutils literal"><span class="pre">O?</span></code> (any value) [json_t *]</dt>
<dd><p class="first">Like <code class="docutils literal"><span class="pre">o</span></code> and <code class="docutils literal"><span class="pre">O</span></code>, respectively, but if the argument is
<em>NULL</em>, output a JSON null value.</p>
<div class="last versionadded">
<p><span class="versionmodified">New in version 2.8.</span></p>
</div>
</dd>
<dt><code class="docutils literal"><span class="pre">o*</span></code>, <code class="docutils literal"><span class="pre">O*</span></code> (any value) [json_t *]</dt>
<dd><p class="first">Like <code class="docutils literal"><span class="pre">o</span></code> and <code class="docutils literal"><span class="pre">O</span></code>, respectively, but if the argument is
<em>NULL</em>, do not output any value. This format can only be used
inside an object or an array. If used inside an object, the
corresponding key is additionally suppressed. See below for an
example.</p>
<div class="last versionadded">
<p><span class="versionmodified">New in version 2.11.</span></p>
</div>
</dd>
<dt><code class="docutils literal"><span class="pre">[fmt]</span></code> (array)</dt>
<dd>Build an array with contents from the inner format string. <code class="docutils literal"><span class="pre">fmt</span></code>
may contain objects and arrays, i.e. recursive value building is
supported.</dd>
<dt><code class="docutils literal"><span class="pre">{fmt}</span></code> (object)</dt>
<dd>Build an object with contents from the inner format string
<code class="docutils literal"><span class="pre">fmt</span></code>. The first, third, etc. format specifier represent a key,
and must be a string (see <code class="docutils literal"><span class="pre">s</span></code>, <code class="docutils literal"><span class="pre">s#</span></code>, <code class="docutils literal"><span class="pre">+</span></code> and <code class="docutils literal"><span class="pre">+#</span></code> above),
as object keys are always strings. The second, fourth, etc. format
specifier represent a value. Any value may be an object or array,
i.e. recursive value building is supported.</dd>
</dl>
<p>Whitespace, <code class="docutils literal"><span class="pre">:</span></code> and <code class="docutils literal"><span class="pre">,</span></code> are ignored.</p>
<dl class="function">
<dt id="c.json_pack">
<a class="reference internal" href="#c.json_t" title="json_t">json_t</a> *<code class="descname">json_pack</code><span class="sig-paren">(</span>const char<em> *fmt</em>, ...<span class="sig-paren">)</span><a class="headerlink" href="#c.json_pack" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Build a new JSON value according to the format string <em>fmt</em>. For
each format specifier (except for <code class="docutils literal"><span class="pre">{}[]n</span></code>), one or more arguments
are consumed and used to build the corresponding value. Returns
<em>NULL</em> on error.</p>
</dd></dl>
<dl class="function">
<dt id="c.json_pack_ex">
<a class="reference internal" href="#c.json_t" title="json_t">json_t</a> *<code class="descname">json_pack_ex</code><span class="sig-paren">(</span><a class="reference internal" href="#c.json_error_t" title="json_error_t">json_error_t</a><em> *error</em>, size_t<em> flags</em>, const char<em> *fmt</em>, ...<span class="sig-paren">)</span><a class="headerlink" href="#c.json_pack_ex" title="Permalink to this definition">¶</a></dt>
<dt id="c.json_vpack_ex">
<a class="reference internal" href="#c.json_t" title="json_t">json_t</a> *<code class="descname">json_vpack_ex</code><span class="sig-paren">(</span><a class="reference internal" href="#c.json_error_t" title="json_error_t">json_error_t</a><em> *error</em>, size_t<em> flags</em>, const char<em> *fmt</em>, va_list<em> ap</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_vpack_ex" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Like <a class="reference internal" href="#c.json_pack" title="json_pack"><code class="xref c c-func docutils literal"><span class="pre">json_pack()</span></code></a>, but an in the case of an error, an error
message is written to <em>error</em>, if it’s not <em>NULL</em>. The <em>flags</em>
parameter is currently unused and should be set to 0.</p>
<p>As only the errors in format string (and out-of-memory errors) can
be caught by the packer, these two functions are most likely only
useful for debugging format strings.</p>
</dd></dl>
<p>More examples:</p>
<div class="highlight-c"><div class="highlight"><pre><span></span><span class="cm">/* Build an empty JSON object */</span>
<span class="n">json_pack</span><span class="p">(</span><span class="s">"{}"</span><span class="p">);</span>
<span class="cm">/* Build the JSON object {"foo": 42, "bar": 7} */</span>
<span class="n">json_pack</span><span class="p">(</span><span class="s">"{sisi}"</span><span class="p">,</span> <span class="s">"foo"</span><span class="p">,</span> <span class="mi">42</span><span class="p">,</span> <span class="s">"bar"</span><span class="p">,</span> <span class="mi">7</span><span class="p">);</span>
<span class="cm">/* Like above, ':', ',' and whitespace are ignored */</span>
<span class="n">json_pack</span><span class="p">(</span><span class="s">"{s:i, s:i}"</span><span class="p">,</span> <span class="s">"foo"</span><span class="p">,</span> <span class="mi">42</span><span class="p">,</span> <span class="s">"bar"</span><span class="p">,</span> <span class="mi">7</span><span class="p">);</span>
<span class="cm">/* Build the JSON array [[1, 2], {"cool": true}] */</span>
<span class="n">json_pack</span><span class="p">(</span><span class="s">"[[i,i],{s:b}]"</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="s">"cool"</span><span class="p">,</span> <span class="mi">1</span><span class="p">);</span>
<span class="cm">/* Build a string from a non-null terminated buffer */</span>
<span class="kt">char</span> <span class="n">buffer</span><span class="p">[</span><span class="mi">4</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span><span class="sc">'t'</span><span class="p">,</span> <span class="sc">'e'</span><span class="p">,</span> <span class="sc">'s'</span><span class="p">,</span> <span class="sc">'t'</span><span class="p">};</span>
<span class="n">json_pack</span><span class="p">(</span><span class="s">"s#"</span><span class="p">,</span> <span class="n">buffer</span><span class="p">,</span> <span class="mi">4</span><span class="p">);</span>
<span class="cm">/* Concatenate strings together to build the JSON string "foobarbaz" */</span>
<span class="n">json_pack</span><span class="p">(</span><span class="s">"s++"</span><span class="p">,</span> <span class="s">"foo"</span><span class="p">,</span> <span class="s">"bar"</span><span class="p">,</span> <span class="s">"baz"</span><span class="p">);</span>
<span class="cm">/* Create an empty object or array when optional members are missing */</span>
<span class="n">json_pack</span><span class="p">(</span><span class="s">"{s:s*,s:o*,s:O*}"</span><span class="p">,</span> <span class="s">"foo"</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">,</span> <span class="s">"bar"</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">,</span> <span class="s">"baz"</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">);</span>
<span class="n">json_pack</span><span class="p">(</span><span class="s">"[s*,o*,O*]"</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">);</span>
</pre></div>
</div>
</div>
<div class="section" id="parsing-and-validating-values">
<span id="apiref-unpack"></span><h2>Parsing and Validating Values<a class="headerlink" href="#parsing-and-validating-values" title="Permalink to this headline">¶</a></h2>
<p>This section describes functions that help to validate complex values
and extract, or <em>unpack</em>, data from them. Like <a class="reference internal" href="#apiref-pack"><span class="std std-ref">building values</span></a>, this is also based on format strings.</p>
<p>While a JSON value is unpacked, the type specified in the format
string is checked to match that of the JSON value. This is the
validation part of the process. In addition to this, the unpacking
functions can also check that all items of arrays and objects are
unpacked. This check be enabled with the format specifier <code class="docutils literal"><span class="pre">!</span></code> or by
using the flag <code class="docutils literal"><span class="pre">JSON_STRICT</span></code>. See below for details.</p>
<p>Here’s the full list of format specifiers. The type in parentheses
denotes the JSON type, and the type in brackets (if any) denotes the C
type whose address should be passed.</p>
<dl class="docutils">
<dt><code class="docutils literal"><span class="pre">s</span></code> (string) [const char *]</dt>
<dd>Convert a JSON string to a pointer to a null terminated UTF-8
string. The resulting string is extracted by using
<a class="reference internal" href="#c.json_string_value" title="json_string_value"><code class="xref c c-func docutils literal"><span class="pre">json_string_value()</span></code></a> internally, so it exists as long as
there are still references to the corresponding JSON string.</dd>
<dt><code class="docutils literal"><span class="pre">s%</span></code> (string) [const char *, size_t *]</dt>
<dd><p class="first">Convert a JSON string to a pointer to a null terminated UTF-8
string and its length.</p>
<div class="last versionadded">
<p><span class="versionmodified">New in version 2.6.</span></p>
</div>
</dd>
<dt><code class="docutils literal"><span class="pre">n</span></code> (null)</dt>
<dd>Expect a JSON null value. Nothing is extracted.</dd>
<dt><code class="docutils literal"><span class="pre">b</span></code> (boolean) [int]</dt>
<dd>Convert a JSON boolean value to a C <code class="xref c c-type docutils literal"><span class="pre">int</span></code>, so that <code class="docutils literal"><span class="pre">true</span></code>
is converted to 1 and <code class="docutils literal"><span class="pre">false</span></code> to 0.</dd>
<dt><code class="docutils literal"><span class="pre">i</span></code> (integer) [int]</dt>
<dd>Convert a JSON integer to C <code class="xref c c-type docutils literal"><span class="pre">int</span></code>.</dd>
<dt><code class="docutils literal"><span class="pre">I</span></code> (integer) [json_int_t]</dt>
<dd>Convert a JSON integer to C <a class="reference internal" href="#c.json_int_t" title="json_int_t"><code class="xref c c-type docutils literal"><span class="pre">json_int_t</span></code></a>.</dd>
<dt><code class="docutils literal"><span class="pre">f</span></code> (real) [double]</dt>
<dd>Convert a JSON real to C <code class="xref c c-type docutils literal"><span class="pre">double</span></code>.</dd>
<dt><code class="docutils literal"><span class="pre">F</span></code> (integer or real) [double]</dt>
<dd>Convert a JSON number (integer or real) to C <code class="xref c c-type docutils literal"><span class="pre">double</span></code>.</dd>
<dt><code class="docutils literal"><span class="pre">o</span></code> (any value) [json_t *]</dt>
<dd>Store a JSON value with no conversion to a <a class="reference internal" href="#c.json_t" title="json_t"><code class="xref c c-type docutils literal"><span class="pre">json_t</span></code></a> pointer.</dd>
<dt><code class="docutils literal"><span class="pre">O</span></code> (any value) [json_t *]</dt>
<dd>Like <code class="docutils literal"><span class="pre">o</span></code>, but the JSON value’s reference count is incremented.
Storage pointers should be initialized NULL before using unpack.
The caller is responsible for releasing all references incremented
by unpack, even when an error occurs.</dd>
<dt><code class="docutils literal"><span class="pre">[fmt]</span></code> (array)</dt>
<dd>Convert each item in the JSON array according to the inner format
string. <code class="docutils literal"><span class="pre">fmt</span></code> may contain objects and arrays, i.e. recursive
value extraction is supported.</dd>
<dt><code class="docutils literal"><span class="pre">{fmt}</span></code> (object)</dt>
<dd><p class="first">Convert each item in the JSON object according to the inner format
string <code class="docutils literal"><span class="pre">fmt</span></code>. The first, third, etc. format specifier represent
a key, and must be <code class="docutils literal"><span class="pre">s</span></code>. The corresponding argument to unpack
functions is read as the object key. The second fourth, etc.
format specifier represent a value and is written to the address
given as the corresponding argument. <strong>Note</strong> that every other
argument is read from and every other is written to.</p>
<p><code class="docutils literal"><span class="pre">fmt</span></code> may contain objects and arrays as values, i.e. recursive
value extraction is supported.</p>
<div class="last versionadded">
<p><span class="versionmodified">New in version 2.3: </span>Any <code class="docutils literal"><span class="pre">s</span></code> representing a key may be suffixed with a <code class="docutils literal"><span class="pre">?</span></code> to
make the key optional. If the key is not found, nothing is
extracted. See below for an example.</p>
</div>
</dd>
<dt><code class="docutils literal"><span class="pre">!</span></code></dt>
<dd>This special format specifier is used to enable the check that
all object and array items are accessed, on a per-value basis. It
must appear inside an array or object as the last format specifier
before the closing bracket or brace. To enable the check globally,
use the <code class="docutils literal"><span class="pre">JSON_STRICT</span></code> unpacking flag.</dd>
<dt><code class="docutils literal"><span class="pre">*</span></code></dt>
<dd>This special format specifier is the opposite of <code class="docutils literal"><span class="pre">!</span></code>. If the
<code class="docutils literal"><span class="pre">JSON_STRICT</span></code> flag is used, <code class="docutils literal"><span class="pre">*</span></code> can be used to disable the
strict check on a per-value basis. It must appear inside an array
or object as the last format specifier before the closing bracket
or brace.</dd>
</dl>
<p>Whitespace, <code class="docutils literal"><span class="pre">:</span></code> and <code class="docutils literal"><span class="pre">,</span></code> are ignored.</p>
<dl class="function">
<dt id="c.json_unpack">
int <code class="descname">json_unpack</code><span class="sig-paren">(</span><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *root</em>, const char<em> *fmt</em>, ...<span class="sig-paren">)</span><a class="headerlink" href="#c.json_unpack" title="Permalink to this definition">¶</a></dt>
<dd><p>Validate and unpack the JSON value <em>root</em> according to the format
string <em>fmt</em>. Returns 0 on success and -1 on failure.</p>
</dd></dl>
<dl class="function">
<dt id="c.json_unpack_ex">
int <code class="descname">json_unpack_ex</code><span class="sig-paren">(</span><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *root</em>, <a class="reference internal" href="#c.json_error_t" title="json_error_t">json_error_t</a><em> *error</em>, size_t<em> flags</em>, const char<em> *fmt</em>, ...<span class="sig-paren">)</span><a class="headerlink" href="#c.json_unpack_ex" title="Permalink to this definition">¶</a></dt>
<dt id="c.json_vunpack_ex">
int <code class="descname">json_vunpack_ex</code><span class="sig-paren">(</span><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *root</em>, <a class="reference internal" href="#c.json_error_t" title="json_error_t">json_error_t</a><em> *error</em>, size_t<em> flags</em>, const char<em> *fmt</em>, va_list<em> ap</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_vunpack_ex" title="Permalink to this definition">¶</a></dt>
<dd><p>Validate and unpack the JSON value <em>root</em> according to the format
string <em>fmt</em>. If an error occurs and <em>error</em> is not <em>NULL</em>, write
error information to <em>error</em>. <em>flags</em> can be used to control the
behaviour of the unpacker, see below for the flags. Returns 0 on
success and -1 on failure.</p>
</dd></dl>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p>The first argument of all unpack functions is <code class="docutils literal"><span class="pre">json_t</span> <span class="pre">*root</span></code>
instead of <code class="docutils literal"><span class="pre">const</span> <span class="pre">json_t</span> <span class="pre">*root</span></code>, because the use of <code class="docutils literal"><span class="pre">O</span></code> format
specifier causes the reference count of <code class="docutils literal"><span class="pre">root</span></code>, or some value
reachable from <code class="docutils literal"><span class="pre">root</span></code>, to be increased. Furthermore, the <code class="docutils literal"><span class="pre">o</span></code>
format specifier may be used to extract a value as-is, which allows
modifying the structure or contents of a value reachable from
<code class="docutils literal"><span class="pre">root</span></code>.</p>
<p class="last">If the <code class="docutils literal"><span class="pre">O</span></code> and <code class="docutils literal"><span class="pre">o</span></code> format specifiers are not used, it’s
perfectly safe to cast a <code class="docutils literal"><span class="pre">const</span> <span class="pre">json_t</span> <span class="pre">*</span></code> variable to plain
<code class="docutils literal"><span class="pre">json_t</span> <span class="pre">*</span></code> when used with these functions.</p>
</div>
<p>The following unpacking flags are available:</p>
<dl class="docutils">
<dt><code class="docutils literal"><span class="pre">JSON_STRICT</span></code></dt>
<dd>Enable the extra validation step checking that all object and
array items are unpacked. This is equivalent to appending the
format specifier <code class="docutils literal"><span class="pre">!</span></code> to the end of every array and object in the
format string.</dd>
<dt><code class="docutils literal"><span class="pre">JSON_VALIDATE_ONLY</span></code></dt>
<dd>Don’t extract any data, just validate the JSON value against the
given format string. Note that object keys must still be specified
after the format string.</dd>
</dl>
<p>Examples:</p>
<div class="highlight-c"><div class="highlight"><pre><span></span><span class="cm">/* root is the JSON integer 42 */</span>
<span class="kt">int</span> <span class="n">myint</span><span class="p">;</span>
<span class="n">json_unpack</span><span class="p">(</span><span class="n">root</span><span class="p">,</span> <span class="s">"i"</span><span class="p">,</span> <span class="o">&</span><span class="n">myint</span><span class="p">);</span>
<span class="n">assert</span><span class="p">(</span><span class="n">myint</span> <span class="o">==</span> <span class="mi">42</span><span class="p">);</span>
<span class="cm">/* root is the JSON object {"foo": "bar", "quux": true} */</span>
<span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">str</span><span class="p">;</span>
<span class="kt">int</span> <span class="n">boolean</span><span class="p">;</span>
<span class="n">json_unpack</span><span class="p">(</span><span class="n">root</span><span class="p">,</span> <span class="s">"{s:s, s:b}"</span><span class="p">,</span> <span class="s">"foo"</span><span class="p">,</span> <span class="o">&</span><span class="n">str</span><span class="p">,</span> <span class="s">"quux"</span><span class="p">,</span> <span class="o">&</span><span class="n">boolean</span><span class="p">);</span>
<span class="n">assert</span><span class="p">(</span><span class="n">strcmp</span><span class="p">(</span><span class="n">str</span><span class="p">,</span> <span class="s">"bar"</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span> <span class="o">&&</span> <span class="n">boolean</span> <span class="o">==</span> <span class="mi">1</span><span class="p">);</span>
<span class="cm">/* root is the JSON array [[1, 2], {"baz": null} */</span>
<span class="n">json_error_t</span> <span class="n">error</span><span class="p">;</span>
<span class="n">json_unpack_ex</span><span class="p">(</span><span class="n">root</span><span class="p">,</span> <span class="o">&</span><span class="n">error</span><span class="p">,</span> <span class="n">JSON_VALIDATE_ONLY</span><span class="p">,</span> <span class="s">"[[i,i], {s:n}]"</span><span class="p">,</span> <span class="s">"baz"</span><span class="p">);</span>
<span class="cm">/* returns 0 for validation success, nothing is extracted */</span>
<span class="cm">/* root is the JSON array [1, 2, 3, 4, 5] */</span>
<span class="kt">int</span> <span class="n">myint1</span><span class="p">,</span> <span class="n">myint2</span><span class="p">;</span>
<span class="n">json_unpack</span><span class="p">(</span><span class="n">root</span><span class="p">,</span> <span class="s">"[ii!]"</span><span class="p">,</span> <span class="o">&</span><span class="n">myint1</span><span class="p">,</span> <span class="o">&</span><span class="n">myint2</span><span class="p">);</span>
<span class="cm">/* returns -1 for failed validation */</span>
<span class="cm">/* root is an empty JSON object */</span>
<span class="kt">int</span> <span class="n">myint</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="n">myint2</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="n">myint3</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
<span class="n">json_unpack</span><span class="p">(</span><span class="n">root</span><span class="p">,</span> <span class="s">"{s?i, s?[ii]}"</span><span class="p">,</span>
<span class="s">"foo"</span><span class="p">,</span> <span class="o">&</span><span class="n">myint1</span><span class="p">,</span>
<span class="s">"bar"</span><span class="p">,</span> <span class="o">&</span><span class="n">myint2</span><span class="p">,</span> <span class="o">&</span><span class="n">myint3</span><span class="p">);</span>
<span class="cm">/* myint1, myint2 or myint3 is no touched as "foo" and "bar" don't exist */</span>
</pre></div>
</div>
</div>
<div class="section" id="equality">
<h2>Equality<a class="headerlink" href="#equality" title="Permalink to this headline">¶</a></h2>
<p>Testing for equality of two JSON values cannot, in general, be
achieved using the <code class="docutils literal"><span class="pre">==</span></code> operator. Equality in the terms of the
<code class="docutils literal"><span class="pre">==</span></code> operator states that the two <a class="reference internal" href="#c.json_t" title="json_t"><code class="xref c c-type docutils literal"><span class="pre">json_t</span></code></a> pointers point to
exactly the same JSON value. However, two JSON values can be equal not
only if they are exactly the same value, but also if they have equal
“contents”:</p>
<ul class="simple">
<li>Two integer or real values are equal if their contained numeric
values are equal. An integer value is never equal to a real value,
though.</li>
<li>Two strings are equal if their contained UTF-8 strings are equal,
byte by byte. Unicode comparison algorithms are not implemented.</li>
<li>Two arrays are equal if they have the same number of elements and
each element in the first array is equal to the corresponding
element in the second array.</li>
<li>Two objects are equal if they have exactly the same keys and the
value for each key in the first object is equal to the value of the
corresponding key in the second object.</li>
<li>Two true, false or null values have no “contents”, so they are equal
if their types are equal. (Because these values are singletons,
their equality can actually be tested with <code class="docutils literal"><span class="pre">==</span></code>.)</li>
</ul>
<dl class="function">
<dt id="c.json_equal">
int <code class="descname">json_equal</code><span class="sig-paren">(</span><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *value1</em>, <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *value2</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_equal" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns 1 if <em>value1</em> and <em>value2</em> are equal, as defined above.
Returns 0 if they are unequal or one or both of the pointers are
<em>NULL</em>.</p>
</dd></dl>
</div>
<div class="section" id="copying">
<h2>Copying<a class="headerlink" href="#copying" title="Permalink to this headline">¶</a></h2>
<p>Because of reference counting, passing JSON values around doesn’t
require copying them. But sometimes a fresh copy of a JSON value is
needed. For example, if you need to modify an array, but still want to
use the original afterwards, you should take a copy of it first.</p>
<p>Jansson supports two kinds of copying: shallow and deep. There is a
difference between these methods only for arrays and objects. Shallow
copying only copies the first level value (array or object) and uses
the same child values in the copied value. Deep copying makes a fresh
copy of the child values, too. Moreover, all the child values are deep
copied in a recursive fashion.</p>
<p>Copying objects preserves the insertion order of keys.</p>
<dl class="function">
<dt id="c.json_copy">
<a class="reference internal" href="#c.json_t" title="json_t">json_t</a> *<code class="descname">json_copy</code><span class="sig-paren">(</span><a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *value</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_copy" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Returns a shallow copy of <em>value</em>, or <em>NULL</em> on error.</p>
</dd></dl>
<dl class="function">
<dt id="c.json_deep_copy">
<a class="reference internal" href="#c.json_t" title="json_t">json_t</a> *<code class="descname">json_deep_copy</code><span class="sig-paren">(</span>const <a class="reference internal" href="#c.json_t" title="json_t">json_t</a><em> *value</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_deep_copy" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Returns a deep copy of <em>value</em>, or <em>NULL</em> on error.</p>
</dd></dl>
</div>
<div class="section" id="custom-memory-allocation">
<span id="apiref-custom-memory-allocation"></span><h2>Custom Memory Allocation<a class="headerlink" href="#custom-memory-allocation" title="Permalink to this headline">¶</a></h2>
<p>By default, Jansson uses <code class="xref c c-func docutils literal"><span class="pre">malloc()</span></code> and <code class="xref c c-func docutils literal"><span class="pre">free()</span></code> for
memory allocation. These functions can be overridden if custom
behavior is needed.</p>
<dl class="type">
<dt id="c.json_malloc_t">
<code class="descname">json_malloc_t</code><a class="headerlink" href="#c.json_malloc_t" title="Permalink to this definition">¶</a></dt>
<dd><p>A typedef for a function pointer with <code class="xref c c-func docutils literal"><span class="pre">malloc()</span></code>’s
signature:</p>
<div class="highlight-c"><div class="highlight"><pre><span></span><span class="k">typedef</span> <span class="kt">void</span> <span class="o">*</span><span class="p">(</span><span class="o">*</span><span class="n">json_malloc_t</span><span class="p">)(</span><span class="kt">size_t</span><span class="p">);</span>
</pre></div>
</div>
</dd></dl>
<dl class="type">
<dt id="c.json_free_t">
<code class="descname">json_free_t</code><a class="headerlink" href="#c.json_free_t" title="Permalink to this definition">¶</a></dt>
<dd><p>A typedef for a function pointer with <code class="xref c c-func docutils literal"><span class="pre">free()</span></code>’s
signature:</p>
<div class="highlight-c"><div class="highlight"><pre><span></span><span class="k">typedef</span> <span class="nf">void</span> <span class="p">(</span><span class="o">*</span><span class="n">json_free_t</span><span class="p">)(</span><span class="kt">void</span> <span class="o">*</span><span class="p">);</span>
</pre></div>
</div>
</dd></dl>
<dl class="function">
<dt id="c.json_set_alloc_funcs">
void <code class="descname">json_set_alloc_funcs</code><span class="sig-paren">(</span><a class="reference internal" href="#c.json_malloc_t" title="json_malloc_t">json_malloc_t</a><em> malloc_fn</em>, <a class="reference internal" href="#c.json_free_t" title="json_free_t">json_free_t</a><em> free_fn</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_set_alloc_funcs" title="Permalink to this definition">¶</a></dt>
<dd><p>Use <em>malloc_fn</em> instead of <code class="xref c c-func docutils literal"><span class="pre">malloc()</span></code> and <em>free_fn</em> instead
of <code class="xref c c-func docutils literal"><span class="pre">free()</span></code>. This function has to be called before any other
Jansson’s API functions to ensure that all memory operations use
the same functions.</p>
</dd></dl>
<dl class="function">
<dt id="c.json_get_alloc_funcs">
void <code class="descname">json_get_alloc_funcs</code><span class="sig-paren">(</span><a class="reference internal" href="#c.json_malloc_t" title="json_malloc_t">json_malloc_t</a><em> *malloc_fn</em>, <a class="reference internal" href="#c.json_free_t" title="json_free_t">json_free_t</a><em> *free_fn</em><span class="sig-paren">)</span><a class="headerlink" href="#c.json_get_alloc_funcs" title="Permalink to this definition">¶</a></dt>
<dd><p>Fetch the current malloc_fn and free_fn used. Either parameter
may be NULL.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.8.</span></p>
</div>
</dd></dl>
<p><strong>Examples:</strong></p>
<p>Circumvent problems with different CRT heaps on Windows by using
application’s <code class="xref c c-func docutils literal"><span class="pre">malloc()</span></code> and <code class="xref c c-func docutils literal"><span class="pre">free()</span></code>:</p>
<div class="highlight-c"><div class="highlight"><pre><span></span><span class="n">json_set_alloc_funcs</span><span class="p">(</span><span class="n">malloc</span><span class="p">,</span> <span class="n">free</span><span class="p">);</span>
</pre></div>
</div>
<p>Use the <a class="reference external" href="http://www.hboehm.info/gc/">Boehm’s conservative garbage collector</a> for memory
operations:</p>
<div class="highlight-c"><div class="highlight"><pre><span></span><span class="n">json_set_alloc_funcs</span><span class="p">(</span><span class="n">GC_malloc</span><span class="p">,</span> <span class="n">GC_free</span><span class="p">);</span>
</pre></div>
</div>
<p>Allow storing sensitive data (e.g. passwords or encryption keys) in
JSON structures by zeroing all memory when freed:</p>
<div class="highlight-c"><div class="highlight"><pre><span></span><span class="k">static</span> <span class="kt">void</span> <span class="o">*</span><span class="nf">secure_malloc</span><span class="p">(</span><span class="kt">size_t</span> <span class="n">size</span><span class="p">)</span>
<span class="p">{</span>
<span class="cm">/* Store the memory area size in the beginning of the block */</span>
<span class="kt">void</span> <span class="o">*</span><span class="n">ptr</span> <span class="o">=</span> <span class="n">malloc</span><span class="p">(</span><span class="n">size</span> <span class="o">+</span> <span class="mi">8</span><span class="p">);</span>
<span class="o">*</span><span class="p">((</span><span class="kt">size_t</span> <span class="o">*</span><span class="p">)</span><span class="n">ptr</span><span class="p">)</span> <span class="o">=</span> <span class="n">size</span><span class="p">;</span>
<span class="k">return</span> <span class="n">ptr</span> <span class="o">+</span> <span class="mi">8</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">static</span> <span class="kt">void</span> <span class="nf">secure_free</span><span class="p">(</span><span class="kt">void</span> <span class="o">*</span><span class="n">ptr</span><span class="p">)</span>
<span class="p">{</span>
<span class="kt">size_t</span> <span class="n">size</span><span class="p">;</span>
<span class="n">ptr</span> <span class="o">-=</span> <span class="mi">8</span><span class="p">;</span>
<span class="n">size</span> <span class="o">=</span> <span class="o">*</span><span class="p">((</span><span class="kt">size_t</span> <span class="o">*</span><span class="p">)</span><span class="n">ptr</span><span class="p">);</span>
<span class="n">guaranteed_memset</span><span class="p">(</span><span class="n">ptr</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">size</span> <span class="o">+</span> <span class="mi">8</span><span class="p">);</span>
<span class="n">free</span><span class="p">(</span><span class="n">ptr</span><span class="p">);</span>
<span class="p">}</span>
<span class="kt">int</span> <span class="nf">main</span><span class="p">()</span>
<span class="p">{</span>
<span class="n">json_set_alloc_funcs</span><span class="p">(</span><span class="n">secure_malloc</span><span class="p">,</span> <span class="n">secure_free</span><span class="p">);</span>
<span class="cm">/* ... */</span>
<span class="p">}</span>
</pre></div>
</div>
<p>For more information about the issues of storing sensitive data in
memory, see
<a class="reference external" href="http://www.dwheeler.com/secure-programs/Secure-Programs-HOWTO/protect-secrets.html">http://www.dwheeler.com/secure-programs/Secure-Programs-HOWTO/protect-secrets.html</a>.
The page also explains the <code class="xref c c-func docutils literal"><span class="pre">guaranteed_memset()</span></code> function used
in the example and gives a sample implementation for it.</p>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h3><a href="index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">API Reference</a><ul>
<li><a class="reference internal" href="#preliminaries">Preliminaries</a></li>
<li><a class="reference internal" href="#library-version">Library Version</a></li>
<li><a class="reference internal" href="#value-representation">Value Representation</a><ul>
<li><a class="reference internal" href="#type">Type</a></li>
<li><a class="reference internal" href="#reference-count">Reference Count</a></li>
<li><a class="reference internal" href="#circular-references">Circular References</a></li>
<li><a class="reference internal" href="#scope-dereferencing">Scope Dereferencing</a></li>
</ul>
</li>
<li><a class="reference internal" href="#true-false-and-null">True, False and Null</a></li>
<li><a class="reference internal" href="#string">String</a></li>
<li><a class="reference internal" href="#number">Number</a></li>
<li><a class="reference internal" href="#array">Array</a></li>
<li><a class="reference internal" href="#object">Object</a></li>
<li><a class="reference internal" href="#error-reporting">Error reporting</a></li>
<li><a class="reference internal" href="#encoding">Encoding</a></li>
<li><a class="reference internal" href="#decoding">Decoding</a></li>
<li><a class="reference internal" href="#building-values">Building Values</a></li>
<li><a class="reference internal" href="#parsing-and-validating-values">Parsing and Validating Values</a></li>
<li><a class="reference internal" href="#equality">Equality</a></li>
<li><a class="reference internal" href="#copying">Copying</a></li>
<li><a class="reference internal" href="#custom-memory-allocation">Custom Memory Allocation</a></li>
</ul>
</li>
</ul>
<div class="relations">
<h3>Related Topics</h3>
<ul>
<li><a href="index.html">Documentation overview</a><ul>
<li>Previous: <a href="portability.html" title="previous chapter">Portability</a></li>
<li>Next: <a href="changes.html" title="next chapter">Changes in Jansson</a></li>
</ul></li>
</ul>
</div>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/apiref.rst.txt"
rel="nofollow">Show Source</a></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<div><input type="text" name="q" /></div>
<div><input type="submit" value="Go" /></div>
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="footer">
©2009-2018, Petri Lehtinen.
|
Powered by <a href="http://sphinx-doc.org/">Sphinx 1.6.6</a>
& <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.8</a>
|
<a href="_sources/apiref.rst.txt"
rel="nofollow">Page source</a>
</div>
</body>
</html>
|