10
10
Module for deserializing Asset Administration Shell data from the official JSON format
11
11
12
12
The module provides custom JSONDecoder classes :class:`~.AASFromJsonDecoder` and :class:`~.StrictAASFromJsonDecoder` to
13
- be used with the Python standard `json` module.
14
-
15
- Furthermore it provides two classes :class:`~aas.adapter.json.json_deserialization.StrippedAASFromJsonDecoder` and
16
- :class:`~aas.adapter.json.json_deserialization.StrictStrippedAASFromJsonDecoder` for parsing stripped JSON objects,
17
- which are used in the http adapter (see https://git.rwth-aachen.de/acplt/pyi40aas/-/issues/91).
18
- The classes contain a custom :meth:`~aas.adapter.json.json_deserialization.AASFromJsonDecoder.object_hook` function
19
- to detect encoded AAS objects within the JSON data and convert them to BaSyx Python SDK objects while parsing.
20
- Additionally, there's the :meth:`~aas.adapter.json.json_deserialization.read_aas_json_file_into` function, that takes a
21
- complete AAS JSON file, reads its contents and stores the objects in the provided
22
- :class:`~aas.model.provider.AbstractObjectStore`. :meth:`~aas.adapter.json.json_deserialization. read_aas_json_file` is
23
- a wrapper for this function. Instead of storing the objects in a given :class:`~aas.model.provider.AbstractObjectStore`,
24
- it returns a :class:`~aas.model.provider.DictObjectStore` containing parsed objects.
25
-
26
- The deserialization is performed in a bottom-up approach: The `object_hook()` method gets called for every parsed JSON
27
- object (as dict) and checks for existence of the `modelType` attribute. If it is present, the `AAS_CLASS_PARSERS` dict
28
- defines, which of the constructor methods of the class is to be used for converting the dict into an object. Embedded
29
- objects that should have a `modelType` themselves are expected to be converted already. Other embedded objects are
30
- converted using a number of helper constructor methods.
13
+ be used with the Python standard :mod: `json` module.
14
+
15
+ Furthermore it provides two classes :class:`~basyx. aas.adapter.json.json_deserialization.StrippedAASFromJsonDecoder` and
16
+ :class:`~basyx. aas.adapter.json.json_deserialization.StrictStrippedAASFromJsonDecoder` for parsing stripped
17
+ JSON objects, which are used in the http adapter (see https://git.rwth-aachen.de/acplt/pyi40aas/-/issues/91).
18
+ The classes contain a custom :meth:`~basyx. aas.adapter.json.json_deserialization.AASFromJsonDecoder.object_hook`
19
+ function to detect encoded AAS objects within the JSON data and convert them to BaSyx Python SDK objects while parsing.
20
+ Additionally, there's the :meth:`~basyx. aas.adapter.json.json_deserialization.read_aas_json_file_into` function, that
21
+ takes a complete AAS JSON file, reads its contents and stores the objects in the provided
22
+ :class:`~basyx. aas.model.provider.AbstractObjectStore`. :meth:`read_aas_json_file` is a wrapper for this function.
23
+ Instead of storing the objects in a given :class:`~basyx. aas.model.provider.AbstractObjectStore`,
24
+ it returns a :class:`~basyx. aas.model.provider.DictObjectStore` containing parsed objects.
25
+
26
+ The deserialization is performed in a bottom-up approach: The `` object_hook()` ` method gets called for every parsed JSON
27
+ object (as dict) and checks for existence of the `` modelType`` attribute. If it is present, the `` AAS_CLASS_PARSERS``
28
+ dict defines, which of the constructor methods of the class is to be used for converting the dict into an object.
29
+ Embedded objects that should have a `` modelType`` themselves are expected to be converted already.
30
+ Other embedded objects are converted using a number of helper constructor methods.
31
31
"""
32
32
import base64
33
33
import json
@@ -101,7 +101,7 @@ def _expect_type(object_: object, type_: Type, context: str, failsafe: bool) ->
101
101
102
102
class AASFromJsonDecoder (json .JSONDecoder ):
103
103
"""
104
- Custom JSONDecoder class to use the `json` module for deserializing Asset Administration Shell data from the
104
+ Custom JSONDecoder class to use the :mod: `json` module for deserializing Asset Administration Shell data from the
105
105
official JSON format
106
106
107
107
The class contains a custom :meth:`~.AASFromJsonDecoder.object_hook` function to detect encoded AAS objects within
@@ -113,17 +113,17 @@ class AASFromJsonDecoder(json.JSONDecoder):
113
113
114
114
data = json.loads(json_string, cls=AASFromJsonDecoder)
115
115
116
- The `object_hook` function uses a set of `_construct_*()` methods, one for each
116
+ The `` object_hook`` function uses a set of `` _construct_*()` ` methods, one for each
117
117
AAS object type to transform the JSON objects in to BaSyx Python SDK objects. These constructor methods are divided
118
- into two parts: "Helper Constructor Methods", that are used to construct AAS object types without a `modelType`
118
+ into two parts: "Helper Constructor Methods", that are used to construct AAS object types without a `` modelType` `
119
119
attribute as embedded objects within other AAS objects, and "Direct Constructor Methods" for AAS object types *with*
120
- `modelType` attribute. The former are called from other constructor methods or utility methods based on the expected
121
- type of an attribute, the latter are called directly from the `object_hook()` function based on the `modelType`
122
- attribute.
120
+ `` modelType`` attribute. The former are called from other constructor methods or utility methods based on the
121
+ expected type of an attribute, the latter are called directly from the `` object_hook()`` function based on the
122
+ ``modelType`` attribute.
123
123
124
124
This class may be subclassed to override some of the constructor functions, e.g. to construct objects of specialized
125
- subclasses of the BaSyx Python SDK object classes instead of these normal classes from the `model` package. To
126
- simplify this tasks, (nearly) all the constructor methods take a parameter `object_type` defaulting to the normal
125
+ subclasses of the BaSyx Python SDK object classes instead of these normal classes from the `` model` ` package. To
126
+ simplify this tasks, (nearly) all the constructor methods take a parameter `` object_type` ` defaulting to the normal
127
127
BaSyx Python SDK object class, that can be overridden in a derived function:
128
128
129
129
.. code-block:: python
@@ -139,11 +139,11 @@ def _construct_submodel(cls, dct, object_class=EnhancedSubmodel):
139
139
return super()._construct_submodel(dct, object_class=object_class)
140
140
141
141
142
- :cvar failsafe: If `True` (the default), don't raise Exceptions for missing attributes and wrong types, but instead
143
- skip defective objects and use logger to output warnings. Use StrictAASFromJsonDecoder for a
142
+ :cvar failsafe: If `` True`` (the default), don't raise Exceptions for missing attributes and wrong types, but
143
+ instead skip defective objects and use logger to output warnings. Use StrictAASFromJsonDecoder for a
144
144
non-failsafe version.
145
- :cvar stripped: If `True`, the JSON objects will be parsed in a stripped manner, excluding some attributes.
146
- Defaults to `False`.
145
+ :cvar stripped: If `` True` `, the JSON objects will be parsed in a stripped manner, excluding some attributes.
146
+ Defaults to `` False` `.
147
147
See https://git.rwth-aachen.de/acplt/pyi40aas/-/issues/91
148
148
"""
149
149
failsafe = True
@@ -160,10 +160,10 @@ def object_hook(cls, dct: Dict[str, object]) -> object:
160
160
return dct
161
161
162
162
# The following dict specifies a constructor method for all AAS classes that may be identified using the
163
- # `modelType` attribute in their JSON representation. Each of those constructor functions takes the JSON
163
+ # `` modelType` ` attribute in their JSON representation. Each of those constructor functions takes the JSON
164
164
# representation of an object and tries to construct a Python object from it. Embedded objects that have a
165
165
# modelType themselves are expected to be converted to the correct PythonType already. Additionally, each
166
- # function takes a bool parameter `failsafe`, which indicates weather to log errors and skip defective objects
166
+ # function takes a bool parameter `` failsafe` `, which indicates weather to log errors and skip defective objects
167
167
# instead of raising an Exception.
168
168
AAS_CLASS_PARSERS : Dict [str , Callable [[Dict [str , object ]], object ]] = {
169
169
'AssetAdministrationShell' : cls ._construct_asset_administration_shell ,
@@ -281,7 +281,7 @@ def _get_kind(cls, dct: Dict[str, object]) -> model.ModellingKind:
281
281
Utility method to get the kind of an HasKind object from its JSON representation.
282
282
283
283
:param dct: The object's dict representation from JSON
284
- :return: The object's `kind` value
284
+ :return: The object's `` kind` ` value
285
285
"""
286
286
return MODELLING_KIND_INVERSE [_get_ts (dct , "kind" , str )] if 'kind' in dct else model .ModellingKind .INSTANCE
287
287
@@ -366,8 +366,8 @@ def _construct_administrative_information(
366
366
@classmethod
367
367
def _construct_operation_variable (cls , dct : Dict [str , object ]) -> model .SubmodelElement :
368
368
"""
369
- Since we don't implement `OperationVariable`, this constructor discards the wrapping `OperationVariable` object
370
- and just returns the contained :class:`~aas.model.submodel.SubmodelElement`.
369
+ Since we don't implement `` OperationVariable`` , this constructor discards the wrapping `` OperationVariable``
370
+ object and just returns the contained :class:`~basyx. aas.model.submodel.SubmodelElement`.
371
371
"""
372
372
# TODO: remove the following type: ignore comments when mypy supports abstract types for Type[T]
373
373
# see https://github.com/python/mypy/issues/5374
@@ -750,7 +750,7 @@ class StrictAASFromJsonDecoder(AASFromJsonDecoder):
750
750
A strict version of the AASFromJsonDecoder class for deserializing Asset Administration Shell data from the
751
751
official JSON format
752
752
753
- This version has set `failsafe = False`, which will lead to Exceptions raised for every missing attribute or wrong
753
+ This version has set `` failsafe = False` `, which will lead to Exceptions raised for every missing attribute or wrong
754
754
object type.
755
755
"""
756
756
failsafe = False
@@ -776,8 +776,8 @@ def _select_decoder(failsafe: bool, stripped: bool, decoder: Optional[Type[AASFr
776
776
Returns the correct decoder based on the parameters failsafe and stripped. If a decoder class is given, failsafe
777
777
and stripped are ignored.
778
778
779
- :param failsafe: If `True`, a failsafe decoder is selected. Ignored if a decoder class is specified.
780
- :param stripped: If `True`, a decoder for parsing stripped JSON objects is selected. Ignored if a decoder class is
779
+ :param failsafe: If `` True` `, a failsafe decoder is selected. Ignored if a decoder class is specified.
780
+ :param stripped: If `` True` `, a decoder for parsing stripped JSON objects is selected. Ignored if a decoder class is
781
781
specified.
782
782
:param decoder: Is returned, if specified.
783
783
:return: An :class:`~.AASFromJsonDecoder` (sub)class.
@@ -801,20 +801,20 @@ def read_aas_json_file_into(object_store: model.AbstractObjectStore, file: IO, r
801
801
Read an Asset Administration Shell JSON file according to 'Details of the Asset Administration Shell', chapter 5.5
802
802
into a given object store.
803
803
804
- :param object_store: The :class:`ObjectStore <aas.model.provider.AbstractObjectStore>` in which the identifiable
805
- objects should be stored
804
+ :param object_store: The :class:`ObjectStore <basyx. aas.model.provider.AbstractObjectStore>` in which the
805
+ identifiable objects should be stored
806
806
:param file: A file-like object to read the JSON-serialized data from
807
807
:param replace_existing: Whether to replace existing objects with the same identifier in the object store or not
808
808
:param ignore_existing: Whether to ignore existing objects (e.g. log a message) or raise an error.
809
- This parameter is ignored if replace_existing is `True`.
810
- :param failsafe: If `True`, the document is parsed in a failsafe way: Missing attributes and elements are logged
809
+ This parameter is ignored if replace_existing is `` True` `.
810
+ :param failsafe: If `` True` `, the document is parsed in a failsafe way: Missing attributes and elements are logged
811
811
instead of causing exceptions. Defect objects are skipped.
812
812
This parameter is ignored if a decoder class is specified.
813
- :param stripped: If `True`, stripped JSON objects are parsed.
813
+ :param stripped: If `` True` `, stripped JSON objects are parsed.
814
814
See https://git.rwth-aachen.de/acplt/pyi40aas/-/issues/91
815
815
This parameter is ignored if a decoder class is specified.
816
816
:param decoder: The decoder class used to decode the JSON objects
817
- :return: A set of :class:`Identifiers <aas.model.base.Identifier>` that were added to object_store
817
+ :return: A set of :class:`Identifiers <basyx. aas.model.base.Identifier>` that were added to object_store
818
818
"""
819
819
ret : Set [model .Identifier ] = set ()
820
820
decoder_ = _select_decoder (failsafe , stripped , decoder )
@@ -866,13 +866,13 @@ def read_aas_json_file_into(object_store: model.AbstractObjectStore, file: IO, r
866
866
867
867
def read_aas_json_file (file : IO , ** kwargs ) -> model .DictObjectStore [model .Identifiable ]:
868
868
"""
869
- A wrapper of :meth:`~aas.adapter.json.json_deserialization.read_aas_json_file_into`, that reads all objects in an
870
- empty :class:`~aas.model.provider.DictObjectStore`. This function supports the same keyword arguments as
871
- :meth:`~aas.adapter.json.json_deserialization.read_aas_json_file_into`.
869
+ A wrapper of :meth:`~basyx. aas.adapter.json.json_deserialization.read_aas_json_file_into`, that reads all objects
870
+ in an empty :class:`~basyx. aas.model.provider.DictObjectStore`. This function supports the same keyword arguments as
871
+ :meth:`~basyx. aas.adapter.json.json_deserialization.read_aas_json_file_into`.
872
872
873
873
:param file: A filename or file-like object to read the JSON-serialized data from
874
- :param kwargs: Keyword arguments passed to :meth:`~aas.adapter.json.json_deserialization. read_aas_json_file_into`
875
- :return: A :class:`~aas.model.provider.DictObjectStore` containing all AAS objects from the JSON file
874
+ :param kwargs: Keyword arguments passed to :meth:`read_aas_json_file_into`
875
+ :return: A :class:`~basyx. aas.model.provider.DictObjectStore` containing all AAS objects from the JSON file
876
876
"""
877
877
object_store : model .DictObjectStore [model .Identifiable ] = model .DictObjectStore ()
878
878
read_aas_json_file_into (object_store , file , ** kwargs )
0 commit comments