Are there any means for JSON serialization/deserialization of Typescript objects so that they don't loose type information? Simple JSON.parse(JSON.stringify)
has too many caveats.
Or I should use adhoc solutions?
In TypeScript, since this is a simple scenario, you can call the JavaScript function JSON. stringify to serialize an object to a JSON string and JSON. parse deserializes the JSON string to an object. Below is an example of a serialized and deserialized Person object using JSON.
obj: any; //new object declaration this. obj = { "col1":{"Attribute1": "value1", "Attribute2": "value2", "Attribute3": "value3"}, "col2":{"Attribute1": "value4", "Attribute2": "value5", "Attribute3": "value6"}, "col3":{"Attribute1": "value7", "Attribute2": "value8", "Attribute3": "value9"} } this. output.
Serialization is the process of saving and loading for state persistence of the diagram.
Serialization is the process of converting an object into a stream of bytes to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed.
Use Interfaces to get strong types:
// Creating var foo:any = {}; foo.x = 3; foo.y='123'; var jsonString = JSON.stringify(foo); alert(jsonString); // Reading interface Bar{ x:number; y?:string; } var baz:Bar = JSON.parse(jsonString); alert(baz.y);
And use type assertion "<>" if you need to.
I think a better way to handle this is to use Object.assign (which however requires ECMAScript 2015).
Given a class
class Pet { name: string; age: number; constructor(name?: string, age?: number) { this.name = name; this.age = age; } getDescription(): string { return "My pet " + this.name + " is " + this.age + " years old."; } static fromJSON(d: Object): Pet { return Object.assign(new Pet(), d); } }
Serialize and deserialize like this...
var p0 = new Pet("Fido", 5); var s = JSON.stringify(p0); var p1 = Pet.fromJSON(JSON.parse(s)); console.log(p1.getDescription());
To take this example to the next level, consider nested objects...
class Type { kind: string; breed: string; constructor(kind?: string, breed?: string) { this.kind = kind; this.breed = breed; } static fromJSON(d: Object) { return Object.assign(new Type(), d); } } class Pet { name: string; age: number; type: Type; constructor(name?: string, age?: number) { this.name = name; this.age = age; } getDescription(): string { return "My pet " + this.name + " is " + this.age + " years old."; } getFullDescription(): string { return "My " + this.type.kind + ", a " + this.type.breed + ", is " + this.age + " years old."; } static fromJSON(d: Object): Pet { var o = Object.assign(new Pet(), d); o.type = Type.fromJSON(o['type']); return o; } }
Serialize and deserialize like this...
var q0 = new Pet("Fido", 5); q0.type = new Type("dog", "Pomeranian"); var t = JSON.stringify(q0); var q1 = Pet.fromJSON(JSON.parse(t)); console.log(q1.getFullDescription());
So unlike using an interface, this approach preserves methods.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With