Improved handling of .json() helper handling. By dumping it with the JsonEncoder and loading it back in we ensure that any recursive objects also meers the json friendly dict criterias. (#1689)

This commit is contained in:
Anton Hvornum 2023-03-26 22:05:52 +02:00 committed by GitHub
parent 942112f871
commit ef5011114d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 4 additions and 1 deletions

View File

@ -130,7 +130,10 @@ class JsonEncoder:
copy[JsonEncoder._encode(key)] = val
return copy
elif hasattr(obj, 'json'):
return obj.json()
# json() is a friendly name for json-helper, it should return
# a dictionary representation of the object so that it can be
# processed by the json library.
return json.loads(json.dumps(obj.json(), cls=JSON))
elif hasattr(obj, '__dump__'):
return obj.__dump__()
elif isinstance(obj, (datetime, date)):