-5

I'm noting that the methods I am looking at to serialize a variable into JSON in python don't really seem to handle it all that well, and for my purpose I just want to quickly dump an objects contents into a string format so I can pick out what I actually want to write custom code to handle. I want to be able to dump the main fields at the very least of any class I pass the python serializer and really if its worth the name this should work.

So take the following code:

import json
    
c = SomeClass()
 

#causes an error if any field in someclass has another class instance.   
json.dumps(c) 

leads to..

TypeError: Object of type {Type} is not JSON serializable

Are there any modules other people have used that would solve my problem ? I really don't see how there would not be. Or maybe one might explain how to circumvent this error ?

The goal is to simply get some output to look at. If I wrote a recursion loop in c# using reflection, excepting circular references, it wouldn't be difficult, so I cannot imagine python users have never tackled this exact issue and I'm not satisfied with the answers that I have seen in older posts which seem to suggest a lot of custom tinkering for something seems to be designed in spirit to just dump any old object's contents out.

I don't even need complex traversal is the funny part, though it would be nice. I just need a dump of the property values which are primitive types in many cases. I know this is possible because the debugger does it.

Additionally I looked at one of the methods given indicating to use default lambda to specify how the json serializer should descend into the object:

json.dumps(o, default=lambda k: k.__dict__)

and the object does not contain the standard dict member.

in the end I just ended up writing a class to do this.

John Sohn
  • 100
  • 1
  • 9
  • 1
    Does this answer your question? [How to make a class JSON serializable](https://stackoverflow.com/questions/3768895/how-to-make-a-class-json-serializable) – buran Sep 18 '21 at 13:01
  • Also _I just want to quickly dump an objects contents into a string format so I can pick out what I actually want to write custom code to handle_ suggest this may be XY problem – buran Sep 18 '21 at 13:03
  • nope it uses the non functioning method I just found. – John Sohn Sep 18 '21 at 13:06
  • I suggest you check this https://stackoverflow.com/q/46575174/4046632 – buran Sep 18 '21 at 13:13
  • The answer is, it depends. If you're open to converting your classes to dataclasses (which makes it much, much easier to write classes and also for maintainability) then you can use the `asdict` helper function that dataclasses provides to serialize an object to JSON. I would also suggest a fast JSON serialization library like [dataclass-wizard](https://dataclass-wizard.readthedocs.io/) that might do exactly what you need. I have actually tested this and use it in production code for interacting with APIs. – rv.kvetch Sep 19 '21 at 01:34
  • 1
    @rv.kvetch if you're interested I added a simple class that lets you get readable output and does all the member substitution for you. doesn't deserialize though. If you want please fork and make a deserializer. – John Sohn Sep 21 '21 at 18:14
  • @buran see last comment – John Sohn Sep 21 '21 at 18:14
  • sure, will take a look when time allows – rv.kvetch Sep 21 '21 at 18:17
  • I had a look but I'm not a fan of reinventing the wheel - just saying. There are lot of existing third party libraries that provide deserialization out of the box, such as `pydantic`; however, there is nothing wrong with rolling your own if you wanted, especially if the goal is to reduce the reliance on 3rd party dependencies. – rv.kvetch Sep 21 '21 at 18:39
  • :P its not really reinventing the wheel ;p I'm just fixing the input for the built in json serializer :P – John Sohn Sep 21 '21 at 18:47

1 Answers1

-1

edit:

Here use this now you can one way serialize a class structure with this nifty little bit of code that I added to address my problem with f**** discord.py !

end edit

There is no fire and forget option that would disentangle a mass of information.

The way of creating this solution would be to manage seperate lists of subclasses to make sure not to recurse until a stackoverflow is reached.

The slots_ can be used with getattr(o,name) when hasattr(o,'dict') is False.

But the answer is you'd have to create a solution that basically does the job that the json serializer should be doing and cut out circular reference by determining the unique complex types and writing them in seperate tabular entries in the json file and replacing them in the referencing classes with ids.

That way you could cross reference these objects while glancing at them.

However the short answer is no. Python does not offer an out of the box way of doing this and all the provided answers encountered thus far only solve a single use-case or scenario, and do not create a incorporated solution to the problem which the above mentioned algorithm WOULD by NORMALIZING the class data into unique elements.

John Sohn
  • 100
  • 1
  • 9