3 ms·
No it does not. It returns a ISO string. You don't lose any info. And you can parse it back with JSON.parse if you give it a reviver function. [1] Do you want
by simlevesque 8y ago
No it does not. It returns a ISO string. You don't lose any info. And you can parse it back with JSON.parse if you give it a reviver function. [1]
Do you want JSON.stringify to keep dates intact ? The whole point is to turn it to a string.
[1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#Parameters https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- Prefinem 8y agoIf you are writing a reviver, what difference is it to instead write a deep clone function? JSON.parse(JSON.stringify(obj)) !== deepClone(obj) EDIT: Also, how can you determine if the string should be a date, or a string? Sure, if it fits, you can always convert it to a Date, but if your first object is coming from something that sends ISO Dates as strings and you used JSON.parse with a reviver, you would get a different object.
- kbenson 8y ago"It works if you provide your own additional code that loops over and fixes known problems" is not the same as "it works". It's useful, but at most it saves you a few lines of boilerplate code (and perhaps a few negligible cycles) to write your own recurser to do the same thing immediately after the transform. > Do you want JSON.stringify to keep dates intact ? The whole point is to turn it to a string. There are ways to serialize data structures to strings. JSON doesn't generally do that, and it's been to its benefit as it helps keep it generic and easily usable from many languages. That said, there are serialization tools which can correctly serialize and unserialize more complex structures, given the correct circumstances (only core constructs, or the objects in question have serialization helpers, or they are ensured to not have features that might cause problems such as references to outside data). The point of this discussion is Javascript object cloning, not turning objects into a string. Offering up JSON and then responding to a criticism of it based on its methods as "the whole point is to turn it into a string" is somewhat ridiculous given many of the other options you're espousing JSON over don't use strings at all.
- freeopinion 8y agoThe whole point is to clone an object. You lost track of the whole point.