This commit is contained in:
nlohmann committed 2026-10-27 12:14:00 +00:00
1 parent b36f8a9821
commit 11a863958e
1180 files changed
+14211 -6025

No files matched your search

+56 -5
View File
@@ -1,4 +1,38 @@
<!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/integration/compile_times/" rel="canonical"><link href=../pkg-config/ rel=prev><link href=../../api/basic_json/ rel=next><link rel=icon href=../../assets/images/favicon.png><meta name=generator content="mkdocs-1.6.1, mkdocs-material-9.7.7"><title>Compile times - 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=#compile-times 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> Compile times </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.5 5"/></svg> </label> 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=description content="JSON for Modern C++ is a header-only C++11 library that makes JSON a first-class data type."><meta name=author content="Niels Lohmann"><link href="https://json.nlohmann.me/integration/compile_times/" rel="canonical"><link href=../pkg-config/ rel=prev><link href=../../api/basic_json/ rel=next><link rel=icon href=../../assets/images/favicon.png><meta name=generator content="mkdocs-1.6.1, mkdocs-material-9.7.7"><title>Compile times - 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=../../css/custom.css><link rel=stylesheet href=../../css/brand.css><link rel=stylesheet href=../../css/fig-sax.css><link rel=stylesheet href=../../css/fig-jsonview.css><link rel=stylesheet href=../../css/fig-exceptions.css><link rel=stylesheet href=../../css/fig-pointer.css><link rel=stylesheet href=../../css/fig-charts.css><link rel=stylesheet href=../../css/fig-integration.css><link rel=stylesheet href=../../css/fig-macros.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=custom data-md-color-accent=custom> <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=#compile-times 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> <img src=../../images/wordmark-header.svg alt=logo> </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> Compile times </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=custom data-md-color-accent=custom 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=custom data-md-color-accent=custom 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.Line truncated
<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>
<span class=w> </span><span class=kt>int</span><span class=w> </span><span class=n>age</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=mi>0</span><span class=p>;</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>std</span><span class=o>::</span><span class=n>string</span><span class=o>&gt;</span><span class=w> </span><span class=n>tags</span><span class=p>;</span>
<span class=p>};</span>
<span class=kt>void</span><span class=w> </span><span class=nf>to_json</span><span class=p>(</span><span class=n>nlohmann</span><span class=o>::</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=p>{</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>nlohmann</span><span class=o>::</span><span class=n>json</span><span class=p>{{</span><span class=s>"name"</span><span class=p>,</span><span class=w> </span><span class=n>p</span><span class=p>.</span><span class=n>name</span><span class=p>},</span><span class=w> </span><span class=p>{</span><span class=s>"age"</span><span class=p>,</span><span class=w> </span><span class=n>p</span><span class=p>.</span><span class=n>age</span><span class=p>},</span><span class=w> </span><span class=p>{</span><span class=s>"tags"</span><span class=p>,</span><span class=w> </span><span class=n>p</span><span class=p>.</span><span class=n>tags</span><span class=p>}};</span>
<span class=p>}</span>
<span class=kt>void</span><span class=w> </span><span class=nf>from_json</span><span class=p>(</span><span class=k>const</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</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=n>person</span><span class=o>&amp;</span><span class=w> </span><span class=n>p</span><span class=p>)</span>
<span class=p>{</span>
<span class=w> </span><span class=n>j</span><span class=p>.</span><span class=n>at</span><span class=p>(</span><span class=s>"name"</span><span class=p>).</span><span class=n>get_to</span><span class=p>(</span><span class=n>p</span><span class=p>.</span><span class=n>name</span><span class=p>);</span>
<span class=w> </span><span class=n>j</span><span class=p>.</span><span class=n>at</span><span class=p>(</span><span class=s>"age"</span><span class=p>).</span><span class=n>get_to</span><span class=p>(</span><span class=n>p</span><span class=p>.</span><span class=n>age</span><span class=p>);</span>
<span class=w> </span><span class=n>j</span><span class=p>.</span><span class=n>at</span><span class=p>(</span><span class=s>"tags"</span><span class=p>).</span><span class=n>get_to</span><span class=p>(</span><span class=n>p</span><span class=p>.</span><span class=n>tags</span><span class=p>);</span>
<span class=p>}</span>
<span class=n>nlohmann</span><span class=o>::</span><span class=n>json</span><span class=w> </span><span class=nf>roundtrip</span><span class=p>(</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=p>{</span>
<span class=w> </span><span class=n>nlohmann</span><span class=o>::</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=k>return</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=n>j</span><span class=p>.</span><span class=n>get</span><span class=o>&lt;</span><span class=n>person</span><span class=o>&gt;</span><span class=p>());</span>
<span class=p>}</span>
</code></pre></div> <div class=highlight><span class=filename>parsing translation unit</span><pre><span></span><code><span class=n>std</span><span class=o>::</span><span class=kt>size_t</span><span class=w> </span><span class=nf>parse_and_dump</span><span class=p>(</span><span class=k>const</span><span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>string</span><span class=o>&amp;</span><span class=w> </span><span class=n>s</span><span class=p>)</span>
<span class=p>{</span>
<span class=w> </span><span class=k>const</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</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>nlohmann</span><span class=o>::</span><span class=n>json</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=n>s</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>dump</span><span class=p>(</span><span class=mi>2</span><span class=p>).</span><span class=n>size</span><span class=p>();</span>
<span class=p>}</span>
</code></pre></div> <div class=highlight><span class=filename>header-only translation unit</span><pre><span></span><code><span class=cp>#include</span><span class=w> </span><span class=cpf>&lt;nlohmann/json.hpp&gt;</span><span class=w> </span><span class=c1>// or &lt;nlohmann/json_fwd.hpp&gt;</span>
<span class=k>struct</span><span class=w> </span><span class=nc>person</span><span class=p>;</span>
<span class=kt>void</span><span class=w> </span><span class=nf>to_json</span><span class=p>(</span><span class=n>nlohmann</span><span class=o>::</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=kt>void</span><span class=w> </span><span class=nf>from_json</span><span class=p>(</span><span class=k>const</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</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=n>person</span><span class=o>&amp;</span><span class=w> </span><span class=n>p</span><span class=p>);</span>
</code></pre></div> </details> <div class=nj-fig-wrap> <p><svg class="nj-fig nj-charts nj-charts--compile" viewbox="0 0 470 516" role=img aria-labelledby="nj-charts-ct nj-charts-ct-d" xmlns=http://www.w3.org/2000/svg> <title id=nj-charts-ct>Change in compile time against the default configuration at -O0, Apple clang and G&#8203;CC 16</title> <desc id=nj-charts-ct-d>Grouped horizontal bars, negative is faster. json_fwd.hpp instead of json.hpp on a header-only translation unit: Apple clang -62 %, G&#8203;CC 16 -82 %; extern template on a parsing translation unit: Apple clang -7 %, G&#8203;CC 16 -21 %; JSON_NO_AUTOMATIC_UDLS on a model translation unit: Apple clang -11 %, G&#8203;CC 16 -11 %; JSON_NO_AUTOMATIC_UDLS on a parsing translation unit: Apple clang -1 %, G&#8203;CC 16 -2 %; JSON_HAS_FILESYSTEM=0 + JSON_NO_IO on a model translation unit: Apple clang -8 %, G&#8203;CC 16 -11 %; JSON_HAS_FILESYSTEM=0 on a model translation unit: Apple clang -5 %, G&#8203;CC 16 -9 %; JSON_NO_IO on a model translation unit: Apple clang -2 %, G&#8203;CC 16 -2 %; JSON_USE_GLOBAL_UDLS=0 on a model translation unit: Apple clang 0 %, G&#8203;CC 16 -1 %.</desc> <rect class=nj-charts-clang x=8 y=10 width=12 height=12 rx=3 /> <text class=nj-charts-leg x=26 y=20.5>Apple clang</text> <rect class=nj-charts-gcc x=126 y=10 width=12 height=12 rx=3 /> <text class=nj-charts-leg x=144 y=20.5>G&#8203;CC 16</text> <line class=nj-charts-grid x1=47.4 y1=58 x2=47.4 y2=472 /> <text class=nj-charts-tick x=47.4 y=488>&#8722;80 %</text> <line class=nj-charts-grid x1=126.2 y1=58 x2=126.2 y2=472 /> <text class=nj-charts-tick x=126.2 y=488>&#8722;60 %</text> <line class=nj-charts-grid x1=205 y1=58 x2=205 y2=472 /> <text class=nj-charts-tick x=205 y=488>&#8722;40 %</text> <line class=nj-charts-grid x1=283.8 y1=58 x2=283.8 y2=472 /> <text class=nj-charts-tick x=283.8 y=488>&#8722;20 %</text> <text class=nj-charts-tick x=362.6 y=488>0 %</text> <text class="nj-charts-tick nj-charts-tick--l" x=8 y=506>&#8592; faster</text> <text class="nj-charts-tick nj-charts-tick--r" x=462 y=506>slower &#8594;</text> <text class=nj-charts-rowlabel x=8 y=62><tspan class=nj-charts-code>json_fwd.hpp instead of json.hpp</tspan><tspan class=nj-charts-sub> &#183; header-only TU</tspan></text> <path class=nj-charts-clang d="M362.6 70 H122.32 Q118.32 70 118.32 74 V78 Q118.32 82 122.32 82 H362.6 Z"><title>json_fwd.hpp instead of json.hpp, header-only TU (Apple clang): &#8722;62 %</title></path> <text class=nj-charts-val x=367.6 y=80.5>&#8722;62 %</text> <path class=nj-charts-gcc d="M362.6 84 H43.52 Q39.52 84 39.52 88 V92 Q39.52 96 43.52 96 H362.6 Z"><title>json_fwd.hpp instead of json.hpp, header-only TU (G&#8203;CC 16): &#8722;82 %</title></path> <text class=nj-charts-val x=367.6 y=94.5>&#8722;82 %</text> <text class=nj-charts-rowlabel x=8 y=114><tspan class=nj-charts-code>extern template</tspan><tspan class=nj-charts-sub> &#183; parsing TU</tspan></text> <path class=nj-charts-clang d="M362.6 122 H339.02 Q335.02 122 335.02 126 V130 Q335.02 134 339.02 134 H362.6 Z"><title>extern template, parsing TU (Apple clang): &#8722;7 %</title></path> <text class=nj-charts-val x=367.6 y=132.5>&#8722;7 %</text> <path class=nj-charts-gcc d="M362.6 136 H283.86 Q279.86 136 279.86 140 V144 Q279.86 148 283.86 148 H362.6 Z"><title>extern template, parsing TU (G&#8203;CC 16): &#8722;21 %</title></path> <text class=nj-charts-val x=367.6 y=146.5>&#8722;21 %</text> <text class=nj-charts-rowlabel x=8 y=166><tspan class=nj-charts-code>JSON_NO_AUTOMATIC_UDLS</tspan><tspan class=nj-charts-sub> &#183; model TU</tspan></text> <path class=nj-charts-clang d="M362.6 174 H323.26 Q319.26 174 319.26 178 V182 Q319.26 186 323.26 186 H362.6 Z"><title>JSON_NO_AUTOMATIC_UDLS, model TU (Apple clang): &#8722;11 %</title></path> <text class=nj-charts-val x=367.6 y=184.5>&#8722;11 %</text> <path class=nj-charts-gcc d="M362.6 188 H323.26 Q319.26 188 319.26 192 V196 Q319.26 200 323.26 200 H362.6 Z"><title>JSON_NO_AUTOMATIC_UDLS, model TU (G&#8203;CC 16): &#8722;11 %</title></path> <text class=nj-charts-val x=367.6 y=198.5>&#8722;11 %</text> <text class=nj-charts-rowlabel x=8 y=218><tspan class=nj-charts-code>JSON_NO_AUTOMATIC_UDLS</tspan><tspan class=nj-charts-sub> &#183; parsing TU</tspan></text> <path class=nj-charts-clang d="M362.6 226 H362.6 Q358.66 226 358.66 229.94 V234.06 Q358.66 238 362.6 238 H362.6 Z"><title>JSON_NO_AUTOMATIC_UDLS, parsing TU (Apple clang): &#8722;1 %</title></path> <text class=nj-charts-val x=367.6 y=236.5>&#8722;1 %</text> <path class=nj-charts-gcc d="M362.6 240 H358.72 Q354.72 240 354.72 244 V248 Q354.72 252 358.72 252 H362.6 Z"><title>JSON_NO_AUTOMATIC_UDLS, parsing TU (G&#8203;CC 16): &#8722;2 %</title></path> <text class=nj-charts-val x=367.6 y=250.5>&#8722;2 %</text> <text class=nj-charts-rowlabel x=8 y=270><tspan class=nj-charts-code>JSON_HAS_FILESYSTEM=0 + JSON_NO_IO</tspan><tspan class=nj-charts-sub> &#183; model TU</tspan></text> <path class=nj-charts-clang d="M362.6 278 H335.08 Q331.08 2Line truncated
<span class=cp>#include</span><span class=w> </span><span class=cpf>&lt;nlohmann/json_fwd.hpp&gt;</span>
<span class=k>struct</span><span class=w> </span><span class=nc>person</span><span class=p>;</span>
@@ -9,14 +43,31 @@
<span class=kt>void</span><span class=w> </span><span class=nf>to_json</span><span class=p>(</span><span class=n>nlohmann</span><span class=o>::</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><span class=w> </span><span class=cm>/* ... */</span><span class=w> </span><span class=p>}</span>
<span class=kt>void</span><span class=w> </span><span class=nf>from_json</span><span class=p>(</span><span class=k>const</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</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=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><span class=w> </span><span class=cm>/* ... */</span><span class=w> </span><span class=p>}</span>
</code></pre></div> <table> <thead> <tr> <th>Compiler</th> <th style="text-align: right;"><code>json.hpp</code> (<code>-O0</code>)</th> <th style="text-align: right;"><code>json_fwd.hpp</code> (<code>-O0</code>)</th> <th style="text-align: right;">Change</th> </tr> </thead> <tbody> <tr> <td>Apple clang</td> <td style="text-align: right;">704 ms</td> <td style="text-align: right;">329 ms</td> <td style="text-align: right;">−53%</td> </tr> <tr> <td><abbr title="GNU Compiler Collection">GCC</abbr> 16</td> <td style="text-align: right;">779 ms</td> <td style="text-align: right;">242 ms</td> <td style="text-align: right;">−69%</td> </tr> </tbody> </table> <p>This is the most effective option, because it avoids the full header in every translation unit that includes <em>your</em> headers.</p> <h2 id=opt-out-of-the-automatic-user-defined-string-literals>Opt out of the automatic user-defined string literals<a class=headerlink href=#opt-out-of-the-automatic-user-defined-string-literals title="Permanent link">&para;</a></h2> <p>The user-defined string literals <a href=../../api/operator_literal_json/ ><code>operator""_json</code></a> and <a href=../../api/operator_literal_json_pointer/ ><code>operator""_json_pointer</code></a> are ordinary inline functions whose bodies call the parser. As <code>&lt;nlohmann/json.hpp&gt;</code> includes them by default, every translation unit instantiates the parser, even if it never parses anything itself.</p> <p>Define <a href=../../api/macros/json_no_automatic_udls/ ><code>JSON_NO_AUTOMATIC_UDLS</code></a> for the whole project and include <code>&lt;nlohmann/json_literals.hpp&gt;</code> instead of <code>&lt;nlohmann/json.hpp&gt;</code> in the files that use the literals (it includes <code>&lt;nlohmann/json.hpp&gt;</code> itself):</p> <div class=highlight><pre><span></span><code><span class=nb>target_compile_definitions</span><span class=p>(</span><span class=s>my_target</span><span class=w> </span><span class=s>PRIVATE</span><span class=w> </span><span class=s>JSON_NO_AUTOMATIC_UDLS</span><span class=p>)</span>
</code></pre></div> <table> <thead> <tr> <th>Compiler</th> <th style="text-align: right;"><code>json.hpp</code> (<code>-O0</code>)</th> <th style="text-align: right;"><code>json_fwd.hpp</code> (<code>-O0</code>)</th> <th style="text-align: right;">Change</th> </tr> </thead> <tbody> <tr> <td>Apple clang</td> <td style="text-align: right;">868 ms</td> <td style="text-align: right;">332 ms</td> <td style="text-align: right;">−62%</td> </tr> <tr> <td><abbr title="GNU Compiler Collection">GCC</abbr> 16</td> <td style="text-align: right;">1164 ms</td> <td style="text-align: right;">210 ms</td> <td style="text-align: right;">−82%</td> </tr> </tbody> </table> <p class=nj-table-caption>Compile time of the header-only translation unit at <code>-O0</code> with each header, as the median wall-clock time of nine runs (see "Measurements" above). A negative change means a faster build.</p> <p>This is the most effective option, because it avoids the full header in every translation unit that includes <em>your</em> headers.</p> <h2 id=opt-out-of-the-automatic-user-defined-string-literals>Opt out of the automatic user-defined string literals<a class=headerlink href=#opt-out-of-the-automatic-user-defined-string-literals title="Permanent link">&para;</a></h2> <p>The user-defined string literals <a href=../../api/operator_literal_json/ ><code>operator""_json</code></a> and <a href=../../api/operator_literal_json_pointer/ ><code>operator""_json_pointer</code></a> are ordinary inline functions whose bodies call the parser. As <code>&lt;nlohmann/json.hpp&gt;</code> includes them by default, every translation unit instantiates the parser, even if it never parses anything itself.</p> <p>Define <a href=../../api/macros/json_no_automatic_udls/ ><code>JSON_NO_AUTOMATIC_UDLS</code></a> for the whole project and include <code>&lt;nlohmann/json_literals.hpp&gt;</code> instead of <code>&lt;nlohmann/json.hpp&gt;</code> in the files that use the literals (it includes <code>&lt;nlohmann/json.hpp&gt;</code> itself):</p> <div class=highlight><pre><span></span><code><span class=nb>target_compile_definitions</span><span class=p>(</span><span class=s>my_target</span><span class=w> </span><span class=s>PRIVATE</span><span class=w> </span><span class=s>JSON_NO_AUTOMATIC_UDLS</span><span class=p>)</span>
</code></pre></div> <div class=highlight><pre><span></span><code><span class=cp>#include</span><span class=w> </span><span class=cpf>&lt;nlohmann/json_literals.hpp&gt;</span><span class=w> </span><span class=c1>// only where "..."_json is used; includes &lt;nlohmann/json.hpp&gt;</span>
</code></pre></div> <p>The saving applies to translation units that do not parse <abbr title="JavaScript Object Notation">JSON</abbr>, for example ones that define types and their conversions or only pass <code>json</code> values around:</p> <table> <thead> <tr> <th>Compiler</th> <th>Translation unit</th> <th style="text-align: right;">Default (<code>-O0</code> / <code>-O2</code>)</th> <th style="text-align: right;"><code>JSON_NO_AUTOMATIC_UDLS</code> (<code>-O0</code> / <code>-O2</code>)</th> <th style="text-align: right;">Change</th> </tr> </thead> <tbody> <tr> <td>Apple clang</td> <td>model</td> <td style="text-align: right;">776 ms / 846 ms</td> <td style="text-align: right;">629 ms / 692 ms</td> <td style="text-align: right;">−19% / −18%</td> </tr> <tr> <td><abbr title="GNU Compiler Collection">GCC</abbr> 16</td> <td>model</td> <td style="text-align: right;">1022 ms / 1120 ms</td> <td style="text-align: right;">882 ms / 965 ms</td> <td style="text-align: right;">−14% / −14%</td> </tr> <tr> <td>Apple clang</td> <td>parsing</td> <td style="text-align: right;">992 ms / 1815 ms</td> <td style="text-align: right;">1006 ms / 1823 ms</td> <td style="text-align: right;">+1% / 0%</td> </tr> <tr> <td><abbr title="GNU Compiler Collection">GCC</abbr> 16</td> <td>parsing</td> <td style="text-align: right;">2018 ms / 3420 ms</td> <td style="text-align: right;">1990 ms / 3454 ms</td> <td style="text-align: right;">−1% / +1%</td> </tr> </tbody> </table> <p>Translation units that include only the header save up to a third. Translation units that parse anyway instantiate the parser regardless and see no difference.</p> <h2 id=instantiate-basic_json-once>Instantiate <code>basic_json</code> once<a class=headerlink href=#instantiate-basic_json-once title="Permanent link">&para;</a></h2> <p>Each translation unit instantiates the member functions of <code>nlohmann::json</code> it uses. An explicit instantiation declaration tells the compiler that the non-template members are instantiated elsewhere, so it can skip them:</p> <div class=highlight><span class=filename>json_instance.hpp</span><pre><span></span><code><span class=cp>#pragma once</span>
</code></pre></div> <p>The saving applies to translation units that do not parse <abbr title="JavaScript Object Notation">JSON</abbr>, for example ones that define types and their conversions or only pass <code>json</code> values around:</p> <table> <thead> <tr> <th>Compiler</th> <th>Translation unit</th> <th style="text-align: right;">Default (<code>-O0</code> / <code>-O2</code>)</th> <th style="text-align: right;"><code>JSON_NO_AUTOMATIC_UDLS</code> (<code>-O0</code> / <code>-O2</code>)</th> <th style="text-align: right;">Change</th> </tr> </thead> <tbody> <tr> <td>Apple clang</td> <td>model</td> <td style="text-align: right;">1051 ms / 1300 ms</td> <td style="text-align: right;">935 ms / 1170 ms</td> <td style="text-align: right;">−11% / −10%</td> </tr> <tr> <td><abbr title="GNU Compiler Collection">GCC</abbr> 16</td> <td>model</td> <td style="text-align: right;">1801 ms / 2106 ms</td> <td style="text-align: right;">1598 ms / 1983 ms</td> <td style="text-align: right;">−11% / −6%</td> </tr> <tr> <td>Apple clang</td> <td>parsing</td> <td style="text-align: right;">1157 ms / 2016 ms</td> <td style="text-align: right;">1146 ms / 2006 ms</td> <td style="text-align: right;">−1% / 0%</td> </tr> <tr> <td><abbr title="GNU Compiler Collection">GCC</abbr> 16</td> <td>parsing</td> <td style="text-align: right;">2529 ms / 4087 ms</td> <td style="text-align: right;">2484 ms / 4059 ms</td> <td style="text-align: right;">−2% / −1%</td> </tr> </tbody> </table> <p class=nj-table-caption>Compile time of the model and parsing translation units without and with <code>JSON_NO_AUTOMATIC_UDLS</code>, at <code>-O0</code> and <code>-O2</code>.</p> <p>The model translation unit saves about a tenth. Translation units that parse anyway instantiate the parser regardless and see no difference.</p> <h2 id=instantiate-basic_json-once>Instantiate <code>basic_json</code> once<a class=headerlink href=#instantiate-basic_json-once title="Permanent link">&para;</a></h2> <p>Each translation unit instantiates the member functions of <code>nlohmann::json</code> it uses. An explicit instantiation declaration tells the compiler that the non-template members are instantiated elsewhere, so it can skip them:</p> <div class=highlight><span class=filename>json_instance.hpp</span><pre><span></span><code><span class=cp>#pragma once</span>
<span class=cp>#include</span><span class=w> </span><span class=cpf>&lt;nlohmann/json.hpp&gt;</span>
<span class=k>extern</span><span class=w> </span><span class=k>template</span><span class=w> </span><span class=k>class</span><span class=w> </span><span class=nc>nlohmann</span><span class=o>::</span><span class=n>basic_json</span><span class=o>&lt;&gt;</span><span class=p>;</span>
</code></pre></div> <div class=highlight><span class=filename>json_instance.cpp</span><pre><span></span><code><span class=cp>#include</span><span class=w> </span><span class=cpf>"json_instance.hpp"</span>
<span class=k>template</span><span class=w> </span><span class=k>class</span><span class=w> </span><span class=nc>nlohmann</span><span class=o>::</span><span class=n>basic_json</span><span class=o>&lt;&gt;</span><span class=p>;</span>
</code></pre></div> <p>Include <code>json_instance.hpp</code> instead of <code>&lt;nlohmann/json.hpp&gt;</code> and compile and link <code>json_instance.cpp</code> once.</p> <table> <thead> <tr> <th>Compiler</th> <th>Translation unit</th> <th style="text-align: right;">Default (<code>-O0</code> / <code>-O2</code>)</th> <th style="text-align: right;"><code>extern template</code> (<code>-O0</code> / <code>-O2</code>)</th> <th style="text-align: right;">Change</th> </tr> </thead> <tbody> <tr> <td>Apple clang</td> <td>parsing</td> <td style="text-align: right;">992 ms / 1815 ms</td> <td style="text-align: right;">953 ms / 1625 ms</td> <td style="text-align: right;">−4% / −10%</td> </tr> <tr> <td><abbr title="GNU Compiler Collection">GCC</abbr> 16</td> <td>parsing</td> <td style="text-align: right;">2018 ms / 3420 ms</td> <td style="text-align: right;">1522 ms / 2728 ms</td> <td style="text-align: right;">−25% / −20%</td> </tr> <tr> <td>Apple clang</td> <td><code>json_instance.cpp</code></td> <td style="text-align: right;">—</td> <td style="text-align: right;">2166 ms / 4660 ms</td> <td style="text-align: right;">—</td> </tr> <tr> <td><abbr title="GNU Compiler Collection">GCC</abbr> 16</td> <td><code>json_instance.cpp</code></td> <td style="text-align: right;">—</td> <td style="text-align: right;">5085 ms / 10616 ms</td> <td style="text-align: right;">—</td> </tr> </tbody> </table> <p>Notes:</p> <ul> <li>The saving grows with the number of translation units that use <code>json</code>, while the instantiation translation unit is compiled only once (and is rarely recompiled, as it does not depend on your code).</li> <li>Member function templates (such as <code>get&lt;T&gt;()</code>, <code>parse(InputType&amp;&amp;)</code>, or <code>value(key, default)</code>) are not covered by the explicit instantiation and are still instantiated where they are used.</li> <li>The declaration covers exactly <code>nlohmann::json</code>. Add the same lines for <code>nlohmann::ordered_json</code> (<code>nlohmann::basic_json&lt;nlohmann::ordered_map&gt;</code>) or your own <code>basic_json</code> specializations if you use them.</li> </ul> <h2 id=use-c20-modules>Use C++20 modules<a class=headerlink href=#use-c20-modules title="Permanent link">&para;</a></h2> <p>With a toolchain that supports named modules, <code>import nlohmann.json;</code> compiles the library once into a module and avoids parsing the header in every translation unit. See <a href=../../features/modules/ >Modules</a> for requirements and known issues. Module support is experimental and currently depends heavily on the compiler version.</p> <h2 id=use-precompiled-headers>Use precompiled headers<a class=headerlink href=#use-precompiled-headers title="Permanent link">&para;</a></h2> <p>Build systems can precompile <code>&lt;nlohmann/json.hpp&gt;</code> together with other stable headers, for example with CMake's <a href="https://cmake.org/cmake/help/latest/command/target_precompile_headers.html"><code>target_precompile_headers</code></a>:</p> <div class=highlight><pre><span></span><code><span class=nb>target_precompile_headers</span><span class=p>(</span><span class=s>my_target</span><span class=w> </span><span class=s>PRIVATE</span><span class=w> </span><span class=s>&lt;nlohmann/json.hpp&gt;</span><span class=p>)</span>
</code></pre></div> <p>This removes the cost of parsing the header, but not of instantiating templates in each translation unit, so it combines well with the options above.</p> <h2 id=options-without-effect-on-compile-times>Options without effect on compile times<a class=headerlink href=#options-without-effect-on-compile-times title="Permanent link">&para;</a></h2> <p>Some configuration macros change what the library declares, but do not measurably change compile times:</p> <table> <thead> <tr> <th>Macro</th> <th style="text-align: right;">Apple clang, model (<code>-O0</code> / <code>-O2</code>)</th> <th style="text-align: right;"><abbr title="GNU Compiler Collection">GCC</abbr> 16, model (<code>-O0</code> / <code>-O2</code>)</th> </tr> </thead> <tbody> <tr> <td>default</td> <td style="text-align: right;">776 ms / 846 ms</td> <td style="text-align: right;">1022 ms / 1120 ms</td> </tr> <tr> <td><a href=../../api/macros/json_no_io/ ><code>JSON_NO_IO</code></a></td> <td style="text-align: right;">764 ms / 836 ms</td> <td style="text-align: right;">1022 ms / 1117 ms</td> </tr> <tr> <td><a href=../../api/macros/json_use_global_udls/ ><code>JSON_USE_GLOBAL_UDLS</code></a><code>=0</code></td> <td style="text-align: right;">763 ms / 852 ms</td> <td style="text-align: right;">1019 ms / 1106 ms</td> </tr> </tbody> </table> <p><code>JSON_USE_GLOBAL_UDLS</code> only controls <em>where</em> the literals are declared; to avoid their cost, use <code>JSON_NO_AUTOMATIC_UDLS</code> instead.</p> <h2 id=see-also>See also<a class=headerlink href=#see-also title="Permanent link">&para;</a></h2> <ul> <li><a href=../../api/macros/json_no_automatic_udls/ ><code>JSON_NO_AUTOMATIC_UDLS</code></a> - do not include the user-defined string literals automatically</li> <li><a href=../../features/modules/ >Modules</a> - C++20 module support</li> <li><a href=../ >Header only</a> - including the library</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 14, 2026 15:40:55 UTC">October 14, 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.8Line truncated
</code></pre></div> <p>Include <code>json_instance.hpp</code> instead of <code>&lt;nlohmann/json.hpp&gt;</code> and compile and link <code>json_instance.cpp</code> once.</p> <table> <thead> <tr> <th>Compiler</th> <th>Translation unit</th> <th style="text-align: right;">Default (<code>-O0</code> / <code>-O2</code>)</th> <th style="text-align: right;"><code>extern template</code> (<code>-O0</code> / <code>-O2</code>)</th> <th style="text-align: right;">Change</th> </tr> </thead> <tbody> <tr> <td>Apple clang</td> <td>parsing</td> <td style="text-align: right;">1157 ms / 2016 ms</td> <td style="text-align: right;">1071 ms / 1844 ms</td> <td style="text-align: right;">−7% / −9%</td> </tr> <tr> <td><abbr title="GNU Compiler Collection">GCC</abbr> 16</td> <td>parsing</td> <td style="text-align: right;">2529 ms / 4087 ms</td> <td style="text-align: right;">1994 ms / 3190 ms</td> <td style="text-align: right;">−21% / −22%</td> </tr> <tr> <td>Apple clang</td> <td><code>json_instance.cpp</code></td> <td style="text-align: right;">—</td> <td style="text-align: right;">2573 ms / 6221 ms</td> <td style="text-align: right;">—</td> </tr> <tr> <td><abbr title="GNU Compiler Collection">GCC</abbr> 16</td> <td><code>json_instance.cpp</code></td> <td style="text-align: right;">—</td> <td style="text-align: right;">6489 ms / 13759 ms</td> <td style="text-align: right;">—</td> </tr> </tbody> </table> <p class=nj-table-caption>Compile time of the parsing translation unit without and with the <code>extern template</code> declaration, at <code>-O0</code> and <code>-O2</code>. The <code>json_instance.cpp</code> rows are the one-off cost of the translation unit that holds the instantiation.</p> <p>Notes:</p> <ul> <li>The saving grows with the number of translation units that use <code>json</code>, while the instantiation translation unit is compiled only once (and is rarely recompiled, as it does not depend on your code).</li> <li>Member function templates (such as <code>get&lt;T&gt;()</code>, <code>parse(InputType&amp;&amp;)</code>, or <code>value(key, default)</code>) are not covered by the explicit instantiation and are still instantiated where they are used.</li> <li>The declaration covers exactly <code>nlohmann::json</code>. Add the same lines for <code>nlohmann::ordered_json</code> (<code>nlohmann::basic_json&lt;nlohmann::ordered_map&gt;</code>) or your own <code>basic_json</code> specializations if you use them.</li> </ul> <h2 id=switch-off-filesystem-support>Switch off filesystem support<a class=headerlink href=#switch-off-filesystem-support title="Permanent link">&para;</a></h2> <p>With C++17, the library includes <code>&lt;filesystem&gt;</code> to provide conversions from and to <code>std::filesystem::path</code> (see <a href=../../api/macros/json_has_filesystem/ ><code>JSON_HAS_FILESYSTEM</code></a>). Parsing <code>&lt;filesystem&gt;</code> is a fixed cost in every translation unit, and it also pulls in <code>&lt;iomanip&gt;</code>, <code>&lt;istream&gt;</code>, and <code>&lt;ostream&gt;</code> (and, with <abbr title="GNU Compiler Collection">GCC</abbr>'s libstdc++, <code>&lt;sstream&gt;</code>). If you do not convert paths, switch the detection off for the whole project:</p> <div class=highlight><pre><span></span><code><span class=nb>target_compile_definitions</span><span class=p>(</span><span class=s>my_target</span><span class=w> </span><span class=s>PRIVATE</span><span class=w> </span><span class=s>JSON_HAS_FILESYSTEM=0</span><span class=w> </span><span class=s>JSON_HAS_EXPERIMENTAL_FILESYSTEM=0</span><span class=p>)</span>
</code></pre></div> <table> <thead> <tr> <th>Compiler</th> <th>Translation unit</th> <th style="text-align: right;">Default (<code>-O0</code> / <code>-O2</code>)</th> <th style="text-align: right;">Filesystem off (<code>-O0</code> / <code>-O2</code>)</th> <th style="text-align: right;">Change</th> </tr> </thead> <tbody> <tr> <td>Apple clang</td> <td>model</td> <td style="text-align: right;">1051 ms / 1300 ms</td> <td style="text-align: right;">998 ms / 1257 ms</td> <td style="text-align: right;">−5% / −3%</td> </tr> <tr> <td><abbr title="GNU Compiler Collection">GCC</abbr> 16</td> <td>model</td> <td style="text-align: right;">1801 ms / 2106 ms</td> <td style="text-align: right;">1636 ms / 2008 ms</td> <td style="text-align: right;">−9% / −5%</td> </tr> <tr> <td>Apple clang</td> <td>parsing</td> <td style="text-align: right;">1157 ms / 2016 ms</td> <td style="text-align: right;">1107 ms / 1971 ms</td> <td style="text-align: right;">−4% / −2%</td> </tr> <tr> <td><abbr title="GNU Compiler Collection">GCC</abbr> 16</td> <td>parsing</td> <td style="text-align: right;">2529 ms / 4087 ms</td> <td style="text-align: right;">2405 ms / 4029 ms</td> <td style="text-align: right;">−5% / −1%</td> </tr> </tbody> </table> <p class=nj-table-caption>Compile time with <code>JSON_HAS_FILESYSTEM=0</code> and <code>JSON_HAS_EXPERIMENTAL_FILESYSTEM=0</code> against the default configuration, at <code>-O0</code> and <code>-O2</code>.</p> <p>Only with filesystem support switched off does <a href=../../api/macros/json_no_io/ ><code>JSON_NO_IO</code></a> remove the stream headers from the translation unit, so the two macros save most together:</p> <table> <thead> <tr> <th>Compiler</th> <th>Translation unit</th> <th style="text-align: right;">Default (<code>-O0</code> / <code>-O2</code>)</th> <th style="text-align: right;">Filesystem off and <code>JSON_NO_IO</code> (<code>-O0</code> / <code>-O2</code>)</th> <th style="text-align: right;">Change</th> </tr> </thead> <tbody> <tr> <td>Apple clang</td> <td>model</td> <td style="text-align: right;">1051 ms / 1300 ms</td> <td style="text-align: right;">969 ms / 1219 ms</td> <td style="text-align: right;">−8% / −6%</td> </tr> <tr> <td><abbr title="GNU Compiler Collection">GCC</abbr> 16</td> <td>model</td> <td style="text-align: right;">1801 ms / 2106 ms</td> <td style="text-align: right;">1605 ms / 1982 ms</td> <td style="text-align: right;">−11% / −6%</td> </tr> <tr> <td>Apple clang</td> <td>parsing</td> <td style="text-align: right;">1157 ms / 2016 ms</td> <td style="text-align: right;">1083 ms / 1947 ms</td> <td style="text-align: right;">−6% / −3%</td> </tr> <tr> <td><abbr title="GNU Compiler Collection">GCC</abbr> 16</td> <td>parsing</td> <td style="text-align: right;">2529 ms / 4087 ms</td> <td style="text-align: right;">2384 ms / 3972 ms</td> <td style="text-align: right;">−6% / −3%</td> </tr> </tbody> </table> <p class=nj-table-caption>Compile time with filesystem support switched off and <code>JSON_NO_IO</code> defined, against the default configuration, at <code>-O0</code> and <code>-O2</code>.</p> <p>The trade-off: the library's own <code>to_json</code>/<code>from_json</code> overloads for <code>std::filesystem::path</code> (which store the path as <abbr title="Unicode Transformation Format">UTF</abbr>-8) are removed. In a test with both compilers on macOS, paths still converted through their implicit conversion from and to <code>std::string</code>; whether that fallback exists depends on the platform's native path string type (<code>std::wstring</code> on Windows), so check code that converts paths. <code>JSON_NO_IO</code> additionally removes all stream functions (see <a href=#options-without-effect-on-compile-times>Options without effect</a>).</p> <h2 id=use-c20-modules>Use C++20 modules<a class=headerlink href=#use-c20-modules title="Permanent link">&para;</a></h2> <p>With a toolchain that supports named modules, <code>import nlohmann.json;</code> compiles the library once into a module and avoids parsing the header in every translation unit. See <a href=../../features/modules/ >Modules</a> for requirements and known issues. Module support is experimental and currently depends heavily on the compiler version.</p> <div class=nj-fig-wrap> <p><svg class="nj-fig nj-charts nj-charts--compile" viewbox="0 0 440 282" role=img aria-labelledby="nj-charts-mod nj-charts-mod-d" xmlns=http://www.w3.org/2000/svg> <title id=nj-charts-mod>Compile time per translation unit with import nlohmann.json at -O0, and the one-off cost of the module</title> <desc id=nj-charts-mod-d>Horizontal bars, negative is faster, medians of nine runs, -std=c++20 for both. model TU, Apple clang: -48 % (1311 ms to 680 ms); model TU, G&#8203;CC 16: -47 % (2156 ms to 1153 ms); parsing TU, Apple clang: -42 % (1387 ms to 800 ms); parsing TU, G&#8203;CC 16: -36 % (2838 ms to 1810 ms); one-off build, Apple clang: 1630 ms; one-off build, G&#8203;CC 16: 2538 ms.</desc> <rect class=nj-charts-cLine truncated
clang++<span class=w> </span>-std<span class=o>=</span>c++20<span class=w> </span>-fcxx-modules<span class=w> </span>-O0<span class=w> </span>-I<span class=w> </span>single_include<span class=w> </span>-c<span class=w> </span>-x<span class=w> </span>c++-module<span class=w> </span>src/modules/json.cppm<span class=w> </span><span class=se>\</span>
<span class=w> </span>-fmodule-output<span class=o>=</span>nlohmann.json.pcm<span class=w> </span>-o<span class=w> </span>json_mod.o
clang++<span class=w> </span>-std<span class=o>=</span>c++20<span class=w> </span>-fcxx-modules<span class=w> </span>-O0<span class=w> </span>-fmodule-file<span class=o>=</span>nlohmann.json<span class=o>=</span>nlohmann.json.pcm<span class=w> </span>-c<span class=w> </span>model.cpp
<span class=c1># GCC 16 (Homebrew on macOS): writes gcm.cache/nlohmann.json.gcm, found by the importing translation unit</span>
g++-16<span class=w> </span>--sysroot<span class=o>=</span><span class=k>$(</span>xcrun<span class=w> </span>--show-sdk-path<span class=k>)</span><span class=w> </span>-std<span class=o>=</span>c++20<span class=w> </span>-fmodules<span class=w> </span>-O0<span class=w> </span>-I<span class=w> </span>single_include<span class=w> </span>-c<span class=w> </span>src/modules/json.cppm<span class=w> </span><span class=se>\</span>
<span class=w> </span>-o<span class=w> </span>json_mod.o
g++-16<span class=w> </span>--sysroot<span class=o>=</span><span class=k>$(</span>xcrun<span class=w> </span>--show-sdk-path<span class=k>)</span><span class=w> </span>-std<span class=o>=</span>c++20<span class=w> </span>-fmodules<span class=w> </span>-O0<span class=w> </span>-c<span class=w> </span>model.cpp
</code></pre></div> <p>Link <code>json_mod.o</code> into the program.</p> </details> <h2 id=use-precompiled-headers>Use precompiled headers<a class=headerlink href=#use-precompiled-headers title="Permanent link">&para;</a></h2> <p>Build systems can precompile <code>&lt;nlohmann/json.hpp&gt;</code> together with other stable headers, for example with CMake's <a href="https://cmake.org/cmake/help/latest/command/target_precompile_headers.html"><code>target_precompile_headers</code></a>:</p> <div class=highlight><pre><span></span><code><span class=nb>target_precompile_headers</span><span class=p>(</span><span class=s>my_target</span><span class=w> </span><span class=s>PRIVATE</span><span class=w> </span><span class=s>&lt;nlohmann/json.hpp&gt;</span><span class=p>)</span>
</code></pre></div> <p>This removes the cost of parsing the header, but not of instantiating templates in each translation unit, so it combines well with the options above.</p> <div class=nj-fig-wrap> <p><svg class="nj-fig nj-charts nj-charts--compile" viewbox="0 0 440 282" role=img aria-labelledby="nj-charts-pch nj-charts-pch-d" xmlns=http://www.w3.org/2000/svg> <title id=nj-charts-pch>Compile time per translation unit with a precompiled json.hpp at -O0, and the one-off cost of the PCH</title> <desc id=nj-charts-pch-d>Horizontal bars, negative is faster, medians of nine runs, -std=c++17. model TU, Apple clang: -43 % (1045 ms to 594 ms); model TU, G&#8203;CC 16: -41 % (1789 ms to 1060 ms); parsing TU, Apple clang: -38 % (1148 ms to 714 ms); parsing TU, G&#8203;CC 16: -25 % (2498 ms to 1884 ms); one-off build, Apple clang: 771 ms; one-off build, G&#8203;CC 16: 2254 ms.</desc> <rect class=nj-charts-clang x=8 y=10 width=12 height=12 rx=3 /> <text class=nj-charts-leg x=26 y=20.5>Apple clang</text> <rect class=nj-charts-gcc x=126 y=10 width=12 height=12 rx=3 /> <text class=nj-charts-leg x=144 y=20.5>G&#8203;CC 16</text> <line class=nj-charts-grid x1=68.6667 y1=58 x2=68.6667 y2=160 /> <text class=nj-charts-tick x=68.6667 y=176>&#8722;50 %</text> <line class=nj-charts-grid x1=220.333 y1=58 x2=220.333 y2=160 /> <text class=nj-charts-tick x=220.333 y=176>&#8722;25 %</text> <text class=nj-charts-tick x=372 y=176>0 %</text> <text class="nj-charts-tick nj-charts-tick--l" x=8 y=194>&#8592; faster per TU</text> <text class=nj-charts-rowlabel x=8 y=62>model TU<tspan class=nj-charts-sub> &#183; with PCH</tspan></text> <path class=nj-charts-clang d="M372 70 H115.133 Q111.133 70 111.133 74 V78 Q111.133 82 115.133 82 H372 Z"><title>model TU, Apple clang: 1045 ms by default, 594 ms with PCH (&#8722;43 %)</title></path> <text class=nj-charts-val x=377 y=80.5>&#8722;43 %</text> <path class=nj-charts-gcc d="M372 84 H127.267 Q123.267 84 123.267 88 V92 Q123.267 96 127.267 96 H372 Z"><title>model TU, G&#8203;CC 16: 1789 ms by default, 1060 ms with PCH (&#8722;41 %)</title></path> <text class=nj-charts-val x=377 y=94.5>&#8722;41 %</text> <text class=nj-charts-rowlabel x=8 y=114>parsing TU<tspan class=nj-charts-sub> &#183; with PCH</tspan></text> <path class=nj-charts-clang d="M372 122 H145.467 Q141.467 122 141.467 126 V130 Q141.467 134 145.467 134 H372 Z"><title>parsing TU, Apple clang: 1148 ms by default, 714 ms with PCH (&#8722;38 %)</title></path> <text class=nj-charts-val x=377 y=132.5>&#8722;38 %</text> <path class=nj-charts-gcc d="M372 136 H224.333 Q220.333 136 220.333 140 V144 Q220.333 148 224.333 148 H372 Z"><title>parsing TU, G&#8203;CC 16: 2498 ms by default, 1884 ms with PCH (&#8722;25 %)</title></path> <text class=nj-charts-val x=377 y=146.5>&#8722;25 %</text> <line class=nj-charts-zero x1=372 y1=58 x2=372 y2=160 /> <text class=nj-charts-rowlabel x=8 y=222>One-off cost<tspan class=nj-charts-sub> &#183; built once, reused by every TU</tspan></text> <rect class=nj-charts-clang x=8 y=232.5 width=10 height=10 rx=2.5 /> <text class=nj-charts-val x=24 y=242>Apple clang: 771 ms once, saves 434 to 451 ms per TU</text> <rect class=nj-charts-gcc x=8 y=252.5 width=10 height=10 rx=2.5 /> <text class=nj-charts-val x=24 y=262>G&#8203;CC 16: 2254 ms once, saves 614 to 729 ms per TU</text> </svg></p> </div> <p class=nj-caption>Change in compile time per translation unit at <code>-O0</code> with a precompiled <code>&lt;nlohmann/json.hpp&gt;</code> (<code>-std=c++17</code>), and the one-off cost of building the precompiled header, taken from the table below.</p> <table> <thead> <tr> <th>Compiler</th> <th>Translation unit</th> <th style="text-align: right;">Default (<code>-O0</code> / <code>-O2</code>)</th> <th style="text-align: right;">With PCH (<code>-O0</code> / <code>-O2</code>)</th> <th style="text-align: right;">Change</th> </tr> </thead> <tbody> <tr> <td>Apple clang</td> <td>model</td> <td style="text-align: right;">1045 ms / 1295 ms</td> <td style="text-align: right;">594 ms / 842 ms</td> <td style="text-align: right;">−43% / −35%</td> </tr> <tr> <td><abbr title="GNU Compiler Collection">GCC</abbr> 16</td> <td>model</td> <td style="text-align: right;">1789 ms / 2101 ms</td> <td style="text-align: right;">1060 ms / 1376 ms</td> <td style="text-align: right;">−41% / −35%</td> </tr> <tr> <td>Apple clang</td> <td>parsing</td> <td style="text-align: right;">1148 ms / 2008 ms</td> <td style="text-align: right;">714 ms / 1584 ms</td> <td style="text-align: right;">−38% / −21%</td> </tr> <tr> <td><abbr title="GNU Compiler Collection">GCC</abbr> 16</td> <td>parsing</td> <td style="text-align: right;">2498 ms / 4075 ms</td> <td style="text-align: right;">1884 ms / 3452 ms</td> <td style="text-align: right;">−25% / −15%</td> </tr> <tr> <td>Apple clang</td> <td>building the PCH</td> <td style="text-align: right;">—</td> <td style="text-align: right;">771 ms / 760 ms</td> <td style="text-align: right;">—</td> </tr> <tr> <tdLine truncated
clang++<span class=w> </span>-std<span class=o>=</span>c++17<span class=w> </span>-O0<span class=w> </span>-I<span class=w> </span>single_include<span class=w> </span>-x<span class=w> </span>c++-header<span class=w> </span>pch.hpp<span class=w> </span>-o<span class=w> </span>pch.hpp.pch
clang++<span class=w> </span>-std<span class=o>=</span>c++17<span class=w> </span>-O0<span class=w> </span>-I<span class=w> </span>single_include<span class=w> </span>-include-pch<span class=w> </span>pch.hpp.pch<span class=w> </span>-include<span class=w> </span>pch.hpp<span class=w> </span>-c<span class=w> </span>model.cpp
<span class=c1># GCC 16 (Homebrew on macOS): pch.hpp.gch next to pch.hpp is picked up by -include pch.hpp</span>
g++-16<span class=w> </span>--sysroot<span class=o>=</span><span class=k>$(</span>xcrun<span class=w> </span>--show-sdk-path<span class=k>)</span><span class=w> </span>-std<span class=o>=</span>c++17<span class=w> </span>-O0<span class=w> </span>-I<span class=w> </span>single_include<span class=w> </span>-x<span class=w> </span>c++-header<span class=w> </span>pch.hpp<span class=w> </span>-o<span class=w> </span>pch.hpp.gch
g++-16<span class=w> </span>--sysroot<span class=o>=</span><span class=k>$(</span>xcrun<span class=w> </span>--show-sdk-path<span class=k>)</span><span class=w> </span>-std<span class=o>=</span>c++17<span class=w> </span>-O0<span class=w> </span>-I<span class=w> </span>single_include<span class=w> </span>-include<span class=w> </span>pch.hpp<span class=w> </span>-Winvalid-pch<span class=w> </span>-c<span class=w> </span>model.cpp
</code></pre></div> </details> <h2 id=options-without-effect-on-compile-times>Options without effect on compile times<a class=headerlink href=#options-without-effect-on-compile-times title="Permanent link">&para;</a></h2> <p>Some configuration macros change what the library declares, but do not measurably change compile times:</p> <table> <thead> <tr> <th>Macro</th> <th style="text-align: right;">Apple clang, model (<code>-O0</code> / <code>-O2</code>)</th> <th style="text-align: right;"><abbr title="GNU Compiler Collection">GCC</abbr> 16, model (<code>-O0</code> / <code>-O2</code>)</th> </tr> </thead> <tbody> <tr> <td>default</td> <td style="text-align: right;">1051 ms / 1300 ms</td> <td style="text-align: right;">1801 ms / 2106 ms</td> </tr> <tr> <td><a href=../../api/macros/json_no_io/ ><code>JSON_NO_IO</code></a></td> <td style="text-align: right;">1034 ms / 1288 ms</td> <td style="text-align: right;">1756 ms / 2087 ms</td> </tr> <tr> <td><a href=../../api/macros/json_use_global_udls/ ><code>JSON_USE_GLOBAL_UDLS</code></a><code>=0</code></td> <td style="text-align: right;">1055 ms / 1298 ms</td> <td style="text-align: right;">1783 ms / 2109 ms</td> </tr> </tbody> </table> <p class=nj-table-caption>Compile time of the model translation unit with each macro defined; the first row is the default configuration for comparison.</p> <p><code>JSON_NO_IO</code> removes the library's stream functions and its <code>#include</code>s of <code>&lt;istream&gt;</code> and <code>&lt;ostream&gt;</code>, but with the default C++17 settings <code>&lt;filesystem&gt;</code> includes these headers anyway (and with libc++, <code>&lt;vector&gt;</code> already includes <code>&lt;streambuf&gt;</code> via <code>&lt;locale&gt;</code>), so only a few hundred lines of the library's own stream code go away. It also does not change what the translation unit instantiates. On the parsing translation unit, the change is 0% / 0% (Apple clang) and −2% / 0% (<abbr title="GNU Compiler Collection">GCC</abbr>). To get a saving, combine it with <a href=#switch-off-filesystem-support>switching off filesystem support</a>.</p> <p><code>JSON_USE_GLOBAL_UDLS</code> only controls <em>where</em> the literals are declared; to avoid their cost, use <code>JSON_NO_AUTOMATIC_UDLS</code> instead.</p> <h2 id=see-also>See also<a class=headerlink href=#see-also title="Permanent link">&para;</a></h2> <ul> <li><a href=../../api/macros/json_has_filesystem/ ><code>JSON_HAS_FILESYSTEM</code></a> - control the conversions from and to <code>std::filesystem::path</code></li> <li><a href=../../api/macros/json_no_automatic_udls/ ><code>JSON_NO_AUTOMATIC_UDLS</code></a> - do not include the user-defined string literals automatically</li> <li><a href=../../features/modules/ >Modules</a> - C++20 module support</li> <li><a href=../ >Header only</a> - including the library</li> <li><a href=../../api/macros/ >Macros</a> - reference of all macros</li> <li><a href=../../features/macros/ >Supported Macros</a> - overview of all configuration macros</li> <li><a href=../../features/performance/ >Performance</a> - run-time performance</li> <li><a href=../../api/macros/json_no_io/ ><code>JSON_NO_IO</code></a> - remove all stream and file I/O</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 27, 2026 06:41:42 UTC">October 27, 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> <nav class="md-footer__inner md-grid" aria-label=Footer> <a href=../pkg-config/ class="md-footer__link md-footer__link--prev" aria-label="Previous: Pkg-config"> <div class="md-footer__button md-icon"> <svLine truncated