2

I have a python module that will generate thousands of classes that will be used for http calls.

These classes must be converted to JSON objects to make the call.

When I try a simple example like:

from json import JSONEncoder
import json

class BaseObject:
    pass

class MyEncoder(JSONEncoder):
    def default(self, o):
        if isinstance(o, BaseObject):
            return JSONEncoder.default(self, dict(o.__dict__))

class foo(BaseObject):
    def __init__(self):
        self.bar = bar()

class bar(BaseObject):
    pass

MyEncoder().encode(foo())

I get a stack with the error:

 File "f:\sdks\Python\testA.py", line 27, in <module>
    MyEncoder().encode(foo())

  File "C:\Users\{user}\AppData\Local\Programs\Python\Python36\lib\json\encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)


  File "C:\Users\{user}\AppData\Local\Programs\Python\Python36\lib\json\encoder.py", line 257, in iterencode
    return _iterencode(o, 0)

  File "f:\sdks\Python\testA.py", line 11, in default
    return JSONEncoder.default(self, dict(o.__dict__))

  File "C:\Users\{user}\AppData\Local\Programs\Python\Python36\lib\json\encoder.py", line 180, in default
    o.__class__.__name__)

TypeError: Object of type 'dict' is not JSON serializable

I thought every object in Python was essentially a dict which should be a valid single tiered JSON object. But this doesn't seem to be the case?

The main problem is that "dict" isn't serializable (clearly stated in the failure output), which documentation explicitly states that dict is serializable to a JSON object https://docs.python.org/3/library/json.html#json.JSONEncoder

User91234
  • 21
  • 3
  • 2
    _thousands of classes_ or instances? – Evgeny Jun 15 '18 at 00:14
  • Also see answers to [Making object JSON serializable with regular encoder](https://stackoverflow.com/questions/18478287/making-object-json-serializable-with-regular-encoder). – martineau Jun 15 '18 at 01:21
  • BTW, every object in Python **does not** necessarily have a `__dict__`. Objects of type `int` don't for example. – martineau Jun 15 '18 at 01:38
  • @EvgenyPogrebnyak, classes. This is a generated SDK. It better not allocate thousands of instances, wasting the consumer developers memory. – User91234 Jun 15 '18 at 14:47
  • @martineau, this looks promising, I'll respond with my results later today. – User91234 Jun 15 '18 at 14:47
  • in what domain does that SDK work though? – Evgeny Jun 15 '18 at 14:55
  • @EvgenyPogrebnyak ideally any python developer should be able to drop this SDK into their python project folder and make calls that I'm generating. So any valid python domain. – User91234 Jun 15 '18 at 16:55
  • I get that TypeError if I use `jsonref.loads` to consume a json string with references, and then use `json.dumps` to turn it back to a string. Apparently I need to use `jsonref.dumps`. I think this is because the types named `dict` created by `jsonref` aren't truly `dict`s but instead something intended to be compatible. It could be that some library is creating such fakes for you too. – MatrixManAtYrService Feb 10 '20 at 05:57

0 Answers0