Starting with Python 3.7, there is something called a dataclass:
from dataclasses import dataclass @dataclass class Foo: x: str
However, the following fails:
>>> import json >>> foo = Foo(x="bar") >>> json.dumps(foo) TypeError: Object of type Foo is not JSON serializable
How can I make json.dumps()
encode instances of Foo
into json objects?
To create a json file from an existing json file, open the existing file in read mode and read the content of that file and use the open() and with statement in write mode and dump the json data to a new json file.
Use toJSON() Method to make class JSON serializable So we don't need to write custom JSONEncoder. This new toJSON() serializer method will return the JSON representation of the Object. i.e., It will convert custom Python Object to JSON string.
Python Supports JSON Natively! Python comes with a built-in package called json for encoding and decoding JSON data.
Much like you can add support to the JSON encoder for datetime
objects or Decimals, you can also provide a custom encoder subclass to serialize dataclasses:
import dataclasses, json class EnhancedJSONEncoder(json.JSONEncoder): def default(self, o): if dataclasses.is_dataclass(o): return dataclasses.asdict(o) return super().default(o) json.dumps(foo, cls=EnhancedJSONEncoder)
Can't you just use the dataclasses.asdict()
function to convert the dataclass to a dict? Something like:
>>> @dataclass ... class Foo: ... a: int ... b: int ... >>> x = Foo(1,2) >>> json.dumps(dataclasses.asdict(x)) '{"a": 1, "b": 2}'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With