开发者

GoogleAppEngine db Model datetimeproperty to JSON

开发者 https://www.devze.com 2023-01-09 08:24 出处:网络
I tried to use this soln (which uses this file) to convert GAE db model to json. But when I tried to use it, I got this error \"TypeError: datetime.date(2010, 7, 27) is not JSON serializable\"

I tried to use this soln (which uses this file) to convert GAE db model to json. But when I tried to use it, I got this error "TypeError: datetime.date(2010, 7, 27) is not JSON serializable"

Does anyone know whats the problem?

Or, if 开发者_如何学Pythonyou know alternative soln to convert GAE db model to JSON please suggest so.


Looks like you need to modify the json.py you referenced and add a block to handle the type.

Look at how datetime.datetime is handled at line 61:

elif isinstance(obj, datetime.datetime):
  output = {}
  fields = ['day', 'hour', 'microsecond', 'minute', 'month', 'second',
      'year']
  methods = ['ctime', 'isocalendar', 'isoformat', 'isoweekday',
      'timetuple']
  for field in fields:
    output[field] = getattr(obj, field)
  for method in methods:
    output[method] = getattr(obj, method)()
  output['epoch'] = time.mktime(obj.timetuple())
  return output

You need to add something to handle datetime.date:

elif isinstance(obj, datetime.date): 
    #your code here...

Or just use datetime.datetime instead of datetime.date.

0

精彩评论

暂无评论...
验证码 换一张
取 消