Skip to content

Support type parameterized Deserialize function  #9

@1ambda

Description

@1ambda

I am writing Dserializable abstract class which indicate it can be deserialized and has deserialize Function. It looks like

const cerialize = require('cerialize'); // import

abstract class Deserializable {
  public static deserialize<T> (json, constructor): T {
    return cerialize.Deserialize(json, constructor);
  }
}

// usage
class Command extends Deserializable {
  @deserialize public help: boolean;
  @deserializeAs(CommandOrder, "_order") public orders: Array<CommandOrder>;
  @deserializeAs("_args") public args: Array<String>;
}

let cmd = Command.deserialize<Command>(json, Command);

But since cerialize.Deserialize requires the constructor function as the second arg, It's somewhat verbose.

If we have a type parameterized cerialize.Deserialize function, we can make more simplified Deserialzable. For example,

export abstract class Deserializable {
  public static deserialize<T> (json): T {
    return Deserialize<T>(json);
  }
}

// more simple
Command.deserialize<Command>(json);

Thanks. :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions