File tree Expand file tree Collapse file tree 3 files changed +50
-2
lines changed Expand file tree Collapse file tree 3 files changed +50
-2
lines changed Original file line number Diff line number Diff line change @@ -34,12 +34,21 @@ Installing
34
34
Using
35
35
-----
36
36
37
+ To encode an object into the canonicaljson:
38
+
37
39
.. code :: python
38
40
39
41
import canonicaljson
40
42
assert canonicaljson.encode_canonical_json({}) == b ' {}'
41
43
42
- The underlying JSON implementation can be choosen with the following:
44
+ There's also an iterator version:
45
+
46
+ .. code :: python
47
+
48
+ import canonicaljson
49
+ assert b ' ' .join(canonicaljson.iterencode_canonical_json({})) == b ' {}'
50
+
51
+ The underlying JSON implementation can be chosen with the following:
43
52
44
53
.. code :: python
45
54
Original file line number Diff line number Diff line change @@ -75,12 +75,45 @@ def encode_canonical_json(json_object):
75
75
return s .encode ("utf-8" )
76
76
77
77
78
+ def iterencode_canonical_json (json_object ):
79
+ """Encodes the shortest UTF-8 JSON encoding with dictionary keys
80
+ lexicographically sorted by unicode code point.
81
+
82
+ Args:
83
+ json_object (dict): The JSON object to encode.
84
+
85
+ Returns:
86
+ generator which yields bytes encoding the JSON object"""
87
+ for chunk in _canonical_encoder .iterencode (json_object ):
88
+ yield chunk .encode ("utf-8" )
89
+
90
+
78
91
def encode_pretty_printed_json (json_object ):
79
- """Encodes the JSON object dict as human readable ascii bytes."""
92
+ """
93
+ Encodes the JSON object dict as human readable ascii bytes.
94
+
95
+ Args:
96
+ json_object (dict): The JSON object to encode.
97
+
98
+ Returns:
99
+ bytes encoding the JSON object"""
80
100
81
101
return _pretty_encoder .encode (json_object ).encode ("ascii" )
82
102
83
103
104
+ def iterencode_pretty_printed_json (json_object ):
105
+ """Encodes the JSON object dict as human readable ascii bytes.
106
+
107
+ Args:
108
+ json_object (dict): The JSON object to encode.
109
+
110
+ Returns:
111
+ generator which yields bytes encoding the JSON object"""
112
+
113
+ for chunk in _pretty_encoder .iterencode (json_object ):
114
+ yield chunk .encode ("ascii" )
115
+
116
+
84
117
if platform .python_implementation () == "PyPy" : # pragma: no cover
85
118
# pypy ships with an optimised JSON encoder/decoder that is faster than
86
119
# simplejson's C extension.
Original file line number Diff line number Diff line change 18
18
from canonicaljson import (
19
19
encode_canonical_json ,
20
20
encode_pretty_printed_json ,
21
+ iterencode_canonical_json ,
22
+ iterencode_pretty_printed_json ,
21
23
set_json_library ,
22
24
)
23
25
@@ -62,6 +64,9 @@ def test_encode_canonical(self):
62
64
b'"\\ \\ u1234"' ,
63
65
)
64
66
67
+ # Iteratively encoding should work.
68
+ self .assertEqual (list (iterencode_canonical_json ({})), [b'{}' ])
69
+
65
70
def test_ascii (self ):
66
71
"""
67
72
Ensure the proper ASCII characters are escaped.
@@ -101,6 +106,7 @@ def test_ascii(self):
101
106
102
107
def test_encode_pretty_printed (self ):
103
108
self .assertEqual (encode_pretty_printed_json ({}), b'{}' )
109
+ self .assertEqual (list (iterencode_pretty_printed_json ({})), [b'{}' ])
104
110
105
111
def test_frozen_dict (self ):
106
112
self .assertEqual (
You can’t perform that action at this time.
0 commit comments