Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
Version next

* Fix producing non-standard JSON for Infinity, -Infinity, and NaN. This could
error when encoding objects into canonical JSON that previously used to work,
but were incompatible with JSON implementations in other languages.

Version 1.3.0 released 2020-08-14

* The minimum version of simplejson was bumped to 3.14.0.
Expand Down
8 changes: 6 additions & 2 deletions canonicaljson.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,16 @@ def set_json_library(json_lib):
"""
global _canonical_encoder
_canonical_encoder = json_lib.JSONEncoder(
ensure_ascii=False, separators=(",", ":"), sort_keys=True, default=_default,
ensure_ascii=False,
allow_nan=False,
separators=(",", ":"),
sort_keys=True,
default=_default,
)

global _pretty_encoder
_pretty_encoder = json_lib.JSONEncoder(
ensure_ascii=False, indent=4, sort_keys=True, default=_default,
ensure_ascii=False, allow_nan=False, indent=4, sort_keys=True, default=_default,
)


Expand Down
26 changes: 26 additions & 0 deletions test_canonicaljson.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from math import inf, nan

from canonicaljson import (
encode_canonical_json,
Expand Down Expand Up @@ -114,6 +115,31 @@ class Unknown(object):
with self.assertRaises(Exception):
encode_canonical_json(unknown_object)

with self.assertRaises(Exception):
encode_pretty_printed_json(unknown_object)

def test_invalid_float_values(self):
"""Infinity/-Infinity/NaN are not allowed in canonicaljson.
"""

with self.assertRaises(ValueError):
encode_canonical_json(inf)

with self.assertRaises(ValueError):
encode_pretty_printed_json(inf)

with self.assertRaises(ValueError):
encode_canonical_json(-inf)

with self.assertRaises(ValueError):
encode_pretty_printed_json(-inf)

with self.assertRaises(ValueError):
encode_canonical_json(nan)

with self.assertRaises(ValueError):
encode_pretty_printed_json(nan)

def test_set_json(self):
"""Ensure that changing the underlying JSON implementation works."""
mock_json = mock.Mock(spec=["JSONEncoder"])
Expand Down