diff --git a/serializing.html b/serializing.html index f6c1a43..2826a65 100644 --- a/serializing.html +++ b/serializing.html @@ -234,6 +234,8 @@ struct_time 357: . STOP highest protocol among opcodes = 3 +

FIXME more here about fix_imports and such? +

Serializing Complex Python Objects

@@ -291,15 +293,27 @@ highest protocol among opcodes = 3

Serializing Datatypes Unsupported by JSON

->>> shell
+>>> shell
 1
 >>> entry
 FIXME
 >>> import json
->>> with open('entry.json', 'w', encoding='utf-8') as f:
-...     json.dump(entry)
+>>> with open('entry.json', 'w', encoding='utf-8') as f:   
+...     json.dump(entry, f)
 ... 
-FIXME
+Traceback (most recent call last): + File "<stdin>", line 5, in <module> + File "C:\Python31\lib\json\__init__.py", line 178, in dump + for chunk in iterable: + File "C:\Python31\lib\json\encoder.py", line 408, in _iterencode + for chunk in _iterencode_dict(o, _current_indent_level): + File "C:\Python31\lib\json\encoder.py", line 382, in _iterencode_dict + for chunk in chunks: + File "C:\Python31\lib\json\encoder.py", line 416, in _iterencode + o = _default(o) + File "C:\Python31\lib\json\encoder.py", line 170, in default + raise TypeError(repr(o) + " is not JSON serializable") +TypeError: b'\xde\xd5\xb4\xf8' is not JSON serializable
  1. FIXME
@@ -325,11 +339,26 @@ def to_json(python_object): >>> with open('entry.json', 'w', encoding='utf-8') as f: ... json.dump(entry, default = customserializer.to_json) ... -FIXME +Traceback (most recent call last): + File "<stdin>", line 9, in <module> + json.dump(entry, f, default=customserializer.to_json) + File "C:\Python31\lib\json\__init__.py", line 178, in dump + for chunk in iterable: + File "C:\Python31\lib\json\encoder.py", line 408, in _iterencode + for chunk in _iterencode_dict(o, _current_indent_level): + File "C:\Python31\lib\json\encoder.py", line 382, in _iterencode_dict + for chunk in chunks: + File "C:\Python31\lib\json\encoder.py", line 416, in _iterencode + o = _default(o) + File "/Users/pilgrim/diveintopython3/examples/customserializer.py", line 12, in to_json + raise TypeError(repr(python_object) + ' is not JSON serializable') +TypeError: time.struct_time(tm_year=2009, tm_mon=3, tm_mday=27, tm_hour=22, tm_min=20, tm_sec=42, tm_wday=4, tm_yday=86, tm_isdst=-1) is not JSON serializable
  1. FIXME
+

FIXME +

# customserializer.py
 def to_json(python_object):
     if isinstance(python_object, time.struct_time):
@@ -359,9 +388,13 @@ def to_json(python_object):
 
 
 you@localhost:~/diveintopython3/examples$ ls -l example.json
-FIXME
+-rw-r--r-- 1 you  you  391 Aug  3 13:34 entry.json
 you@localhost:~/diveintopython3/examples$ cat example.json
-FIXME
+{"published_date": {"__class__": "time.asctime", "__value__": "Fri Mar 27 22:20:42 2009"}, +"comments_link": null, "internal_id": {"__class__": "bytes", "__value__": [222, 213, 180, 248]}, +"tags": ["diveintopython", "docbook", "html"], "title": "Dive into history, 2009 edition", +"article_link": "http://diveintomark.org/archives/2009/03/27/dive-into-history-2009-edition", +"published": true}
  1. FIXME
@@ -380,7 +413,14 @@ NameError: name 'entry' is not defined >>> with open('entry.json', 'r', encoding='utf-8') as f: ... entry = json.load(f) ... -FIXME +>>> entry +{'comments_link': None, + 'internal_id': {'__class__': 'bytes', '__value__': [222, 213, 180, 248]}, + 'title': 'Dive into history, 2009 edition', + 'tags': ['diveintopython', 'docbook', 'html'], + 'article_link': 'http://diveintomark.org/archives/2009/03/27/dive-into-history-2009-edition', + 'published_date': {'__class__': 'time.asctime', '__value__': 'Fri Mar 27 22:20:42 2009'}, + 'published': True}
  1. FIXME
@@ -404,7 +444,13 @@ def from_json(json_object): ... entry = json.load(f, object_hook = customserializer.from_json) ... >>> entry -FIXME +{'comments_link': None, + 'internal_id': b'\xde\xd5\xb4\xf8', + 'title': 'Dive into history, 2009 edition', + 'tags': ['diveintopython', 'docbook', 'html'], + 'article_link': 'http://diveintomark.org/archives/2009/03/27/dive-into-history-2009-edition', + 'published_date': time.struct_time(tm_year=2009, tm_mon=3, tm_mday=27, tm_hour=22, tm_min=20, tm_sec=42, tm_wday=4, tm_yday=86, tm_isdst=-1), + 'published': True}
  1. FIXME