class-transformer
class-transformer copied to clipboard
feature: @Alias annotation to accept many alternative property names
Description
This library seems pretty neat, but for me, it is missing a critical feature. Whenever I try to accept JSON with a property which can have multiple names, I am unable to process it since @Expose
annotation only allows to specify one name.
Proposed solution
An @Alias
annotation similar to @JsonAlias
used in Jackson for Java. It can be used to define one or more alternative names for a property which should be mapped during deserialization.
This annotation has no effect during serialization where actual property name is always used. In class-transformer's case, the actual property name is the default property name or the one defined with @Expose
.
Example:
export class Employee {
@Alias({"firstName"})
name: string;
@Alias({"department", "employeeDept" })
dept: string;
}
Also it would have been great if we could use multiple Expose on one property,
export class Employee {
@Expose({name: "firstName"})
name: string;
@Expose({name: "department" })
@Expose({name: "employeeDept" })
dept: string;
}
@RezaRahmati , if multiple @Expose
annotations would be allowed, then it becomes ambigious which name should be used for serialization?
Duplicate of https://github.com/typestack/class-transformer/issues/121