problem: in class or object, we need define json encode data
Solution:
add def __jsonencode__(self):
create AdvancedJSONEncoder
example:
-------------
import json
class User(object):
def __init__(self, username):
self.username = username
def __jsonencode__(self):
return {'username': self.username}
class AdvancedJSONEncoder(json.JSONEncoder):
def default(self, obj):
if hasattr(obj, '__jsonencode__'):
return obj.__jsonencode__()
#if isinstance(obj, set):
# return list(obj)
return json.JSONEncoder.default(self, obj)
user = User('foo')
print(json.dumps(user,cls=AdvancedJSONEncoder))
-------------
good
https://myapollo.com.tw/zh-tw/python-make-json-serializable-class
other
https://www.kmp.tw/post/jsondumpsdatetimeerror/