4343# front of PDF base64-encoded
4444PDF64 = b'JVBER'
4545
46+
4647def encode_images (format_dict ):
4748 """b64-encodes images in a displaypub format dict
4849
@@ -91,15 +92,15 @@ def json_clean(obj):
9192
9293 """
9394 # types that are 'atomic' and ok in json as-is.
94- atomic_ok = (str , type (None ))
95+ atomic_ok = (bool , str , type (None ))
9596
9697 # containers that we need to convert into lists
97- container_to_list = (tuple , set , types .GeneratorType )
98+ container_to_list = (list , tuple , set , types .GeneratorType )
9899
99100 # Since bools are a subtype of Integrals, which are a subtype of Reals,
100101 # we have to check them in that order.
101102
102- if isinstance (obj , bool ):
103+ if isinstance (obj , atomic_ok ):
103104 return obj
104105
105106 if isinstance (obj , numbers .Integral ):
@@ -111,20 +112,14 @@ def json_clean(obj):
111112 if math .isnan (obj ) or math .isinf (obj ):
112113 return repr (obj )
113114 return float (obj )
114-
115- if isinstance (obj , atomic_ok ):
116- return obj
117-
115+
118116 if isinstance (obj , bytes ):
119117 # unanmbiguous binary data is base64-encoded
120118 # (this probably should have happened upstream)
121119 return b2a_base64 (obj ).decode ('ascii' )
122120
123121 if isinstance (obj , container_to_list ) or (
124122 hasattr (obj , '__iter__' ) and hasattr (obj , next_attr_name )):
125- obj = list (obj )
126-
127- if isinstance (obj , list ):
128123 return [json_clean (x ) for x in obj ]
129124
130125 if isinstance (obj , dict ):
@@ -138,11 +133,12 @@ def json_clean(obj):
138133 'key collision would lead to dropped values' )
139134 # If all OK, proceed by making the new dict that will be json-safe
140135 out = {}
141- for k ,v in obj .items ():
136+ for k , v in obj .items ():
142137 out [str (k )] = json_clean (v )
143138 return out
139+
144140 if isinstance (obj , datetime ):
145141 return obj .strftime (ISO8601 )
146-
142+
147143 # we don't understand it, it's probably an unserializable object
148144 raise ValueError ("Can't clean for JSON: %r" % obj )
0 commit comments