Comments (5)
As of Python 3.11, datetime.fromisoformat
can handle all the important bits of the ISO 8601 spec.
I'll be backporting its code in movermeyer/backports.datetime_fromisoformat#21, so it'll be available for earlier versions of Python 3.
FWIW, ciso8601
is still faster than any other parser.
from mashumaro.
Yes, there is such a problem. Native fromisoformat
doesn't cover all the cases but it's the fastest way to parse most ISO 8601 datetime strings: closeio/ciso8601#66 (comment). But you're right it's the mashumaro's scope to handle generic ISO-8601 strings, so I decided to add a way to choose the parser using field's metadata:
class DataClass(DataClassDictMixin):
x: datetime = field(metadata={"deserialize": "ciso8601"})
See more examples here: https://github.com/Fatal1ty/mashumaro/tree/datetime-parsers#using-field-metadata
Moreover, I think it's an obvious way to make mashumaro more extensible and I'm going to use it for other things like #11. Also it would be convenient to configure a default behaviour for all fields in one place like Meta class in Django models.
from mashumaro.
We can change deserialization method with version 1.18: https://github.com/Fatal1ty/mashumaro#using-field-metadata
from mashumaro.
To fully cover the original problem, here is an example with dateutil
:
from datetime import datetime
from dataclasses import dataclass, field
from mashumaro import DataClassDictMixin
import dateutil.parser
@dataclass
class A(DataClassDictMixin):
x: datetime = field(metadata={"deserialize": dateutil.parser.isoparse})
print(A.from_dict({"x": "2019-01-02T07:00:00Z"}))
from mashumaro.
Perfect. I'll test it out soon, thank you!
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.