GillianPerard/typescript-json-serializer

[BUG]: serialize returns an object

Slion opened this issue · 7 comments

Slion commented

Version

4.0.1

Description

Not so much a bug but more a usage question I guess.
The serialize method returns an object when I would have expected it to return a JSON string much like JSON.stringify does.
I'm therefore not sure how to use that library. Should I just JSON.stringify the object provided by JsonSerializer.serialize?

Error

No response

Reproduction

Just use JsonSerializer.serialize.

On which OS the bug appears?

Windows 10

What is your project type?

Node.js

On which build mode the bug appears?

No response

Anything else?

Overwolf app

Hi, it's a good question and you're right, the result is not stringified when serialize.

It would be a feature you want or just the fact to know the current correct way to use the lib is to stringify on your side is acceptable?

Slion commented

I'm really confused then. The point of a JSON serializer is to turn an object into a string right?
I guess what I would need is a complete example on how to use that lib.

Object [serialize> String [deserialize> Object

Slion commented

So I did stringify the output of serialize, even though I don't quite get what serialize does then, then I deserialize it but when trying to call methods of objects contained in an array inside the root object it get that TypeError is not a function exception. So it looks like those object are not properly instantiated…

All in all I'm feeling stupid cause it looks like I can't get more out of that library than JSON.stringify and JSON.parse… 😁

Slion commented

So I did stringify the output of serialize, even though I don't quite get what serialize does then, then I deserialize it but when trying to call methods of objects contained in an array inside the root object it get that TypeError is not a function exception. So it looks like those object are not properly instantiated…

All in all I'm feeling stupid cause it looks like I can't get more out of that library than JSON.stringify and JSON.parse… 😁

Ok looks like for array of objects you need to specify the type as shown in @JsonProperty({ type: Zoo }) zoos: Array<Zoo>;.

Slion commented

Still I don't understand what serialise does if it does not produce the json string.

If you read the entire documentation (README), you can see the deserialize method allows users to modify your json object on the fly to fit your need, for example cast a string into Date object or bind a json property to the property of your class that does not have the same name and so on.

So the serialize method transforms back your class object into an anonyme object you could call a dictionnary of unknown/any values.

Slion commented

Thanks, for the help. Great library!