简体   繁体   English

将 nlohmann::json 与 cppyy 一起使用?

[英]using nlohmann::json with cppyy?

Is it possible to pass a python dict into a function expecting a nlohmann::json ( nlohmann/json ) object via cppyy ?是否可以通过 cppyy 将 python dict传递到期望nlohmann::json ( nlohmann/json ) object 的cppyy This question has to have come up by now, but I wasn't able to find anything on it.这个问题现在必须提出来,但我找不到任何关于它的东西。

Minimal example to reproduce (without regard to performance/safety, pls forgive):重现的最小示例(不考虑性能/安全,请原谅):

test-json.h

#include <iostream>
#include <nlohmann/json.hpp>

using nlohmann::json;

void print_name_and_age(json j) {
    std::cout << j["name"] << "\n"
              << j["age"] << "\n";
} 

test-cppyy.py

import cppyy
cppyy.include('test-json.h')

from cppyy.gbl import print_name_and_age

some_dict = {
    "name": "alfred",
    "age": 25
}

print_name_and_age(some_dict)

runs into跑进

    print_name_and_age(some_dict)
NotImplementedError: void ::print_name_and_age(nlohmann::json j) =>
    NotImplementedError: could not convert argument 1 (this method cannot (yet) be called)

I would like to be able to pass a python dict into the C++ function, and receive it as a nlohmann::json object. I presume I would need to write some custom converter for this?我希望能够将 python dict 传递到 C++ function,并将其作为nlohmann::json object 接收。我想我需要为此编写一些自定义转换器吗?

Design requirement/background (optional)设计要求/背景(可选)

I have a reinforcement learning environment class (written in C++) that needs to accept some configuration to initialize it (in its constructor).我有一个强化学习环境 class(用 C++ 编写)需要接受一些配置来初始化它(在其构造函数中)。 Everything's all fine passing a nlohmann::json object into the constructor while in the C++ domain, but I have a Python wrapper around the class too, written with cppyy that provides similar functionality to the C++ interface.在 C++ 域中将nlohmann::json object 传递给构造函数时,一切都很好,但我也有一个 Python 包装器围绕 class,用cppyy编写,提供与 8814968887 接口类似的功能Uptill now, because of the aforementioned issue, I've been forced to receive a const std::map<std::string, float>& in the constructor instead of a nlohmann::json , which is what a python dict containing only str -> float mappings easily gets converted to by cppyy .到目前为止,由于上述问题,我被迫在构造函数中接收一个const std::map<std::string, float>&而不是nlohmann::json ,这是一个仅包含 python 的dict str -> float映射很容易被cppyy转换。 But this obviously limits my input json files to only contain float s as values (my usecase requires having strings as keys but string s, int s, float s and bool s as values in the JSON file).但这显然将我的输入 json 文件限制为仅包含float s 作为值(我的用例要求将strings作为键,但string s、 int s、 float s 和bool s 作为 JSON 文件中的值)。 I can ofcourse write some pre-processing code to encode my heterogenous python dict into a homogenous str->float mapping on the python front (and do the same for C++) but I'd like a cleaner solution, if possible.我当然可以编写一些预处理代码来将我的异构python dict编码为 python 前面的同质str->float映射(对 C++ 执行相同的操作),但如果可能的话,我想要一个更简洁的解决方案。

Could anyone please help me achieve passing a python dict into the cppyy -imported C++ function and have it converted into a nlohmann::json object in the C++ function?任何人都可以帮助我将 python dict传递到 cppyy -导入的cppyy function 并将其转换为nlohmann::json object 在 88146498729880848888888 中吗? If this requires forking cppyy to add extra converter code/too much trouble I presume I would need to use a std::map<std::string, std::any / variant> alternative?如果这需要分叉cppyy以添加额外的转换器代码/太麻烦,我想我需要使用std::map<std::string, std::any / variant>替代方案? I haven't worked alot with std::any/variant , would like to ask if this would even be possible - python dict to map<string, any> - if this is the best alternative to a custom converter - in terms of performance / clean elegant code.我没有使用过很多std::any/variant ,想问一下这是否可能 - python dict to map<string, any> - 如果这是自定义转换器的最佳替代方案 - in terms of performance / 干净优雅的代码。

Environment:环境:

Python 3.9.13 Python 3.9.13

C++17 ( I believe cppyy-2.4.0 doesn't support C++20 yet, I don't have any constraint on the C++ standard) C++17(我相信cppyy-2.4.0还不支持C++20 ,我对 C++ 标准没有任何限制)

cppyy==2.3.1
cppyy-backend==1.14.8
cppyy-cling==6.25.3
cppyythonizations==1.2.1

MacOS Monterey, Apple M1 MacOS 蒙特雷,Apple M1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM