python Programming Glossary: myencoder
Why does json serialization of datetime objects in python not work out of the box for datetime objects http://stackoverflow.com/questions/10721409/why-does-json-serialization-of-datetime-objects-in-python-not-work-out-of-the-bo import json import datetime from time import mktime class MyEncoder json.JSONEncoder def default self obj if isinstance obj datetime.datetime.. json.JSONEncoder.default self obj print json.dumps obj cls MyEncoder As others correctly pointed out the reason is that the standard..
Format floats with standard json module http://stackoverflow.com/questions/1447287/format-floats-with-standard-json-module ' I have tried defining my own JSON Encoder class class MyEncoder json.JSONEncoder def encode self obj if isinstance obj float.. This works for a sole float object json.dumps 23.67 cls MyEncoder '23.67' But fails for nested objects json.dumps 23.67 23.97..
Python serializable objects json http://stackoverflow.com/questions/1458450/python-serializable-objects-json is None childTrees self.childTrees childTrees class MyEncoder json.JSONEncoder def default self obj if not isinstance obj.. default self obj if not isinstance obj Tree return super MyEncoder self .default obj return obj.__dict__ c1 Tree c1 c2 Tree c2.. Tree c1 c2 Tree c2 t Tree t c1 c2 print json.dumps t cls MyEncoder it prints childTrees childTrees name c1 childTrees name c2 name..
How to change json encoding behaviour for serializable python object? http://stackoverflow.com/questions/16405969/how-to-change-json-encoding-behaviour-for-serializable-python-object from customencoder import CustomObjectEncoder class MyEncoder CustomObjectEncoder def isinstance self obj cls if isinstance..
Python: how to make a class JSON serializable http://stackoverflow.com/questions/3768895/python-how-to-make-a-class-json-serializable example see below. from json import JSONEncoder class MyEncoder JSONEncoder def default self o return o.__dict__ MyEncoder .encode.. MyEncoder JSONEncoder def default self o return o.__dict__ MyEncoder .encode f ' fname foo bar ' Then you pass this class into the.. into the json.dumps method as cls kwarg json.dumps cls MyEncoder If you also want to decode then you'll have to supply a custom..
Serializing a Python namedtuple to json http://stackoverflow.com/questions/5906831/serializing-a-python-namedtuple-to-json import namedtuple from json import JSONEncoder class MyEncoder JSONEncoder def _iterencode self obj markers None if isinstance.. yield chunk class foobar namedtuple 'f' 'foo bar' pass enc MyEncoder for obj in foobar 'a' 1 'a' 1 'outer' foobar 'x' 'y' print enc.encode..
|