JohnWeisz / TypedJSON

Typed JSON parsing and serializing for TypeScript that preserves type information.
MIT License
603 stars 64 forks source link

Using custom method do serialize one specific property #102

Closed Ivanca closed 5 years ago

Ivanca commented 5 years ago

I'm using a third-party library (konva) that has is own custom method to convert to json called toJSON(), how do I specify such method to be called?

I imagine it should be something like this:

class Foo {
    @toJson({ initializer: Konva.Node.create, serializer: Konva.Sprite.prototype.toJson })
    public sprite: Konva.Sprite
}
Neos3452 commented 5 years ago

Hey Ivanca, you almost got it, you need to use jsonMember decorator and annotate the Foo class with jsonObject. You can take a look at the test in here https://github.com/JohnWeisz/TypedJSON/blob/master/spec/custom-serializer.spec.ts