Giter Site home page Giter Site logo

Comments (10)

martinRenou avatar martinRenou commented on July 28, 2024 1

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.

martinRenou avatar martinRenou commented on July 28, 2024 1

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.

martinRenou avatar martinRenou commented on July 28, 2024

I think py::list should work

from pybind11_json.

nyckmaia avatar nyckmaia commented on July 28, 2024

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.

martinRenou avatar martinRenou commented on July 28, 2024

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.

nyckmaia avatar nyckmaia commented on July 28, 2024

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.

nyckmaia avatar nyckmaia commented on July 28, 2024

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.

nyckmaia avatar nyckmaia commented on July 28, 2024

Ok...now it worked! Thank you @martinRenou !

from pybind11_json.

martinRenou avatar martinRenou commented on July 28, 2024

Sure!

from pybind11_json.

martinRenou avatar martinRenou commented on July 28, 2024

This will change in the next release. The py::object to nlohmann::json conversion will be done automatically.

from pybind11_json.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.