From 812ebcb249076b2ed672401b5b7bff2a575d88b4 Mon Sep 17 00:00:00 2001 From: zrgt Date: Thu, 24 Sep 2026 16:32:58 +0200 Subject: [PATCH 1/5] adapter.json: Speed up JSON (de-)serialization Profiling `read_aas_json_file()` and `write_aas_json_file()` on real-world AAS JSON files revealed some avoidable overhead: - `write_aas_json_file()` used `json.dump()`, which always falls back to the pure-Python encoder. Use `json.dumps()` instead, which uses the C-accelerated encoder. The downside is that the whole JSON string is held in memory before writing. - `AASToJsonEncoder.default()` and `AASFromJsonDecoder.object_hook()` rebuilt their class mapping for every single object. Cache it once per encoder/decoder class, so subclasses that override `_get_aas_class_serializers()` / `_get_aas_class_parsers()` still work. - `LangStringSet._check_language_tag_constraints()` rebuilt the BCP 47 regex from its parts for every language tag. Compile it once at import time. - `_expect_type()` received `str(ret)` as context, which computes the id_short path of the parent for every child element, although it is only needed if the check fails. Pass the object and let the error message stringify it lazily. - In failsafe mode, `object_hook()` formatted the defective JSON object with `pprint.pformat()` even if the log message was discarded. Only format it, if the logger is enabled for ERROR. Measured on 70 real-world JSON files (54 MB), single process: load 744 ms -> 606 ms (1.23x), save 964 ms -> 314 ms (3.07x). The (order-independent) output and error messages are unchanged for 2000 sampled files. --- .../aas/adapter/json/json_deserialization.py | 61 ++++++++++++------ .../aas/adapter/json/json_serialization.py | 26 ++++++-- sdk/basyx/aas/model/base.py | 62 ++++++++++--------- .../adapter/json/test_json_deserialization.py | 5 ++ .../adapter/json/test_json_serialization.py | 14 +++++ 5 files changed, 115 insertions(+), 53 deletions(-) diff --git a/sdk/basyx/aas/adapter/json/json_deserialization.py b/sdk/basyx/aas/adapter/json/json_deserialization.py index 33fd1a88a..b54d1fd0c 100644 --- a/sdk/basyx/aas/adapter/json/json_deserialization.py +++ b/sdk/basyx/aas/adapter/json/json_deserialization.py @@ -102,7 +102,7 @@ def _get_ts(dct: Dict[str, object], key: str, type_: Type[T]) -> T: return val -def _expect_type(object_: object, type_: Type, context: str, failsafe: bool) -> bool: +def _expect_type(object_: object, type_: Type, context: object, failsafe: bool) -> bool: """ Helper function to check type of an embedded object. @@ -111,13 +111,14 @@ def _expect_type(object_: object, type_: Type, context: str, failsafe: bool) -> and raise a TypeError if not. In failsafe mode, we want to log the error and prevent the object from being added to the parent object. A typical use of this function would look like this: - if _expect_type(element, model.SubmodelElement, str(submodel), failsafe): + if _expect_type(element, model.SubmodelElement, submodel, failsafe): submodel.submodel_element.add(element) :param object_: The object to be type-checked :param type_: The expected type - :param context: A string to add to the exception message / log message, that describes the context in that the - object has been found + :param context: An object (typically the parent object) describing the context in that the object has been + found. It is only converted to a string for the exception message / log message, if the check + fails, since computing the string representation of a Referable is expensive. :param failsafe: Log error and return false instead of raising a TypeError :return: True if the object is of the expected type :raises TypeError: If the object is not of the expected type and the failsafe mode is not active @@ -229,6 +230,21 @@ def _get_aas_class_parsers(cls) -> Dict[str, Callable[[Dict[str, object]], objec } return aas_class_parsers + @classmethod + def _get_cached_aas_class_parsers( + cls, + ) -> Dict[str, Callable[[Dict[str, object]], object]]: + """ + Returns the result of :meth:`_get_aas_class_parsers`, which is computed only once per decoder class, since + :meth:`object_hook` needs it for every single JSON object. + """ + # Look up in cls.__dict__ (not via getattr), so subclasses don't reuse the parsers of their base class + parsers = cls.__dict__.get("_aas_class_parsers_cache") + if parsers is None: + parsers = cls._get_aas_class_parsers() + setattr(cls, "_aas_class_parsers_cache", parsers) + return parsers + @classmethod def object_hook(cls, dct: Dict[str, object]) -> object: # Check if JSON object seems to be a deserializable AAS object (i.e. it has a modelType). Otherwise, the JSON @@ -236,7 +252,7 @@ def object_hook(cls, dct: Dict[str, object]) -> object: if "modelType" not in dct: return dct - AAS_CLASS_PARSERS = cls._get_aas_class_parsers() + AAS_CLASS_PARSERS = cls._get_cached_aas_class_parsers() # Get modelType and constructor function if not isinstance(dct["modelType"], str): @@ -264,24 +280,31 @@ def object_hook(cls, dct: Dict[str, object]) -> object: try: return AAS_CLASS_PARSERS[model_type](dct) except (KeyError, TypeError, model.AASConstraintViolation) as e: - error_message = ( - "Error while trying to convert JSON object into {}: {} >>> {}".format( - model_type, - e, - pprint.pformat(dct, depth=2, width=2**14, compact=True), - ) - ) if cls.failsafe: - logger.error(error_message, exc_info=e) + # Formatting the JSON object is expensive, so we skip it, if the message would be discarded anyway + if logger.isEnabledFor(logging.ERROR): + logger.error( + cls._object_hook_error_message(model_type, e, dct), exc_info=e + ) # In failsafe mode, we return the raw JSON object dict, if there were errors while parsing an object, so # a client application is able to handle this data. The read_json_aas_file() function and all # constructors for complex objects will skip those items by using _expect_type(). return dct else: raise (type(e) if isinstance(e, (KeyError, TypeError)) else TypeError)( - error_message + cls._object_hook_error_message(model_type, e, dct) ) from e + @staticmethod + def _object_hook_error_message( + model_type: str, e: Exception, dct: Dict[str, object] + ) -> str: + return "Error while trying to convert JSON object into {}: {} >>> {}".format( + model_type, + e, + pprint.pformat(dct, depth=2, width=2**14, compact=True), + ) + # ################################################################################################## # Utility Methods used in constructor methods to add general attributes (from abstract base classes) # ################################################################################################## @@ -680,7 +703,7 @@ def _construct_entity( cls._amend_abstract_attributes(ret, dct) if not cls.stripped and "statements" in dct: for element in _get_ts(dct, "statements", list): - if _expect_type(element, model.SubmodelElement, str(ret), cls.failsafe): + if _expect_type(element, model.SubmodelElement, ret, cls.failsafe): ret.statement.add(element) return ret @@ -732,7 +755,7 @@ def _construct_submodel( cls._amend_abstract_attributes(ret, dct) if not cls.stripped and "submodelElements" in dct: for element in _get_ts(dct, "submodelElements", list): - if _expect_type(element, model.SubmodelElement, str(ret), cls.failsafe): + if _expect_type(element, model.SubmodelElement, ret, cls.failsafe): ret.submodel_element.add(element) return ret @@ -842,7 +865,7 @@ def _construct_annotated_relationship_element( cls._amend_abstract_attributes(ret, dct) if not cls.stripped and "annotations" in dct: for element in _get_ts(dct, "annotations", list): - if _expect_type(element, model.DataElement, str(ret), cls.failsafe): + if _expect_type(element, model.DataElement, ret, cls.failsafe): ret.annotation.add(element) return ret @@ -854,7 +877,7 @@ def _construct_submodel_element_collection( cls._amend_abstract_attributes(ret, dct) if not cls.stripped and "value" in dct: for element in _get_ts(dct, "value", list): - if _expect_type(element, model.SubmodelElement, str(ret), cls.failsafe): + if _expect_type(element, model.SubmodelElement, ret, cls.failsafe): ret.value.add(element) return ret @@ -894,7 +917,7 @@ def _construct_submodel_element_list( if not cls.stripped and "value" in dct: for element in _get_ts(dct, "value", list): if _expect_type( - element, type_value_list_element, str(ret), cls.failsafe + element, type_value_list_element, ret, cls.failsafe ): ret.value.add(element) return ret diff --git a/sdk/basyx/aas/adapter/json/json_serialization.py b/sdk/basyx/aas/adapter/json/json_serialization.py index 0817f2bac..714b8d9d0 100644 --- a/sdk/basyx/aas/adapter/json/json_serialization.py +++ b/sdk/basyx/aas/adapter/json/json_serialization.py @@ -112,13 +112,25 @@ def default(self, obj: object) -> object: :param obj: The object to serialize to json :return: The serialized object """ - mapping = self._get_aas_class_serializers() - for typ in mapping: + mapping = self._get_cached_aas_class_serializers() + for typ, mapping_method in mapping.items(): if isinstance(obj, typ): - mapping_method = mapping[typ] return mapping_method(obj) return super().default(obj) + @classmethod + def _get_cached_aas_class_serializers(cls) -> Dict[Type, Callable]: + """ + Returns the result of :meth:`_get_aas_class_serializers`, which is computed only once per encoder class, since + :meth:`default` needs it for every single object. + """ + # Look up in cls.__dict__ (not via getattr), so subclasses don't reuse the serializers of their base class + mapping = cls.__dict__.get("_aas_class_serializers_cache") + if mapping is None: + mapping = cls._get_aas_class_serializers() + setattr(cls, "_aas_class_serializers_cache", mapping) + return mapping + @classmethod def _abstract_classes_to_json(cls, obj: object) -> Dict[str, object]: """ @@ -857,11 +869,11 @@ def write_aas_json_file( See https://git.rwth-aachen.de/acplt/pyi40aas/-/issues/91 This parameter is ignored if an encoder class is specified. :param encoder: The encoder class used to encode the JSON objects - :param kwargs: Additional keyword arguments to be passed to `json.dump()` + :param kwargs: Additional keyword arguments to be passed to `json.dumps()` """ encoder_ = _select_encoder(stripped, encoder) - # json.dump() only accepts TextIO + # we write str, so we need TextIO cm: ContextManager[TextIO] if isinstance(file, get_args(_generic.Path)): # 'file' is a path, needs to be opened first @@ -876,5 +888,7 @@ def write_aas_json_file( cm = contextlib.nullcontext(file) # type: ignore[arg-type] # serialize object to json + # We use json.dumps() instead of json.dump(), since the latter always uses the (much slower) pure-Python encoder, + # while json.dumps() uses the C-accelerated one. The downside is that the whole JSON string is held in memory. with cm as fp: - json.dump(_create_dict(data), fp, cls=encoder_, **kwargs) + fp.write(json.dumps(_create_dict(data), cls=encoder_, **kwargs)) diff --git a/sdk/basyx/aas/model/base.py b/sdk/basyx/aas/model/base.py index 84650fe17..95eede657 100644 --- a/sdk/basyx/aas/model/base.py +++ b/sdk/basyx/aas/model/base.py @@ -304,6 +304,39 @@ class StateOfEvent(Enum): OFF = 1 +def _compile_language_tag_re() -> re.Pattern[str]: + alphanum = "[a-zA-Z0-9]" + singleton = "[0-9A-WY-Za-wy-z]" + extension = f"{singleton}(-({alphanum}){{2,8}})+" + extlang = "[a-zA-Z]{3}(-[a-zA-Z]{3}){0,2}" + irregular = ( + "(en-GB-oed|i-ami|i-bnn|i-default|i-enochian|i-hak|" + "i-klingon|i-lux|i-mingo|i-navajo|i-pwn|i-tao|i-tay|" + "i-tsu|sgn-BE-FR|sgn-BE-NL|sgn-CH-DE)" + ) + regular = ( + "(art-lojban|cel-gaulish|no-bok|no-nyn|zh-guoyu|zh-hakka|" + "zh-min|zh-min-nan|zh-xiang)" + ) + grandfathered = f"({irregular}|{regular})" + language = f"([a-zA-Z]{{2,3}}(-{extlang})?|[a-zA-Z]{{4}}|[a-zA-Z]{{5,8}})" + script = "[a-zA-Z]{4}" + region = "([a-zA-Z]{2}|[0-9]{3})" + variant = f"(({alphanum}){{5,8}}|[0-9]({alphanum}){{3}})" + privateuse = f"[xX](-({alphanum}){{1,8}})+" + langtag = ( + f"{language}(-{script})?(-{region})?(-{variant})*(-{extension})*(-" + f"{privateuse})?" + ) + language_tag = f"({langtag}|{privateuse}|{grandfathered})" + + return re.compile(f"^{language_tag}$") + + +# Compiled once at import time, since language tags are checked for every LangStringSet entry +_LANGUAGE_TAG_RE = _compile_language_tag_re() + + class LangStringSet(MutableMapping[str, str]): """ A mapping of language code to string. Must be non-empty. @@ -329,34 +362,7 @@ def __init__(self, dict_: Dict[str, str]): @classmethod def _check_language_tag_constraints(cls, ltag: str): - alphanum = "[a-zA-Z0-9]" - singleton = "[0-9A-WY-Za-wy-z]" - extension = f"{singleton}(-({alphanum}){{2,8}})+" - extlang = "[a-zA-Z]{3}(-[a-zA-Z]{3}){0,2}" - irregular = ( - "(en-GB-oed|i-ami|i-bnn|i-default|i-enochian|i-hak|" - "i-klingon|i-lux|i-mingo|i-navajo|i-pwn|i-tao|i-tay|" - "i-tsu|sgn-BE-FR|sgn-BE-NL|sgn-CH-DE)" - ) - regular = ( - "(art-lojban|cel-gaulish|no-bok|no-nyn|zh-guoyu|zh-hakka|" - "zh-min|zh-min-nan|zh-xiang)" - ) - grandfathered = f"({irregular}|{regular})" - language = f"([a-zA-Z]{{2,3}}(-{extlang})?|[a-zA-Z]{{4}}|[a-zA-Z]{{5,8}})" - script = "[a-zA-Z]{4}" - region = "([a-zA-Z]{2}|[0-9]{3})" - variant = f"(({alphanum}){{5,8}}|[0-9]({alphanum}){{3}})" - privateuse = f"[xX](-({alphanum}){{1,8}})+" - langtag = ( - f"{language}(-{script})?(-{region})?(-{variant})*(-{extension})*(-" - f"{privateuse})?" - ) - language_tag = f"({langtag}|{privateuse}|{grandfathered})" - - pattern = f"^{language_tag}$" - - if re.match(pattern, ltag) is None: + if _LANGUAGE_TAG_RE.match(ltag) is None: raise ValueError( f"The language tag must follow the format defined in BCP 47. " f"Given language tag: {ltag}" diff --git a/sdk/test/adapter/json/test_json_deserialization.py b/sdk/test/adapter/json/test_json_deserialization.py index 3255e9d36..0c5f28106 100644 --- a/sdk/test/adapter/json/test_json_deserialization.py +++ b/sdk/test/adapter/json/test_json_deserialization.py @@ -256,6 +256,11 @@ def _construct_submodel(cls, dct, object_class=EnhancedSubmodel): "id": "https://example.org/Test_Submodel" } ]""" + # Decode with the base class first, so its cached constructor mapping exists. The derived class must not + # reuse it. + parsed_data = json.loads(data, cls=StrictAASFromJsonDecoder) + self.assertNotIsInstance(parsed_data[0], EnhancedSubmodel) + parsed_data = json.loads(data, cls=EnhancedAASDecoder) self.assertEqual(1, len(parsed_data)) self.assertIsInstance(parsed_data[0], EnhancedSubmodel) diff --git a/sdk/test/adapter/json/test_json_serialization.py b/sdk/test/adapter/json/test_json_serialization.py index e53cf6041..164892c0f 100644 --- a/sdk/test/adapter/json/test_json_serialization.py +++ b/sdk/test/adapter/json/test_json_serialization.py @@ -42,6 +42,20 @@ def test_serialize_object(self) -> None: ) json.dumps(test_object, cls=AASToJsonEncoder) + def test_serializer_overriding(self) -> None: + class EnhancedAASToJsonEncoder(AASToJsonEncoder): + @classmethod + def _get_aas_class_serializers(cls): + serializers = super()._get_aas_class_serializers() + serializers[model.Property] = lambda obj: {"enhanced": True} + return serializers + + test_object = model.Property("test_id_short", model.datatypes.String) + # Serialize with the base class first, so its cached serializer mapping exists. The derived class must not + # reuse it. + self.assertNotIn("enhanced", json.loads(json.dumps(test_object, cls=AASToJsonEncoder))) + self.assertEqual({"enhanced": True}, json.loads(json.dumps(test_object, cls=EnhancedAASToJsonEncoder))) + def test_random_object_serialization(self) -> None: aas_identifier = "AAS1" submodel_key = (model.Key(model.KeyTypes.SUBMODEL, "SM1"),) From 485805bbb6a0217c871ee720a566e6a001e9706d Mon Sep 17 00:00:00 2001 From: zrgt Date: Fri, 25 Sep 2026 09:38:25 +0200 Subject: [PATCH 2/5] model.base: Move language tag regex into `LangStringSet` --- sdk/basyx/aas/model/base.py | 67 ++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 34 deletions(-) diff --git a/sdk/basyx/aas/model/base.py b/sdk/basyx/aas/model/base.py index 95eede657..aa6e00d92 100644 --- a/sdk/basyx/aas/model/base.py +++ b/sdk/basyx/aas/model/base.py @@ -304,39 +304,6 @@ class StateOfEvent(Enum): OFF = 1 -def _compile_language_tag_re() -> re.Pattern[str]: - alphanum = "[a-zA-Z0-9]" - singleton = "[0-9A-WY-Za-wy-z]" - extension = f"{singleton}(-({alphanum}){{2,8}})+" - extlang = "[a-zA-Z]{3}(-[a-zA-Z]{3}){0,2}" - irregular = ( - "(en-GB-oed|i-ami|i-bnn|i-default|i-enochian|i-hak|" - "i-klingon|i-lux|i-mingo|i-navajo|i-pwn|i-tao|i-tay|" - "i-tsu|sgn-BE-FR|sgn-BE-NL|sgn-CH-DE)" - ) - regular = ( - "(art-lojban|cel-gaulish|no-bok|no-nyn|zh-guoyu|zh-hakka|" - "zh-min|zh-min-nan|zh-xiang)" - ) - grandfathered = f"({irregular}|{regular})" - language = f"([a-zA-Z]{{2,3}}(-{extlang})?|[a-zA-Z]{{4}}|[a-zA-Z]{{5,8}})" - script = "[a-zA-Z]{4}" - region = "([a-zA-Z]{2}|[0-9]{3})" - variant = f"(({alphanum}){{5,8}}|[0-9]({alphanum}){{3}})" - privateuse = f"[xX](-({alphanum}){{1,8}})+" - langtag = ( - f"{language}(-{script})?(-{region})?(-{variant})*(-{extension})*(-" - f"{privateuse})?" - ) - language_tag = f"({langtag}|{privateuse}|{grandfathered})" - - return re.compile(f"^{language_tag}$") - - -# Compiled once at import time, since language tags are checked for every LangStringSet entry -_LANGUAGE_TAG_RE = _compile_language_tag_re() - - class LangStringSet(MutableMapping[str, str]): """ A mapping of language code to string. Must be non-empty. @@ -348,6 +315,38 @@ class LangStringSet(MutableMapping[str, str]): ISO 3166 and ISO 15924. """ + @staticmethod + def _compile_language_tag_re() -> re.Pattern[str]: + alphanum = "[a-zA-Z0-9]" + singleton = "[0-9A-WY-Za-wy-z]" + extension = f"{singleton}(-({alphanum}){{2,8}})+" + extlang = "[a-zA-Z]{3}(-[a-zA-Z]{3}){0,2}" + irregular = ( + "(en-GB-oed|i-ami|i-bnn|i-default|i-enochian|i-hak|" + "i-klingon|i-lux|i-mingo|i-navajo|i-pwn|i-tao|i-tay|" + "i-tsu|sgn-BE-FR|sgn-BE-NL|sgn-CH-DE)" + ) + regular = ( + "(art-lojban|cel-gaulish|no-bok|no-nyn|zh-guoyu|zh-hakka|" + "zh-min|zh-min-nan|zh-xiang)" + ) + grandfathered = f"({irregular}|{regular})" + language = f"([a-zA-Z]{{2,3}}(-{extlang})?|[a-zA-Z]{{4}}|[a-zA-Z]{{5,8}})" + script = "[a-zA-Z]{4}" + region = "([a-zA-Z]{2}|[0-9]{3})" + variant = f"(({alphanum}){{5,8}}|[0-9]({alphanum}){{3}})" + privateuse = f"[xX](-({alphanum}){{1,8}})+" + langtag = ( + f"{language}(-{script})?(-{region})?(-{variant})*(-{extension})*(-" + f"{privateuse})?" + ) + language_tag = f"({langtag}|{privateuse}|{grandfathered})" + + return re.compile(f"^{language_tag}$") + + # Compiled once when the class is created, since language tags are checked for every LangStringSet entry + _LANGUAGE_TAG_RE = _compile_language_tag_re() + def __init__(self, dict_: Dict[str, str]): self._dict: Dict[str, str] = {} if not isinstance(dict_, dict): @@ -362,7 +361,7 @@ def __init__(self, dict_: Dict[str, str]): @classmethod def _check_language_tag_constraints(cls, ltag: str): - if _LANGUAGE_TAG_RE.match(ltag) is None: + if cls._LANGUAGE_TAG_RE.match(ltag) is None: raise ValueError( f"The language tag must follow the format defined in BCP 47. " f"Given language tag: {ltag}" From 2abd39c32d067ea8665a514e65d4c162ca5bdee3 Mon Sep 17 00:00:00 2001 From: zrgt Date: Fri, 25 Sep 2026 09:42:03 +0200 Subject: [PATCH 3/5] model.base: Keep language tag regex compatible with Python < 3.10 Calling a `staticmethod` object in the class body only works since Python 3.10, so we call its underlying function via `__func__` instead. Also, quote the `re.Pattern[str]` return annotation, which can't be evaluated at runtime before Python 3.9. --- sdk/basyx/aas/model/base.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/sdk/basyx/aas/model/base.py b/sdk/basyx/aas/model/base.py index aa6e00d92..8f76959a9 100644 --- a/sdk/basyx/aas/model/base.py +++ b/sdk/basyx/aas/model/base.py @@ -316,7 +316,7 @@ class LangStringSet(MutableMapping[str, str]): """ @staticmethod - def _compile_language_tag_re() -> re.Pattern[str]: + def _compile_language_tag_re() -> "re.Pattern[str]": alphanum = "[a-zA-Z0-9]" singleton = "[0-9A-WY-Za-wy-z]" extension = f"{singleton}(-({alphanum}){{2,8}})+" @@ -344,8 +344,9 @@ def _compile_language_tag_re() -> re.Pattern[str]: return re.compile(f"^{language_tag}$") - # Compiled once when the class is created, since language tags are checked for every LangStringSet entry - _LANGUAGE_TAG_RE = _compile_language_tag_re() + # Compiled once when the class is created, since language tags are checked for every LangStringSet entry. + # Calling the staticmethod object directly in the class body only works since Python 3.10, hence ``__func__``. + _LANGUAGE_TAG_RE = _compile_language_tag_re.__func__() # type: ignore[attr-defined] def __init__(self, dict_: Dict[str, str]): self._dict: Dict[str, str] = {} From b10c0750543e35e2a70542b026813a5b2aa95ffe Mon Sep 17 00:00:00 2001 From: zrgt Date: Tue, 17 Nov 2026 10:46:42 +0100 Subject: [PATCH 4/5] model.base: Compile the language tag regex at module level The package requires Python 3.11, so the `__func__` workaround for Python < 3.10 in the class body is obsolete. --- sdk/basyx/aas/model/base.py | 69 +++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 34 deletions(-) diff --git a/sdk/basyx/aas/model/base.py b/sdk/basyx/aas/model/base.py index 8f76959a9..19a8e59a9 100644 --- a/sdk/basyx/aas/model/base.py +++ b/sdk/basyx/aas/model/base.py @@ -18,6 +18,7 @@ Any, Callable, Dict, + Final, Generic, Iterable, Iterator, @@ -304,6 +305,39 @@ class StateOfEvent(Enum): OFF = 1 +def _compile_language_tag_re() -> re.Pattern[str]: + alphanum = "[a-zA-Z0-9]" + singleton = "[0-9A-WY-Za-wy-z]" + extension = f"{singleton}(-({alphanum}){{2,8}})+" + extlang = "[a-zA-Z]{3}(-[a-zA-Z]{3}){0,2}" + irregular = ( + "(en-GB-oed|i-ami|i-bnn|i-default|i-enochian|i-hak|" + "i-klingon|i-lux|i-mingo|i-navajo|i-pwn|i-tao|i-tay|" + "i-tsu|sgn-BE-FR|sgn-BE-NL|sgn-CH-DE)" + ) + regular = ( + "(art-lojban|cel-gaulish|no-bok|no-nyn|zh-guoyu|zh-hakka|" + "zh-min|zh-min-nan|zh-xiang)" + ) + grandfathered = f"({irregular}|{regular})" + language = f"([a-zA-Z]{{2,3}}(-{extlang})?|[a-zA-Z]{{4}}|[a-zA-Z]{{5,8}})" + script = "[a-zA-Z]{4}" + region = "([a-zA-Z]{2}|[0-9]{3})" + variant = f"(({alphanum}){{5,8}}|[0-9]({alphanum}){{3}})" + privateuse = f"[xX](-({alphanum}){{1,8}})+" + langtag = ( + f"{language}(-{script})?(-{region})?(-{variant})*(-{extension})*(-" + f"{privateuse})?" + ) + language_tag = f"({langtag}|{privateuse}|{grandfathered})" + + return re.compile(f"^{language_tag}$") + + +# Compiled once at import, since language tags are checked for every LangStringSet entry. +_LANGUAGE_TAG_RE: Final[re.Pattern[str]] = _compile_language_tag_re() + + class LangStringSet(MutableMapping[str, str]): """ A mapping of language code to string. Must be non-empty. @@ -315,39 +349,6 @@ class LangStringSet(MutableMapping[str, str]): ISO 3166 and ISO 15924. """ - @staticmethod - def _compile_language_tag_re() -> "re.Pattern[str]": - alphanum = "[a-zA-Z0-9]" - singleton = "[0-9A-WY-Za-wy-z]" - extension = f"{singleton}(-({alphanum}){{2,8}})+" - extlang = "[a-zA-Z]{3}(-[a-zA-Z]{3}){0,2}" - irregular = ( - "(en-GB-oed|i-ami|i-bnn|i-default|i-enochian|i-hak|" - "i-klingon|i-lux|i-mingo|i-navajo|i-pwn|i-tao|i-tay|" - "i-tsu|sgn-BE-FR|sgn-BE-NL|sgn-CH-DE)" - ) - regular = ( - "(art-lojban|cel-gaulish|no-bok|no-nyn|zh-guoyu|zh-hakka|" - "zh-min|zh-min-nan|zh-xiang)" - ) - grandfathered = f"({irregular}|{regular})" - language = f"([a-zA-Z]{{2,3}}(-{extlang})?|[a-zA-Z]{{4}}|[a-zA-Z]{{5,8}})" - script = "[a-zA-Z]{4}" - region = "([a-zA-Z]{2}|[0-9]{3})" - variant = f"(({alphanum}){{5,8}}|[0-9]({alphanum}){{3}})" - privateuse = f"[xX](-({alphanum}){{1,8}})+" - langtag = ( - f"{language}(-{script})?(-{region})?(-{variant})*(-{extension})*(-" - f"{privateuse})?" - ) - language_tag = f"({langtag}|{privateuse}|{grandfathered})" - - return re.compile(f"^{language_tag}$") - - # Compiled once when the class is created, since language tags are checked for every LangStringSet entry. - # Calling the staticmethod object directly in the class body only works since Python 3.10, hence ``__func__``. - _LANGUAGE_TAG_RE = _compile_language_tag_re.__func__() # type: ignore[attr-defined] - def __init__(self, dict_: Dict[str, str]): self._dict: Dict[str, str] = {} if not isinstance(dict_, dict): @@ -362,7 +363,7 @@ def __init__(self, dict_: Dict[str, str]): @classmethod def _check_language_tag_constraints(cls, ltag: str): - if cls._LANGUAGE_TAG_RE.match(ltag) is None: + if _LANGUAGE_TAG_RE.match(ltag) is None: raise ValueError( f"The language tag must follow the format defined in BCP 47. " f"Given language tag: {ltag}" From 068829bf4d8650570a4b67ca95502d1cb5a85e3e Mon Sep 17 00:00:00 2001 From: zrgt Date: Tue, 17 Nov 2026 15:01:01 +0100 Subject: [PATCH 5/5] adapter.json: Inline the cached mapping lookups Each helper had a single caller and is a few lines long. --- .../aas/adapter/json/json_deserialization.py | 22 +++++-------------- .../aas/adapter/json/json_serialization.py | 21 ++++++------------ 2 files changed, 13 insertions(+), 30 deletions(-) diff --git a/sdk/basyx/aas/adapter/json/json_deserialization.py b/sdk/basyx/aas/adapter/json/json_deserialization.py index b54d1fd0c..036ed316e 100644 --- a/sdk/basyx/aas/adapter/json/json_deserialization.py +++ b/sdk/basyx/aas/adapter/json/json_deserialization.py @@ -230,21 +230,6 @@ def _get_aas_class_parsers(cls) -> Dict[str, Callable[[Dict[str, object]], objec } return aas_class_parsers - @classmethod - def _get_cached_aas_class_parsers( - cls, - ) -> Dict[str, Callable[[Dict[str, object]], object]]: - """ - Returns the result of :meth:`_get_aas_class_parsers`, which is computed only once per decoder class, since - :meth:`object_hook` needs it for every single JSON object. - """ - # Look up in cls.__dict__ (not via getattr), so subclasses don't reuse the parsers of their base class - parsers = cls.__dict__.get("_aas_class_parsers_cache") - if parsers is None: - parsers = cls._get_aas_class_parsers() - setattr(cls, "_aas_class_parsers_cache", parsers) - return parsers - @classmethod def object_hook(cls, dct: Dict[str, object]) -> object: # Check if JSON object seems to be a deserializable AAS object (i.e. it has a modelType). Otherwise, the JSON @@ -252,7 +237,12 @@ def object_hook(cls, dct: Dict[str, object]) -> object: if "modelType" not in dct: return dct - AAS_CLASS_PARSERS = cls._get_cached_aas_class_parsers() + # Computed once per decoder class, since it is needed for every single JSON object. Look up in __dict__ (not + # via getattr), so subclasses don't reuse the parsers of their base class + AAS_CLASS_PARSERS = cls.__dict__.get("_aas_class_parsers_cache") + if AAS_CLASS_PARSERS is None: + AAS_CLASS_PARSERS = cls._get_aas_class_parsers() + setattr(cls, "_aas_class_parsers_cache", AAS_CLASS_PARSERS) # Get modelType and constructor function if not isinstance(dct["modelType"], str): diff --git a/sdk/basyx/aas/adapter/json/json_serialization.py b/sdk/basyx/aas/adapter/json/json_serialization.py index a068c51bf..cb357ef0d 100644 --- a/sdk/basyx/aas/adapter/json/json_serialization.py +++ b/sdk/basyx/aas/adapter/json/json_serialization.py @@ -112,24 +112,17 @@ def default(self, obj: object) -> object: :param obj: The object to serialize to json :return: The serialized object """ - mapping = self._get_cached_aas_class_serializers() - for typ, mapping_method in mapping.items(): - if isinstance(obj, typ): - return mapping_method(obj) - return super().default(obj) - - @classmethod - def _get_cached_aas_class_serializers(cls) -> Dict[Type, Callable]: - """ - Returns the result of :meth:`_get_aas_class_serializers`, which is computed only once per encoder class, since - :meth:`default` needs it for every single object. - """ - # Look up in cls.__dict__ (not via getattr), so subclasses don't reuse the serializers of their base class + # Computed once per encoder class, since it is needed for every single object. Look up in __dict__ (not via + # getattr), so subclasses don't reuse the serializers of their base class + cls = type(self) mapping = cls.__dict__.get("_aas_class_serializers_cache") if mapping is None: mapping = cls._get_aas_class_serializers() setattr(cls, "_aas_class_serializers_cache", mapping) - return mapping + for typ, mapping_method in mapping.items(): + if isinstance(obj, typ): + return mapping_method(obj) + return super().default(obj) @classmethod def _abstract_classes_to_json(cls, obj: object) -> Dict[str, object]: