<html> <head> <title> JsonCpp - JSON data format manipulation library </title> <link href="doxygen.css" rel="stylesheet" type="text/css"> <link href="tabs.css" rel="stylesheet" type="text/css"> </head> <body bgcolor="#ffffff"> <table width="100%"> <tr> <td width="40%" align="left" valign="center"> <a href="https://github.com/open-source-parsers/jsoncpp"> JsonCpp project page </a> </td> <td width="40%" align="right" valign="center"> <a href="http://open-source-parsers.github.io/jsoncpp-docs/doxygen/">JsonCpp home page</a> </td> </tr> </table> <hr> <!-- Generated by Doxygen 1.8.8 --> <div id="navrow1" class="tabs"> <ul class="tablist"> <li class="current"><a href="index.html"><span>Main Page</span></a></li> <li><a href="pages.html"><span>Related Pages</span></a></li> <li><a href="namespaces.html"><span>Namespaces</span></a></li> <li><a href="annotated.html"><span>Classes</span></a></li> <li><a href="files.html"><span>Files</span></a></li> </ul> </div> </div><!-- top --> <div class="header"> <div class="headertitle"> <div class="title">JsonCpp Documentation</div> </div> </div><!--header--> <div class="contents"> <div class="textblock"><h1><a class="anchor" id="_intro"></a> Introduction</h1> <p><a href="http://www.json.org/">JSON (JavaScript Object Notation)</a> is a lightweight data-interchange format.</p> <p>Here is an example of JSON data: </p><pre class="fragment">{ "encoding" : "UTF-8", "plug-ins" : [ "python", "c++", "ruby" ], "indent" : { "length" : 3, "use_space": true } } </pre><p> <b>JsonCpp</b> supports comments as <em>meta-data</em>: </p><div class="fragment"><div class="line"><span class="comment">// Configuration options</span></div> <div class="line">{</div> <div class="line"> <span class="comment">// Default encoding for text</span></div> <div class="line"> <span class="stringliteral">"encoding"</span> : <span class="stringliteral">"UTF-8"</span>,</div> <div class="line"> </div> <div class="line"> <span class="comment">// Plug-ins loaded at start-up</span></div> <div class="line"> <span class="stringliteral">"plug-ins"</span> : [</div> <div class="line"> <span class="stringliteral">"python"</span>,</div> <div class="line"> <span class="stringliteral">"c++"</span>, <span class="comment">// trailing comment</span></div> <div class="line"> <span class="stringliteral">"ruby"</span></div> <div class="line"> ],</div> <div class="line"> </div> <div class="line"> <span class="comment">// Tab indent size</span></div> <div class="line"> <span class="comment">// (multi-line comment)</span></div> <div class="line"> <span class="stringliteral">"indent"</span> : { <span class="comment">/*embedded comment*/</span> <span class="stringliteral">"length"</span> : 3, <span class="stringliteral">"use_space"</span>: <span class="keyword">true</span> }</div> <div class="line">}</div> </div><!-- fragment --><h1><a class="anchor" id="_features"></a> Features</h1> <ul> <li>read and write JSON document</li> <li>attach C++ style comments to element during parsing</li> <li>rewrite JSON document preserving original comments</li> </ul> <p>Notes: Comments used to be supported in JSON but were removed for portability (C like comments are not supported in Python). Since comments are useful in configuration/input file, this feature was preserved.</p> <h1><a class="anchor" id="_example"></a> Code example</h1> <div class="fragment"><div class="line"><a class="code" href="class_json_1_1_value.html">Json::Value</a> root; <span class="comment">// 'root' will contain the root value after parsing.</span></div> <div class="line">std::cin >> root;</div> <div class="line"></div> <div class="line"><span class="comment">// You can also read into a particular sub-value.</span></div> <div class="line">std::cin >> root[<span class="stringliteral">"subtree"</span>];</div> <div class="line"></div> <div class="line"><span class="comment">// Get the value of the member of root named 'encoding',</span></div> <div class="line"><span class="comment">// and return 'UTF-8' if there is no such member.</span></div> <div class="line">std::string encoding = root.<a class="code" href="class_json_1_1_value.html#a28282c9b76fa031eba7a1843c47c16fe">get</a>(<span class="stringliteral">"encoding"</span>, <span class="stringliteral">"UTF-8"</span> ).<a class="code" href="class_json_1_1_value.html#a03ee3d5df576640c93ba683f140828bd">asString</a>();</div> <div class="line"></div> <div class="line"><span class="comment">// Get the value of the member of root named 'plug-ins'; return a 'null' value if</span></div> <div class="line"><span class="comment">// there is no such member.</span></div> <div class="line"><span class="keyword">const</span> <a class="code" href="class_json_1_1_value.html">Json::Value</a> plugins = root[<span class="stringliteral">"plug-ins"</span>];</div> <div class="line"></div> <div class="line"><span class="comment">// Iterate over the sequence elements.</span></div> <div class="line"><span class="keywordflow">for</span> ( <span class="keywordtype">int</span> index = 0; index < plugins.<a class="code" href="class_json_1_1_value.html#a4ca8ee6c48a34ca6c2f131956bab5e05">size</a>(); ++index )</div> <div class="line"> loadPlugIn( plugins[index].asString() );</div> <div class="line"> </div> <div class="line"><span class="comment">// Try other datatypes. Some are auto-convertible to others.</span></div> <div class="line">foo::setIndentLength( root[<span class="stringliteral">"indent"</span>].<span class="keyword">get</span>(<span class="stringliteral">"length"</span>, 3).asInt() );</div> <div class="line">foo::setIndentUseSpace( root[<span class="stringliteral">"indent"</span>].<span class="keyword">get</span>(<span class="stringliteral">"use_space"</span>, <span class="keyword">true</span>).asBool() );</div> <div class="line"></div> <div class="line"><span class="comment">// Since Json::Value has an implicit constructor for all value types, it is not</span></div> <div class="line"><span class="comment">// necessary to explicitly construct the Json::Value object.</span></div> <div class="line">root[<span class="stringliteral">"encoding"</span>] = foo::getCurrentEncoding();</div> <div class="line">root[<span class="stringliteral">"indent"</span>][<span class="stringliteral">"length"</span>] = foo::getCurrentIndentLength();</div> <div class="line">root[<span class="stringliteral">"indent"</span>][<span class="stringliteral">"use_space"</span>] = foo::getCurrentIndentUseSpace();</div> <div class="line"></div> <div class="line"><span class="comment">// If you like the defaults, you can insert directly into a stream.</span></div> <div class="line">std::cout << root;</div> <div class="line"><span class="comment">// Of course, you can write to `std::ostringstream` if you prefer.</span></div> <div class="line"></div> <div class="line"><span class="comment">// If desired, remember to add a linefeed and flush.</span></div> <div class="line">std::cout << std::endl;</div> </div><!-- fragment --><h1><a class="anchor" id="_advanced"></a> Advanced usage</h1> <p>Configure <em>builders</em> to create <em>readers</em> and <em>writers</em>. For configuration, we use our own <code><a class="el" href="class_json_1_1_value.html" title="Represents a JSON value. ">Json::Value</a></code> (rather than standard setters/getters) so that we can add features without losing binary-compatibility.</p> <div class="fragment"><div class="line"><span class="comment">// For convenience, use `writeString()` with a specialized builder.</span></div> <div class="line"><a class="code" href="class_json_1_1_stream_writer_builder.html">Json::StreamWriterBuilder</a> wbuilder;</div> <div class="line">wbuilder[<span class="stringliteral">"indentation"</span>] = <span class="stringliteral">"\t"</span>;</div> <div class="line">std::string document = <a class="code" href="namespace_json.html#afd767fe4c7e962d0ff3d1a6d1622619f">Json::writeString</a>(wbuilder, root);</div> <div class="line"></div> <div class="line"><span class="comment">// Here, using a specialized Builder, we discard comments and</span></div> <div class="line"><span class="comment">// record errors as we parse.</span></div> <div class="line"><a class="code" href="class_json_1_1_char_reader_builder.html">Json::CharReaderBuilder</a> rbuilder;</div> <div class="line">rbuilder[<span class="stringliteral">"collectComments"</span>] = <span class="keyword">false</span>;</div> <div class="line">std::string errs;</div> <div class="line"><span class="keywordtype">bool</span> ok = <a class="code" href="namespace_json.html#acfebeaf759a841173ddce34c4da22486">Json::parseFromStream</a>(rbuilder, std::cin, &root, &errs);</div> </div><!-- fragment --><p>Yes, compile-time configuration-checking would be helpful, but <code><a class="el" href="class_json_1_1_value.html" title="Represents a JSON value. ">Json::Value</a></code> lets you write and read the builder configuration, which is better! In other words, you can configure your JSON parser using JSON.</p> <p>CharReaders and StreamWriters are not thread-safe, but they are re-usable. </p><div class="fragment"><div class="line"><a class="code" href="class_json_1_1_char_reader_builder.html">Json::CharReaderBuilder</a> rbuilder;</div> <div class="line">cfg >> rbuilder.<a class="code" href="class_json_1_1_char_reader_builder.html#ac69b7911ad64c171c51ebaf2ea26d958">settings_</a>;</div> <div class="line">std::unique_ptr<Json::CharReader> <span class="keyword">const</span> reader(rbuilder.<a class="code" href="class_json_1_1_char_reader_builder.html#a3e3c9f4aeb07023ef0c5f6255003078a">newCharReader</a>());</div> <div class="line">reader->parse(start, stop, &value1, &errs);</div> <div class="line"><span class="comment">// ...</span></div> <div class="line">reader->parse(start, stop, &value2, &errs);</div> <div class="line"><span class="comment">// etc.</span></div> </div><!-- fragment --><h1><a class="anchor" id="_pbuild"></a> Build instructions</h1> <p>The build instructions are located in the file <a href="https://github.com/open-source-parsers/jsoncpp/blob/master/README.md">README.md</a> in the top-directory of the project.</p> <p>The latest version of the source is available in the project's GitHub repository: <a href="https://github.com/open-source-parsers/jsoncpp/">jsoncpp</a></p> <h1><a class="anchor" id="_news"></a> What's New?</h1> <p>The description of latest changes can be found in <a href="https://github.com/open-source-parsers/jsoncpp/wiki/NEWS">the NEWS wiki </a>.</p> <h1><a class="anchor" id="_rlinks"></a> Related links</h1> <ul> <li><a href="http://www.json.org/">JSON</a> Specification and alternate language implementations.</li> <li><a href="http://www.yaml.org/">YAML</a> A data format designed for human readability.</li> <li><a href="http://www.cl.cam.ac.uk/~mgk25/unicode.html">UTF-8 and Unicode FAQ</a>.</li> </ul> <h1><a class="anchor" id="_plinks"></a> Old project links</h1> <ul> <li><a href="https://sourceforge.net/projects/jsoncpp/">https://sourceforge.net/projects/jsoncpp/</a></li> <li><a href="http://jsoncpp.sourceforge.net">http://jsoncpp.sourceforge.net</a></li> <li><a href="http://sourceforge.net/projects/jsoncpp/files/">http://sourceforge.net/projects/jsoncpp/files/</a></li> <li><a href="http://jsoncpp.svn.sourceforge.net/svnroot/jsoncpp/trunk/">http://jsoncpp.svn.sourceforge.net/svnroot/jsoncpp/trunk/</a></li> <li><a href="http://jsoncpp.sourceforge.net/old.html">http://jsoncpp.sourceforge.net/old.html</a></li> </ul> <h1><a class="anchor" id="_license"></a> License</h1> <p>See file <a href="https://github.com/open-source-parsers/jsoncpp/blob/master/LICENSE"><code>LICENSE</code></a> in the top-directory of the project.</p> <p>Basically JsonCpp is licensed under MIT license, or public domain if desired and recognized in your jurisdiction.</p> <dl class="section author"><dt>Author</dt><dd>Baptiste Lepilleur <a href="#" onclick="location.href='mai'+'lto:'+'ble'+'p@'+'use'+'rs'+'.so'+'ur'+'cef'+'or'+'ge.'+'ne'+'t'; return false;">blep@<span style="display: none;">.nosp@m.</span>user<span style="display: none;">.nosp@m.</span>s.sou<span style="display: none;">.nosp@m.</span>rcef<span style="display: none;">.nosp@m.</span>orge.<span style="display: none;">.nosp@m.</span>net</a> (originator) </dd> <dd> Christopher Dunn <a href="#" onclick="location.href='mai'+'lto:'+'cdu'+'nn'+'200'+'1@'+'gma'+'il'+'.co'+'m'; return false;">cdunn<span style="display: none;">.nosp@m.</span>2001<span style="display: none;">.nosp@m.</span>@gmai<span style="display: none;">.nosp@m.</span>l.co<span style="display: none;">.nosp@m.</span>m</a> (primary maintainer) </dd></dl> <dl class="section version"><dt>Version</dt><dd><div class="fragment"><div class="line">1.6.5</div> </div><!-- fragment --> We make strong guarantees about binary-compatibility, consistent with <a href="http://apr.apache.org/versioning.html">the Apache versioning scheme</a>. </dd></dl> <dl class="section see"><dt>See also</dt><dd><a class="el" href="version_8h.html">version.h</a> </dd></dl> </div></div><!-- contents --> <hr> </body> </html>