pybind11_json is an nlohmann::json to pybind11 bridge, it allows you to automatically convert nlohmann::json to py::object and the other way around. Simply include the header, and the automatic conversion will be enabled.
C++ API: Automatic conversion between nlohmann::json and pybind11 Python objects
You can easily make bindings for C++ classes/functions that make use of nlohmann::json.
For example, making a binding for the following two functions is automatic, thanks to pybind11_json
void take_json(const nlohmann::json& json) {
std::cout << "This function took an nlohmann::json instance as argument: " << s << std::endl;
}
nlohmann::json return_json() {
nlohmann::json j = {{"value", 1}};
std::cout << "This function returns an nlohmann::json instance: " << j << std::endl;
return j;
}
Bindings:
PYBIND11_MODULE(my_module, m) {
m.doc() = "My awesome module";
m.def("take_json", &take_json, "pass py::object to a C++ function that takes an nlohmann::json");
m.def("return_json", &return_json, "return py::object from a C++ function that returns an nlohmann::json");
}
pybind11_json
pybind11_json
is annlohmann::json
topybind11
bridge, it allows you to automatically convertnlohmann::json
topy::object
and the other way around. Simply include the header, and the automatic conversion will be enabled.C++ API: Automatic conversion between
nlohmann::json
andpybind11
Python objectsMaking bindings
You can easily make bindings for C++ classes/functions that make use of
nlohmann::json
.For example, making a binding for the following two functions is automatic, thanks to
pybind11_json
Bindings:
You can then use your functions Python side:
Example
You can find an example of simple Python bindings using pybind11_json here: https://github.com/martinRenou/xjson
Installation
Using conda
You can install
pybind11_json
using condaFrom sources
We encourage you to use conda for installing dependencies, but it is not a requirement for
pybind11_json
to workThen you can install the sources
Header only usage
Download the “pybind11_json.hpp” single file into your project, and install/download
pybind11
andnlohmann_json
or use as git submodule.Run tests
You can compile and run tests locally doing
Dependencies
pybind11_json
depends onpybind11_json
nlohmann_json
pybind11
License
We use a shared copyright model that enables all contributors to maintain the copyright on their contributions.
This software is licensed under the BSD-3-Clause license. See the LICENSE file for details.