2023-03-08 23:39:33 +00:00
|
|
|
try:
|
|
|
|
import xmltodict
|
2020-01-20 15:19:53 +00:00
|
|
|
|
2023-03-08 23:39:33 +00:00
|
|
|
xml_installed = True
|
|
|
|
except ModuleNotFoundError:
|
|
|
|
xml_installed = False
|
|
|
|
|
|
|
|
|
|
|
|
from benedict.extras import require_xml
|
2022-10-14 14:53:06 +00:00
|
|
|
from benedict.serializers.abstract import AbstractSerializer
|
|
|
|
|
2020-01-20 15:19:53 +00:00
|
|
|
|
|
|
|
class XMLSerializer(AbstractSerializer):
|
2022-02-13 10:56:44 +00:00
|
|
|
"""
|
|
|
|
This class describes a xml serializer.
|
|
|
|
"""
|
|
|
|
|
2020-02-20 16:14:48 +00:00
|
|
|
def __init__(self):
|
2022-12-31 17:33:20 +00:00
|
|
|
super().__init__(
|
2022-10-05 14:43:46 +00:00
|
|
|
extensions=[
|
|
|
|
"xml",
|
|
|
|
],
|
|
|
|
)
|
2020-02-20 16:14:48 +00:00
|
|
|
|
|
|
|
def decode(self, s, **kwargs):
|
2023-03-08 23:39:33 +00:00
|
|
|
require_xml(installed=xml_installed)
|
2022-02-13 10:35:43 +00:00
|
|
|
kwargs.setdefault("dict_constructor", dict)
|
2020-01-20 15:19:53 +00:00
|
|
|
data = xmltodict.parse(s, **kwargs)
|
|
|
|
return data
|
|
|
|
|
2020-02-20 16:14:48 +00:00
|
|
|
def encode(self, d, **kwargs):
|
2023-03-08 23:39:33 +00:00
|
|
|
require_xml(installed=xml_installed)
|
2020-01-20 15:19:53 +00:00
|
|
|
data = xmltodict.unparse(d, **kwargs)
|
|
|
|
return data
|