Skip to content

Commit

Permalink
fix(jsonld renderer): stable ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
aaxelb committed Dec 6, 2024
1 parent cdcadf2 commit a81abf7
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions trove/render/jsonld.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,9 @@ def _list_or_single_value(self, predicate_iri: str, objectlist: list):
return None
else:
return _only_obj
if predicate_iri in _PREDICATES_OF_FLEXIBLE_CARDINALITY:
return (
objectlist
if len(objectlist) != 1
else objectlist[0]
)
return objectlist
if predicate_iri in _PREDICATES_OF_FLEXIBLE_CARDINALITY and len(objectlist) == 1:
return objectlist[0]
return sorted(objectlist, key=_naive_sort_key)

@contextlib.contextmanager
def __visiting(self, iri: str):
Expand All @@ -165,3 +161,8 @@ def __visiting(self, iri: str):

def __already_visiting(self, iri: str) -> bool:
return bool(self.__visiting_iris and (iri in self.__visiting_iris))


def _naive_sort_key(jsonable_obj):
_json = json.dumps(jsonable_obj)
return (len(_json), _json)

0 comments on commit a81abf7

Please sign in to comment.