GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — support for type information using typescript decorators ? · Issue #91 · msgpack/msgpack-javascript

support for type information using typescript decorators ? #91

New issue
Copy link
New issue
Copy link
Open
Open
support for type information using typescript decorators ?#91
Copy link
Labels
Feature

Description

@prunelsigma
prunelsigma
opened on Nov 12, 2019
Issue body actions

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
    gfx
    on Nov 19, 2019
    Member
    More actions

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

  2. gfx
    added
    Feature
    on Nov 19, 2019
  3. rmja commented on Feb 19, 2021

    @rmja
    rmja
    on Feb 19, 2021
    More actions

    @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"]])))
    
  4. camnewnham commented on Mar 2, 2022

    @camnewnham
    camnewnham
    on Mar 2, 2022
    More actions

    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

    Feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.