3

When serializing Python objects with Python's json library, there seems to be more work involved when serializing schemas with nested objects and dates, compared to other language libraries. This functionality is mostly achieved in Python by writing custom encoders or defining a schema with a library like marshmallow or django. Other libraries in different languages seem to be able to simply accept an object no matter how complex and serialize the object as a JSON string with no additional information (Unless you really need customized formatting, etc). Why is this?

Just to name a few libraries that I am familiar with, Java's Jackson and Gson, JavaScripts JSON.stringify() method and C#'s System.Text.Json are all able to accept complex objects and produce a JSON string with no additional information.

Edit (Apr 9th)

Some pseudo code describing some models

class Person:
    firstName: string
    lastName: string
    address: Address

class Address:
    street: string
    zip: string
    country: string
    city: string
# Will not serialize Address unless some additional steps are taken.
person = Person(...)
json.dump(person)
// Serializes person with nested address
Person person = new Person(...)
new ObjectMapper().writeValueAsString(person)
// Serializes person with nested address
Person person = new Person(...)
new Gson().toJson(person)
// Serializes person with nested address
const person = new Person(...)
JSON.stringify(person)
JSextonn
  • 226
  • 3
  • 13

1 Answers1

0

I just ran across jsonpickle and it is able to serialize and deserialize objects that are complex without having to define any custom decoders or schemas. I haven't been able to really test how resilient the library is but I've been using it for a few hours now and it seems very promising.

JSextonn
  • 226
  • 3
  • 13