ramonhagenaars / jsons

🐍 A Python lib for (de)serializing Python objects to/from JSON
https://jsons.readthedocs.io
MIT License
289 stars 41 forks source link

Add abitlity to deserialize properties with custom name without having change the entity #105

Closed sunshinecool closed 4 years ago

sunshinecool commented 4 years ago

Use case: entity definition

@dataclass
class Car
    theName: str

external resource (json)

{
    'name': 'chevy'
}

I want to have the ability to define the json property name instead of changing my entity definition, in this case from theName to name. Is this possible today somehow?

I think this is possible by adding custom annotations like @JsonProperty etc. something like this

@dataclass
class Car
    @propertyname('name')
    theName: str
ramonhagenaars commented 4 years ago

Hi @sunshinecool ,

There is actually a feature that allows you to do this. Check out key_transformer in the api. It is a keyword argument that takes a function that transforms a key name.