0

I am trying to serialize complex python object with multiple nested objects and lists. Problem I encountered is described here: Python serialize objects list to JSON

Now, I have some objects, lets say:

class A:
    id = 0
    objects_b = []
    objects_c = []

class B:
    id = 0

class C:
    id = 0

a = A()
b = B()
c = C()
a.objects_b = [b]
a.objects_c = [c]

I don't want to add custom methods to each new class I add to my structure. Is there any way to make unified serialization method that will handle each new class no matter how is it included, in a list or as a parameter? Method that will use any object type would be great, but I could tolerate subclassing all my objects from some general type too. Trying to figure it for last hour and going a little crazy, I am almost ready to create my own serialization method without using json lib, but thats seems too much...

Alexandr
  • 150
  • 2
  • 12
  • 2
    Uh, if you are willing to write *your own serialization method* instead of using `json`, why not use `pickle`? That is part of the standard library and it should handle custom Python types. – juanpa.arrivillaga Sep 11 '17 at 19:40
  • In any event, generally [solutions to this problem](https://marshmallow.readthedocs.io/en/latest/index.html) for serializing arbitrary Python objects to JSON involve steps that amount to writing a custom method for each class type. Because essentially, JSON is *not designed to handle arbitrary objects*, but essentially two abstract data-types, the JSON array and the JSON object. In Python, JSON is for serializing complex combinations of `dict` and `list` with `str` and `int` type fields/values. It sounds like you don't want JSON, you want `pickle`. – juanpa.arrivillaga Sep 11 '17 at 19:46
  • 1
    Have you considered jsonpickle: https://pypi.python.org/pypi/jsonpickle ? – quamrana Sep 11 '17 at 20:03
  • @quamrana pickle and jsonpickle have same problem. They just ignore lists of custom type objects – Alexandr Sep 12 '17 at 11:19
  • 1
    Hmm, works for me. I create stub objects like you show and add attributes of a mixture of python types (int,str, list, dict etc) and other stubs and jsonpickle has its own way of representing my stubs within regular json. – quamrana Sep 12 '17 at 12:56
  • @quamrana True. It works o.O when I was doing a.objects_b.append(b) it wasn't. Even objects were present in array and I could access them. – Alexandr Sep 12 '17 at 13:51

1 Answers1

1

JSON can only serialize simple types (strings, dicts, lists etc). It doesn't know how to deal with Python objects.

So if you want to serialize objects, there is no way around defining your own serialization of the classes these objects belong to. You can use the json library (read about default) to produce a serialization of an object, but then you have your own application-specific (or at least python-specific) extension of json and you might as well use something that's really python-specific, such as pickle, which already works out of the box. People use json because it's simple, readable and language-agnostic. If you define your own protocol extension to json in order to serialize your python objects, there really isn't any good reason to use json in the first place.

Writing your own object serialization is hard. Think about detecting cycles, writing serializers for every object that might happen to live in your object graph (even if it's just something simple like a python DateTime), and so on.

Pascal
  • 448
  • 3
  • 11