-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathtest_dict_subclass.py
More file actions
57 lines (44 loc) · 1.63 KB
/
test_dict_subclass.py
File metadata and controls
57 lines (44 loc) · 1.63 KB
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}]}