Serializing and Deserializing object with JSON Serializing and Deserializing object with JSON json json

Serializing and Deserializing object with JSON


I can only speak for Python. There is a built in library for JSON access, it can be viewed in the docs here.

Unfortunately, out of the box, you cannot serialize/deserialize objects, just dicts, lists and simply types. You have to write specific object encoders to do so. This is pretty much covered in the docs.


For AS3 you can use as3corelib by Mike Chambers.

https://github.com/mikechambers/as3corelib/tree/master/src/com/adobe/serialization/json

Edit: After some Googling I ended up back on SO at this question: Typed AS3 JSON Encoder and Decoder? It seems that there is a library for doing typed deserialization, but it is not totally robust and fails on some data types. If you think you can handle the restrictions then it might be the best option short of writing your own parser or gettting into something heavy like BlazeDS.

http://code.google.com/p/ason/


Please try with this:

import jsonclass Serializer:    @staticmethod    def encode_obj(obj):        if type(obj).__name__ =='instance':            return obj.__dict__     @staticmethod    def serialize(obj):        return json.dumps(obj, default=Serializer.encode_obj)class TestClass:    def __init__(self):        self.a = 1t = TestClass()   json_str = Serializer.serialize(t)