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

Change case (de)serialization when using DataClassJsonMixin ? #272

Closed
eLvErDe opened this issue Dec 13, 2020 · 4 comments
Closed

Change case (de)serialization when using DataClassJsonMixin ? #272

eLvErDe opened this issue Dec 13, 2020 · 4 comments

Comments

@eLvErDe
Copy link

eLvErDe commented Dec 13, 2020

Hello,

I tried to switch from decorator to mixin because Mypy struggles to understand what methods are available but I cannot figure out how to set case handling.
I tried the following:

class DataClassJsonMixinCamelCase(dataclasses_json.DataClassJsonMixin):
    """
    Inherit from DataClassJsonMixin and configure camelCase serialization/deserialization
    """

    dataclass_json_config = dataclasses_json.cfg.config(letter_case=dataclasses_json.LetterCase.CAMEL)

I also tried to overwrite dataclasses_json.config from file scope with no luck.

How can I do that ?

Thanks.

@AbdulRahmanAlHamali
Copy link

AbdulRahmanAlHamali commented Dec 23, 2020

Hi @eLvErDe , can you try this:

dataclass_json_config = dataclasses_json.cfg.config(letter_case=dataclasses_json.LetterCase.CAMEL)['dataclasses_json']

I'm not sure if it'll work, but I stole it from here:
https://github.com/lidatong/dataclasses-json/blob/master/dataclasses_json/api.py#L140

@gubser
Copy link

gubser commented Dec 28, 2020

@AbdulRahmanAlHamali thank you for the hint. I put it into a new class for easier reuse:

from dataclasses_json import DataClassJsonMixin
import dataclasses_json

class DataClassJsonCamelMixIn(DataClassJsonMixin):
    dataclass_json_config = dataclasses_json.config( # type: ignore
        letter_case=dataclasses_json.LetterCase.CAMEL, # type: ignore
        undefined=dataclasses_json.Undefined.EXCLUDE
    )['dataclasses_json']

@jmhodges-color
Copy link

Would love to hear of an API for this! We're using the mixin for the type checking (which is great!) and this particular construction feels a lil funny to add.

@george-zubrienko
Copy link
Collaborator

#442 v1 API should make things easier here

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

No branches or pull requests

5 participants