klahnakoski / mo-dots

Attribute accessors for dicts
Mozilla Public License 2.0
6 stars 3 forks source link

Object of type Data is not JSON serializable #2

Closed bitnom closed 4 years ago

bitnom commented 4 years ago

I have a rather complex codebase in which I was previously dumping the dict form of my data to a JSON file. I refactored the entire code to use mo-dots only to finally encounter an error dumping to JSON:

TypeError: Object of type Data is not JSON serializable

Is it possible to stringify Data objects?

klahnakoski commented 4 years ago

json.dumps(team, default=lambda o: from_data(o)) may work. Give me some time to add some serialization tests; this short code does not handle FlatList (also found in this module).

I agree it is important that Data be able to serialize to JSON easily using the standard library. I use mo-json to perform the JSON serialization; I do not expect you to need that to do this.

klahnakoski commented 4 years ago

.... confirmed: It can be done like so:

json.dumps(team, default=from_data)

https://github.com/klahnakoski/mo-dots/commit/db536a8384497243a93a65af2fd62b2a994327f9#diff-5c84f15471db58e86c919d922881b8d8d31f4f6710063cc293bdbdd740d8aca0R19

klahnakoski commented 4 years ago

@TensorTom reopen if there are other combinations that cause problems.