2012-09-20 16:06:27 +04:00
|
|
|
"""
|
|
|
|
Helper classes for parsers.
|
|
|
|
"""
|
2013-02-05 00:55:35 +04:00
|
|
|
from __future__ import unicode_literals
|
2013-09-25 13:30:04 +04:00
|
|
|
from django.utils import timezone
|
2014-01-17 16:05:10 +04:00
|
|
|
from django.db.models.query import QuerySet
|
2013-02-05 00:55:35 +04:00
|
|
|
from django.utils.datastructures import SortedDict
|
2013-05-25 02:44:23 +04:00
|
|
|
from django.utils.functional import Promise
|
2013-09-25 13:30:04 +04:00
|
|
|
from rest_framework.compat import force_text
|
2013-02-05 00:55:35 +04:00
|
|
|
from rest_framework.serializers import DictWithMetadata, SortedDictWithMetadata
|
2012-09-20 16:06:27 +04:00
|
|
|
import datetime
|
|
|
|
import decimal
|
2012-10-10 19:34:00 +04:00
|
|
|
import types
|
2013-01-05 16:40:02 +04:00
|
|
|
import json
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
|
|
|
|
class JSONEncoder(json.JSONEncoder):
|
|
|
|
"""
|
2013-01-15 17:02:53 +04:00
|
|
|
JSONEncoder subclass that knows how to encode date/time/timedelta,
|
2012-09-20 16:06:27 +04:00
|
|
|
decimal types, and generators.
|
|
|
|
"""
|
|
|
|
def default(self, o):
|
|
|
|
# For Date Time string spec, see ECMA 262
|
|
|
|
# http://ecma-international.org/ecma-262/5.1/#sec-15.9.1.15
|
2013-05-25 02:44:23 +04:00
|
|
|
if isinstance(o, Promise):
|
|
|
|
return force_text(o)
|
|
|
|
elif isinstance(o, datetime.datetime):
|
2012-09-20 16:06:27 +04:00
|
|
|
r = o.isoformat()
|
|
|
|
if o.microsecond:
|
|
|
|
r = r[:23] + r[26:]
|
|
|
|
if r.endswith('+00:00'):
|
|
|
|
r = r[:-6] + 'Z'
|
|
|
|
return r
|
|
|
|
elif isinstance(o, datetime.date):
|
|
|
|
return o.isoformat()
|
|
|
|
elif isinstance(o, datetime.time):
|
|
|
|
if timezone and timezone.is_aware(o):
|
|
|
|
raise ValueError("JSON can't represent timezone-aware times.")
|
|
|
|
r = o.isoformat()
|
|
|
|
if o.microsecond:
|
|
|
|
r = r[:12]
|
|
|
|
return r
|
2013-01-15 17:02:53 +04:00
|
|
|
elif isinstance(o, datetime.timedelta):
|
|
|
|
return str(o.total_seconds())
|
2012-09-20 16:06:27 +04:00
|
|
|
elif isinstance(o, decimal.Decimal):
|
|
|
|
return str(o)
|
2014-01-17 16:05:10 +04:00
|
|
|
elif isinstance(o, QuerySet):
|
|
|
|
return list(o)
|
2013-09-13 07:36:18 +04:00
|
|
|
elif hasattr(o, 'tolist'):
|
|
|
|
return o.tolist()
|
2013-11-22 00:09:48 +04:00
|
|
|
elif hasattr(o, '__getitem__'):
|
|
|
|
try:
|
|
|
|
return dict(o)
|
2013-12-09 13:24:10 +04:00
|
|
|
except:
|
2013-11-22 00:09:48 +04:00
|
|
|
pass
|
2012-09-20 16:06:27 +04:00
|
|
|
elif hasattr(o, '__iter__'):
|
|
|
|
return [i for i in o]
|
|
|
|
return super(JSONEncoder, self).default(o)
|
2012-10-10 19:34:00 +04:00
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
import yaml
|
|
|
|
except ImportError:
|
|
|
|
SafeDumper = None
|
|
|
|
else:
|
|
|
|
# Adapted from http://pyyaml.org/attachment/ticket/161/use_ordered_dict.py
|
|
|
|
class SafeDumper(yaml.SafeDumper):
|
|
|
|
"""
|
|
|
|
Handles decimals as strings.
|
|
|
|
Handles SortedDicts as usual dicts, but preserves field order, rather
|
|
|
|
than the usual behaviour of sorting the keys.
|
|
|
|
"""
|
|
|
|
def represent_decimal(self, data):
|
|
|
|
return self.represent_scalar('tag:yaml.org,2002:str', str(data))
|
|
|
|
|
|
|
|
def represent_mapping(self, tag, mapping, flow_style=None):
|
|
|
|
value = []
|
|
|
|
node = yaml.MappingNode(tag, value, flow_style=flow_style)
|
|
|
|
if self.alias_key is not None:
|
|
|
|
self.represented_objects[self.alias_key] = node
|
|
|
|
best_style = True
|
|
|
|
if hasattr(mapping, 'items'):
|
|
|
|
mapping = list(mapping.items())
|
|
|
|
if not isinstance(mapping, SortedDict):
|
|
|
|
mapping.sort()
|
|
|
|
for item_key, item_value in mapping:
|
|
|
|
node_key = self.represent_data(item_key)
|
|
|
|
node_value = self.represent_data(item_value)
|
|
|
|
if not (isinstance(node_key, yaml.ScalarNode) and not node_key.style):
|
|
|
|
best_style = False
|
|
|
|
if not (isinstance(node_value, yaml.ScalarNode) and not node_value.style):
|
|
|
|
best_style = False
|
|
|
|
value.append((node_key, node_value))
|
|
|
|
if flow_style is None:
|
|
|
|
if self.default_flow_style is not None:
|
|
|
|
node.flow_style = self.default_flow_style
|
|
|
|
else:
|
|
|
|
node.flow_style = best_style
|
|
|
|
return node
|
|
|
|
|
2014-08-19 16:28:07 +04:00
|
|
|
SafeDumper.add_representer(
|
|
|
|
decimal.Decimal,
|
|
|
|
SafeDumper.represent_decimal
|
|
|
|
)
|
|
|
|
SafeDumper.add_representer(
|
|
|
|
SortedDict,
|
|
|
|
yaml.representer.SafeRepresenter.represent_dict
|
|
|
|
)
|
|
|
|
SafeDumper.add_representer(
|
|
|
|
DictWithMetadata,
|
|
|
|
yaml.representer.SafeRepresenter.represent_dict
|
|
|
|
)
|
|
|
|
SafeDumper.add_representer(
|
|
|
|
SortedDictWithMetadata,
|
|
|
|
yaml.representer.SafeRepresenter.represent_dict
|
|
|
|
)
|
|
|
|
SafeDumper.add_representer(
|
|
|
|
types.GeneratorType,
|
|
|
|
yaml.representer.SafeRepresenter.represent_list
|
|
|
|
)
|