This commit is contained in:
nlohmann committed 2026-10-26 21:24:35 +00:00
1 parent 41c601a7d1
commit d2e264bd84
1400 files changed
+11859 -5440

No files matched your search

+11 -11
View File
@@ -1,4 +1,4 @@
<!doctype html><html lang=en class=no-js> <head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=author content="Niels Lohmann"><link href="https://json.nlohmann.me/features/arbitrary_types/" rel="canonical"><link href=../ rel=prev><link href=../binary_formats/ rel=next><link rel=icon href=../../assets/images/favicon.png><meta name=generator content="mkdocs-1.6.1, mkdocs-material-9.7.7"><title>Arbitrary Type Conversions - JSON for Modern C++</title><link rel=stylesheet href=../../assets/stylesheets/main.ec1eaa64.min.css><link rel=stylesheet href=../../assets/stylesheets/palette.ab4e12ef.min.css><link rel="stylesheet" href="../../assets/external/fonts.googleapis.com/css.61a430c9.css"><style>:root{--md-text-font:"Roboto";--md-code-font:"JetBrains Mono"}</style><link rel=stylesheet href=../../css/custom.css><script>__md_scope=new URL("../..",location),__md_hash=e=>[...e].reduce(((e,_)=>(e<<5)-e+_.charCodeAt(0)),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script></head> <body dir=ltr data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo> <input class=md-toggle data-md-toggle=drawer type=checkbox id=__drawer autocomplete=off> <input class=md-toggle data-md-toggle=search type=checkbox id=__search autocomplete=off> <label class=md-overlay for=__drawer></label> <div data-md-component=skip> <a href=#arbitrary-type-conversions class=md-skip> Skip to content </a> </div> <div data-md-component=announce> </div> <header class=md-header data-md-component=header> <nav class="md-header__inner md-grid" aria-label=Header> <a href=../.. title="JSON for Modern C++" class="md-header__button md-logo" aria-label="JSON for Modern C++" data-md-component=logo> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54"/></svg> </a> <label class="md-header__button md-icon" for=__drawer> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z"/></svg> </label> <div class=md-header__title data-md-component=header-title> <div class=md-header__ellipsis> <div class=md-header__topic> <span class=md-ellipsis> JSON for Modern C++ </span> </div> <div class=md-header__topic data-md-component=header-topic> <span class=md-ellipsis> Arbitrary Type Conversions </span> </div> </div> </div> <form class=md-header__option data-md-component=palette> <input class=md-option data-md-color-media="(prefers-color-scheme: light)" data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to dark mode" type=radio name=__palette id=__palette_0> <label class="md-header__button md-icon" title="Switch to dark mode" for=__palette_1 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a4 4 0 0 0-4 4 4 4 0 0 0 4 4 4 4 0 0 0 4-4 4 4 0 0 0-4-4m0 10a6 6 0 0 1-6-6 6 6 0 0 1 6-6 6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> <input class=md-option data-md-color-media="(prefers-color-scheme: dark)" data-md-color-scheme=slate data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to light mode" type=radio name=__palette id=__palette_1> <label class="md-header__button md-icon" title="Switch to light mode" for=__palette_0 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12s-1.44-4.5-3.5-5.45C10.26 6.2 11.11 6 12 6a6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> </form> <script>var palette=__md_get("__palette");if(palette&&palette.color){if("(prefers-color-scheme)"===palette.color.media){var media=matchMedia("(prefers-color-scheme: light)"),input=document.querySelector(media.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");palette.color.media=input.getAttribute("data-md-color-media"),palette.color.scheme=input.getAttribute("data-md-color-scheme"),palette.color.primary=input.getAttribute("data-md-color-primary"),palette.color.accent=input.getAttribute("data-md-color-accent")}for(var[key,value]of Object.entries(palette.color))document.body.setAttribute("data-md-color-"+key,value)}</script> <label class="md-header__button md-icon" for=__search> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.52 6.52 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 Line truncated
<!doctype html><html lang=en class=no-js> <head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=author content="Niels Lohmann"><link href="https://json.nlohmann.me/features/arbitrary_types/" rel="canonical"><link href=../ rel=prev><link href=../type_recipes/ rel=next><link rel=icon href=../../assets/images/favicon.png><meta name=generator content="mkdocs-1.6.1, mkdocs-material-9.7.7"><title>Arbitrary Type Conversions - JSON for Modern C++</title><link rel=stylesheet href=../../assets/stylesheets/main.ec1eaa64.min.css><link rel=stylesheet href=../../assets/stylesheets/palette.ab4e12ef.min.css><link rel="stylesheet" href="../../assets/external/fonts.googleapis.com/css.61a430c9.css"><style>:root{--md-text-font:"Roboto";--md-code-font:"JetBrains Mono"}</style><link rel=stylesheet href=../../css/custom.css><script>__md_scope=new URL("../..",location),__md_hash=e=>[...e].reduce(((e,_)=>(e<<5)-e+_.charCodeAt(0)),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script></head> <body dir=ltr data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo> <input class=md-toggle data-md-toggle=drawer type=checkbox id=__drawer autocomplete=off> <input class=md-toggle data-md-toggle=search type=checkbox id=__search autocomplete=off> <label class=md-overlay for=__drawer></label> <div data-md-component=skip> <a href=#arbitrary-type-conversions class=md-skip> Skip to content </a> </div> <div data-md-component=announce> </div> <header class=md-header data-md-component=header> <nav class="md-header__inner md-grid" aria-label=Header> <a href=../.. title="JSON for Modern C++" class="md-header__button md-logo" aria-label="JSON for Modern C++" data-md-component=logo> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54"/></svg> </a> <label class="md-header__button md-icon" for=__drawer> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z"/></svg> </label> <div class=md-header__title data-md-component=header-title> <div class=md-header__ellipsis> <div class=md-header__topic> <span class=md-ellipsis> JSON for Modern C++ </span> </div> <div class=md-header__topic data-md-component=header-topic> <span class=md-ellipsis> Arbitrary Type Conversions </span> </div> </div> </div> <form class=md-header__option data-md-component=palette> <input class=md-option data-md-color-media="(prefers-color-scheme: light)" data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to dark mode" type=radio name=__palette id=__palette_0> <label class="md-header__button md-icon" title="Switch to dark mode" for=__palette_1 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a4 4 0 0 0-4 4 4 4 0 0 0 4 4 4 4 0 0 0 4-4 4 4 0 0 0-4-4m0 10a6 6 0 0 1-6-6 6 6 0 0 1 6-6 6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> <input class=md-option data-md-color-media="(prefers-color-scheme: dark)" data-md-color-scheme=slate data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to light mode" type=radio name=__palette id=__palette_1> <label class="md-header__button md-icon" title="Switch to light mode" for=__palette_0 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12s-1.44-4.5-3.5-5.45C10.26 6.2 11.11 6 12 6a6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> </form> <script>var palette=__md_get("__palette");if(palette&&palette.color){if("(prefers-color-scheme)"===palette.color.media){var media=matchMedia("(prefers-color-scheme: light)"),input=document.querySelector(media.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");palette.color.media=input.getAttribute("data-md-color-media"),palette.color.scheme=input.getAttribute("data-md-color-scheme"),palette.color.primary=input.getAttribute("data-md-color-primary"),palette.color.accent=input.getAttribute("data-md-color-accent")}for(var[key,value]of Object.entries(palette.color))document.body.setAttribute("data-md-color-"+key,value)}</script> <label class="md-header__button md-icon" for=__search> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.52 6.52 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.Line truncated
<span class=w> </span><span class=c1>// a simple struct to model a person</span>
<span class=w> </span><span class=k>struct</span><span class=w> </span><span class=nc>person</span><span class=w> </span><span class=p>{</span>
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>string</span><span class=w> </span><span class=n>name</span><span class=p>;</span>
@@ -37,7 +37,7 @@
<span class=c1>// that's it</span>
<span class=n>assert</span><span class=p>(</span><span class=n>p</span><span class=w> </span><span class=o>==</span><span class=w> </span><span class=n>p2</span><span class=p>);</span>
</code></pre></div> <h2 id=basic-usage>Basic usage<a class=headerlink href=#basic-usage title="Permanent link">&para;</a></h2> <p>To make this work with one of your types, you only need to provide two functions:</p> <div class=highlight><pre><span></span><code><span class=k>using</span><span class=w> </span><span class=n>json</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json</span><span class=p>;</span>
</code></pre></div> <h2 id=basic-usage>Basic usage<a class=headerlink href=#basic-usage title="Permanent link">&para;</a></h2> <p>To make this work with one of your types, you only need to provide two functions:</p> <div class=highlight><pre><span></span><code><span class=k>using</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json</span><span class=p>;</span>
<span class=k>namespace</span><span class=w> </span><span class=nn>ns</span><span class=w> </span><span class=p>{</span>
<span class=w> </span><span class=kt>void</span><span class=w> </span><span class=nf>to_json</span><span class=p>(</span><span class=n>json</span><span class=o>&amp;</span><span class=w> </span><span class=n>j</span><span class=p>,</span><span class=w> </span><span class=k>const</span><span class=w> </span><span class=n>person</span><span class=o>&amp;</span><span class=w> </span><span class=n>p</span><span class=p>)</span><span class=w> </span><span class=p>{</span>
@@ -53,7 +53,7 @@
</code></pre></div> <p>That's all! When calling the <code>json</code> constructor with your type, your custom <code>to_json</code> method will be automatically called. Likewise, when calling <code>get&lt;your_type&gt;()</code> or <code>get_to(your_type&amp;)</code>, the <code>from_json</code> method will be called.</p> <p>Some important things:</p> <ul> <li>Those methods <strong>MUST</strong> be in your type's namespace (which can be the global namespace), or the library will not be able to locate them (in this example, they are in namespace <code>ns</code>, where <code>person</code> is defined).</li> <li>Those methods <strong>MUST</strong> be available (e.g., proper headers must be included) everywhere you use these conversions. Look at <a href="https://github.com/nlohmann/json/issues/1108">#1108</a> for errors that may occur otherwise.</li> <li>When using <code>get&lt;your_type&gt;()</code>, <code>your_type</code> <strong>MUST</strong> be <a href="https://en.cppreference.com/w/cpp/named_req/DefaultConstructible">DefaultConstructible</a>. (There is a way to bypass this requirement described later.)</li> <li>In function <code>from_json</code>, use function <a href=../../api/basic_json/at/ ><code>at()</code></a> to access the object values rather than <code>operator[]</code>. In case a key does not exist, <code>at</code> throws an exception that you can handle, whereas <code>operator[]</code> exhibits undefined behavior.</li> <li>You do not need to add serializers or deserializers for <abbr title="Standard Template Library">STL</abbr> types like <code>std::vector</code>: the library already implements these.</li> <li>If you control the type, consider defining <code>to_json</code>/<code>from_json</code> as <code>friend</code> functions inside the class ("hidden friends"). Argument-dependent lookup then only finds them for your type, which also avoids a <a href=../../home/faq/#incomplete-detector-type-with-gcc-11><abbr title="GNU Compiler Collection">GCC</abbr> &lt; 11 compilation error</a>.</li> </ul> <details class=example> <summary>Example: serialize a <code>person</code> to <abbr title="JavaScript Object Notation">JSON</abbr> with <code>to_json</code></summary> <div class=highlight><pre><span></span><code><span class=cp>#include</span><span class=w> </span><span class=cpf>&lt;iostream&gt;</span>
<span class=cp>#include</span><span class=w> </span><span class=cpf>&lt;nlohmann/json.hpp&gt;</span>
<span class=k>using</span><span class=w> </span><span class=n>json</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json</span><span class=p>;</span>
<span class=k>using</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json</span><span class=p>;</span>
<span class=k>namespace</span><span class=w> </span><span class=nn>ns</span>
<span class=p>{</span>
@@ -80,13 +80,13 @@
<span class=w> </span><span class=n>json</span><span class=w> </span><span class=n>j</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>p</span><span class=p>;</span>
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o>&lt;&lt;</span><span class=w> </span><span class=n>j</span><span class=w> </span><span class=o>&lt;&lt;</span><span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>endl</span><span class=p>;</span>
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o>&lt;&lt;</span><span class=w> </span><span class=n>j</span><span class=w> </span><span class=o>&lt;&lt;</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
<span class=p>}</span>
</code></pre></div> <p>Output:</p> <div class=highlight><pre><span></span><code><span class=p>{</span><span class=nt>"address"</span><span class=p>:</span><span class=s2>"744 Evergreen Terrace"</span><span class=p>,</span><span class=nt>"age"</span><span class=p>:</span><span class=mi>60</span><span class=p>,</span><span class=nt>"name"</span><span class=p>:</span><span class=s2>"Ned Flanders"</span><span class=p>}</span>
</code></pre></div> </details> <details class=example> <summary>Example: deserialize a <code>person</code> from <abbr title="JavaScript Object Notation">JSON</abbr> with <code>from_json</code></summary> <div class=highlight><pre><span></span><code><span class=cp>#include</span><span class=w> </span><span class=cpf>&lt;iostream&gt;</span>
<span class=cp>#include</span><span class=w> </span><span class=cpf>&lt;nlohmann/json.hpp&gt;</span>
<span class=k>using</span><span class=w> </span><span class=n>json</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json</span><span class=p>;</span>
<span class=k>using</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json</span><span class=p>;</span>
<span class=k>namespace</span><span class=w> </span><span class=nn>ns</span>
<span class=p>{</span>
@@ -118,7 +118,7 @@
<span class=w> </span><span class=k>auto</span><span class=w> </span><span class=n>p</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>j</span><span class=p>.</span><span class=n>get</span><span class=o>&lt;</span><span class=n>ns</span><span class=o>::</span><span class=n>person</span><span class=o>&gt;</span><span class=p>();</span>
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o>&lt;&lt;</span><span class=w> </span><span class=n>p</span><span class=p>.</span><span class=n>name</span><span class=w> </span><span class=o>&lt;&lt;</span><span class=w> </span><span class=s>" ("</span><span class=w> </span><span class=o>&lt;&lt;</span><span class=w> </span><span class=n>p</span><span class=p>.</span><span class=n>age</span><span class=w> </span><span class=o>&lt;&lt;</span><span class=w> </span><span class=s>") lives in "</span><span class=w> </span><span class=o>&lt;&lt;</span><span class=w> </span><span class=n>p</span><span class=p>.</span><span class=n>address</span><span class=w> </span><span class=o>&lt;&lt;</span><span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>endl</span><span class=p>;</span>
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o>&lt;&lt;</span><span class=w> </span><span class=n>p</span><span class=p>.</span><span class=n>name</span><span class=w> </span><span class=o>&lt;&lt;</span><span class=w> </span><span class=s>" ("</span><span class=w> </span><span class=o>&lt;&lt;</span><span class=w> </span><span class=n>p</span><span class=p>.</span><span class=n>age</span><span class=w> </span><span class=o>&lt;&lt;</span><span class=w> </span><span class=s>") lives in "</span><span class=w> </span><span class=o>&lt;&lt;</span><span class=w> </span><span class=n>p</span><span class=p>.</span><span class=n>address</span><span class=w> </span><span class=o>&lt;&lt;</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
<span class=p>}</span>
</code></pre></div> <p>Output:</p> <div class=highlight><pre><span></span><code>Ned Flanders (60) lives in 744 Evergreen Terrace
</code></pre></div> </details> <h2 id=simplify-your-life-with-macros>Simplify your life with macros<a class=headerlink href=#simplify-your-life-with-macros title="Permanent link">&para;</a></h2> <p>If you just want to serialize/deserialize some structs, the <code>to_json</code>/<code>from_json</code> functions can be a lot of boilerplate.</p> <p>There are several macros to make your life easier as long as you want to use a <abbr title="JavaScript Object Notation">JSON</abbr> object as serialization. The macros are following the naming pattern, and you can choose the macro based on the needed features:</p> <ul> <li>All the macros start with <code>NLOHMANN_DEFINE</code>.</li> <li>If you want a macro for the derived object, use the <a href=../../api/macros/nlohmann_define_derived_type/ ><code>DERIVED_TYPE</code></a> variant, otherwise use <code>TYPE</code>.<ul> <li>The <code>DERIVED_TYPE</code> variant requires an additional parameter of a base type, which should have the <code>to_json</code>/<code>from_json</code> functions defined. For instance, with a macro of its own.</li> </ul> </li> <li>If you need access to the private fields use <a href=../../api/macros/nlohmann_define_type_intrusive/ ><code>INTRUSIVE</code></a> variant, otherwise use <a href=../../api/macros/nlohmann_define_type_non_intrusive/ ><code>NON_INTRUSIVE</code></a>.<ul> <li>The <code>INTRUSIVE</code> macro should be defined <strong>inside</strong> the target class/struct, <code>NON_INTRUSIVE</code> should be defined within the same namespace.</li> </ul> </li> <li>If you want to deserialize the incomplete JSONs, use the <code>WITH_DEFAULTS</code> variant, which will use the default values for the member variables absent in <abbr title="JavaScript Object Notation">JSON</abbr>, the variant without <code>WITH_DEFAULTS</code> will raise an exception.</li> <li>If you do not need deserialization at all and only interested in <code>to_json</code> function, you can use the <code>ONLY_SERIALIZE</code> variant.</li> <li>If you want to use the custom <abbr title="JavaScript Object Notation">JSON</abbr> names for member variables, use <a href=../../api/macros/nlohmann_define_type_with_names/ ><code>WITH_NAMES</code></a> variant, otherwise the <abbr title="JavaScript Object Notation">JSON</abbr> name of the variable will be the same as its regular name. </li> </ul> <p>For all the macros, the first parameter is the name of the class/struct. The <code>DERIVED_TYPE</code> macros require a second parameter of a base class. All the remaining parameters name the member variables. The <code>WITH_NAMES</code> macros require a <abbr title="JavaScript Object Notation">JSON</abbr> name before each of the variables.</p> <pre class=mermaid><code>flowchart TD
@@ -225,10 +225,10 @@
<span class=w> </span><span class=p>}</span>
<span class=p>};</span>
<span class=n>NLOHMANN_JSON_NAMESPACE_END</span>
</code></pre></div> <div class="admonition tip"> <p class=admonition-title><code>std::optional</code> needs no serializer</p> <p>Since version 3.12.0, <code>std::optional&lt;T&gt;</code> is supported out of the box when compiling with C++17 (<code>std::nullopt</code> is converted to and from <code>null</code>). Do not write an <code>adl_serializer</code> for it; this pattern is only needed for types such as <code>boost::optional</code> or for custom semantics. See <a href=../conversions/ >Conversions</a> and <a href=../conversions/#omitting-a-field-when-serializing-stdoptional>Omitting a field when serializing <code>std::optional</code></a>.</p> </div> <div class="admonition note"> <p class=admonition-title>ABI compatibility</p> <p>Use <a href=../../api/macros/nlohmann_json_namespace_begin/ ><code>NLOHMANN_JSON_NAMESPACE_BEGIN</code></a> and <code>NLOHMANN_JSON_NAMESPACE_END</code> instead of <code class=highlight><span class=k>namespace</span><span class=w> </span><span class=nn>nlohmann</span><span class=w> </span><span class=p>{</span><span class=w> </span><span class=p>}</span></code> in code which may be linked with different versions of this library.</p> </div> <h2 id=how-can-i-use-get-for-non-default-constructiblenon-copyable-types>How can I use <code>get()</code> for non-default constructible/non-copyable types?<a class=headerlink href=#how-can-i-use-get-for-non-default-constructiblenon-copyable-types title="Permanent link">&para;</a></h2> <p>For a type that is not <a href="https://en.cppreference.com/w/cpp/named_req/DefaultConstructible">DefaultConstructible</a> but is otherwise an ordinary value type, specialize <code>adl_serializer</code> with a <code>from_json</code> overload that returns the value instead of writing into a reference:</p> <details class=example> <summary>Example: <code>get()</code> for a non-default-constructible type</summary> <div class=highlight><pre><span></span><code><span class=cp>#include</span><span class=w> </span><span class=cpf>&lt;iostream&gt;</span>
</code></pre></div> <div class="admonition tip"> <p class=admonition-title><code>std::optional</code> needs no serializer</p> <p>Since version 3.12.0, <code>std::optional&lt;T&gt;</code> is supported out of the box when compiling with C++17 (<code>std::nullopt</code> is converted to and from <code>null</code>). Do not write an <code>adl_serializer</code> for it; this pattern is only needed for types such as <code>boost::optional</code> or for custom semantics. See <a href=../conversions/ >Conversions</a> and <a href=../conversions/#omitting-a-field-when-serializing-stdoptional>Omitting a field when serializing <code>std::optional</code></a>.</p> </div> <div class="admonition note"> <p class=admonition-title><abbr title="Application Binary Interface">ABI</abbr> compatibility</p> <p>Use <a href=../../api/macros/nlohmann_json_namespace_begin/ ><code>NLOHMANN_JSON_NAMESPACE_BEGIN</code></a> and <code>NLOHMANN_JSON_NAMESPACE_END</code> instead of <code class=highlight><span class=k>namespace</span><span class=w> </span><span class=nn>nlohmann</span><span class=w> </span><span class=p>{</span><span class=w> </span><span class=p>}</span></code> in code which may be linked with different versions of this library.</p> </div> <h2 id=how-can-i-use-get-for-non-default-constructiblenon-copyable-types>How can I use <code>get()</code> for non-default constructible/non-copyable types?<a class=headerlink href=#how-can-i-use-get-for-non-default-constructiblenon-copyable-types title="Permanent link">&para;</a></h2> <p>For a type that is not <a href="https://en.cppreference.com/w/cpp/named_req/DefaultConstructible">DefaultConstructible</a> but is otherwise an ordinary value type, specialize <code>adl_serializer</code> with a <code>from_json</code> overload that returns the value instead of writing into a reference:</p> <details class=example> <summary>Example: <code>get()</code> for a non-default-constructible type</summary> <div class=highlight><pre><span></span><code><span class=cp>#include</span><span class=w> </span><span class=cpf>&lt;iostream&gt;</span>
<span class=cp>#include</span><span class=w> </span><span class=cpf>&lt;nlohmann/json.hpp&gt;</span>
<span class=k>using</span><span class=w> </span><span class=n>json</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json</span><span class=p>;</span>
<span class=k>using</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json</span><span class=p>;</span>
<span class=k>namespace</span><span class=w> </span><span class=nn>ns</span>
<span class=p>{</span>
@@ -276,7 +276,7 @@
<span class=w> </span><span class=k>auto</span><span class=w> </span><span class=n>p</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>j</span><span class=p>.</span><span class=n>get</span><span class=o>&lt;</span><span class=n>ns</span><span class=o>::</span><span class=n>person</span><span class=o>&gt;</span><span class=p>();</span>
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o>&lt;&lt;</span><span class=w> </span><span class=n>p</span><span class=p>.</span><span class=n>name</span><span class=w> </span><span class=o>&lt;&lt;</span><span class=w> </span><span class=s>" ("</span><span class=w> </span><span class=o>&lt;&lt;</span><span class=w> </span><span class=n>p</span><span class=p>.</span><span class=n>age</span><span class=w> </span><span class=o>&lt;&lt;</span><span class=w> </span><span class=s>") lives in "</span><span class=w> </span><span class=o>&lt;&lt;</span><span class=w> </span><span class=n>p</span><span class=p>.</span><span class=n>address</span><span class=w> </span><span class=o>&lt;&lt;</span><span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>endl</span><span class=p>;</span>
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o>&lt;&lt;</span><span class=w> </span><span class=n>p</span><span class=p>.</span><span class=n>name</span><span class=w> </span><span class=o>&lt;&lt;</span><span class=w> </span><span class=s>" ("</span><span class=w> </span><span class=o>&lt;&lt;</span><span class=w> </span><span class=n>p</span><span class=p>.</span><span class=n>age</span><span class=w> </span><span class=o>&lt;&lt;</span><span class=w> </span><span class=s>") lives in "</span><span class=w> </span><span class=o>&lt;&lt;</span><span class=w> </span><span class=n>p</span><span class=p>.</span><span class=n>address</span><span class=w> </span><span class=o>&lt;&lt;</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
<span class=p>}</span>
</code></pre></div> <p>Output:</p> <div class=highlight><pre><span></span><code>Ned Flanders (60) lives in 744 Evergreen Terrace
</code></pre></div> </details> <p>The same technique also works if your type is not copyable, as long as it is <a href="https://en.cppreference.com/w/cpp/named_req/MoveConstructible">MoveConstructible</a>:</p> <div class=highlight><pre><span></span><code><span class=k>struct</span><span class=w> </span><span class=nc>move_only_type</span><span class=w> </span><span class=p>{</span>
@@ -305,7 +305,7 @@
<span class=w> </span><span class=p>}</span>
<span class=w> </span><span class=p>};</span>
<span class=p>}</span>
</code></pre></div> <h2 id=why-cant-i-convert-tofrom-stdany>Why can't I convert to/from <code>std::any</code>?<a class=headerlink href=#why-cant-i-convert-tofrom-stdany title="Permanent link">&para;</a></h2> <p><code>std::any</code> is intentionally excluded from <code>get&lt;T&gt;()</code>/generic conversion support, so <code>get&lt;std::any&gt;()</code> and containers like <code>std::map&lt;std::string, std::any&gt;</code> fail to compile by design -- there is no way to know, from a <code>json</code> value alone, which concrete type to store inside the <code>std::any</code>. To work with heterogeneous <abbr title="JavaScript Object Notation">JSON</abbr> values, dispatch on the value's type manually and construct the <code>std::any</code> (or extract from it) yourself:</p> <div class=highlight><pre><span></span><code><span class=n>std</span><span class=o>::</span><span class=n>any</span><span class=w> </span><span class=nf>value_to_any</span><span class=p>(</span><span class=k>const</span><span class=w> </span><span class=n>json</span><span class=o>&amp;</span><span class=w> </span><span class=n>j</span><span class=p>)</span><span class=w> </span><span class=p>{</span>
</code></pre></div> <h2 id=why-isnt-my-to_json-or-from_json-found>Why isn't my <code>to_json</code> or <code>from_json</code> found?<a class=headerlink href=#why-isnt-my-to_json-or-from_json-found title="Permanent link">&para;</a></h2> <p>The library calls <code>to_json</code> and <code>from_json</code> without qualification, so the compiler finds them by <a href="https://en.cppreference.com/w/cpp/language/adl">argument-dependent lookup</a> in the namespace of the type, or in the namespaces of its template arguments. The usual reasons for a function that is "not found" are:</p> <ul> <li><strong>The type is an alias of a standard type.</strong> After <code class=highlight><span class=k>using</span><span class=w> </span><span class=n>Hash</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>array</span><span class=o>&lt;</span><span class=n>std</span><span class=o>::</span><span class=kt>uint8_t</span><span class=p>,</span><span class=w> </span><span class=mi>6</span><span class=o>&gt;</span><span class=p>;</span></code> the type <em>is</em> <code>std::array&lt;std::uint8_t, 6&gt;</code>, which lives in namespace <code>std</code>. A <code>to_json(json&amp;, const Hash&amp;)</code> in your namespace is never selected. Either give the type its own identity, for example <code class=highlight><span class=k>struct</span><span class=w> </span><span class=nc>Hash</span><span class=w> </span><span class=o>:</span><span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>array</span><span class=o>&lt;</span><span class=n>std</span><span class=o>::</span><span class=kt>uint8_t</span><span class=p>,</span><span class=w> </span><span class=mi>6</span><span class=o>&gt;</span><span class=w> </span><span class=p>{};</span></code> (the function is then found in <code>Hash</code>'s namespace), or specialize <a href=#how-do-i-convert-third-party-types><code>adl_serializer&lt;Hash&gt;</code></a>.</li> <li><strong>The function is in the wrong namespace.</strong> It must be in the namespace of the type (a global type needs a global function), not in the namespace of the code that calls the conversion. Specializations of <code>adl_serializer</code> belong in namespace <code>nlohmann</code>.</li> <li><strong>It is declared too late.</strong> A conversion can only use what has been declared when it is instantiated. If <code class=highlight><span class=n>json</span><span class=w> </span><span class=n>j</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>vector</span><span class=o>&lt;</span><span class=n>third_party</span><span class=o>::</span><span class=n>string</span><span class=o>&gt;</span><span class=p>{...};</span></code> appears before the <code>adl_serializer</code> specialization, the compiler reports an error such as "explicit specialization of <code>adl_serializer&lt;...&gt;</code> after instantiation". Put the specialization in a small header that every file includes <strong>before</strong> it uses the type with <code>json</code>.</li> <li><strong>It is a template.</strong> For <code>template &lt;class T&gt; struct box</code>, write <code>template &lt;class T&gt; void to_json(json&amp;, const box&lt;T&gt;&amp;)</code>, not an overload for one <code>T</code>.</li> <li><strong>It worked by accident.</strong> Some versions found <code>to_json</code> overloads in <code>nlohmann::detail</code> through argument-dependent lookup of the base class of <code>basic_json</code>. Do not rely on this; it is not part of the interface.</li> </ul> <p>For smart pointers, class hierarchies, <code>std::variant</code>, and <code>std::chrono</code>, see <a href=../type_recipes/ >Recipes for common types</a>.</p> <h2 id=why-cant-i-convert-tofrom-stdany>Why can't I convert to/from <code>std::any</code>?<a class=headerlink href=#why-cant-i-convert-tofrom-stdany title="Permanent link">&para;</a></h2> <p><code>std::any</code> is intentionally excluded from <code>get&lt;T&gt;()</code>/generic conversion support, so <code>get&lt;std::any&gt;()</code> and containers like <code>std::map&lt;std::string, std::any&gt;</code> fail to compile by design -- there is no way to know, from a <code>json</code> value alone, which concrete type to store inside the <code>std::any</code>. To work with heterogeneous <abbr title="JavaScript Object Notation">JSON</abbr> values, dispatch on the value's type manually and construct the <code>std::any</code> (or extract from it) yourself:</p> <div class=highlight><pre><span></span><code><span class=n>std</span><span class=o>::</span><span class=n>any</span><span class=w> </span><span class=nf>value_to_any</span><span class=p>(</span><span class=k>const</span><span class=w> </span><span class=n>json</span><span class=o>&amp;</span><span class=w> </span><span class=n>j</span><span class=p>)</span><span class=w> </span><span class=p>{</spanLine truncated
<span class=w> </span><span class=k>if</span><span class=w> </span><span class=p>(</span><span class=n>j</span><span class=p>.</span><span class=n>is_boolean</span><span class=p>())</span><span class=w> </span><span class=p>{</span><span class=w> </span><span class=k>return</span><span class=w> </span><span class=n>j</span><span class=p>.</span><span class=n>get</span><span class=o>&lt;</span><span class=kt>bool</span><span class=o>&gt;</span><span class=p>();</span><span class=w> </span><span class=p>}</span>
<span class=w> </span><span class=k>if</span><span class=w> </span><span class=p>(</span><span class=n>j</span><span class=p>.</span><span class=n>is_number_integer</span><span class=p>())</span><span class=w> </span><span class=p>{</span><span class=w> </span><span class=k>return</span><span class=w> </span><span class=n>j</span><span class=p>.</span><span class=n>get</span><span class=o>&lt;</span><span class=kt>int</span><span class=o>&gt;</span><span class=p>();</span><span class=w> </span><span class=p>}</span>
<span class=w> </span><span class=k>if</span><span class=w> </span><span class=p>(</span><span class=n>j</span><span class=p>.</span><span class=n>is_number_float</span><span class=p>())</span><span class=w> </span><span class=p>{</span><span class=w> </span><span class=k>return</span><span class=w> </span><span class=n>j</span><span class=p>.</span><span class=n>get</span><span class=o>&lt;</span><span class=kt>double</span><span class=o>&gt;</span><span class=p>();</span><span class=w> </span><span class=p>}</span>
@@ -357,4 +357,4 @@
<span class=w> </span><span class=n>value</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>j</span><span class=p>.</span><span class=k>template</span><span class=w> </span><span class=n>get</span><span class=o>&lt;</span><span class=n>T</span><span class=o>&gt;</span><span class=p>();</span><span class=w> </span><span class=c1>// oops!</span>
<span class=w> </span><span class=p>}</span>
<span class=p>};</span>
</code></pre></div> <h2 id=see-also>See also<a class=headerlink href=#see-also title="Permanent link">&para;</a></h2> <ul> <li><a href=../conversions/ >Converting values</a> - the general overview of <code>get</code>/<code>get_to</code> and implicit conversions</li> <li><a href=../enum_conversion/ >Specializing enum conversion</a> - map enums to <abbr title="JavaScript Object Notation">JSON</abbr> strings instead of integers</li> <li><a href=../macros/ >Supported macros</a> - reference for <code>NLOHMANN_DEFINE_TYPE_*</code> and related macros</li> <li><a href=../../api/adl_serializer/ ><code>adl_serializer</code></a> - the default <code>JSONSerializer</code> used in the conversion dispatch</li> </ul> <!-- https://squidfunk.github.io/mkdocs-material/reference/tooltips/#adding-a-glossary --> <aside class=md-source-file> <span class=md-source-file__fact> <span class=md-icon title="Last update"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M21 13.1c-.1 0-.3.1-.4.2l-1 1 2.1 2.1 1-1c.2-.2.2-.6 0-.8l-1.3-1.3c-.1-.1-.2-.2-.4-.2m-1.9 1.8-6.1 6V23h2.1l6.1-6.1zM12.5 7v5.2l4 2.4-1 1L11 13V7zM11 21.9c-5.1-.5-9-4.8-9-9.9C2 6.5 6.5 2 12 2c5.3 0 9.6 4.1 10 9.3-.3-.1-.6-.2-1-.2s-.7.1-1 .2C19.6 7.2 16.2 4 12 4c-4.4 0-8 3.6-8 8 0 4.1 3.1 7.5 7.1 7.9l-.1.2z"/></svg> </span> <span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date" title="October 11, 2026 06:22:10 UTC">October 11, 2026</span> </span> </aside> </article> </div> <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div> <button type=button class="md-top md-icon" data-md-component=top hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg> Back to top </button> </main> <footer class=md-footer> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-copyright> <div class=md-copyright__highlight> Copyright &copy; 2013-2026 Niels Lohmann </div> </div> <div class=md-social> <a href="https://github.com/nlohmann" target="_blank" rel="noopener" title="github.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 512 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M173.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9M252.8 8C114.1 8 8 113.3 8 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C436.2 457.8 504 362.9 504 252 504 113.3 391.5 8 252.8 8M105.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1m-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7m32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1m-11.4-14.7c-1.6 1-1.6 3.6 0 5.9s4.3 3.3 5.6 2.3c1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2"/></svg> </a> <a href="https://www.linkedin.com/in/nielslohmann/" target="_blank" rel="noopener" title="www.linkedin.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 448 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M416 32H31.9C14.3 32 0 46.5 0 64.3v383.4C0 465.5 14.3 480 31.9 480H416c17.6 0 32-14.5 32-32.3V64.3c0-17.8-14.4-32.3-32-32.3M135.4 416H69V202.2h66.5V416zM102.2 96a38.5 38.5 0 1 1 0 77 38.5 38.5 0 1 1 0-77m282.1 320h-66.4V312c0-24.8-.5-56.7-34.5-56.7-34.6 0-39.9 27-39.9 54.9V416h-66.4V202.2h63.7v29.2h.9c8.9-16.8 30.6-34.5 62.9-34.5 67.2 0 79.7 44.3 79.7 101.9z"/></svg> </a> <a href="https://www.xing.Line truncated
</code></pre></div> <h2 id=see-also>See also<a class=headerlink href=#see-also title="Permanent link">&para;</a></h2> <ul> <li><a href=../type_recipes/ >Recipes for common types</a> - smart pointers, class hierarchies, <code>std::variant</code>, and <code>std::chrono</code></li> <li><a href=../conversions/ >Converting values</a> - the general overview of <code>get</code>/<code>get_to</code> and implicit conversions</li> <li><a href=../enum_conversion/ >Specializing enum conversion</a> - map enums to <abbr title="JavaScript Object Notation">JSON</abbr> strings instead of integers</li> <li><a href=../macros/ >Supported macros</a> - reference for <code>NLOHMANN_DEFINE_TYPE_*</code> and related macros</li> <li><a href=../../api/adl_serializer/ ><code>adl_serializer</code></a> - the default <code>JSONSerializer</code> used in the conversion dispatch</li> </ul> <!-- https://squidfunk.github.io/mkdocs-material/reference/tooltips/#adding-a-glossary --> <aside class=md-source-file> <span class=md-source-file__fact> <span class=md-icon title="Last update"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M21 13.1c-.1 0-.3.1-.4.2l-1 1 2.1 2.1 1-1c.2-.2.2-.6 0-.8l-1.3-1.3c-.1-.1-.2-.2-.4-.2m-1.9 1.8-6.1 6V23h2.1l6.1-6.1zM12.5 7v5.2l4 2.4-1 1L11 13V7zM11 21.9c-5.1-.5-9-4.8-9-9.9C2 6.5 6.5 2 12 2c5.3 0 9.6 4.1 10 9.3-.3-.1-.6-.2-1-.2s-.7.1-1 .2C19.6 7.2 16.2 4 12 4c-4.4 0-8 3.6-8 8 0 4.1 3.1 7.5 7.1 7.9l-.1.2z"/></svg> </span> <span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date" title="October 25, 2026 11:07:51 UTC">October 25, 2026</span> </span> </aside> </article> </div> <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div> <button type=button class="md-top md-icon" data-md-component=top hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg> Back to top </button> </main> <footer class=md-footer> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-copyright> <div class=md-copyright__highlight> Copyright &copy; 2013-2026 Niels Lohmann </div> </div> <div class=md-social> <a href="https://github.com/nlohmann" target="_blank" rel="noopener" title="github.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 512 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M173.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9M252.8 8C114.1 8 8 113.3 8 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C436.2 457.8 504 362.9 504 252 504 113.3 391.5 8 252.8 8M105.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1m-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7m32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1m-11.4-14.7c-1.6 1-1.6 3.6 0 5.9s4.3 3.3 5.6 2.3c1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2"/></svg> </a> <a href="https://www.linkedin.com/in/nielslohmann/" target="_blank" rel="noopener" title="www.linkedin.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 448 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M416 32H31.9C14.3 32 0 46.5 0 64.3v383.4C0 465.5 14.3 480 31.9 480H416c17.6 0 32-14.5 32-32.3V64.3c0-17.8-14.4-32.3-32-32.3M135.4 416H69V202.2h66.5V416zM102.2 96a38.5 38.5 0 1 1 0 77 38.5 38.5 0 1 1 0-77m282.1 320h-66.4V312c0-24.8-.5-56.7-34.5-Line truncated
+20 -7
View File
@@ -54,7 +54,7 @@ assert(p == p2);
To make this work with one of your types, you only need to provide two functions:
```
using json = nlohmann::json;
using nlohmann::json;
namespace ns {
void to_json(json& j, const person& p) {
@@ -86,7 +86,7 @@ Example: serialize a `person` to JSON with `to_json`
#include <iostream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
using nlohmann::json;
namespace ns
{
@@ -113,7 +113,7 @@ int main()
json j = p;
std::cout << j << std::endl;
std::cout << j << '\n';
}
```
@@ -129,7 +129,7 @@ Example: deserialize a `person` from JSON with `from_json`
#include <iostream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
using nlohmann::json;
namespace ns
{
@@ -161,7 +161,7 @@ int main()
auto p = j.get<ns::person>();
std::cout << p.name << " (" << p.age << ") lives in " << p.address << std::endl;
std::cout << p.name << " (" << p.age << ") lives in " << p.address << '\n';
}
```
@@ -409,7 +409,7 @@ Example: `get()` for a non-default-constructible type
#include <iostream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
using nlohmann::json;
namespace ns
{
@@ -457,7 +457,7 @@ int main()
auto p = j.get<ns::person>();
std::cout << p.name << " (" << p.age << ") lives in " << p.address << std::endl;
std::cout << p.name << " (" << p.age << ") lives in " << p.address << '\n';
}
```
@@ -498,6 +498,18 @@ namespace nlohmann {
}
```
## Why isn't my `to_json` or `from_json` found?
The library calls `to_json` and `from_json` without qualification, so the compiler finds them by [argument-dependent lookup](https://en.cppreference.com/w/cpp/language/adl) in the namespace of the type, or in the namespaces of its template arguments. The usual reasons for a function that is "not found" are:
- **The type is an alias of a standard type.** After `using Hash = std::array<std::uint8_t, 6>;` the type *is* `std::array<std::uint8_t, 6>`, which lives in namespace `std`. A `to_json(json&, const Hash&)` in your namespace is never selected. Either give the type its own identity, for example `struct Hash : std::array<std::uint8_t, 6> {};` (the function is then found in `Hash`'s namespace), or specialize [`adl_serializer<Hash>`](#how-do-i-convert-third-party-types).
- **The function is in the wrong namespace.** It must be in the namespace of the type (a global type needs a global function), not in the namespace of the code that calls the conversion. Specializations of `adl_serializer` belong in namespace `nlohmann`.
- **It is declared too late.** A conversion can only use what has been declared when it is instantiated. If `json j = std::vector<third_party::string>{...};` appears before the `adl_serializer` specialization, the compiler reports an error such as "explicit specialization of `adl_serializer<...>` after instantiation". Put the specialization in a small header that every file includes **before** it uses the type with `json`.
- **It is a template.** For `template <class T> struct box`, write `template <class T> void to_json(json&, const box<T>&)`, not an overload for one `T`.
- **It worked by accident.** Some versions found `to_json` overloads in `nlohmann::detail` through argument-dependent lookup of the base class of `basic_json`. Do not rely on this; it is not part of the interface.
For smart pointers, class hierarchies, `std::variant`, and `std::chrono`, see [Recipes for common types](https://json.nlohmann.me/features/type_recipes/index.md).
## Why can't I convert to/from `std::any`?
`std::any` is intentionally excluded from `get<T>()`/generic conversion support, so `get<std::any>()` and containers like `std::map<std::string, std::any>` fail to compile by design -- there is no way to know, from a `json` value alone, which concrete type to store inside the `std::any`. To work with heterogeneous JSON values, dispatch on the value's type manually and construct the `std::any` (or extract from it) yourself:
@@ -587,6 +599,7 @@ struct bad_serializer
## See also
- [Recipes for common types](https://json.nlohmann.me/features/type_recipes/index.md) - smart pointers, class hierarchies, `std::variant`, and `std::chrono`
- [Converting values](https://json.nlohmann.me/features/conversions/index.md) - the general overview of `get`/`get_to` and implicit conversions
- [Specializing enum conversion](https://json.nlohmann.me/features/enum_conversion/index.md) - map enums to JSON strings instead of integers
- [Supported macros](https://json.nlohmann.me/features/macros/index.md) - reference for `NLOHMANN_DEFINE_TYPE_*` and related macros