python-benedict/benedict/serializers/xml.py

35 lines
779 B
Python
Raw Normal View History

try:
import xmltodict
xml_installed = True
except ModuleNotFoundError:
xml_installed = False
from benedict.extras import require_xml
from benedict.serializers.abstract import AbstractSerializer
class XMLSerializer(AbstractSerializer):
2022-02-13 10:56:44 +00:00
"""
This class describes a xml serializer.
"""
def __init__(self):
super().__init__(
extensions=[
"xml",
],
)
def decode(self, s, **kwargs):
require_xml(installed=xml_installed)
2022-02-13 10:35:43 +00:00
kwargs.setdefault("dict_constructor", dict)
data = xmltodict.parse(s, **kwargs)
return data
def encode(self, d, **kwargs):
require_xml(installed=xml_installed)
data = xmltodict.unparse(d, **kwargs)
return data