Fails to write `CustardList` in python
ensan-hcl opened this issue · 4 comments
ensan-hcl commented
Fails to write `CustardList` in python
ensan-hcl commented
Maybe bug in the to_json
. Unnecessary label custards:
applied to the list.
#desired
[
custard1,
custard2,
custard3,
]
#now
{
"custards": [
custard1,
custard2,
custard3,
]
}
shundroid commented
I think this is because CustardList
has a member named custards
and now we use to_json
for instances of CustardList
.
shundroid commented
If you want to put lists, you need to use to_json
for the member custards
of CustardList
.
ensan-hcl commented
class CustardJSONEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, Custard):
return to_json(o)
if isinstance(o, CustardList):
return to_json(o.custards)
# 他の型はdefaultのエンコード方式を使用
return super(CustardJSONEncoder, self).default(o)
It results as desired. Nice!