konradhalas / dacite

Simple creation of data classes from dictionaries.
MIT License
1.76k stars 106 forks source link

Is there a functionality like this in dacite? #221

Open isaackogan opened 1 year ago

isaackogan commented 1 year ago

Hi, I'm used to using a serialization library like Gson for java. I have some data with very abstract and confusing naming schemes and I want to map it to my own custom serialized names.

E.g. the dict might have the data 'cheeseburger_123_ab': 'foobar', whereas I would like my dataclass to have the field name 'cheeseburger' without all the excess.

Looks like marshmallow has this capability: https://user-images.githubusercontent.com/65869106/220189507-543466eb-bcc9-4051-9ef7-247d9dd8d01b.png

isaackogan commented 1 year ago

Looks like author does not want this in their library https://github.com/konradhalas/dacite/issues/194 (there are a few of those such issues) which is unfortunate because it's a major drawback.

I get the scope argument, but honestly, the whole point of dacite is to serialize, and when I'm getting data from scraping with camelCase, snake_case, and NeitherCase I want to be able to serialize to a standard, instead of having dataclass definitions like this, which are absurd:

@dataclass
class User:
      name: str
      birthDate: str
      country_of_origin: str

Author's suggestion is to pre-process the data before putting it into dacite, but that defeats the purpose of using the library. If I'm pre-processing half of the fields for every class, I may as well not use dacite.

Similar library really hits the nail on the coffin. I've moved over to this one instead. For others with the same issue, I recommend looking into it: https://github.com/Fatal1ty/mashumaro#alias-option

That library took about 10m to convert my types to, but worth it.