FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-rapidjson/tests/test_dict_subclass.py at master · python-rapidjson/python-rapidjson · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
python-rapidjson
/
python-rapidjson
Public
Notifications
You must be signed in to change notification settings
Fork
53
Star
532
Code
Issues
18
Pull requests
4
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
python-rapidjson
/
tests
/
test_dict_subclass.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
57 lines (44 loc) · 1.63 KB
Breadcrumbs
python-rapidjson
/
tests
/
test_dict_subclass.py
Copy path
File metadata and controls
57 lines (44 loc) · 1.63 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# -*- coding: utf-8 -*-
# :Project: python-rapidjson -- Test dict subclasses
# :Created: dom 17 mar 2019 12:38:24 CET
# :Author: Lele Gaifax <lele@metapensiero.it>
# :License: MIT License
# :Copyright: © 2019, 2021 Lele Gaifax
#
from
collections
import
OrderedDict
from
rapidjson
import
Decoder
class
OrderedDecoder
(
Decoder
):
def
start_object
(
self
):
return
OrderedDict
()
def
test_ordered_dict
():
od
=
OrderedDecoder
()
result
=
od
(
'{"foo": "bar", "bar": "foo"}'
)
assert
isinstance
(
result
,
OrderedDict
)
assert
list
(
result
.
keys
())
==
[
"foo"
,
"bar"
]
class
ObjectsAsKeyValuePairsDecoder
(
Decoder
):
def
start_object
(
self
):
return
[]
def
test_objects_as_key_value_pairs
():
kvp
=
ObjectsAsKeyValuePairsDecoder
()
result
=
kvp
(
'{"a": 1, "b": {"b1": 1, "b2": 2}}'
)
assert
result
==
[(
'a'
,
1
), (
'b'
, [(
'b1'
,
1
), (
'b2'
,
2
)])]
class
JoinDuplicatedKeysDecoder
(
ObjectsAsKeyValuePairsDecoder
):
def
end_object
(
self
,
ordered_pairs
):
# Adapted from https://stackoverflow.com/a/38307621
d
=
{}
for
k
,
v
in
ordered_pairs
:
if
k
in
d
:
if
type
(
d
[
k
])
==
list
:
d
[
k
].
append
(
v
)
else
:
newlist
=
[]
newlist
.
append
(
d
[
k
])
newlist
.
append
(
v
)
d
[
k
]
=
newlist
else
:
d
[
k
]
=
v
return
d
def
test_join_duplicated_keys
():
jdk
=
JoinDuplicatedKeysDecoder
()
result
=
jdk
(
'{"a": 1, "b": {"b1": 1, "b2": 2}, "b": {"b1": 3, "b2": 2,"b4": 8}}'
)
assert
result
==
{
'a'
:
1
,
'b'
: [{
'b1'
:
1
,
'b2'
:
2
}, {
'b1'
:
3
,
'b2'
:
2
,
'b4'
:
8
}]}
Back
|
FazBrowse Home
|
New Git URL