ensan-hcl/CustardKit

Fails to write `CustardList` in python

ensan-hcl opened this issue · 4 comments

Fails to write `CustardList` in python

Maybe bug in the to_json. Unnecessary label custards: applied to the list.

#desired
[
    custard1,
    custard2,
    custard3,
]

#now
{
    "custards": [
        custard1,
        custard2,
        custard3,
    ]
}

I think this is because CustardList has a member named custards and now we use to_json for instances of CustardList.

If you want to put lists, you need to use to_json for the member custards of CustardList.

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!