擅长:python、mysql、java
<p>我为simplejson编码器编写了一个扩展。它返回图像的路径,而不是将图像序列化为base643。以下是一个片段:</p>
<pre><code>def encode_datetime(obj):
"""
Extended encoder function that helps to serialize dates and images
"""
if isinstance(obj, datetime.date):
try:
return obj.strftime('%Y-%m-%d')
except ValueError, e:
return ''
if isinstance(obj, ImageFieldFile):
try:
return obj.path
except ValueError, e:
return ''
raise TypeError(repr(obj) + " is not JSON serializable")
</code></pre>