Possible use of JSON.stringify
See original GitHub issueGood evening!
I was wondering if there’s a good way of doing the serialization while making use of JSON.stringify. The decorator could implement a function toJSON which is called by JSON.stringify by definition (if the function exists).
So it would basically do the following:
toJSON(): string {
const serializer = new TypedJSON(__CLASSNAME__);
return serializer.stringify(this);
}
But decorators are run at declaration time, so obtaining the instance of the class calling the stringifyfunction is not an option.
Any ideas? Extending a class doesn’t seem a good solution…
Issue Analytics
- State:
- Created 5 years ago
- Comments:11
Top Results From Across the Web
JSON.stringify() - JavaScript - MDN Web Docs
The JSON.stringify() method converts a JavaScript value to a JSON string, optionally replacing values if a replacer function is specified or ...
Read more >JSON.stringify() - W3Schools
A common use of JSON is to exchange data to/from a web server. When sending data to a web server, the data has...
Read more >What is use of Json.stringify() and without stringify [duplicate]
The JSON.stringify method basically converts a JavaScript value into a JSON string. Typically used to convert JavaScript arrays or objects to ...
Read more >JSON Stringify Example – How to Parse a JSON Object with JS
The easiest way to get data from an API is with fetch , which includes the .json() method to parse JSON responses into...
Read more >5 places we can use JSON.stringify and JSON.parse
We can convert any kind of data type value into a string by using JSON.stringify . While developing an application using JavaScript, many...
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

This is to support inheritance. You can use the decorator on a super class and it will still work on subclasses; take a look at the test. You sow a grain of doubt in me, but I am pretty sure minifing only changes the name, the object in the prototype stays the same.
Hey Apidcloud, I pushed 1.3.0 with
@toJson. You can try it out.Regarding the second point, I agree, such function would be helpful and would make the encapsulation better, but what about the function name? Currently there is no standard way for that, like for the
stringify—JSON.parseuses a reviver which does not seem to be much better than just usingTypedJSON.parse. And, also this does not give any feedback to typescript. You will get an error whenever you would want to use it. You could write a declaration for that, but it is as much work as writing the function itself. And, it seems it is not possible to do this inside a decorator for the time being: https://github.com/Microsoft/TypeScript/issues/4881