Ticket #5553: json.diff
File json.diff, 2.9 KB (added by , 17 years ago) |
---|
-
django/utils/encoding.py
1 1 import types 2 2 import urllib 3 import datetime 3 4 from django.utils.functional import Promise 4 5 5 6 class StrAndUnicode(object): … … 30 31 31 32 If strings_only is True, don't convert (some) non-string-like objects. 32 33 """ 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)): 34 35 return s 35 36 if not isinstance(s, basestring,): 36 37 if hasattr(s, '__unicode__'): -
tests/modeltests/serializers/models.py
63 63 64 64 def __unicode__(self): 65 65 return self.title 66 67 class Score(models.Model): 68 score = models.FloatField() 66 69 67 70 __test__ = {'API_TESTS':""" 68 71 # Create some data: … … 83 86 >>> a2 = Article( 84 87 ... author = joe, 85 88 ... 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)) 87 90 >>> a1.save(); a2.save() 88 91 >>> a1.categories = [sports, op_ed] 89 92 >>> a2.categories = [music, op_ed] … … 181 184 182 185 # Serializer output can be restricted to a subset of fields 183 186 >>> 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"}}] 185 188 186 189 # Every string is serialized as a unicode object, also primary key 187 190 # which is 'varchar' … … 207 210 >>> print list(serializers.deserialize('json', serializers.serialize('json', [mv2])))[0].object.id 208 211 None 209 212 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 218 3.4 219 210 220 """}