Closed vkouts closed 6 months ago
@vkouts Hi
UNORDERED mode doesn't keep fields order by design so they are serialized in an arbitrary order.
Starting from version 2.5.0 it is possible to group adjacent elements in tuples. See the documentation.
In your case the model may be described like this:
import pprint
from typing import List, Tuple
from pydantic_xml import RootXmlModel
class Year(RootXmlModel[str], tag='Year'): pass
class DateFrom(RootXmlModel[str], tag='Date_from'): pass
class Periods(RootXmlModel, tag='Periods'):
root: List[Tuple[Year, DateFrom]] = []
xml_text = '''<Periods>
<Year>01.01.2023 0:00:00</Year>
<Date_from>18.02.2023 0:00:00</Date_from>
<Year>01.01.2023 0:00:00</Year>
<Date_from>30.05.2023 0:00:00</Date_from>
<Year>01.01.2023 0:00:00</Year>
<Date_from>06.04.2023 0:00:00</Date_from>
<Year>01.01.2023 0:00:00</Year>
<Date_from>28.02.2023 0:00:00</Date_from>
<Year>01.01.2023 0:00:00</Year>
<Date_from>19.06.2023 0:00:00</Date_from>
<Year>01.01.2023 0:00:00</Year>
<Date_from>07.06.2023 0:00:00</Date_from>
</Periods>'''
data = Periods.from_xml(xml_text)
pprint.pprint(data)
I have xml document:
and following code:
With search mode UNORDERED I getting list with wrong ordering: