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

Feature: async Deserialize/Serialize #91

Open
Brendenjvv opened this issue Mar 8, 2019 · 8 comments
Open

Feature: async Deserialize/Serialize #91

Brendenjvv opened this issue Mar 8, 2019 · 8 comments
Assignees
Labels
enhancement New feature or request

Comments

@Brendenjvv
Copy link

Hi, will it be possible to add serialize/deserialize methods that return Promises? Our use case here is we are loading types from a server call and need to await the server result before continuing with the deserialize logic.

@andreas-aeschlimann andreas-aeschlimann self-assigned this Mar 8, 2019
@andreas-aeschlimann
Copy link
Member

Can you show me an example how you are working now and how you mean that?

We are coming from Angular/RXJS and we usually work with the pipe operator to achieve what you like to do (at least I think so).

@Brendenjvv
Copy link
Author

Hi, the method we are using to get our plugins is async so we have to use await to get the result in our deserialize method. The issue here is that the JsonCustomConvert interface doesn't provide a promise or promiseLike so we can't await the deserialize on the interface.

See example:

export class PluginConverter2 implements JsonCustomConvert<Base> {
    
    private jsonConvert = new JsonConvert(undefined, undefined, true);

    serialize(plugin: Base) {
        return this.jsonConvert.serialize(plugin);
    }

    async deserialize(plugin: Base) {
        const type = await Helper.getType(plugin.type);
        return this.jsonConvert.deserialize(plugin, type);
    }

}

@andreas-aeschlimann
Copy link
Member

Can't you wait until the plugin is loaded before the deserialization?

What kind of "magic" is happening in Helper.getType? I would just execute this code before doing the deserialization.

@seanbotha123
Copy link

Hi Andreas

What if the plugin can only be loaded when the deserialization happens? i.e. what if the plugin.type dictates that we need to load a plugin from the server? This would mean that we need to get the actual class definition from the server, and need to wait for this to be completed.

So the magic, as you call it, is a plain server call to get the class definition. And this cannot happen beforehand as we need to know the correct type to load it from the server. We have a large amount of types, so preloading all of them at the start is not an option.

@andreas-aeschlimann andreas-aeschlimann added the enhancement New feature or request label Mar 18, 2019
@andreas-aeschlimann
Copy link
Member

I see, thanks for your comments.

I don't know yet if this can be implemented without a breaking change. If not, I would postpone this for v2.0. As soon as I work on the library, I will look into it.

@gsusI
Copy link
Contributor

gsusI commented Jul 10, 2020

@andreas-aeschlimann any update on this?

@gsusI
Copy link
Contributor

gsusI commented Jul 10, 2020

@seanbotha123, did you manage to find a workaround?

I'm facing the same problem myself, and I use this in combination with TypeORM, which makes returning a promise infeasable.

@seanbotha123
Copy link

Hi @gsusI,

Our solution was basically to go fetch the needed types before running the deserialize call and an then getting the types from a helper as it was deserializing.

Hope this helps.

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

4 participants