Comments (10)
Mmh, it might be a good idea to add support for array_t
in this extension indeed.
You might be able to make it work with something like:
pybind11::object test(pybind11::list& a) const
{
nlohmann::json j = a;
nlohmann::json node;
node["my key"] = "my string";
j.push_back(node);
return j;
}
from pybind11_json.
Sure. Let's turn it into a py::object
first then.
pybind11::object test(pybind11::list& a) const
{
nlohmann::json j = py::object(a);
nlohmann::json node;
node["my key"] = "my string";
j.push_back(node);
return j;
}
This is really not ideal, but it should work.
from pybind11_json.
I think py::list
should work
from pybind11_json.
Thank you @martinRenou for your quick response.
Could you please look this example below?
C++ side:
nlohmann::json::array_t my_array;
nlohmann::json firstElement;
firstElement["first"] = 1;
my_array.push_back(firstElement);
// Do another stuff ...
test(my_array);
C++ function:
pybind11::list test(pybind11::list& input)
{
// Try to bind:
nlohmann::json::array_t out = input;
// New JSON field:
nlohmann::json node;
node["my key"] = "my string";
// Insert JSON element:
out.push_back(node);
return out;
}
I try to compile but, t I got these 2 errors below:
error: conversion from ‘pybind11::list’ to non-scalar type ‘nlohmann::basic_json<>::array_t’ {aka ‘std::vector<nlohmann::basic_json<>, std::allocator<nlohmann::basic_json<> > >’} requested
nlohmann::json::array_t j = a;
^
error: could not convert ‘j’ from ‘nlohmann::basic_json<>::array_t’ {aka ‘std::vector<nlohmann::basic_json<>, std::allocator<nlohmann::basic_json<> > >’} to ‘pybind11::list’
return j;
Could you help me again?
from pybind11_json.
Actually, I think if you return a pybind11::object
you will get a Python list when executing your function Python side, did you try?
from pybind11_json.
Yes, I tried...
pybind11::object test(pybind11::list& a) const
{
nlohmann::json::array_t j = a;
nlohmann::json node;
node["my key"] = "my string";
j.push_back(node);
return j;
}
C++ Compile time error:
error: conversion from ‘pybind11::list’ to non-scalar type ‘nlohmann::basic_json<>::array_t’ {aka ‘std::vector<nlohmann::basic_json<>, std::allocator<nlohmann::basic_json<> > >’} requested
nlohmann::json::array_t j = a;
^
error: could not convert ‘j’ from ‘nlohmann::basic_json<>::array_t’ {aka ‘std::vector<nlohmann::basic_json<>, std::allocator<nlohmann::basic_json<> > >’} to ‘pybind11::object’
return j;
from pybind11_json.
I tried it too, but I got 1 compile time error:
error: conversion from ‘pybind11::list’ to non-scalar type ‘nlohmann::json’ {aka ‘nlohmann::basic_json<>’} requested
nlohmann::json j = a;
from pybind11_json.
Ok...now it worked! Thank you @martinRenou !
from pybind11_json.
Sure!
from pybind11_json.
This will change in the next release. The py::object to nlohmann::json conversion will be done automatically.
from pybind11_json.
Related Issues (20)
- AppVeyor badge url HOT 2
- C++ returning nlohmann::json to Python. Is it possible? HOT 2
- Error: nlohmann::json as input argument HOT 2
- How can I debug? HOT 3
- Allow automatic cast from nlohmann::json to py::object in return type of functions HOT 7
- bytes, bytearray, memoryview support
- set, frozenset support HOT 1
- Build fails with CMake version < 3.14
- Infinity mishandled in nl::json => py::object conversion
- pybind11_json with c++ class HOT 4
- Cannot include dependencies with add_directories
- Support for binary arrays HOT 3
- local variable 'obj' will be copied despite being returned by name HOT 1
- What about enumerations?
- No 'test' target when BUILD_TESTS=ON
- [question] Python + C++ full example?
- support for `nlohmann::ordered_json` in addition to `nlohmann::json` HOT 2
- CMake deprecation warning HOT 3
- Activate AppVeyor CI HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pybind11_json.