Sunday 7 March 2021

How to serialise and deserialise complex POCO data structures in Python to/from JSON

We have been researching this for hours now, with no luck, there are many ways to serialise and deserialise objects in Python, but we need a simple and standard one that respects typings, for example:

from typings import List, NamedTuple

class Address(object):
    city:str
    postcode:str

class Person(NamedTuple):
    name:str
    addresses:List[Address]

My ask is extremely simple, I am looking for a standard way to convert to and from JSON, without the need to write the serialisation/deserlialisation code for every class, for example:

json = '{"name":"John", addresses: [{"postcode":"EC2 2FA", "city":"London"}, {"city":"Paris", "postcode":"545887", "extra_attribute":""}]}'}

I need a way to:

p= magic(json, Person) # or something similar
print(type(p)) # should print Person
for a in p.addresses:
    print(type(a)) # prints Address
    print(a.city) # should print London then Paris
json2 = unmagic(p)
print(json2 == json) # prints true (probably there will be difference in spacing, but just to clarify the idea)

I have worked in programming for 15 years, and have been using Python for a year, and still not sure what is the best way of very simply serialise/deserialise a structure of POCO objects even after extensive research, I feel dumb.

Edit

Options explored so far have one or more of the following limitations:

  • Depend on the order of elements within the JSON / class definition instead of names of the attributes (the previous example would fail because city and postcode are mixed up).
  • Fail if there are extra details in the JSON (the previous example would fail because there is an extra_attribute).
  • Return dictionary instead of a typed object, or SimpleNamespace, and not an object of the intended type.
  • Require writing serialisation/deserialization code for each and every different class, which is extremely error-prone.


from How to serialise and deserialise complex POCO data structures in Python to/from JSON

No comments:

Post a Comment