Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
D
django
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
django
Commits
c1b6f554
Kaydet (Commit)
c1b6f554
authored
Haz 28, 2016
tarafından
Berker Peksag
Kaydeden (comit)
Tim Graham
Haz 28, 2016
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Fixed #15091 -- Allowed passing custom encoder to JSON serializer.
üst
6bf79640
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
37 additions
and
2 deletions
+37
-2
json.py
django/core/serializers/json.py
+2
-2
1.11.txt
docs/releases/1.11.txt
+4
-0
serialization.txt
docs/topics/serialization.txt
+11
-0
test_json.py
tests/serializers/test_json.py
+20
-0
No files found.
django/core/serializers/json.py
Dosyayı görüntüle @
c1b6f554
...
...
@@ -37,6 +37,7 @@ class Serializer(PythonSerializer):
if
self
.
options
.
get
(
'indent'
):
# Prevent trailing spaces
self
.
json_kwargs
[
'separators'
]
=
(
','
,
': '
)
self
.
json_kwargs
.
setdefault
(
'cls'
,
DjangoJSONEncoder
)
def
start_serialization
(
self
):
self
.
_init_options
()
...
...
@@ -58,8 +59,7 @@ class Serializer(PythonSerializer):
self
.
stream
.
write
(
" "
)
if
indent
:
self
.
stream
.
write
(
"
\n
"
)
json
.
dump
(
self
.
get_dump_object
(
obj
),
self
.
stream
,
cls
=
DjangoJSONEncoder
,
**
self
.
json_kwargs
)
json
.
dump
(
self
.
get_dump_object
(
obj
),
self
.
stream
,
**
self
.
json_kwargs
)
self
.
_current
=
None
def
getvalue
(
self
):
...
...
docs/releases/1.11.txt
Dosyayı görüntüle @
c1b6f554
...
...
@@ -206,6 +206,10 @@ Serialization
* The new ``django.core.serializers.base.Serializer.stream_class`` attribute
allows subclasses to customize the default stream.
* The encoder used by the :ref:`JSON serializer <serialization-formats-json>`
can now be customized by passing a ``cls`` keyword argument to the
``serializers.serialize()`` function.
Signals
~~~~~~~
...
...
docs/topics/serialization.txt
Dosyayı görüntüle @
c1b6f554
...
...
@@ -265,6 +265,17 @@ work::
return force_text(obj)
return super(LazyEncoder, self).default(obj)
You can then pass ``cls=LazyEncoder`` to the ``serializers.serialize()``
function::
from django.core.serializers import serialize
serialize('json', SomeModel.objects.all(), cls=LazyEncoder)
.. versionchanged:: 1.11
The ability to use a custom encoder using ``cls=...`` was added.
Also note that GeoDjango provides a :doc:`customized GeoJSON serializer
</ref/contrib/gis/serializers>`.
...
...
tests/serializers/test_json.py
Dosyayı görüntüle @
c1b6f554
# -*- coding: utf-8 -*-
from
__future__
import
unicode_literals
import
decimal
import
json
import
re
from
django.core
import
serializers
from
django.core.serializers.base
import
DeserializationError
from
django.core.serializers.json
import
DjangoJSONEncoder
from
django.db
import
models
from
django.test
import
SimpleTestCase
,
TestCase
,
TransactionTestCase
from
django.test.utils
import
isolate_apps
from
django.utils.translation
import
override
,
ugettext_lazy
from
.models
import
Score
...
...
@@ -80,6 +83,23 @@ class JsonSerializerTestCase(SerializersTestBase, TestCase):
if
re
.
search
(
r'.+,\s*$'
,
line
):
self
.
assertEqual
(
line
,
line
.
rstrip
())
@isolate_apps
(
'serializers'
)
def
test_custom_encoder
(
self
):
class
ScoreDecimal
(
models
.
Model
):
score
=
models
.
DecimalField
()
class
CustomJSONEncoder
(
json
.
JSONEncoder
):
def
default
(
self
,
o
):
if
isinstance
(
o
,
decimal
.
Decimal
):
return
str
(
o
)
return
super
(
CustomJSONEncoder
,
self
)
.
default
(
o
)
s
=
serializers
.
json
.
Serializer
()
json_data
=
s
.
serialize
(
[
ScoreDecimal
(
score
=
decimal
.
Decimal
(
1.0
))],
cls
=
CustomJSONEncoder
)
self
.
assertIn
(
'"fields": {"score": "1"}'
,
json_data
)
def
test_json_deserializer_exception
(
self
):
with
self
.
assertRaises
(
DeserializationError
):
for
obj
in
serializers
.
deserialize
(
"json"
,
"""[{"pk":1}"""
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment