You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to get mashumaro to correctly handle a union type that can be either int or float. But when I call to_json() the value is automatically cast to an integer
What I Did
from mashumaro.mixins.json import DataClassJSONMixin
from dataclasses import dataclass, field
import json
@dataclass
class Foo(DataClassJSONMixin):
x: int | float
_x: str = field(init=False, repr=False)
@property
def x(self) -> int | float:
return json.loads(self._x)
@x.setter
def x(self, x: int | float) -> None:
self._x = json.dumps(x)
test1=Foo(7.8)
print(test1.to_json()) # prints '{"x": 7, "_x": "7.8"}' instead of '{"x": 7.8, "_x": "7.8"}'
The text was updated successfully, but these errors were encountered:
gvashishtha
changed the title
Union type of [int | float] not serialized/deserialized correctly
Union type of [int | float] not serialized correctly
Apr 16, 2024
Description
I am trying to get mashumaro to correctly handle a union type that can be either
int
orfloat
. But when I callto_json()
the value is automatically cast to an integerWhat I Did
The text was updated successfully, but these errors were encountered: