Ticket #5553: json.diff

File json.diff, 2.9 KB (added by Maciej Wiśniowski, 17 years ago)
  • django/utils/encoding.py

     
    11import types
    22import urllib
     3import datetime
    34from django.utils.functional import Promise
    45
    56class StrAndUnicode(object):
     
    3031
    3132    If strings_only is True, don't convert (some) non-string-like objects.
    3233    """
    33     if strings_only and isinstance(s, (types.NoneType, int, long)):
     34    if strings_only and isinstance(s, (types.NoneType, int, long, datetime.datetime, datetime.time, float)):
    3435        return s
    3536    if not isinstance(s, basestring,):
    3637        if hasattr(s, '__unicode__'):
  • tests/modeltests/serializers/models.py

     
    6363
    6464    def __unicode__(self):
    6565        return self.title
     66       
     67class Score(models.Model):
     68    score = models.FloatField()
    6669
    6770__test__ = {'API_TESTS':"""
    6871# Create some data:
     
    8386>>> a2 = Article(
    8487...     author = joe,
    8588...     headline = "Time to reform copyright",
    86 ...     pub_date = datetime(2006, 6, 16, 13, 00))
     89...     pub_date = datetime(2006, 6, 16, 13, 00, 11, 345))
    8790>>> a1.save(); a2.save()
    8891>>> a1.categories = [sports, op_ed]
    8992>>> a2.categories = [music, op_ed]
     
    181184
    182185# Serializer output can be restricted to a subset of fields
    183186>>> print serializers.serialize("json", Article.objects.all(), fields=('headline','pub_date'))
    184 [{"pk": 1, "model": "serializers.article", "fields": {"headline": "Just kidding; I love TV poker", "pub_date": "2006-06-16 11:00:00"}}, {"pk": 2, "model": "serializers.article", "fields": {"headline": "Time to reform copyright", "pub_date": "2006-06-16 13:00:00"}}, {"pk": 3, "model": "serializers.article", "fields": {"headline": "Forward references pose no problem", "pub_date": "2006-06-16 15:00:00"}}]
     187[{"pk": 1, "model": "serializers.article", "fields": {"headline": "Just kidding; I love TV poker", "pub_date": "2006-06-16 11:00:00"}}, {"pk": 2, "model": "serializers.article", "fields": {"headline": "Time to reform copyright", "pub_date": "2006-06-16 13:00:11"}}, {"pk": 3, "model": "serializers.article", "fields": {"headline": "Forward references pose no problem", "pub_date": "2006-06-16 15:00:00"}}]
    185188
    186189# Every string is serialized as a unicode object, also primary key
    187190# which is 'varchar'
     
    207210>>> print list(serializers.deserialize('json', serializers.serialize('json', [mv2])))[0].object.id
    208211None
    209212
     213# Serialization and deserialization of floats:
     214>>> sc = Score(score=3.4)
     215>>> print serializers.serialize("json", [sc])
     216[{"pk": null, "model": "serializers.score", "fields": {"score": 3.4}}]
     217>>> print list(serializers.deserialize('json', serializers.serialize('json', [sc])))[0].object.score
     2183.4
     219
    210220"""}
Back to Top