json/classnlohmann_1_1basic__json_a444d8376d63d7a5edb85f33bc5976c14.html
2015-06-22 23:22:59 +02:00

221 lines
17 KiB
HTML

<!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/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.9.1"/>
<title>JSON for Modern C++: nlohmann::basic_json::basic_json</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
$(document).ready(initResizable);
$(window).load(resizeHeight);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
$(document).ready(function() { init_search(); });
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="mylayout.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 56px;">
<td style="padding-left: 0.5em;">
<div id="projectname">JSON for Modern C++
&#160;<span id="projectnumber">3.0</span>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.9.1 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
<div id="navrow1" class="tabs">
<ul class="tablist">
<li><a href="index.html"><span>Main&#160;Page</span></a></li>
<li><a href="pages.html"><span>Related&#160;Pages</span></a></li>
<li><a href="annotated.html"><span>Classes</span></a></li>
<li>
<div id="MSearchBox" class="MSearchBoxInactive">
<span class="left">
<img id="MSearchSelect" src="search/mag_sel.png"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
alt=""/>
<input type="text" id="MSearchField" value="Search" accesskey="S"
onfocus="searchBox.OnSearchFieldFocus(true)"
onblur="searchBox.OnSearchFieldFocus(false)"
onkeyup="searchBox.OnSearchFieldChange(event)"/>
</span><span class="right">
<a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
</span>
</div>
</li>
</ul>
</div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
<div id="nav-tree">
<div id="nav-tree-contents">
<div id="nav-sync" class="sync"></div>
</div>
</div>
<div id="splitbar" style="-moz-user-select:none;"
class="ui-resizable-handle">
</div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('classnlohmann_1_1basic__json_a444d8376d63d7a5edb85f33bc5976c14.html','');});
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</div>
<div class="contents">
<a class="anchor" id="a444d8376d63d7a5edb85f33bc5976c14"></a>
<div class="memitem">
<div class="memproto">
<div class="memtemplate">
template&lt;template&lt; typename U, typename V, typename...Args &gt; class ObjectType = std::map, template&lt; typename U, typename...Args &gt; class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = int64_t, class NumberFloatType = double, template&lt; typename U &gt; class AllocatorType = std::allocator&gt; </div>
<table class="mlabels">
<tr>
<td class="mlabels-left">
<table class="memname">
<tr>
<td class="memname"><a class="el" href="classnlohmann_1_1basic__json.html">nlohmann::basic_json</a>::<a class="el" href="classnlohmann_1_1basic__json.html">basic_json</a> </td>
<td>(</td>
<td class="paramtype"><a class="el" href="classnlohmann_1_1basic__json_aa23ac5808b9aa1a08d18e3ebea25c7ce.html#aa23ac5808b9aa1a08d18e3ebea25c7ce">list_init_t</a>&#160;</td>
<td class="paramname"><em>init</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">bool&#160;</td>
<td class="paramname"><em>type_deduction</em> = <code>true</code>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype"><a class="el" href="classnlohmann_1_1basic__json_a231b02148577b69a154b2ce2c87a5522.html#a231b02148577b69a154b2ce2c87a5522">value_t</a>&#160;</td>
<td class="paramname"><em>manual_type</em> = <code><a class="el" href="classnlohmann_1_1basic__json_a231b02148577b69a154b2ce2c87a5522.html#a231b02148577b69a154b2ce2c87a5522af1f713c9e000f5d3f280adbd124df4f5">value_t::array</a></code>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</td>
<td class="mlabels-right">
<span class="mlabels"><span class="mlabel">inline</span></span> </td>
</tr>
</table>
</div><div class="memdoc">
<p>Creates a JSON value of type array or object from the passed initializer list <em>init</em>. In case <em>type_deduction</em> is <code>true</code> (default), the type of the JSON value to be created is deducted from the initializer list <em>init</em> according to the following rules:</p>
<ol type="1">
<li>If the list is empty, an empty JSON object value <code>{}</code> is created.</li>
<li>If the list consists of pairs whose first element is a string, a JSON object value is created where the first elements of the pairs are treated as keys and the second elements are as values.</li>
<li>In all other cases, an array is created.</li>
</ol>
<p>The rules aim to create the best fit between a C++ initializer list and JSON values. The ratioinale is as follows:</p>
<ol type="1">
<li>The empty initializer list is written as <code>{}</code> which is exactly an empty JSON object.</li>
<li>C++ has now way of describing mapped types other than to list a list of pairs. As JSON requires that keys must be of type string, rule 2 is the weakest constraint one can pose on initializer lists to interpret them as an object.</li>
<li>In all other cases, the initializer list could not be interpreted as JSON object type, so interpreting it as JSON array type is safe.</li>
</ol>
<p>With the rules described above, the following JSON values cannot be expressed by an initializer list:</p>
<ul>
<li>the empty array (<code>[]</code>): use <a class="el" href="classnlohmann_1_1basic__json_a53590b99e380c464a83a0a5edf6da9ab.html#a53590b99e380c464a83a0a5edf6da9ab">array(list_init_t)</a> with an empty initializer list in this case</li>
<li>arrays whose elements satisfy rule 2: use <a class="el" href="classnlohmann_1_1basic__json_a53590b99e380c464a83a0a5edf6da9ab.html#a53590b99e380c464a83a0a5edf6da9ab">array(list_init_t)</a> with the same initializer list in this case</li>
</ul>
<dl class="section note"><dt>Note</dt><dd>When used without parentheses around an empty initializer list, <a class="el" href="classnlohmann_1_1basic__json_a5a2d4dbcdbc8ea065b87daf52cd786b9.html#a5a2d4dbcdbc8ea065b87daf52cd786b9">basic_json()</a> is called instead of this function, yielding the JSON null value.</dd></dl>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramdir">[in]</td><td class="paramname">init</td><td>initializer list with JSON values</td></tr>
<tr><td class="paramdir">[in]</td><td class="paramname">type_deduction</td><td>internal parameter; when set to <code>true</code>, the type of the JSON value is deducted from the initializer list <em>init</em>; when set to <code>false</code>, the type provided via <em>manual_type</em> is forced. This mode is used by the functions <a class="el" href="classnlohmann_1_1basic__json_a53590b99e380c464a83a0a5edf6da9ab.html#a53590b99e380c464a83a0a5edf6da9ab">array(list_init_t)</a> and <a class="el" href="classnlohmann_1_1basic__json_afb9664e6f30038e691f2e864e7f6a740.html#afb9664e6f30038e691f2e864e7f6a740">object(list_init_t)</a>.</td></tr>
<tr><td class="paramdir">[in]</td><td class="paramname">manual_type</td><td>internal parameter; when <em>type_deduction</em> is set to <code>false</code>, the created JSON value will use the provided type (only <a class="el" href="classnlohmann_1_1basic__json_a231b02148577b69a154b2ce2c87a5522.html#a231b02148577b69a154b2ce2c87a5522af1f713c9e000f5d3f280adbd124df4f5">value_t::array</a> and <a class="el" href="classnlohmann_1_1basic__json_a231b02148577b69a154b2ce2c87a5522.html#a231b02148577b69a154b2ce2c87a5522aa8cfde6331bd59eb2ac96f8911c4b666">value_t::object</a> are valid); when <em>type_deduction</em> is set to <code>true</code>, this parameter has no effect</td></tr>
</table>
</dd>
</dl>
<dl class="exception"><dt>Exceptions</dt><dd>
<table class="exception">
<tr><td class="paramname">std::domain_error</td><td>if <em>type_deduction</em> is <code>false</code>, <em>manual_type</em> is <code><a class="el" href="classnlohmann_1_1basic__json_a231b02148577b69a154b2ce2c87a5522.html#a231b02148577b69a154b2ce2c87a5522aa8cfde6331bd59eb2ac96f8911c4b666" title="object (unordered set of name/value pairs) ">value_t::object</a></code>, but <em>init</em> contains an element which is not a pair whose first element is a string</td></tr>
</table>
</dd>
</dl>
<dl class="section user"><dt>Complexity</dt><dd>Linear in the size of the initializer list <em>init</em>.</dd></dl>
<dl class="section user"><dt>Example</dt><dd>The example below shows how JSON values are created from initializer lists <div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno"> 1</span>&#160;<span class="preprocessor">#include &lt;json.hpp&gt;</span></div>
<div class="line"><a name="l00002"></a><span class="lineno"> 2</span>&#160;</div>
<div class="line"><a name="l00003"></a><span class="lineno"> 3</span>&#160;<span class="keyword">using namespace </span><a class="code" href="namespacenlohmann.html">nlohmann</a>;</div>
<div class="line"><a name="l00004"></a><span class="lineno"> 4</span>&#160;</div>
<div class="line"><a name="l00005"></a><span class="lineno"> 5</span>&#160;<span class="keywordtype">int</span> main()</div>
<div class="line"><a name="l00006"></a><span class="lineno"> 6</span>&#160;{</div>
<div class="line"><a name="l00007"></a><span class="lineno"> 7</span>&#160; <span class="comment">// create JSON values</span></div>
<div class="line"><a name="l00008"></a><span class="lineno"> 8</span>&#160; <a class="code" href="classnlohmann_1_1basic__json.html">json</a> j_empty_init_list = <a class="code" href="namespacenlohmann_a2bfd99e845a2e5cd90aeaf1b1431f474.html#a2bfd99e845a2e5cd90aeaf1b1431f474">json</a>({});</div>
<div class="line"><a name="l00009"></a><span class="lineno"> 9</span>&#160; <a class="code" href="classnlohmann_1_1basic__json.html">json</a> j_object = { {<span class="stringliteral">&quot;one&quot;</span>, 1}, {<span class="stringliteral">&quot;two&quot;</span>, 2} };</div>
<div class="line"><a name="l00010"></a><span class="lineno"> 10</span>&#160; <a class="code" href="classnlohmann_1_1basic__json.html">json</a> j_array = {1, 2, 3, 4};</div>
<div class="line"><a name="l00011"></a><span class="lineno"> 11</span>&#160; <a class="code" href="classnlohmann_1_1basic__json.html">json</a> j_nested_object = { {<span class="stringliteral">&quot;one&quot;</span>, {1}}, {<span class="stringliteral">&quot;two&quot;</span>, {1, 2}} };</div>
<div class="line"><a name="l00012"></a><span class="lineno"> 12</span>&#160; <a class="code" href="classnlohmann_1_1basic__json.html">json</a> j_nested_array = { {{1}, <span class="stringliteral">&quot;one&quot;</span>}, {{1, 2}, <span class="stringliteral">&quot;two&quot;</span>} };</div>
<div class="line"><a name="l00013"></a><span class="lineno"> 13</span>&#160;</div>
<div class="line"><a name="l00014"></a><span class="lineno"> 14</span>&#160; <span class="comment">// serialize the JSON value</span></div>
<div class="line"><a name="l00015"></a><span class="lineno"> 15</span>&#160; std::cout &lt;&lt; j_empty_init_list &lt;&lt; <span class="charliteral">&#39;\n&#39;</span>;</div>
<div class="line"><a name="l00016"></a><span class="lineno"> 16</span>&#160; std::cout &lt;&lt; j_object &lt;&lt; <span class="charliteral">&#39;\n&#39;</span>;</div>
<div class="line"><a name="l00017"></a><span class="lineno"> 17</span>&#160; std::cout &lt;&lt; j_array &lt;&lt; <span class="charliteral">&#39;\n&#39;</span>;</div>
<div class="line"><a name="l00018"></a><span class="lineno"> 18</span>&#160; std::cout &lt;&lt; j_nested_object &lt;&lt; <span class="charliteral">&#39;\n&#39;</span>;</div>
<div class="line"><a name="l00019"></a><span class="lineno"> 19</span>&#160; std::cout &lt;&lt; j_nested_array &lt;&lt; <span class="charliteral">&#39;\n&#39;</span>;</div>
<div class="line"><a name="l00020"></a><span class="lineno"> 20</span>&#160;}</div>
<div class="ttc" id="namespacenlohmann_a2bfd99e845a2e5cd90aeaf1b1431f474_html_a2bfd99e845a2e5cd90aeaf1b1431f474"><div class="ttname"><a href="namespacenlohmann_a2bfd99e845a2e5cd90aeaf1b1431f474.html#a2bfd99e845a2e5cd90aeaf1b1431f474">nlohmann::json</a></div><div class="ttdeci">basic_json&lt;&gt; json</div><div class="ttdoc">default JSON class </div><div class="ttdef"><b>Definition:</b> json.hpp:5979</div></div>
<div class="ttc" id="classnlohmann_1_1basic__json_html"><div class="ttname"><a href="classnlohmann_1_1basic__json.html">nlohmann::basic_json</a></div><div class="ttdoc">a class to store JSON values </div><div class="ttdef"><b>Definition:</b> json.hpp:121</div></div>
<div class="ttc" id="namespacenlohmann_html"><div class="ttname"><a href="namespacenlohmann.html">nlohmann</a></div><div class="ttdoc">namespace for Niels Lohmann </div><div class="ttdef"><b>Definition:</b> json.hpp:56</div></div>
</div><!-- fragment --> Output: <pre class="fragment">{}
{"one":1,"two":2}
[1,2,3,4]
{"one":[1],"two":[1,2]}
[[[1],"one"],[[1,2],"two"]]
</pre> The example code above can be translated with<pre class="fragment">g++ -std=c++11 -Isrc doc/examples/basic_json__list_init_t.cpp -o basic_json__list_init_t </pre>.</dd></dl>
<dl class="section see"><dt>See also</dt><dd><a class="el" href="classnlohmann_1_1basic__json.html" title="a class to store JSON values ">basic_json</a> <a class="el" href="classnlohmann_1_1basic__json_a53590b99e380c464a83a0a5edf6da9ab.html#a53590b99e380c464a83a0a5edf6da9ab" title="explicitly create an array from an initializer list ">array(list_init_t)</a> - create a JSON <a class="el" href="classnlohmann_1_1basic__json_a53590b99e380c464a83a0a5edf6da9ab.html#a53590b99e380c464a83a0a5edf6da9ab" title="explicitly create an array from an initializer list ">array</a> value from an initializer list </dd>
<dd>
<a class="el" href="classnlohmann_1_1basic__json.html" title="a class to store JSON values ">basic_json</a> <a class="el" href="classnlohmann_1_1basic__json_afb9664e6f30038e691f2e864e7f6a740.html#afb9664e6f30038e691f2e864e7f6a740" title="explicitly create an object from an initializer list ">object(list_init_t)</a> - create a JSON <a class="el" href="classnlohmann_1_1basic__json_afb9664e6f30038e691f2e864e7f6a740.html#afb9664e6f30038e691f2e864e7f6a740" title="explicitly create an object from an initializer list ">object</a> value from an initializer list </dd></dl>
</div>
</div>
</div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<li class="navelem"><a class="el" href="namespacenlohmann.html">nlohmann</a></li><li class="navelem"><a class="el" href="classnlohmann_1_1basic__json.html">basic_json</a></li>
<li class="footer">Generated on Mon Jun 22 2015 23:22:00 for JSON for Modern C++ by
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.9.1 </li>
</ul>
</div>
</body>
</html>