How to overcome “datetime.datetime not JSON serializable”?

后端 未结 30 2702
梦谈多话
梦谈多话 2020-11-22 03:31

I have a basic dict as follows:

sample = {}
sample[\'title\'] = \"String\"
sample[\'somedate\'] = somedatetimehere
         


        
30条回答
  •  既然无缘
    2020-11-22 03:55

    This library superjson can do it. And you can easily custom json serializer for your own Python Object by following this instruction https://superjson.readthedocs.io/index.html#extend.

    The general concept is:

    your code need to locate the right serialization / deserialization method based on the python object. Usually, the full classname is a good identifier.

    And then your ser / deser method should be able to transform your object to a regular Json serializable object, a combination of generic python type, dict, list, string, int, float. And implement your deser method reversely.

提交回复
热议问题