Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
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
cpython
Commits
0ad98d85
Kaydet (Commit)
0ad98d85
authored
Nis 21, 2009
tarafından
Raymond Hettinger
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Forward port r70471: Add object_pairs_hook. Issue 5381.
üst
e5925773
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
54 additions
and
8 deletions
+54
-8
__init__.py
Lib/json/__init__.py
+8
-5
decoder.py
Lib/json/decoder.py
+11
-3
test_decode.py
Lib/json/tests/test_decode.py
+19
-0
test_unicode.py
Lib/json/tests/test_unicode.py
+16
-0
No files found.
Lib/json/__init__.py
Dosyayı görüntüle @
0ad98d85
...
...
@@ -237,11 +237,12 @@ def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True,
**
kw
)
.
encode
(
obj
)
_default_decoder
=
JSONDecoder
(
encoding
=
None
,
object_hook
=
None
)
_default_decoder
=
JSONDecoder
(
encoding
=
None
,
object_hook
=
None
,
object_pairs_hook
=
None
)
def
load
(
fp
,
encoding
=
None
,
cls
=
None
,
object_hook
=
None
,
parse_float
=
None
,
parse_int
=
None
,
parse_constant
=
None
,
**
kw
):
parse_int
=
None
,
parse_constant
=
None
,
object_pairs_hook
=
None
,
**
kw
):
"""Deserialize ``fp`` (a ``.read()``-supporting file-like object
containing a JSON document) to a Python object.
...
...
@@ -264,11 +265,11 @@ def load(fp, encoding=None, cls=None, object_hook=None, parse_float=None,
return
loads
(
fp
.
read
(),
encoding
=
encoding
,
cls
=
cls
,
object_hook
=
object_hook
,
parse_float
=
parse_float
,
parse_int
=
parse_int
,
parse_constant
=
parse_constant
,
**
kw
)
parse_constant
=
parse_constant
,
object_pairs_hook
=
object_pairs_hook
,
**
kw
)
def
loads
(
s
,
encoding
=
None
,
cls
=
None
,
object_hook
=
None
,
parse_float
=
None
,
parse_int
=
None
,
parse_constant
=
None
,
**
kw
):
parse_int
=
None
,
parse_constant
=
None
,
object_pairs_hook
=
None
,
**
kw
):
"""Deserialize ``s`` (a ``str`` or ``unicode`` instance containing a JSON
document) to a Python object.
...
...
@@ -303,12 +304,14 @@ def loads(s, encoding=None, cls=None, object_hook=None, parse_float=None,
"""
if
(
cls
is
None
and
encoding
is
None
and
object_hook
is
None
and
parse_int
is
None
and
parse_float
is
None
and
parse_constant
is
None
and
not
kw
):
parse_constant
is
None
and
object_pairs_hook
is
None
and
not
kw
):
return
_default_decoder
.
decode
(
s
)
if
cls
is
None
:
cls
=
JSONDecoder
if
object_hook
is
not
None
:
kw
[
'object_hook'
]
=
object_hook
if
object_pairs_hook
is
not
None
:
kw
[
'object_pairs_hook'
]
=
object_pairs_hook
if
parse_float
is
not
None
:
kw
[
'parse_float'
]
=
parse_float
if
parse_int
is
not
None
:
...
...
Lib/json/decoder.py
Dosyayı görüntüle @
0ad98d85
...
...
@@ -164,7 +164,8 @@ WHITESPACE = re.compile(r'\s*', FLAGS)
def
JSONObject
(
match
,
context
,
_w
=
WHITESPACE
.
match
):
pairs
=
{}
pairs
=
[]
pairs_append
=
pairs
.
append
s
=
match
.
string
end
=
_w
(
s
,
match
.
end
())
.
end
()
nextchar
=
s
[
end
:
end
+
1
]
...
...
@@ -187,7 +188,7 @@ def JSONObject(match, context, _w=WHITESPACE.match):
value
,
end
=
next
(
iterscan
(
s
,
idx
=
end
,
context
=
context
))
except
StopIteration
:
raise
ValueError
(
errmsg
(
"Expecting object"
,
s
,
end
))
pairs
[
key
]
=
value
pairs
_append
((
key
,
value
))
end
=
_w
(
s
,
end
)
.
end
()
nextchar
=
s
[
end
:
end
+
1
]
end
+=
1
...
...
@@ -200,6 +201,11 @@ def JSONObject(match, context, _w=WHITESPACE.match):
end
+=
1
if
nextchar
!=
'"'
:
raise
ValueError
(
errmsg
(
"Expecting property name"
,
s
,
end
-
1
))
object_pairs_hook
=
getattr
(
context
,
'object_pairs_hook'
,
None
)
if
object_pairs_hook
is
not
None
:
result
=
object_pairs_hook
(
pairs
)
return
result
,
end
pairs
=
dict
(
pairs
)
object_hook
=
getattr
(
context
,
'object_hook'
,
None
)
if
object_hook
is
not
None
:
pairs
=
object_hook
(
pairs
)
...
...
@@ -278,7 +284,8 @@ class JSONDecoder(object):
__all__
=
[
'__init__'
,
'decode'
,
'raw_decode'
]
def
__init__
(
self
,
encoding
=
None
,
object_hook
=
None
,
parse_float
=
None
,
parse_int
=
None
,
parse_constant
=
None
,
strict
=
True
):
parse_int
=
None
,
parse_constant
=
None
,
strict
=
True
,
object_pairs_hook
=
None
):
"""``encoding`` determines the encoding used to interpret any ``str``
objects decoded by this instance (utf-8 by default). It has no
effect when decoding ``unicode`` objects.
...
...
@@ -309,6 +316,7 @@ class JSONDecoder(object):
"""
self
.
encoding
=
encoding
self
.
object_hook
=
object_hook
self
.
object_pairs_hook
=
object_pairs_hook
self
.
parse_float
=
parse_float
self
.
parse_int
=
parse_int
self
.
parse_constant
=
parse_constant
...
...
Lib/json/tests/test_decode.py
Dosyayı görüntüle @
0ad98d85
import
decimal
from
unittest
import
TestCase
from
io
import
StringIO
import
json
from
collections
import
OrderedDict
class
TestDecode
(
TestCase
):
def
test_decimal
(
self
):
...
...
@@ -13,3 +15,20 @@ class TestDecode(TestCase):
rval
=
json
.
loads
(
'1'
,
parse_int
=
float
)
self
.
assert_
(
isinstance
(
rval
,
float
))
self
.
assertEquals
(
rval
,
1.0
)
def
test_object_pairs_hook
(
self
):
s
=
'{"xkd":1, "kcw":2, "art":3, "hxm":4, "qrt":5, "pad":6, "hoy":7}'
p
=
[(
"xkd"
,
1
),
(
"kcw"
,
2
),
(
"art"
,
3
),
(
"hxm"
,
4
),
(
"qrt"
,
5
),
(
"pad"
,
6
),
(
"hoy"
,
7
)]
self
.
assertEqual
(
json
.
loads
(
s
),
eval
(
s
))
self
.
assertEqual
(
json
.
loads
(
s
,
object_pairs_hook
=
lambda
x
:
x
),
p
)
self
.
assertEqual
(
json
.
load
(
StringIO
(
s
),
object_pairs_hook
=
lambda
x
:
x
),
p
)
od
=
json
.
loads
(
s
,
object_pairs_hook
=
OrderedDict
)
self
.
assertEqual
(
od
,
OrderedDict
(
p
))
self
.
assertEqual
(
type
(
od
),
OrderedDict
)
# the object_pairs_hook takes priority over the object_hook
self
.
assertEqual
(
json
.
loads
(
s
,
object_pairs_hook
=
OrderedDict
,
object_hook
=
lambda
x
:
None
),
OrderedDict
(
p
))
Lib/json/tests/test_unicode.py
Dosyayı görüntüle @
0ad98d85
from
unittest
import
TestCase
import
json
from
collections
import
OrderedDict
class
TestUnicode
(
TestCase
):
def
test_encoding1
(
self
):
...
...
@@ -53,3 +54,18 @@ class TestUnicode(TestCase):
u
=
chr
(
i
)
js
=
'"
\\
u{0:04x}"'
.
format
(
i
)
self
.
assertEquals
(
json
.
loads
(
js
),
u
)
def
test_object_pairs_hook_with_unicode
(
self
):
s
=
'{"xkd":1, "kcw":2, "art":3, "hxm":4, "qrt":5, "pad":6, "hoy":7}'
p
=
[(
"xkd"
,
1
),
(
"kcw"
,
2
),
(
"art"
,
3
),
(
"hxm"
,
4
),
(
"qrt"
,
5
),
(
"pad"
,
6
),
(
"hoy"
,
7
)]
self
.
assertEqual
(
json
.
loads
(
s
),
eval
(
s
))
self
.
assertEqual
(
json
.
loads
(
s
,
object_pairs_hook
=
lambda
x
:
x
),
p
)
od
=
json
.
loads
(
s
,
object_pairs_hook
=
OrderedDict
)
self
.
assertEqual
(
od
,
OrderedDict
(
p
))
self
.
assertEqual
(
type
(
od
),
OrderedDict
)
# the object_pairs_hook takes priority over the object_hook
self
.
assertEqual
(
json
.
loads
(
s
,
object_pairs_hook
=
OrderedDict
,
object_hook
=
lambda
x
:
None
),
OrderedDict
(
p
))
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