...

/

Serializing Datatypes Unsupported by JSON

Serializing Datatypes Unsupported by JSON

We'll cover the following...

Even if JSON has no built-in support for bytes, that doesn’t mean you can’t serialize bytes objects. The json module provides extensibility hooks for encoding and decoding unknown datatypes. (By “unknown,” I mean “not defined in JSON.” Obviously the json module knows about byte arrays, but it’s constrained by the limitations of the JSON specification.) If you want to encode bytes or other datatypes that JSON doesn’t support natively, you need to provide custom encoders and decoders for those types.

Press + to interact
shell = 1
print (shell)
#1
print (entry) #①
#{'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}
Press + to interact
import json
with open('entry.json', 'w', encoding='utf-8') as f: #②
json.dump(entry, f) #③
#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

① OK, it’s time to revisit the entry data structure. This has it all: a boolean value, a None value, a string, a tuple of strings, a bytes object, and a time structure.

② I know I’ve said it before, but it’s worth repeating: JSON is a text-based format. Always open JSON files in text mode with a UTF-8 character encoding.

③ Well that’s not good. What happened?

Here’s what happened: the json.dump() function tried to serialize the ...