I have some classes being serialized via frugal (this is an open source extension of Thrift). The primary reason this is relevant is that the serialization process is out of my control and needs to support multiple languages, so I cannot easily modify it.
Imagine my classes look like:
class response_component:
value = 'string value'
another_value = 4
another_string = 'another string'
class the_response:
contained = response_component
This gets serialized into json similar to:
{"response": {"the_response": {"value": "string value", "another_value": 4, "another_string": "another string"}}}
This can be interpreted and recreated by the underlying messaging framework into classes on the recipient end. What I am interested in doing is having middleware make modifications to underlying attributes.
For example, I may wish to change the "another_string" value in the response example here to "hello."
The difficulty I have is understanding how to denote this reference in such a way that is meaningful within the json context.
I can do something which feels hokey like articulating the absolute path through the classes, such as RESPONSE.contained.another_string
and then in the deserialization process unpacking this (or, in python, doing a series of getattr until I find the last value. This might look like, in Python:
r = the_response()
print(r.contained.another_string)
path = 'the_response.contained.another_string'
v = None
paths = path.split('.')
for p in paths[1:-1]:
if not v: v = r
print(p)
v = getattr(v, p)
setattr(v, paths[-1], 'hello')
print(r.contained.another_string)
communicated via json such as:
{"modified_value": {"key": "the_response.contained.another_string", "val": "hello"}}
However, I really dislike the path approach and am hoping for a different and better way to convey where to modify the values.