</code></pre></div></div><p>The package configuration file, <code>nlohmann_jsonConfig.cmake</code>, can be used either from an install tree or directly out of the build tree.</p><h3id=embedded>Embedded<aclass=headerlinkhref=#embeddedtitle="Permanent link">¶</a></h3><p>To embed the library directly into an existing CMake project, place the entire source tree in a subdirectory and call <code>add_subdirectory()</code> in your <code>CMakeLists.txt</code> file.</p><divclass="admonition example"><pclass=admonition-title>Example</p><divclass=highlight><spanclass=filename>CMakeLists.txt</span><pre><span></span><code><spanclass=nb>cmake_minimum_required</span><spanclass=p>(</span><spanclass=s>VERSION</span><spanclass=w></span><spanclass=s>3.1</span><spanclass=p>)</span>
</code></pre></div></div><divclass="admonition note"><pclass=admonition-title>Note</p><p>Do not use <codeclass=highlight><spanclass=nb>include</span><spanclass=p>(</span><spanclass=s>nlohmann_json/CMakeLists.txt</span><spanclass=p>)</span></code>, since that carries with it unintended consequences that will break the build. It is generally discouraged (although not necessarily well documented as such) to use <codeclass=highlight><spanclass=nb>include</span><spanclass=p>(</span><spanclass=s>...</span><spanclass=p>)</span></code> for pulling in other CMake projects anyways.</p></div><h3id=supporting-both>Supporting Both<aclass=headerlinkhref=#supporting-bothtitle="Permanent link">¶</a></h3><p>To allow your project to support either an externally supplied or an embedded JSON library, you can use a pattern akin to the following.</p><divclass="admonition example"><pclass=admonition-title>Example</p><divclass=highlight><spanclass=filename>CMakeLists.txt</span><pre><span></span><code><spanclass=nb>project</span><spanclass=p>(</span><spanclass=s>ExampleProject</span><spanclass=w></span><spanclass=s>LANGUAGES</span><spanclass=w></span><spanclass=s>CXX</span><spanclass=p>)</span>
<spanclass=nb>option</span><spanclass=p>(</span><spanclass=s>EXAMPLE_USE_EXTERNAL_JSON</span><spanclass=w></span><spanclass=s2>"Use an external JSON library"</span><spanclass=w></span><spanclass=s>OFF</span><spanclass=p>)</span>
</code></pre></div><p><code>thirdparty/nlohmann_json</code> is then a complete copy of this source tree.</p></div><h3id=fetchcontent>FetchContent<aclass=headerlinkhref=#fetchcontenttitle="Permanent link">¶</a></h3><p>Since CMake v3.11, <ahref=https://cmake.org/cmake/help/v3.11/module/FetchContent.html>FetchContent</a> can be used to automatically download a release as a dependency at configure type.</p><divclass="admonition example"><pclass=admonition-title>Example</p><divclass=highlight><spanclass=filename>CMakeLists.txt</span><pre><span></span><code><spanclass=nb>cmake_minimum_required</span><spanclass=p>(</span><spanclass=s>VERSION</span><spanclass=w></span><spanclass=s>3.11</span><spanclass=p>)</span>
</code></pre></div></div><divclass="admonition note"><pclass=admonition-title>Note</p><p>It is recommended to use the URL approach described above which is supported as of version 3.10.0. It is also possible to pass the Git repository like</p><divclass=highlight><pre><span></span><code><spanclass=nb>FetchContent_Declare</span><spanclass=p>(</span><spanclass=s>json</span>
</code></pre></div><p>However, the repository <ahref=https://github.com/nlohmann/json>https://github.com/nlohmann/json</a> download size is quite large. You might want to depend on a smaller repository. For instance, you might want to replace the URL in the example by <ahref=https://github.com/ArthurSonzogni/nlohmann_json_cmake_fetchcontent>https://github.com/ArthurSonzogni/nlohmann_json_cmake_fetchcontent</a>.</p></div><h2id=cmake-options>CMake Options<aclass=headerlinkhref=#cmake-optionstitle="Permanent link">¶</a></h2><h3id=json_buildtests><code>JSON_BuildTests</code><aclass=headerlinkhref=#json_buildteststitle="Permanent link">¶</a></h3><p>Build the unit tests when <ahref=https://cmake.org/cmake/help/latest/command/enable_testing.html><code>BUILD_TESTING</code></a> is enabled. This option is <code>ON</code> by default if the library's CMake project is the top project. That is, when integrating the library as described above, the test suite is not built unless explicitly switched on with this option.</p><h3id=json_ci><code>JSON_CI</code><aclass=headerlinkhref=#json_cititle="Permanent link">¶</a></h3><p>Enable CI build targets. The exact targets are used during the several CI steps and are subject to change without notice. This option is <code>OFF</code> by default.</p><h3id=json_diagnostics><code>JSON_Diagnostics</code><aclass=headerlinkhref=#json_diagnosticstitle="Permanent link">¶</a></h3><p>Enable <ahref=../../home/exceptions/#extended-diagnostic-messages>extended diagnostic messages</a> by defining macro <ahref=../../api/macros/json_diagnostics/><code>JSON_DIAGNOSTICS</code></a>. This option is <code>OFF</code> by default.</p><h3id=json_disableenumserialization><code>JSON_DisableEnumSerialization</code><aclass=headerlinkhref=#json_disableenumserializationtitle="Permanent link">¶</a></h3><p>Disable default <code>enum</code> serialization by defining the macro <ahref=../../api/macros/json_disable_enum_serialization/><code>JSON_DISABLE_ENUM_SERIALIZATION</code></a>. This option is <code>OFF</code> by default.</p><h3id=json_fasttests><code>JSON_FastTests</code><aclass=headerlinkhref=#json_fastteststitle="Permanent link">¶</a></h3><p>Skip expensive/slow test suites. This option is <code>OFF</code> by default. Depends on <code>JSON_BuildTests</code>.</p><h3id=json_globaludls><code>JSON_GlobalUDLs</code><aclass=headerlinkhref=#json_globaludlstitle="Permanent link">¶</a></h3><p>Place user-defined string literals in the global namespace by defining the macro <ahref=../../api/macros/json_use_global_udls/><code>JSON_USE_GLOBAL_UDLS</code></a>. This option is <code>OFF</code> by default.</p><h3id=json_implicitconversions><code>JSON_ImplicitConversions</code><aclass=headerlinkhref=#json_implicitconversionstitle="Permanent link">¶</a></h3><p>Enable implicit conversions by defining macro <ahref=../../api/macros/json_use_implicit_conversions/><code>JSON_USE_IMPLICIT_CONVERSIONS</code></a>. This option is <code>ON</code> by default.</p><h3id=json_install><code>JSON_Install</code><aclass=headerlinkhref=#json_installtitle="Permanent link">¶</a></h3><p>Install CMake targets during install step. This option is <code>ON</code> by default if the library's CMake project is the top project.</p><h3id=json_multipleheaders><code>JSON_MultipleHeaders</code><aclass=headerlinkhref=#json_multipleheaderstitle="Permanent link">¶</a></h3><p>Use non-amalgamated version of the library. This option is <code>OFF</code> by default.</p><h3id=json_systeminclude><code>JSON_SystemInclude</code><aclass=headerlinkhref=#json_systemincludetitle="Permanent link">¶</a></h3><p>Treat the library headers like system headers (i.e., adding <code>SYSTEM</code> to the <ahref=https://cmake.org/cmake/help/latest/command/target_include_directories.html><code>target_include_directories</code></a> call) to checks for this library by tools like Clang-Tidy. This option is <code>OFF</code> by default.</p><h3id=json_valgrind><code>JSON_Valgrin