question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeScript: Serialization of property of type any causing problems

See original GitHub issue

My 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:closed
  • Created 5 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found