Closed koeppe-at-pdtec closed 6 days ago
@koeppe-at-pdtec Hi,
The problem is that dict
type is dict[Any, Any]
under the hood, which is not supported by the pydantic-xml. Try to define key and value types of the dict like this:
import json
from pydantic import StringConstraints, computed_field
from pydantic_xml import BaseXmlModel
from typing_extensions import Annotated
value = '<Data>{"abc":123}</Data>'
class Data(BaseXmlModel, tag="Data"):
value: Annotated[str, StringConstraints(strip_whitespace=True)]
@computed_field
@property
def value_dict(self) -> dict[str, int]:
return json.loads(self.value)
data = Data.from_xml(value)
print(data.model_dump())
Thanks for quick response. This totally makes sense. Works perfectly.
First of all thanks for providing this piece of software.
I like to compute a field after serialization from other fields:
It fails to use the type dict, but runs at least for the serialisation from xml. Is this intentional or am I on the wrong path?
here is a minimum example: