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...

6
  • 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. Commented Sep 11, 2017 at 19:40
  • In any event, generally solutions to this problem 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. Commented Sep 11, 2017 at 19:46
  • 1
    Have you considered jsonpickle: pypi.python.org/pypi/jsonpickle ? Commented Sep 11, 2017 at 20:03
  • @quamrana pickle and jsonpickle have same problem. They just ignore lists of custom type objects Commented Sep 12, 2017 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. Commented Sep 12, 2017 at 12:56

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.