From 5cbf1694e5172480f4164d67b8497bf945cbed94 Mon Sep 17 00:00:00 2001 From: Mark Pilgrim Date: Wed, 2 Sep 2009 15:49:31 -0400 Subject: [PATCH] mention that from_json should go in customserializer.py, fix mismatched tags --- serializing.html | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/serializing.html b/serializing.html index 2f6ec3a..0ffbb7a 100644 --- a/serializing.html +++ b/serializing.html @@ -569,12 +569,13 @@ NameError: name 'entry' is not defined

json.load() doesn’t know anything about any conversion function you may have passed to json.dump(). What you need is the opposite of the to_json() function — a function that will take a custom-converted JSON object and convert it back to the original Python datatype.


-def from_json(json_object):                                   
-    if '__class__' in json_object:                            
+# add this to customserializer.py
+def from_json(json_object):                                   
+    if '__class__' in json_object:                            
         if json_object['__class__'] == 'time.asctime':
-            return time.strptime(json_object['__value__'])    
+            return time.strptime(json_object['__value__'])    
         if json_object['__class__'] == 'bytes':
-            return bytes(json_object['__value__'])            
+            return bytes(json_object['__value__'])            
     return json_object
  1. 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 JSON-encoded string into Python.