Page 1 of 1

How can I serializing a class into JSON?

Posted: Tue May 28, 2024 9:35 pm
by johnthomas
I am trying to create a JSON string representation of a class instance and having difficulty. Let's say the class is built like this:

Code: Select all

class testclass:
    value1 = "a"
    value2 = "b"
A call to the json.dumps is made like this:

Code: Select all

t = testclass()
json.dumps(t)
It is failing and telling me that the testclass is not JSON serializable.

Code: Select all

TypeError: <__main__.testclass object at 0x000000000227A400> is not JSON serializable
I have also tried using the pickle module :

Code: Select all

t = testclass()
print(pickle.dumps(t, pickle.HIGHEST_PROTOCOL))
And it gives class instance information but not a serialized content of the class instance.

Code: Select all

b'\x80\x03c__main__\ntestclass\nq\x00)\x81q\x01}q\x02b.'
What am I doing wrong?

Re: How can I serializing a class into JSON?

Posted: Tue May 28, 2024 10:33 pm
by mary_davis75
The basic problem is that the JSON encoder json.dumps() only knows how to serialize a limited set of object types by default, all built-in types. List here: https://docs.python.org/3.3/library/jso ... d-decoders

One good solution would be to make your class inherit from JSONEncoder and then implement the JSONEncoder.default() function, and make that function emit the correct JSON for your class.

A simple solution would be to call json.dumps() on the .__dict__ member of that instance. That is a standard Python dict and if your class is simple it will be JSON serializable.

Code: Select all

class Foo(object):
    def __init__(self):
        self.x = 1
        self.y = 2

foo = Foo()
s = json.dumps(foo) # raises TypeError with "is not JSON serializable"

s = json.dumps(foo.__dict__) # s set to: {"x":1, "y":2}
The above approach is discussed in this blog posting:

Serializing arbitrary Python objects to JSON using _dict_

And, of course, Python offers a built-in function that accesses .__dict__ for you, called vars().

So the above example can also be done as:

Code: Select all

s = json.dumps(vars(foo)) # s set to: {"x":1, "y":2}