diff --git a/sdk/basyx/aas/adapter/json/json_deserialization.py b/sdk/basyx/aas/adapter/json/json_deserialization.py index 33fd1a88..036ed316 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 @@ -236,7 +237,12 @@ def object_hook(cls, dct: Dict[str, object]) -> object: if "modelType" not in dct: return dct - AAS_CLASS_PARSERS = cls._get_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): @@ -264,24 +270,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 +693,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 +745,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 +855,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 +867,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 +907,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 155aced5..cb357ef0 100644 --- a/sdk/basyx/aas/adapter/json/json_serialization.py +++ b/sdk/basyx/aas/adapter/json/json_serialization.py @@ -112,10 +112,15 @@ 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: + # 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) + for typ, mapping_method in mapping.items(): if isinstance(obj, typ): - mapping_method = mapping[typ] return mapping_method(obj) return super().default(obj) @@ -857,11 +862,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 +881,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 7ee70155..bcaeff04 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. @@ -329,34 +363,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 3255e9d3..0c5f2810 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 b3d689d1..a34f6980 100644 --- a/sdk/test/adapter/json/test_json_serialization.py +++ b/sdk/test/adapter/json/test_json_serialization.py @@ -44,6 +44,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"),)