I am building a C++ project that connects to a customer-owned Python codebase and sending a json object to the python code. Because the python codebase is customer-owned, I cannot modify the python codebase to receive a json string. I am using Python C/API for the interface.
I've built a small python file to convert the json string to json object using python's json library. I'd prefer to write the json object on the C++ side using rapidjson. Can I build a python json object from C++ rapid json and have it read by python's json library without converting the object to a string? If it's possible, which function would be the right one to convert the json Document to a Python Object?
C++:
PyObject* packJsonDict;
PyObject* customerDict;
int PYCPP::getJsonToInterface()
{
std::string json= "{\"pet\": \"dog\", \"count\": 5}";
PyObject* JsonObject = m_pImpl->pack_JsonString(json);
std::string function_string = "read_json";
PyObject* args = PyTuple_Pack(1, JsonObject);
//trying packing just the json string
PyObject* runnableFunction = PyDict_GetItemString(customerDict, (char*)function_string);
PyObject* result = PyObject_CallObject(runnableFunction, args);
}
PyObject* PYCPP::pack_JsonString(std::string json_string)
{
std::string function_string = "convert_jsonString_to_jsonObject";
PyObject* args = PyTuple_Pack(1, PyString_FromString((char*) json_string.c_str()) );
//trying packing just the json string
PyObject* runnableFunction = PyDict_GetItemString(packJsonDict, (char*)function_string);
PyObject* result = PyObject_CallObject(runnableFunction, args);
if(result == nullptr)
{
std::cout << "result was null!\n";
}
return result;
}
Python:
import json
def convert_jsonString_to_jsonObject(jsonString):
print "converting:", jsonString
return json.loads(jsonString)