Skip to content

support for type information using typescript decorators ? #91

Open

Description

@prunelsigma

Hi, Thank for your work on this library.
I have one question:
Do you have some plans to add features to serialize/deserialize using specific types? like the type reflection in C# using the typescript decorators and reflect-metadata api.
https://www.typescriptlang.org/docs/handbook/decorators.html.
The goal is to be able to encode/decode some class in a custom way like with IMessagePackFormatter<T> in https://github.com/neuecc/MessagePack-CSharp.

Activity

  1. gfx commented on Nov 19, 2019

    @gfx
    Member

    Thanks. This seems an interesting idea, but decorators are not standardized yet, so I won't implement them for the time being.

  2. rmja commented on Feb 19, 2021

    @rmja

    @prunelsigma I have made a very basic version of this over here: https://github.com/utiliread/ur-msgpack
    Feel free to use any part of it.

    See the tests for example use: https://github.com/utiliread/ur-msgpack/blob/master/src/deserialize.spec.ts

    For example:

    class ModelWithArrayKey {
        @msgpackKey(0)
        number!: number;
        @msgpackKey(1)
        string!: string;
        @msgpackKey(2)
        numberArray!: number[];
        @msgpackKey(3)
        stringArray!: string[];
    }
    
    const result = deserialize(ModelWithArrayKey, decode(encode([1337, "hello", [1, 2], ["a", "b"]])))
    
  3. camnewnham commented on Mar 2, 2022

    @camnewnham

    I've created a similar library which supports unions (similar to MessagePack C#), over at https://github.com/camnewnham/msgpack-decorators

    Example definition:

    abstract class Animal {
      @key(0)
      name: string;
      @key(1)
      legs: number;
    }
    
    @union(0, Animal)
    class Lizard extends Animal{
      @key(3)
      scales: number
    }
    
    @union(1, Animal)
    class Bird extends Animal {
      @key(4)
      feathers: number
    }
    

    Example usage:

    const animal: Animal = new Lizard();
    animal.legs = 2;
    
    const encoded:Uint8Array = encode(animal, Animal);
    const decoded:Animal = decode(encoded, Animal);
    

    This is a peer to @msgpack/msgpack - in future it would be more efficient to fork and integrate directly, and even better would be codegen like protobufjs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions