-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
56 lines (49 loc) · 1.45 KB
/
main.py
File metadata and controls
56 lines (49 loc) · 1.45 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
import json
from decimal import Decimal
from pprint import pprint
import arrow
from JsonEncoder import json_encode, json_decode
from MyClass import MyClass
# Encode a dict
json_encode({
"name": "Paul",
"birthdate": arrow.get("1992-02-22").datetime,
"decimal_number": Decimal(14.456),
"float_number": 14.456,
"int_number": 122
})
# Encode a custom object
my_obj = MyClass("value1", "value2")
json_encode(my_obj)
# Encode nested dicts
json_encode({
"name": "Paul",
"birthdate": arrow.get("1992-02-22").datetime,
"decimal_number": Decimal(14.456),
"float_number": 14.456,
"int_number": 122,
"my_dict": {
"value_1": 1,
"value_2": "2",
},
"list": [
{
"value_1": 1,
"value_2": "2",
},
{
"value_1": 1,
"value_2": "2",
},
{
"value_1": 1,
"value_2": "2",
}
]
})
# Decode a dict with various types
json_decode('{"name": "Paul", "birthdate": "1992-02-22T00:00:00+00:00", "float_number": 14.456, "int_number": 122}')
# Decode a dict, with nested dicts and list
json_decode('{"name": "Paul", "birthdate": "1992-02-22T00:00:00+00:00", "decimal_number": 14.46, "float_number": 14.456, "int_number": 122, "my_dict": {"value_1": 1, "value_2": "2"}, "list": [{"value_1": 1, "value_2": "2"}, {"value_1": 1, "value_2": "2"}, {"value_1": 1, "value_2": "2"}]}')
# Decode an object of MyClass
json_decode("{\"value1\": \"value1\", \"value2\": \"value2\"}")