1

I am using a third party python library, which returns some data in lists and dictionaries inside them (JSON format). For example, here's a sample entry:

data = [{'a': 1, 'b': 80, 'c': 42, 'd': [{'r': 0, 's': '1', 'u': 5}], 'id': 10, 'k': 60, 'v': 0, 'm': 
{'ty': 'djp', 'nr': '10', 'bc': Adder('179'), 'in': 3}}, {'a': 1, 'b': 70, 'c': 42, 'd': [{'r': 0, 's': 
'1', 'u': 5}], 'y': 10, 'k': 60, 'v': 0, 'm': {'ty': 'djp', 'dx': '10', 'tx': Adder('179'), 'in': 3}}]

My problem is with 'Adder' class which is an object. Everything else are just strings.

So, when I do:

json.dumps(data)

It causes an error message:

Adder('179') is not JSON serializable.

I don't care about serializing this Adder class, so I would somehow like to just make it a string e.g. "Adder(179)" if possible. Is it possible to make the dumps function work? Class Adder is not my own, maybe I can make it serialized or tell it what to do with it by editing the source or something? Any simple way would be fine.

user963241
  • 6,758
  • 19
  • 65
  • 93
  • Does this answer your question? [How to make a class JSON serializable](https://stackoverflow.com/questions/3768895/how-to-make-a-class-json-serializable) – RoadRunner Mar 25 '20 at 07:25

1 Answers1

3

Just specify a custom encoder class:

class RobustEncoder(json.JSONEncoder):
    def default(self, o):
        try:
            return super(RobustEncoder, self).default(o)
        except TypeError:
            return str(o)    

json.dumps(data, cls=RobustEncoder)

The keyword argument cls is documented in the docs of json.dump.

user2390182
  • 72,016
  • 6
  • 67
  • 89
  • Assuming OP has `__str__` or `__repr__` defined in the `Adder` class. – RoadRunner Mar 25 '20 at 07:06
  • 1
    Since the OP states *"I don't care about serializing this Adder class, so I would somehow like to just make it a string"* and `__str__` is inherited from `object`, this should be fairly robust for any type. – user2390182 Mar 25 '20 at 07:07
  • 1
    Thats true, but he did state *"I would somehow like to just make it a string e.g. "Adder(179)" if possible"*. So you would have to add `__str__`, otherwise this will print the inherited `object` `__str__` representation instead. Not really an issue because the OP can make their own pretty easily, and this approach solves the bulk of the problem. +1 from me :) – RoadRunner Mar 25 '20 at 07:10
  • 1
    @RoadRunner True, that step to control the string representation is quite small. This answer jsut demonstrates the principle. I think it is quite clear how to adopt the encoder class to basically do anything you want with the object `o` that is to be serialized. – user2390182 Mar 25 '20 at 07:13