Closed ggydush closed 6 months ago
Default converters for int
and float
types on serialization are int
and float
functions respectively. It has its roots in how Union
types are currently processed. Imagine that you have Union[int, <another_dataclass>]
. If int
values were passing as is, then we would be passing <another_dataclass>
values as is because "passing through" strategy appears first. I have plans to rework Union
types and get rid of explicit converters for int
, float
types but before that you can use pass_through
helper:
from dataclasses import dataclass
from mashumaro import pass_through
from mashumaro.config import BaseConfig
from mashumaro.mixins.json import DataClassJSONMixin
@dataclass
class Example(DataClassJSONMixin):
val1: float | int
val2: int | float
class Config(BaseConfig):
serialization_strategy = {
int: {"serialize": pass_through},
float: {"serialize": pass_through},
}
print(Example(val1=3, val2=3.5).to_dict())
That makes total sense, and the Config overrides should work fine for my use case! Thanks for the prompt response and providing a solution!
@ggydush you might be interested in this pull request:
Description
Using a dataclass with Union comprised of int/float causes the value to be cast to whichever appears first in the Union definition. Looking at the pack_union method, the output makes sense, but wondering if special cases should be added to preserve int/float types.
What I Did
Output: