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.

Possible use of JSON.stringify

See original GitHub issue

Good 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:closed
  • Created 5 years ago
  • Comments:11

github_iconTop GitHub Comments

1reaction
Neos3452commented, Mar 15, 2019

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.

1reaction
Neos3452commented, Mar 14, 2019

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 stringifyJSON.parse uses a reviver which does not seem to be much better than just using TypedJSON.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

Read more comments on GitHub >

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

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