template<template< typename U, typename V, typename... Args > class ObjectType = std::map, template< typename U, typename... Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer></div>
<divclass="memtemplate">
template<typename CompatibleType , typename U = detail::uncvref_t<CompatibleType>, detail::enable_if_t< not std::is_base_of< std::istream, U >::value and not std::is_same< U, basic_json_t >::value and not detail::is_basic_json_nested_type< basic_json_t, U >::value and detail::has_to_json< basic_json, U >::value, int > = 0></div>
<p>This is a "catch all" constructor for all compatible JSON types; that is, types for which a <code>to_json()</code> method exsits. The constructor forwards the parameter <em>val</em> to that method (to <code>json_serializer<U>to_json</code> method with <code>U = uncvref_t<CompatibleType></code>, to be exact).</p>
<p>Template type <em>CompatibleType</em> includes, but is not limited to, the following types:</p><ul>
<li><b>arrays</b>: <aclass="el"href="classnlohmann_1_1basic__json_ae095578e03df97c5b3991787f1056374.html#ae095578e03df97c5b3991787f1056374">array_t</a> and all kinds of compatible containers such as <code>std::vector</code>, <code>std::deque</code>, <code>std::list</code>, <code>std::forward_list</code>, <code>std::array</code>, <code>std::set</code>, <code>std::unordered_set</code>, <code>std::multiset</code>, and <code>unordered_multiset</code> with a <code>value_type</code> from which a <aclass="el"href="classnlohmann_1_1basic__json.html">basic_json</a> value can be constructed.</li>
<li><b>objects</b>: <aclass="el"href="classnlohmann_1_1basic__json_aa1eb13d5aa86f80cbee6c58e90fbaf49.html#aa1eb13d5aa86f80cbee6c58e90fbaf49">object_t</a> and all kinds of compatible associative containers such as <code>std::map</code>, <code>std::unordered_map</code>, <code>std::multimap</code>, and <code>std::unordered_multimap</code> with a <code>key_type</code> compatible to <aclass="el"href="classnlohmann_1_1basic__json_a61f8566a1a85a424c7266fb531dca005.html#a61f8566a1a85a424c7266fb531dca005">string_t</a> and a <code>value_type</code> from which a <aclass="el"href="classnlohmann_1_1basic__json.html">basic_json</a> value can be constructed.</li>
<li><b>strings</b>: <aclass="el"href="classnlohmann_1_1basic__json_a61f8566a1a85a424c7266fb531dca005.html#a61f8566a1a85a424c7266fb531dca005">string_t</a>, string literals, and all compatible string containers can be used.</li>
<li><b>numbers</b>: <aclass="el"href="classnlohmann_1_1basic__json_a98e611d67b7bd75307de99c9358ab2dc.html#a98e611d67b7bd75307de99c9358ab2dc">number_integer_t</a>, <aclass="el"href="classnlohmann_1_1basic__json_ab906e29b5d83ac162e823ada2156b989.html#ab906e29b5d83ac162e823ada2156b989">number_unsigned_t</a>, <aclass="el"href="classnlohmann_1_1basic__json_a88d6103cb3620410b35200ee8e313d97.html#a88d6103cb3620410b35200ee8e313d97">number_float_t</a>, and all convertible number types such as <code>int</code>, <code>size_t</code>, <code>int64_t</code>, <code>float</code> or <code>double</code> can be used.</li>
<li><b>boolean</b>: <aclass="el"href="classnlohmann_1_1basic__json_a4c919102a9b4fe0d588af64801436082.html#a4c919102a9b4fe0d588af64801436082">boolean_t</a> / <code>bool</code> can be used.</li>
<tr><tdclass="paramname">CompatibleType</td><td>a type such that:<ul>
<li><em>CompatibleType</em> is not derived from <code>std::istream</code>,</li>
<li><em>CompatibleType</em> is not <aclass="el"href="classnlohmann_1_1basic__json.html">basic_json</a> (to avoid hijacking copy/move constructors),</li>
<li><em>CompatibleType</em> is not a <aclass="el"href="classnlohmann_1_1basic__json.html">basic_json</a> nested type (e.g., <aclass="el"href="classnlohmann_1_1basic__json_1_1json__pointer.html">json_pointer</a>, <aclass="el"href="classnlohmann_1_1basic__json_a099316232c76c034030a38faa6e34dca.html#a099316232c76c034030a38faa6e34dca">iterator</a>, etc ...)</li>
<li><aclass="el"href="classnlohmann_1_1basic__json_a7768841baaaa7a21098a401c932efaff.html#a7768841baaaa7a21098a401c932efaff">json_serializer<U></a> has a <code>to_json(basic_json_t&, CompatibleType&&)</code> method</li>
<tr><tdclass="paramdir">[in]</td><tdclass="paramname">val</td><td>the value to be forwarded</td></tr>
</table>
</dd>
</dl>
<dlclass="section user"><dt>Complexity</dt><dd>Usually linear in the size of the passed <em>val</em>, also depending on the implementation of the called <code>to_json()</code> method.</dd></dl>
<dlclass="section user"><dt>Example</dt><dd>The following code shows the constructor with several compatible types. <divclass="fragment"><divclass="line"><aname="l00001"></a><spanclass="lineno"> 1</span> <spanclass="preprocessor">#include <json.hpp></span></div><divclass="line"><aname="l00002"></a><spanclass="lineno"> 2</span> <spanclass="preprocessor">#include <deque></span></div><divclass="line"><aname="l00003"></a><spanclass="lineno"> 3</span> <spanclass="preprocessor">#include <list></span></div><divclass="line"><aname="l00004"></a><spanclass="lineno"> 4</span> <spanclass="preprocessor">#include <forward_list></span></div><divclass="line"><aname="l00005"></a><spanclass="lineno"> 5</span> <spanclass="preprocessor">#include <set></span></div><divclass="line"><aname="l00006"></a><spanclass="lineno"> 6</span> <spanclass="preprocessor">#include <unordered_map></span></div><divclass="line"><aname="l00007"></a><spanclass="lineno"> 7</span> <spanclass="preprocessor">#include <unordered_set></span></div><divclass="line"><aname="l00008"></a><spanclass="lineno"> 8</span> </div><divclass="line"><aname="l00009"></a><spanclass="lineno"> 9</span> <spanclass="keyword">using</span><aclass="code"href="namespacenlohmann_a2bfd99e845a2e5cd90aeaf1b1431f474.html#a2bfd99e845a2e5cd90aeaf1b1431f474">json</a> = <aclass="code"href="namespacenlohmann_a2bfd99e845a2e5cd90aeaf1b1431f474.html#a2bfd99e845a2e5cd90aeaf1b1431f474">nlohmann::json</a>;</div><divclass="line"><aname="l00010"></a><spanclass="lineno"> 10</span> </div><divclass="line"><aname="l00011"></a><spanclass="lineno"> 11</span> <spanclass="keywordtype">int</span> main()</div><divclass="line"><aname="l00012"></a><spanclass="lineno"> 12</span> {</div><divclass="line"><aname="l00013"></a><spanclass="lineno"> 13</span> <spanclass="comment">// ============</span></div><divclass="line"><aname="l00014"></a><spanclass="lineno"> 14</span> <spanclass="comment">// object types</span></div><divclass="line"><aname="l00015"></a><spanclass="lineno"> 15</span> <spanclass="comment">// ============</span></div><divclass="line"><aname="l00016"></a><spanclass="lineno"> 16</span> </div><divclass="line"><aname="l00017"></a><spanclass="lineno"> 17</span> <spanclass="comment">// create an object from an object_t value</span></div><divclass="line"><aname="l00018"></a><spanclass="lineno"> 18</span> <aclass="code"href="classnlohmann_1_1basic__json_aa1eb13d5aa86f80cbee6c58e90fbaf49.html#aa1eb13d5aa86f80cbee6c58e90fbaf49">json::object_t</a> object_value = { {<spanclass="stringliteral">"one"</span>, 1}, {<spanclass="stringliteral">"two"</span>, 2} };</div><divclass="line"><aname="l00019"></a><spanclass="lineno"> 19</span> <aclass="code"href="namespacenlohmann_a2bfd99e845a2e5cd90aeaf1b1431f474.html#a2bfd99e845a2e5cd90aeaf1b1431f474">json</a> j_object_t(object_value);</div><divclass="line"><aname="l00020"></a><spanclass="lineno"> 20</span> </div><divclass="line"><aname="l00021"></a><spanclass="lineno"> 21</span> <spanclass="comment">// create an object from std::map</span></div><divclass="line"><aname="l00022"></a><spanclass="lineno"> 22</span>  std::map<std::string, int> c_map</div><divclass="line"><aname="l00023"></a><spanclass="lineno"> 23</span>  {</div><divclass="line"><aname="l00024"></a><spanclass="lineno"> 24</span>  {<spanclass="stringliteral">"one"</span>, 1}, {<spanclass="stringliteral">"two"</span>, 2}, {<spanclass="stringliteral">"three"</span>, 3}</div><divclass="line"><aname="l00025"></a><spanclass="lineno"> 25</span>  };</div><divclass="line"><aname="l00026"></a><spanclass="lineno"> 26</span> <aclass="code"href="namespacenlohmann_a2bfd99e845a2e5cd90aeaf1b1431f474
<divclass="ttc"id="namespacenlohmann_a2bfd99e845a2e5cd90aeaf1b1431f474_html_a2bfd99e845a2e5cd90aeaf1b1431f474"><divclass="ttname"><ahref="namespacenlohmann_a2bfd99e845a2e5cd90aeaf1b1431f474.html#a2bfd99e845a2e5cd90aeaf1b1431f474">nlohmann::json</a></div><divclass="ttdeci">basic_json<> json</div><divclass="ttdoc">default JSON class </div><divclass="ttdef"><b>Definition:</b><ahref="json_8hpp_source.html#l12916">json.hpp:12916</a></div></div>
<divclass="ttc"id="classnlohmann_1_1basic__json_aa1eb13d5aa86f80cbee6c58e90fbaf49_html_aa1eb13d5aa86f80cbee6c58e90fbaf49"><divclass="ttname"><ahref="classnlohmann_1_1basic__json_aa1eb13d5aa86f80cbee6c58e90fbaf49.html#aa1eb13d5aa86f80cbee6c58e90fbaf49">nlohmann::basic_json::object_t</a></div><divclass="ttdeci">ObjectType< StringType, basic_json, std::less< StringType >, AllocatorType< std::pair< const StringType, basic_json >>> object_t</div><divclass="ttdoc">a type for an object </div><divclass="ttdef"><b>Definition:</b><ahref="json_8hpp_source.html#l01282">json.hpp:1282</a></div></div>
<divclass="ttc"id="classnlohmann_1_1basic__json_a98e611d67b7bd75307de99c9358ab2dc_html_a98e611d67b7bd75307de99c9358ab2dc"><divclass="ttname"><ahref="classnlohmann_1_1basic__json_a98e611d67b7bd75307de99c9358ab2dc.html#a98e611d67b7bd75307de99c9358ab2dc">nlohmann::basic_json::number_integer_t</a></div><divclass="ttdeci">NumberIntegerType number_integer_t</div><divclass="ttdoc">a type for a number (integer) </div><divclass="ttdef"><b>Definition:</b><ahref="json_8hpp_source.html#l01479">json.hpp:1479</a></div></div>
<divclass="ttc"id="classnlohmann_1_1basic__json_ae095578e03df97c5b3991787f1056374_html_ae095578e03df97c5b3991787f1056374"><divclass="ttname"><ahref="classnlohmann_1_1basic__json_ae095578e03df97c5b3991787f1056374.html#ae095578e03df97c5b3991787f1056374">nlohmann::basic_json::array_t</a></div><divclass="ttdeci">ArrayType< basic_json, AllocatorType< basic_json >> array_t</div><divclass="ttdoc">a type for an array </div><divclass="ttdef"><b>Definition:</b><ahref="json_8hpp_source.html#l01328">json.hpp:1328</a></div></div>
<divclass="ttc"id="classnlohmann_1_1basic__json_a61f8566a1a85a424c7266fb531dca005_html_a61f8566a1a85a424c7266fb531dca005"><divclass="ttname"><ahref="classnlohmann_1_1basic__json_a61f8566a1a85a424c7266fb531dca005.html#a61f8566a1a85a424c7266fb531dca005">nlohmann::basic_json::string_t</a></div><divclass="ttdeci">StringType string_t</div><divclass="ttdoc">a type for a string </div><divclass="ttdef"><b>Definition:</b><ahref="json_8hpp_source.html#l01381">json.hpp:1381</a></div></div>
</div><!-- fragment --> Output (play with this example <atarget="_blank"href="http://melpon.org/wandbox/permlink/VM7W2kpE7sIYJ5DW"><b>online</b></a>): <preclass="fragment">{"one":1,"two":2}
</pre> The example code above can be translated with<preclass="fragment">g++ -std=c++11 -Isrc doc/examples/basic_json__CompatibleType.cpp -o basic_json__CompatibleType </pre></dd></dl>
<pclass="definition">Definition at line <aclass="el"href="json_8hpp_source.html#l02006">2006</a> of file <aclass="el"href="json_8hpp_source.html">json.hpp</a>.</p>