You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi there,
I am currently working on a project where I have a C++ struct containing a nlohmann::json object. After wrapping the struct with pybind11, the JSON object correctly translates to a Python dictionary. However, I am unable to modify the dictionary in Python and have the changes propagate back to the C++ object.
frommytestimportTTest# Create an instance of the classdata=TTest()
# Attempt to modify the JSON objectdata.json["key_1"] =1data.json["new_key"] =123print(data.json)
# Expected output: {'key_1': 1, 'new_key': 123}# Actual output: {'key_1': 0}# Workaround: Assign the JSON object to a local variable, modify it, and reassign itjson=data.jsonjson["key_1"] =1json["new_key"] =123data.json=jsonprint(data.json)
# Expected output: {'key_1': 1, 'new_key': 123}# Actual output: {'key_1': 1, 'new_key': 123}
Expected Behavior
The data.json object in Python should reflect the changes made to it, without requiring the workaround of assigning and reassigning the object.
Actual Behavior
Changes made directly to data.json in Python are not propagated back to the json field in the underlying C++ object. The changes only take effect when the entire JSON object is reassigned after modification.
Additional Notes
pybind11 Version: 2.13.6
pybind_json: 0.2.14
nlohmann::json Version: 3.11.2
python Version: 3.12.7
The text was updated successfully, but these errors were encountered:
Hi there,
I am currently working on a project where I have a C++ struct containing a nlohmann::json object. After wrapping the struct with pybind11, the JSON object correctly translates to a Python dictionary. However, I am unable to modify the dictionary in Python and have the changes propagate back to the C++ object.
Minmal example
C++ Code
Python Code
Expected Behavior
The data.json object in Python should reflect the changes made to it, without requiring the workaround of assigning and reassigning the object.
Actual Behavior
Changes made directly to data.json in Python are not propagated back to the json field in the underlying C++ object. The changes only take effect when the entire JSON object is reassigned after modification.
Additional Notes
The text was updated successfully, but these errors were encountered: