[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Bulletproof json.dump?

You can achieve round-tripping by maintaining a type mapping in code, for a
single datatype it would look like:

newloads(datetime, newdumps(

If those would rely on __dump__ and __load__ functions in the fashion of
pickle then nested data structures would also be easy:

class YourStruct:
    dt = datetime
    children = []

    def __load__(cls, data):
        return cls(
            children=[cls.__load__(c) for c in data['children']])

    def __dump__(self):
        return dict(
            children=[c.__dump__() for c in self.children],

If your datetime is not being loaded from C-code you can even monkey patch
it and add __load__ and __dump__ on it and data round-trip as long as you
keep the type mapping in a method.