Code

Ticket #5553: json.diff

File json.diff, 2.9 KB (added by pigletto, 7 years ago)
Line 
1Index: django/utils/encoding.py
2===================================================================
3--- django/utils/encoding.py    (wersja 6393)
4+++ django/utils/encoding.py    (kopia robocza)
5@@ -1,5 +1,6 @@
6 import types
7 import urllib
8+import datetime
9 from django.utils.functional import Promise
10 
11 class StrAndUnicode(object):
12@@ -30,7 +31,7 @@
13 
14     If strings_only is True, don't convert (some) non-string-like objects.
15     """
16-    if strings_only and isinstance(s, (types.NoneType, int, long)):
17+    if strings_only and isinstance(s, (types.NoneType, int, long, datetime.datetime, datetime.time, float)):
18         return s
19     if not isinstance(s, basestring,):
20         if hasattr(s, '__unicode__'):
21Index: tests/modeltests/serializers/models.py
22===================================================================
23--- tests/modeltests/serializers/models.py      (wersja 6393)
24+++ tests/modeltests/serializers/models.py      (kopia robocza)
25@@ -63,6 +63,9 @@
26 
27     def __unicode__(self):
28         return self.title
29+       
30+class Score(models.Model):
31+    score = models.FloatField()
32 
33 __test__ = {'API_TESTS':"""
34 # Create some data:
35@@ -83,7 +86,7 @@
36 >>> a2 = Article(
37 ...     author = joe,
38 ...     headline = "Time to reform copyright",
39-...     pub_date = datetime(2006, 6, 16, 13, 00))
40+...     pub_date = datetime(2006, 6, 16, 13, 00, 11, 345))
41 >>> a1.save(); a2.save()
42 >>> a1.categories = [sports, op_ed]
43 >>> a2.categories = [music, op_ed]
44@@ -181,7 +184,7 @@
45 
46 # Serializer output can be restricted to a subset of fields
47 >>> print serializers.serialize("json", Article.objects.all(), fields=('headline','pub_date'))
48-[{"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"}}]
49+[{"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"}}]
50 
51 # Every string is serialized as a unicode object, also primary key
52 # which is 'varchar'
53@@ -207,4 +210,11 @@
54 >>> print list(serializers.deserialize('json', serializers.serialize('json', [mv2])))[0].object.id
55 None
56 
57+# Serialization and deserialization of floats:
58+>>> sc = Score(score=3.4)
59+>>> print serializers.serialize("json", [sc])
60+[{"pk": null, "model": "serializers.score", "fields": {"score": 3.4}}]
61+>>> print list(serializers.deserialize('json', serializers.serialize('json', [sc])))[0].object.score
62+3.4
63+
64 """}