Skip to content

Commit

Permalink
more progressive serializing
Browse files Browse the repository at this point in the history
-When serializing datetime, date, time or DateTime properties, just use the
 unicode representation which can be parsed.

- When serializing values, if there is no special handler for a field
  type, just try to unicode the value.
  • Loading branch information
thet committed Apr 9, 2015
1 parent 0c2ffc4 commit b35daa3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
8 changes: 8 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ Changelog
0.3 (unreleased)
----------------

- When serializing datetime, date, time or DateTime properties, just use the
unicode representation which can be parsed.
[thet]

- When serializing values, if there is no special handler for a field type,
just try to unicode the value.
[thet]

- Fix export of defaultPage and layout. Before, always the defaultPage was set
now layout is always set and defaultPage only, if there is one defined.
[thet]
Expand Down
19 changes: 12 additions & 7 deletions collective/jsonify/wrapper.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from Acquisition import aq_base
from DateTime import DateTime
import datetime
import os


Expand All @@ -7,7 +10,6 @@ class Wrapper(dict):
"""

def __init__(self, context):
from Acquisition import aq_base
self.context = context
self._context = aq_base(context)
self.charset = None
Expand Down Expand Up @@ -130,10 +132,9 @@ def get_dexterity_fields(self):
)
if type(value) in BASIC_TYPES:
pass
elif field is not None:
value = unicode(value)
else:
raise ValueError('Unable to serialize field value')
# E.g. DateTime or datetime are nicely representated
value = unicode(value)

self[unicode(fieldname)] = value

Expand Down Expand Up @@ -340,9 +341,8 @@ def _enc(val):
self[unicode(fieldname)] = value

else:
raise TypeError(
'ArchetypesWrapper: Unknown field %s (type: %s) at %s' % (
fieldname, type_, self.context.absolute_url()))
# Just try to stringify value
self[unicode(fieldname)] = unicode(value)

def get_references(self):
"""AT references.
Expand Down Expand Up @@ -424,6 +424,11 @@ def get_properties(self):
typ = self.context.getPropertyType(pid)
if typ == 'string' and isinstance(val, str):
val = self.decode(val)
if isinstance(val, DateTime)\
or isinstance(val, datetime.time)\
or isinstance(val, datetime.datetime)\
or isinstance(val, datetime.date):
val = unicode(val)
self['_properties'].append(
(pid, val, self.context.getPropertyType(pid))
)
Expand Down

0 comments on commit b35daa3

Please sign in to comment.