mirror of
https://github.com/kennethreitz/dive-into-python3.git
synced 2026-06-05 23:10:17 +00:00
some rough notes
This commit is contained in:
@@ -2,7 +2,7 @@ import pickle
|
||||
import json
|
||||
import time
|
||||
|
||||
def custom_encoder(python_object):
|
||||
def to_json(python_object):
|
||||
if isinstance(python_object, time.struct_time):
|
||||
return {'__class__': 'time.asctime',
|
||||
'__value__': time.asctime(python_object)}
|
||||
@@ -11,7 +11,7 @@ def custom_encoder(python_object):
|
||||
'__value__': list(python_object)}
|
||||
raise TypeError(repr(python_object) + ' is not JSON serializable')
|
||||
|
||||
def custom_decoder(json_object):
|
||||
def from_json(json_object):
|
||||
if '__class__' in json_object:
|
||||
if json_object['__class__'] == 'time.asctime':
|
||||
return time.strptime(json_object['__value__'])
|
||||
|
||||
Reference in New Issue
Block a user