mirror of
https://github.com/kennethreitz/dive-into-python3.git
synced 2026-06-05 15:00:18 +00:00
mention that from_json should go in customserializer.py, fix mismatched <a> tags
This commit is contained in:
+5
-4
@@ -569,12 +569,13 @@ NameError: name 'entry' is not defined</samp>
|
||||
<p><code>json.load()</code> doesn’t know anything about any conversion function you may have passed to <code>json.dump()</code>. What you need is the opposite of the <code>to_json()</code> function — a function that will take a custom-converted <abbr>JSON</abbr> object and convert it back to the original Python datatype.
|
||||
|
||||
<pre class=pp><code>
|
||||
<a>def from_json(json_object): <span class=u>①</span>
|
||||
<a> if '__class__' in json_object: <span class=u>②</span>
|
||||
# add this to customserializer.py
|
||||
<a>def from_json(json_object): <span class=u>①</span></a>
|
||||
<a> if '__class__' in json_object: <span class=u>②</span></a>
|
||||
if json_object['__class__'] == 'time.asctime':
|
||||
<a> return time.strptime(json_object['__value__']) <span class=u>③</span>
|
||||
<a> return time.strptime(json_object['__value__']) <span class=u>③</span></a>
|
||||
if json_object['__class__'] == 'bytes':
|
||||
<a> return bytes(json_object['__value__']) <span class=u>④</span>
|
||||
<a> return bytes(json_object['__value__']) <span class=u>④</span></a>
|
||||
return json_object</code></pre>
|
||||
<ol>
|
||||
<li>This conversion function also takes one parameter and returns one value. But the parameter it takes is not a string, it’s a Python object — the result of deserializing a <abbr>JSON</abbr>-encoded string into Python.
|
||||
|
||||
Reference in New Issue
Block a user