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)