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.

1

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Assuming OP has __str__ or __repr__ defined in the Adder class.
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.
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 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.