diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 1461b05..f4352c7 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -22,4 +22,4 @@ repos: additional_dependencies: - --no-compile - ruyaml==0.20.0 - - jschon==0.7.3 + - jschon==0.8.3 diff --git a/jschon_sort/_main.py b/jschon_sort/_main.py index 910fd70..d0ed954 100644 --- a/jschon_sort/_main.py +++ b/jschon_sort/_main.py @@ -6,7 +6,7 @@ from typing import Tuple import jschon.jsonschema -from jschon.json import AnyJSONCompatible +from jschon.json import JSONCompatible def _get_sort_keys_for_json_nodes(root_node: jschon.JSON) -> Mapping[jschon.JSONPointer, Tuple[int, ...]]: @@ -35,7 +35,7 @@ def _recurse(node: jschon.JSON, node_sort_key: Tuple[int, ...]) -> None: return mapping -def sort_doc_by_schema(*, doc_data: AnyJSONCompatible, schema_data: AnyJSONCompatible) -> AnyJSONCompatible: +def sort_doc_by_schema(*, doc_data: JSONCompatible, schema_data: Dict[str, JSONCompatible]) -> JSONCompatible: try: root_schema = jschon.JSONSchema(schema_data) except jschon.CatalogError: @@ -62,7 +62,7 @@ def _get_sort_keys_for_schema(schema: jschon.JSONSchema) -> Mapping[jschon.JSONP def _traverse_scope(scope: jschon.jsonschema.Scope) -> None: schema_sort_keys = _get_sort_keys_for_schema(scope.schema) - doc_sort_keys.setdefault(scope.instpath, schema_sort_keys[scope.relpath]) + doc_sort_keys.setdefault(scope.instance.path, schema_sort_keys[scope.relpath]) for child in scope.iter_children(): _traverse_scope(child) @@ -70,15 +70,19 @@ def _traverse_scope(scope: jschon.jsonschema.Scope) -> None: end_sort_key = (math.inf,) - def _sort_json_node(node: AnyJSONCompatible, json_node: jschon.JSON) -> AnyJSONCompatible: - """Traverses the nodes while also keeping at pointer at a high-level JSON object (to get the JSON pointers).""" - if json_node.type == "object": + def _sort_json_node(node: JSONCompatible, json_node: jschon.JSON) -> JSONCompatible: + """ + @param node: the node being traversed (the data) + @param json_node: the node being traversed (jschon's representation) + @return: sorted copy + """ + if isinstance(node, Dict): key_sort_keys: Dict[str, Tuple[Tuple[float, ...], str]] = {} - properties: List[Tuple[str, AnyJSONCompatible]] = [] + properties: List[Tuple[str, JSONCompatible]] = [] k: str - v: AnyJSONCompatible + v: JSONCompatible v_json: jschon.JSON for (k, v), v_json in zip(node.items(), json_node.data.values()): properties.append((k, _sort_json_node(v, v_json))) @@ -97,7 +101,7 @@ def _sort_json_node(node: AnyJSONCompatible, json_node: jschon.JSON) -> AnyJSONC return node_copy - elif json_node.type == "array": + elif isinstance(node, list): return [_sort_json_node(node[idx], v_json) for idx, v_json in enumerate(json_node.data)] return node diff --git a/requirements-dev.txt b/requirements-dev.txt index 2f38736..09852a3 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1,5 +1,5 @@ pytest pytest-cov ruyaml==0.20.0 -jschon==0.7.3 +jschon>=0.8 -e . diff --git a/setup.cfg b/setup.cfg index ee23c99..8c9d9a6 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = jschon-sort -version = 0.0.3 +version = 0.0.4 description = Sorts a JSON or YAML document to match a JSON Schema's order of properties long_description = file: README.md long_description_content_type = text/markdown @@ -13,7 +13,7 @@ classifiers = [options] packages = find: install_requires = - jschon + jschon>=0.8 ruyaml python_requires = >=3.8