dataclasses-json icon indicating copy to clipboard operation
dataclasses-json copied to clipboard

Ignore custom name mappings on serialization

Open tsanton opened this issue 2 years ago • 2 comments

Hi @lidatong , and many thanks for this wonderful library!

I'm wondering if there's a implementation of "ignore_custom_names_on_serialization" that I might have missed? The case if as follows: I have an API that returns horrible local language variable names as such:

[{"kod": "01","namn": "xyz},{"kod": "03","namn": "abc}]

I've defined my dataclass along these lines:

@dataclass_json
@dataclass
class Demo:
    code:str = field(metadata=config(field_name="kod", decoder=default))
    name:str = field(metadata=config(field_name="namn"))

And this loads perfectly (the classes are populated):

data = Demo.schema().loads(*json_string*, many=True)

Now I'd like to persist my beautifully formatted data, and the first step would be to dump my dataclasses to a list of dicts:

output = Demo.schema().dump(data , many=True)

And here the problem arise -> my data goes back to[{"kod": "01","namn": "xyz},{"kod": "03","namn": "abc}]

Hoping there's a fix for this (maybe you could mention it in the docs if so?), but if not: any suggestions where I should start in terms of creating a PR?

/Tobias

tsanton avatar Apr 20 '22 19:04 tsanton

Hi again @lidatong. Please see #356

Adding "ignore_custom_naming" to handle the above mentioned case

.to_dict(ignore_custom_naming=False)
.to_json(ignore_custom_naming=False)
.schema().dump(ignore_custom_naming=False)
.schema().dumps(ignore_custom_naming=False)

tsanton avatar Apr 21 '22 05:04 tsanton

I think the library behaviour is correct in this case, otherwise you break ser-deser consistency. I think in your case, just remapping values to another class should do the trick. Let me know if you want a concrete code example

george-zubrienko avatar Jul 01 '23 13:07 george-zubrienko