TypeScript: Serialization of property of type any causing problems
See original GitHub issueMy swagger.json contains a type with a property of type object.
...
"properties": {
"value": { "type": "object" },
...
} }
NSwag generates Typescript like
value?: any | undefined;
and a toJSON method like
toJSON(data?: any) {
data = typeof data === 'object' ? data : {};
if (this.value) {
data["value"] = {};
for (let key in this.value) {
if (this.value.hasOwnProperty(key))
data["value"][key] = this.value[key];
}
}
...
return data;
}
But this is causing troubles when serializing the object. In case, the property contains a string, it is serialized to an array of characters. And a boolean for example is serialized to an empty property!
Objects:
{value: "Test"}
{value: true}
Serialized to
value = {{ "0": "T", "1": "e", "2": "s", "3": "t" }}
value = {{}}
Not sure why the complex serialization logic is generated here.
A simple
data["value"] = this.value;
would solve everything in my case.
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (5 by maintainers)
Top Results From Across the Web
One naïve man's struggle with TypeScript class serialization
The crux of the problem is that when you round-trip serialize/deserialize a TypeScript class to JSON, what you get back is not an...
Read more >Understanding TypeScript object serialization
Learn about object serialization in TypeScript, the way systems communicate seamlessly, and the issues with serialization in TypeScript.
Read more >Serializing an object in typescript with methods and a ...
I have a fairly substantial project in typescript that uses a lot of object-oriented programming, resulting in a nontrivial object graph ( ...
Read more >4.8.2: Type serialization error with unique symbol #50720
Bug Report Original issue: stitchesjs/stitches#1078 When two types refer to the same unique symbol and they are combined via inference, ...
Read more >Simple way to serialize objects to JSON in TypeScript
We add a static unserialize(serialized: string) method to recreate the User instance. JSON.parse has any as return type, which results in no ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Fixed: https://github.com/RSuter/NSwag/commit/9e3828c67cb98c2ad28e52d531e5af9d76b2cb79
Ref: https://github.com/RSuter/NJsonSchema/issues/697