|
1 | 1 | """Canonical encoding for the JSONSchema semantics, where 1 == 1.0.""" |
2 | | -import functools |
3 | 2 | import json |
4 | 3 | import math |
5 | 4 | from json.encoder import _make_iterencode, encode_basestring_ascii # type: ignore |
6 | | -from typing import Any, Callable, Dict, Tuple, Type, Union |
| 5 | +from typing import Any, Dict, Tuple, Union |
7 | 6 |
|
8 | 7 | # Mypy does not (yet!) support recursive type definitions. |
9 | 8 | # (and writing a few steps by hand is a DoS attack on the AST walker in Pytest) |
@@ -36,62 +35,9 @@ def floatstr(o: float) -> str: |
36 | 35 | )(o, 0) |
37 | 36 |
|
38 | 37 |
|
39 | | -def _make_cache_key( |
40 | | - value: JSONType, |
41 | | -) -> Tuple[Type, Union[None, bool, float, str, tuple, frozenset]]: |
42 | | - """Make a hashable object from any JSON value. |
43 | | -
|
44 | | - The idea is to recursively convert all mutable values to immutable and adding values types as a discriminant. |
45 | | - """ |
46 | | - if isinstance(value, dict): |
47 | | - return (dict, frozenset((k, _make_cache_key(v)) for k, v in value.items())) |
48 | | - if isinstance(value, list): |
49 | | - return (list, tuple(map(_make_cache_key, value))) |
50 | | - # Primitive types are hashable |
51 | | - # `type` is needed to distinguish false-ish values - 0, "", False have the same hash (0) |
52 | | - return (type(value), value) |
53 | | - |
54 | | - |
55 | | -class HashedJSON: |
56 | | - """A proxy that holds a JSON value. |
57 | | -
|
58 | | - Adds a capability for the inner value to be cached, loosely based on `functools._HashedSeq`. |
59 | | - """ |
60 | | - |
61 | | - __slots__ = ("value", "hashedvalue") |
62 | | - |
63 | | - def __init__(self, value: JSONType): |
64 | | - self.value = value |
65 | | - # `hash` is called multiple times on cache miss, therefore it is evaluated only once |
66 | | - self.hashedvalue = hash(_make_cache_key(value)) |
67 | | - |
68 | | - def __hash__(self) -> int: |
69 | | - return self.hashedvalue |
70 | | - |
71 | | - def __eq__(self, other: "HashedJSON") -> bool: # type: ignore |
72 | | - # TYPES: This class should be used only for caching purposes and there should be |
73 | | - # no values of other types to compare |
74 | | - return self.hashedvalue == other.hashedvalue |
75 | | - |
76 | | - |
77 | | -def cached_json(func: Callable[[HashedJSON], str]) -> Callable[[JSONType], str]: |
78 | | - """Cache calls to `encode_canonical_json`. |
79 | | -
|
80 | | - The same schemas are encoded multiple times during canonicalisation and caching gives visible performance impact. |
81 | | - """ |
82 | | - cached_func = functools.lru_cache(maxsize=1024)(func) |
83 | | - |
84 | | - @functools.wraps(cached_func) |
85 | | - def wrapped(value: JSONType) -> str: |
86 | | - return cached_func(HashedJSON(value)) |
87 | | - |
88 | | - return wrapped |
89 | | - |
90 | | - |
91 | | -@cached_json |
92 | | -def encode_canonical_json(value: HashedJSON) -> str: |
| 38 | +def encode_canonical_json(value: JSONType) -> str: |
93 | 39 | """Canonical form serialiser, for uniqueness testing.""" |
94 | | - return json.dumps(value.value, sort_keys=True, cls=CanonicalisingJsonEncoder) |
| 40 | + return json.dumps(value, sort_keys=True, cls=CanonicalisingJsonEncoder) |
95 | 41 |
|
96 | 42 |
|
97 | 43 | def sort_key(value: JSONType) -> Tuple[int, float, Union[float, str]]: |
|
0 commit comments