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

support for type information using typescript decorators ? #91

Open
prunelsigma opened this issue Nov 12, 2019 · 3 comments
Open

support for type information using typescript decorators ? #91

prunelsigma opened this issue Nov 12, 2019 · 3 comments
Labels

Comments

@prunelsigma
Copy link

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.

@gfx
Copy link
Member

gfx commented Nov 19, 2019

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

@rmja
Copy link

rmja commented Feb 19, 2021

@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"]])))

@camnewnham
Copy link

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
Labels
Projects
None yet
Development

No branches or pull requests

4 participants