Comments (3)
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())
from mashumaro.
That makes total sense, and the Config overrides should work fine for my use case! Thanks for the prompt response and providing a solution!
from mashumaro.
@ggydush you might be interested in this pull request:
from mashumaro.
Related Issues (20)
- Annotated SerializationStrategy used as a field serialization strategy leads to RecursionError
- Suppress warning about Union type HOT 4
- Take description from docstring HOT 4
- Incorrect type annotation for the result of `to_json` in `DataClassORJSONMixin`
- Different results between standard library json and orjson HOT 3
- Python 3.12.4 Compatibility
- Some tests error out with typing_extensions>=4.12
- More issues on Python 3.12.4 HOT 5
- [BUG] to_msgpack doesn't behave properly with Discriminator(include_subtypes=True) HOT 1
- [BUG] Can't generate expected JSON Schema when using `dict` HOT 6
- Unable to convert msg to msgpack (was originally working on mashumaro 2.9 HOT 1
- strict_map_keys for DataClassMessagePackMixin HOT 1
- unable to serialize torch.tensor as float HOT 1
- `Discriminator` not respected by `JSONEncoder` when it works for `to_json`. HOT 6
- Importing module in a particular order causes a SyntaxError HOT 2
- can not use `datetime` as `datetime` directly when using `from_dict` HOT 2
- Support slices as fields HOT 1
- Tuples are not hashable. HOT 7
- Unable to use PEP 604 annotations HOT 3
- Is it possible to serialise a dataclass-mixin within another dataclass-mixin? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mashumaro.