I wrote a custom handler for jsonpickle in order to transform an enum value before serializing the object container.
import jsonpickle
from enum import Enum
class Bar(Enum):
A = 1
B = 2
class Foo:
def __init__(self):
self.hello = 'hello'
self.bar = [Bar.A, Bar.B]
class Handler(jsonpickle.handlers.BaseHandler):
def flatten(self, obj, data): # data contains {}
print(obj)
### How should I handle the enum? ###
return data
jsonpickle.handlers.registry.register(Bar, Handler)
def main():
fizbuz = Foo()
encoded = jsonpickle.encode(fizbuz)
print(encoded)
if __name__ == '__main__':
main()
The handler is called with the obj containing the enum value all right. But then the data dict contains already a key, value pair so I can't just return a single value representing the enum.
So my question is what should be the key, value pair I need to add to the data dict when I am custom-handling elements that return one unique value while I need to fit it in the data dict that has been pre=-populated with reflection data that will be needed for the object to be reconstructed later.