Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wider config scopes #139

Open
lidatong opened this issue Sep 7, 2019 · 3 comments
Open

wider config scopes #139

lidatong opened this issue Sep 7, 2019 · 3 comments
Assignees
Labels
enhancement New feature or request

Comments

@lidatong
Copy link
Owner

lidatong commented Sep 7, 2019

Sharing encoder / decoder in wider scopes (currently must be per-field. type, class, global are all potential scopes)

@lidatong lidatong self-assigned this Sep 7, 2019
@lidatong lidatong added the enhancement New feature or request label Sep 7, 2019
@lidatong lidatong changed the title class level config wider config scopes Feb 22, 2020
@MichaelAzimov
Copy link

Is this issue up-for-grabs? I would love to tackle this, because it would really help my use case

@lidatong
Copy link
Owner Author

lidatong commented Jun 6, 2020

@MichaelAzimov sorry for not getting back to you on this. There currently is undocumented global config functionality (mainly because it's pretty experimental). For example:

from datetime import date
import dataclasses_json.cfg

dataclasses_json.cfg.global_config.encoders[date] = date.isoformat
dataclasses_json.cfg.global_config.decoders[date] = date.fromisoformat

This was referenced Jun 6, 2020
@powellnorma
Copy link

powellnorma commented Apr 21, 2023

Note:
In case you have an optional field like day: date|None, you have to add:

dataclasses_json.cfg.global_config.encoders[date|None] = lambda x: x and date.isoformat(x)

otherwise you still get the TypeError: Object of type date is not JSON serializable exception

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants