Package Managers¶
Throughout this page, we will describe how to compile the example file example.cpp
below.
#include <nlohmann/json.hpp>
#include <iostream>
#include <iomanip>
using json = nlohmann::json;
int main()
{
std::cout << std::setw(4) << json::meta() << std::endl;
}
When executed, this program should create output similar to
{
"compiler": {
"c++": "201103",
"family": "gcc",
"version": "12.3.0"
},
"copyright": "(C) 2013-2022 Niels Lohmann",
"name": "JSON for Modern C++",
"platform": "apple",
"url": "https://github.com/nlohmann/json",
"version": {
"major": 3,
"minor": 11,
"patch": 3,
"string": "3.11.3"
}
}
Homebrew¶
If you are using OS X and Homebrew, just type
brew install nlohmann-json
and you're set. If you want the bleeding edge rather than the latest release, use
brew install nlohmann-json --HEAD
instead. See nlohmann-json for more information.
Example
-
Create the following file:
example.cpp#include <nlohmann/json.hpp> #include <iostream> #include <iomanip> using json = nlohmann::json; int main() { std::cout << std::setw(4) << json::meta() << std::endl; }
-
Install the package
brew install nlohmann-json
-
Determine the include path, which defaults to
/usr/local/Cellar/nlohmann-json/$version/include
, where$version
is the version of the library, e.g.3.7.3
. The path of the library can be determined withbrew list nlohmann-json
-
Compile the code. For instance, the code can be compiled using Clang with
clang++ example.cpp -I/usr/local/Cellar/nlohmann-json/3.7.3/include -std=c++11 -o example
The formula is updated automatically.
Meson¶
If you are using the Meson Build System, add this source tree as a meson subproject. You may also use the include.zip
published in this project's Releases to reduce the size of the vendored source tree. Alternatively, you can get a wrap file by downloading it from Meson WrapDB, or simply use meson wrap install nlohmann_json
. Please see the meson project for any issues regarding the packaging.
The provided meson.build
can also be used as an alternative to cmake for installing nlohmann_json
system-wide in which case a pkg-config file is installed. To use it, simply have your build system require the nlohmann_json
pkg-config dependency. In Meson, it is preferred to use the dependency()
object with a subproject fallback, rather than using the subproject directly.
Bazel¶
This repository provides a Bazel WORKSPACE.bazel
and a corresponding BUILD.bazel
file. Therefore, this repository can be referenced by workspace rules such as http_archive
, git_repository
, or local_repository
from other Bazel workspaces. To use the library you only need to depend on the target @nlohmann_json//:json
(e.g. via deps
attribute).
Conan¶
If you are using Conan to manage your dependencies, merely add nlohmann_json/x.y.z
to your conanfile
's requires, where x.y.z
is the release version you want to use. Please file issues here if you experience problems with the packages.
Example
-
Create the following files:
Conanfile.txt[requires] nlohmann_json/3.7.3 [generators] cmake
CMakeLists.txtproject(json_example) cmake_minimum_required(VERSION 2.8.12) add_definitions("-std=c++11") include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) conan_basic_setup() add_executable(json_example example.cpp) target_link_libraries(json_example ${CONAN_LIBS})
example.cpp#include <nlohmann/json.hpp> #include <iostream> using json = nlohmann::json; int main() { std::cout << json::meta() << std::endl; }
-
Build:
mkdir build cd build conan install .. cmake .. cmake --build .
The package is updated automatically.
Spack¶
If you are using Spack to manage your dependencies, you can use the nlohmann-json
package. Please see the spack project for any issues regarding the packaging.
Hunter¶
If you are using hunter on your project for external dependencies, then you can use the nlohmann_json package. Please see the hunter project for any issues regarding the packaging.
Buckaroo¶
If you are using Buckaroo, you can install this library's module with buckaroo add github.com/buckaroo-pm/nlohmann-json
. Please file issues here. There is a demo repo here.
vcpkg¶
If you are using vcpkg on your project for external dependencies, then you can install the nlohmann-json package with vcpkg install nlohmann-json
and follow the then displayed descriptions. Please see the vcpkg project for any issues regarding the packaging.
Example
-
Create the following files:
CMakeLists.txtproject(json_example) cmake_minimum_required(VERSION 2.8.12) find_package(nlohmann_json CONFIG REQUIRED) add_executable(json_example example.cpp) target_link_libraries(json_example PRIVATE nlohmann_json::nlohmann_json)
example.cpp#include <nlohmann/json.hpp> #include <iostream> using json = nlohmann::json; int main() { std::cout << json::meta() << std::endl; }
-
Install package:
vcpkg install nlohmann-json
-
Build:
mkdir build cd build cmake .. -DCMAKE_TOOLCHAIN_FILE=/path/to/vcpkg/scripts/buildsystems/vcpkg.cmake cmake --build .
Note you need to adjust /path/to/vcpkg/scripts/buildsystems/vcpkg.cmake
to your system.
cget¶
If you are using cget, you can install the latest development version with cget install nlohmann/json
. A specific version can be installed with cget install nlohmann/json@v3.1.0
. Also, the multiple header version can be installed by adding the -DJSON_MultipleHeaders=ON
flag (i.e., cget install nlohmann/json -DJSON_MultipleHeaders=ON
).
cget reads directly from the GitHub repository and is always up-to-date.
CocoaPods¶
If you are using CocoaPods, you can use the library by adding pod "nlohmann_json", '~>3.1.2'
to your podfile (see an example). Please file issues here.
NuGet¶
If you are using NuGet, you can use the package nlohmann.json. Please check this extensive description on how to use the package. Please file issues here.
Conda¶
If you are using conda, you can use the package nlohmann_json from conda-forge executing conda install -c conda-forge nlohmann_json
. Please file issues here.
MSYS2¶
If you are using MSYS2, you can use the mingw-w64-nlohmann-json package, just type pacman -S mingw-w64-i686-nlohmann-json
or pacman -S mingw-w64-x86_64-nlohmann-json
for installation. Please file issues here if you experience problems with the packages.
The package is updated automatically.
MacPorts¶
If you are using MacPorts, execute sudo port install nlohmann-json
to install the nlohmann-json package.
The package is updated automatically.
build2¶
If you are using build2
, you can use the nlohmann-json
package from the public repository http://cppget.org or directly from the package's sources repository. In your project's manifest
file, just add depends: nlohmann-json
(probably with some version constraints). If you are not familiar with using dependencies in build2
, please read this introduction. Please file issues here if you experience problems with the packages.
The package is updated automatically.
wsjcpp¶
If you are using wsjcpp
, you can use the command wsjcpp install "https://github.com/nlohmann/json:develop"
to get the latest version. Note you can change the branch ":develop" to an existing tag or another branch.
wsjcpp reads directly from the GitHub repository and is always up-to-date.
CPM.cmake¶
If you are using CPM.cmake
, you can check this example
. After adding CPM script to your project, implement the following snippet to your CMake:
CPMAddPackage(
NAME nlohmann_json
GITHUB_REPOSITORY nlohmann/json
VERSION 3.9.1)