json2typescript icon indicating copy to clipboard operation
json2typescript copied to clipboard

Feature: async Deserialize/Serialize

Open Brendenjvv opened this issue 6 years ago • 8 comments

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.

Brendenjvv avatar Mar 08 '19 05:03 Brendenjvv

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).

andreas-aeschlimann avatar Mar 08 '19 13:03 andreas-aeschlimann

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);
    }

}

Brendenjvv avatar Mar 08 '19 14:03 Brendenjvv

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.

andreas-aeschlimann avatar Mar 08 '19 15:03 andreas-aeschlimann

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.

seanbotha123 avatar Mar 11 '19 07:03 seanbotha123

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.

andreas-aeschlimann avatar Mar 18 '19 15:03 andreas-aeschlimann

@andreas-aeschlimann any update on this?

gsusI avatar Jul 10 '20 16:07 gsusI

@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.

gsusI avatar Jul 10 '20 16:07 gsusI

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.

seanbotha123 avatar Jul 14 '20 05:07 seanbotha123