Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialize JSON to enum. #67

Open
sagarnayak opened this issue May 23, 2018 · 7 comments
Open

Serialize JSON to enum. #67

sagarnayak opened this issue May 23, 2018 · 7 comments
Assignees
Labels
enhancement New feature or request

Comments

@sagarnayak
Copy link

I would like to convert a string to an enum type. lets say i have a enum.

enum Test{
YES="1",
NO="0"
}

and when i get {"value":"1"} from server i want to have YES in my typescript class not 1. is it possible?

@andreas-aeschlimann
Copy link
Member

Yes, of course.

You need to check out the example of „custom converters“ in the ReadMe. If you follow this example, you should have no problems implementing your request.

@sagarnayak
Copy link
Author

I was also thinking of that approach. but if you have worked with java you might know that we can just add the @SerializedName attribute to the enum constants. and it will auto parse from json according to its type and value.
I was expecting something like that.
anyways thanks for the help.

@andreas-aeschlimann
Copy link
Member

json2typescript is as small as it can be. Therefore, no "magic" is happening under the hood. It could be possible that we support such a deserialization by default at some point, but for now you may easily implement it for yourself.

@andreas-aeschlimann andreas-aeschlimann self-assigned this Aug 10, 2018
@andreas-aeschlimann andreas-aeschlimann added the enhancement New feature or request label Aug 10, 2018
@andreas-aeschlimann andreas-aeschlimann added this to the version 2.x milestone Feb 27, 2019
@andreas-aeschlimann andreas-aeschlimann removed this from the version 2.x milestone May 28, 2020
@Almar
Copy link

Almar commented Apr 23, 2021

Hi all,
I wrote the following enum converter base class to help me out.

  export class EnumConverter<T> implements JsonCustomConvert<T> {
    validValues: string[];

    constructor(private enumType: unknown, private enumName: string) {
      this.validValues = Object.values(Object.getOwnPropertyDescriptors(enumType)).map(
        (value) => value.value
      );
    }

    deserialize(value: string): T {
      if (!this.validValues.includes(value)) {
        throw new Error(
          `JsonConvert error; invalid value for enum ${this.enumName}, expected one of '${this.validValues}', found '${value}'`
        );
      }
      return (value as unknown) as T;
    }

    serialize(data: T): any {
      return data;
    }
  }

Usage:

enum MyEnum {
  RED = "red",
  WHITE = "white",
  BLUE = "blue",
}

@JsonConverter
class MyEnumConverter extends EnumConverter<MyEnum> {
  constructor() {
    super(MyEnum, "MyEnum");
  }
}

@andreas-aeschlimann
Copy link
Member

Thank you @Almar, very cool. Do you like to make a PR or shall I implement it occasionally?

@Almar
Copy link

Almar commented May 5, 2021

Hey @andreas-aeschlimann,
It would be cool if I can contribute via a PR. I'll give it a try ;-)

@andreas-aeschlimann
Copy link
Member

Nice! Please try to add some tests as well for enums.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants