Serialize complex python objects with multiple classes and lists to JSON -


i trying serialize complex python object multiple nested objects , lists. problem encountered described here: python serialize objects list json

now, have objects, lets say:

class a:     id = 0     objects_b = []     objects_c = []  class b:     id = 0  class c:     id = 0  = a() b = b() c = c() a.objects_b = [b] a.objects_c = [c] 

i don't want add custom methods each new class add structure. there way make unified serialization method handle each new class no matter how included, in list or parameter? method use object type great, tolerate subclassing objects general type too. trying figure last hour , going little crazy, ready create own serialization method without using json lib, thats seems much...

json can serialize simple types (strings, dicts, lists etc). doesn't know how deal python objects.

so if want serialize objects, there no way around defining own serialization of classes these objects belong to. can use json library (read default) produce serialization of object, have own application-specific (or @ least python-specific) extension of json , might use that's python-specific, such pickle, works out of box. people use json because it's simple, readable , language-agnostic. if define own protocol extension json in order serialize python objects, there isn't reason use json in first place.

writing own object serialization hard. think detecting cycles, writing serializers every object might happen live in object graph (even if it's simple python datetime), , on.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -