2

I am trying to encode JSON with json.JSONEncoder with another pattern by overriding the default method:

class X(json.JSONEncoder):
    def default(self, obj):
        return ["ok"]

json.dumps(self, cls=X)

But the default method is not being called... I saw a few examples who looks like this, but I can't figure out what is missing?

currently, I get my json as {"status": "Fail"}

Note: followed this link.

I am using python 3.6 Thank you

Ami Hollander
  • 2,435
  • 3
  • 29
  • 47

2 Answers2

3

I found the problem. my object was inherited from dict, while default method is not being called on object like dict and list

Ami Hollander
  • 2,435
  • 3
  • 29
  • 47
1

Having the same kind of problem, I found the complete answer here: https://stackoverflow.com/a/46205398/12199445

As mentioned is earlier, if the standard JSONEncoder is able to encode the value, the default method is not called.

vpoulailleau
  • 54
  • 1
  • 4