Operator Framework serializing storedDict to json

I’m writing an application that receives a dictionary in the form of a string over a relation, json.loads it to restore it from a string into its correct dictionary type, and stores it in stored state so that it can be used in the main charm to be able to send it to other applications.

My problem comes when retrieving the object from stored state and json.dumping the dictionary to convert it back to a string so that it is able to be sent over the relation again to another charm. Json.dumps errors trying to convert the operator class object that my dictionary is wrapped in when stored to the state.

gives the error

File "/usr/lib/python3.8/json/encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type StoredDict is not JSON serializable

My only work around is to iterate through the relation data each time I want to access it. But I wish to iterate through the relation data only once, store it some where and be able to retrieve it as a normal python object so that it can be used other places in the main charm. I believe even if I emitted the dictionary object to the main charm as an event and tried to store it in the state I would run into the same problem.

I was hoping to open up a discussion about storing objects in the main charm or if I could be pointed to some examples would be very helpful, thank you

1 Like