mcollina / msgpack5

A msgpack v5 implementation for node.js, with extension points / msgpack.org[Node]
MIT License
492 stars 76 forks source link

A little more detail? #44

Closed trusktr closed 8 years ago

trusktr commented 8 years ago

So, can we in theory encode an instance of a class then decode it multiple times in order to create new instances instead of using the new keyword over and over?

class Cat extends Animal { ... }
let geneSample = encode(new Cat())

// make clones
let cat1 = decode(geneSample)
let cat2 = decode(geneSample)

let geneSample2 = encode(new Cat({some: 'characteristic'}))

// make more clones
let cat3 = decode(geneSample2)
let cat4 = decode(geneSample2)

?

I was wondering, because of the example in the README:

console.log(decode(encode(a)) instanceof MyType)

What if an instance to the MyType constructor isn't available in the destination where the encoded value is sent? Does it encode the full prototype chain, so when decoded the new object is the same as one that would have been created with new?

mcollina commented 8 years ago

What if an instance to the MyType constructor isn't available in the destination where the encoded value is sent?

No, a type needs to be registered on both side.

Does it encode the full prototype chain, so when decoded the new object is the same as one that would have been created with new?

No, the prototype chain is not encoded. The object will be the same as if it has been created with the local prototype.

trusktr commented 8 years ago

@mcollina Any examples of those cases?

mcollina commented 8 years ago

https://github.com/mcollina/msgpack5/blob/master/test/ext-custom-encode-check.js

trusktr commented 8 years ago

@mcollina Ah, gotcha. Thanks!