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

Consider adding generic type as parameter for custom converters #102

Open
ErlendEllingsen opened this issue Jun 3, 2019 · 4 comments
Open
Assignees
Labels
enhancement New feature or request

Comments

@ErlendEllingsen
Copy link

ErlendEllingsen commented Jun 3, 2019

This is a proposal, and may not be the correct way of doing stuff. My goal is to implement a generic custom converter for string based enums, and I think this would allow that.

Whilst trying to write a generic custom converter for string based Enum types, I find that the current design of JsonCustomConvert will not allow such behavior (to my knowledge).

I was thinking about implementing a design such as:

import {JsonConverter, JsonCustomConvert} from "json2typescript";

@JsonConverter
export class EnumConverter<T extends any> implements JsonCustomConvert<T> {
    serialize(value: T): string {
        return value.toString();
    }
    deserialize<T extends any>(data: string, t: T): T {
        return t[data] as T;
    }
}

The current JsonCustomConvert-interface only includes data as a parameter. What I am proposing is adding a second parameter, type, which passes the generic type on.

Interface today:

deserialize(data: any): T;

my proposal:

deserialize(data: any, type: T): T;

This will allow us to access the types value in the converter, allowing for a generic custom enum converter.

Perhaps I am doing something wrong? :)

@andreas-aeschlimann
Copy link
Member

I am not sure if it is possible, but you are welcome to make a pull request! I do not know at the moment when I will be investing time in development again, but I will keep this issue open for this purpose.

@andreas-aeschlimann andreas-aeschlimann added the enhancement New feature or request label Jun 26, 2019
@ErlendEllingsen
Copy link
Author

@andreas-aeschlimann I will see what I can do

@andreas-aeschlimann andreas-aeschlimann self-assigned this Sep 23, 2019
@eduard2014
Copy link

Have you found any solution for this proposal?

@andreas-aeschlimann
Copy link
Member

Not yet, unfortunately.

Can it be done, considering that generics are gone in the JS compilation?

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