Skip to content
This repository has been archived by the owner on May 13, 2020. It is now read-only.

Commit

Permalink
- Added serializer for datetime.time, since the loaded reduced st…
Browse files Browse the repository at this point in the history
…ate is

  not usable (due to string to unicode conversion).
  • Loading branch information
strichter committed Oct 4, 2013
1 parent 7d11b12 commit f82bb29
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGES.txt
Expand Up @@ -5,7 +5,8 @@ CHANGES
0.8.5 (unreleased)
------------------

- Nothing changed yet.
- Added serializer for ``datetime.time``, since the loaded reduced state is
not usable (due to string to unicode conversion).


0.8.4 (2013-06-13)
Expand Down
17 changes: 17 additions & 0 deletions src/mongopersist/serializers.py
Expand Up @@ -30,3 +30,20 @@ def can_write(self, obj):
def write(self, obj):
return {'_py_type': 'datetime.date',
'ordinal': obj.toordinal()}


class TimeSerializer(serialize.ObjectSerializer):

def can_read(self, state):
return isinstance(state, dict) and \
state.get('_py_type') == 'datetime.time'

def read(self, state):
return datetime.time(*state['components'])

def can_write(self, obj):
return isinstance(obj, datetime.time)

def write(self, obj):
return {'_py_type': 'datetime.time',
'components': [obj.hour, obj.minute, obj.second]}

0 comments on commit f82bb29

Please sign in to comment.