class-transformer icon indicating copy to clipboard operation
class-transformer copied to clipboard

feature: @Alias annotation to accept many alternative property names

Open hsellik opened this issue 2 years ago • 2 comments

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;

}

hsellik avatar May 10 '22 13:05 hsellik

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 avatar Jun 01 '22 18:06 RezaRahmati

@RezaRahmati , if multiple @Expose annotations would be allowed, then it becomes ambigious which name should be used for serialization?

hsellik avatar Jul 29 '22 11:07 hsellik

Duplicate of https://github.com/typestack/class-transformer/issues/121

diffy0712 avatar May 09 '24 21:05 diffy0712