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

Custom Extension Types (Codecs) for TypeScript class #59

Open
yohei1126 opened this issue Feb 2, 2017 · 0 comments
Open

Custom Extension Types (Codecs) for TypeScript class #59

yohei1126 opened this issue Feb 2, 2017 · 0 comments

Comments

@yohei1126
Copy link

I want to write Custom Extension Types (Codecs) for TypeScript class but I got the following error.
Please let me know how I can fix this issue. Thanks!

my target TypeScript code

import * as msgpack from "msgpack-lite";

var codec = msgpack.createCodec();
codec.addExtPacker(0x3F, MyVector, myVectorPacker);
codec.addExtUnpacker(0x3F, myVectorUnpacker);

var data = new MyVector(1, 2);
var encoded = msgpack.encode(data, {codec: codec});
var decoded = msgpack.decode(encoded, {codec: codec});

console.log("encoded = " + JSON.stringify(encoded));
console.log("decoded = " + JSON.stringify(decoded));

class MyVector {
  x: number;
  y: number;
  constructor (x: number, y: number) {
    this.x = x;
    this.y = y;
  }
}

// return Buffer serialized
function myVectorPacker(vector: MyVector) {
  var array = [vector.x, vector.y];
  return msgpack.encode(array);
}

// return Object deserialized
function myVectorUnpacker(buffer: Buffer) {
  var array = msgpack.decode(buffer);
  return new MyVector(array[0], array[1]);
}

compiled JS code

"use strict";
var msgpack = require("msgpack-lite");
var codec = msgpack.createCodec();
codec.addExtPacker(0x3F, MyVector, myVectorPacker);
codec.addExtUnpacker(0x3F, myVectorUnpacker);
var data = new MyVector(1, 2);
var encoded = msgpack.encode(data, { codec: codec });
var decoded = msgpack.decode(encoded, { codec: codec });
console.log("encoded = " + JSON.stringify(encoded));
console.log("decoded = " + JSON.stringify(decoded));
var MyVector = (function () {
    function MyVector(x, y) {
        this.x = x;
        this.y = y;
    }
    return MyVector;
}());
// return Buffer serialized
function myVectorPacker(vector) {
    var array = [vector.x, vector.y];
    return msgpack.encode(array);
}
// return Object deserialized
function myVectorUnpacker(buffer) {
    var array = msgpack.decode(buffer);
    return new MyVector(array[0], array[1]);
}

run time error

D:\work\msgpack-lite-cutome-extention-types>node .
D:\work\msgpack-lite-cutome-extention-types\node_modules\msgpack-lite\lib\write-core.js:40
  var name = Class.name;
                  ^

TypeError: Cannot read property 'name' of undefined
    at Codec.addExtPacker (D:\work\msgpack-lite-cutome-extention-types\node_modules\msgpack-lite\lib\write-core.js:40:19)
    at Object.<anonymous> (D:\work\msgpack-lite-cutome-extention-types\dist\index.js:4:7)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:394:7)
    at startup (bootstrap_node.js:149:9)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant