Skip to content

Serializers

The RESQML standard is formulated as XML objects. We have used xsdata to generate Python dataclasses to avoid working directly with XML. When passing the data to RDDMS, or other RESQML readers, we have to serialize the RESQML dataclasses into XML. This is done via the functions listed below.

RESQML v2.0.1 serialization

resqml_objects.serializers.serialize_resqml_v201_object

serialize_resqml_v201_object(
    obj: AbstractObject | DerivedElement[AbstractObject],
) -> bytes
Source code in src/resqml_objects/serializers.py
def serialize_resqml_v201_object(
    obj: ro_201.AbstractObject | DerivedElement[ro_201.AbstractObject],
) -> bytes:
    serializer = XmlSerializer(config=SerializerConfig())

    namespace = getattr(obj.Meta, "namespace", None) or obj.Meta.target_namespace
    name = obj.__class__.__name__

    # This is a solution to enforce the inclusion of the `xsi:type`-attribute
    # on the generated XML-elements.
    if not isinstance(obj, DerivedElement) and name.startswith("obj_"):
        obj = DerivedElement(
            qname=f"{{{namespace}}}{name[4:]}",
            value=obj,
            type=f"{{{namespace}}}{name}",
        )

    return str.encode(
        serializer.render(
            obj,
            ns_map={
                "eml": "http://www.energistics.org/energyml/data/commonv2",
                "resqml2": "http://www.energistics.org/energyml/data/resqmlv2",
            },
        )
    )