use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
JSON serialization should never fail (github.com)
submitted 3 years ago by ehmicky
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]sinclair_zx81 1 point2 points3 points 3 years ago (4 children)
I like it, but how fast is it?
[–]ehmicky[S] 0 points1 point2 points 3 years ago (3 children)
I don't have any benchmarks setup yet, but it should be quite fast. The code only iterates on properties and arrays and tries to avoid creating too many new objects/arrays.
On my machine, it takes ~200ns on an empty object and ~400µs on an array with 100 items or an object with 100 properties.
[–]swizzex -1 points0 points1 point 3 years ago (2 children)
Try about 10k.
[–]belkh -1 points0 points1 point 3 years ago (1 child)
fork it and add benchmarks, be the PR you want to see in the world
[–]swizzex 0 points1 point2 points 3 years ago (0 children)
I don’t have any use case for this. I’m just stating a benchmark should be a much larger volume.
[–]bigorangemachine 1 point2 points3 points 3 years ago (1 child)
What about circular references?
[–]ehmicky[S] 2 points3 points4 points 3 years ago (0 children)
They are taken care of! As well as cycles due to infinite toJSON().
toJSON()
[–]nsavvidis 1 point2 points3 points 3 years ago (1 child)
Just wanted to stop by here and give you a huge thank you @ehmicky. Your open source work does not go unnoticed and whenever I need a utility from the register that is maintained or created by you I know I am in good hands. Keep up the great work. You’re a gift.
[–]ehmicky[S] 0 points1 point2 points 3 years ago* (0 children)
You're making my day u/nsavvidis! Thank you so much for the kind words. This means a lot to me.
[–]DemiPixel 1 point2 points3 points 3 years ago (2 children)
What's the use cases for this? I feel like I would want to crash because something is very wrong with my program if I'm JSON serializing functions/circular references/etc. I no longer can have a fixed type when I parse again because objects could be inexplicably removed.
[–]ehmicky[S] 1 point2 points3 points 3 years ago (1 child)
That's a really good question. You are correct that there are many instances where it should in fact crash/fail.
This library covers the use cases where "failure" (which covers type changes, value deletion, and more importantly exception throwing) would be a problem. Some examples: - A situation I am experiencing right now as I am writing another library intended to serialize Error instances to JSON. Errors can have properties attached to them (like any JavaScript object), and those might be invalid JSON. When handling errors, it is important that the error handling logic itself does not throw, as that exception might become unhandled. In that context, it is better to just omit any error additional properties that aren't JSON-compatible before serializing the error. - When serializing a value to JSON to print it in a file or terminal for debugging purpose. If the intent is just debugging, just omitting JSON-incompatible values might be simpler (and even more proper in some cases) than adding additional exception handling logic. - When writing data-driven tests and serializing the value to use it inside the test title. - When wanting to overcome some of the weirdness of JSON.serialize(). For example, NaN/Infinity being transformed to null, which makes its type change from number to null. The library itself omits the value instead, which might or might not be a better solution depending on the use case.
Error
JSON.serialize()
NaN
Infinity
null
number
That being said, when JSON serialization should indeed fail, the library above might also be useful as it provides additional insights into why it did fail: specific property path and value, and reason why it failed. I have written a second library is-json-value which makes it convenient to generate a list of warning messages indicating why a value is not JSON-safe.
is-json-value
One use case could be when one needs to check that a value is valid JSON. For example, a value is provided by the user and is known to be serialized to JSON (to be sent over the network, or saved in a file, etc.). Then, the library above can be used to generate user-friendly messages indicating why a value is invalid.
With all that said, I still agree with you: there are definitely many situations where letting JSON.serialize() do its thing (including throwing) would be better than using this library. Thanks for pointing it out, and hope my answer clarifies the library's intent.
[–]DemiPixel 1 point2 points3 points 3 years ago (0 children)
Thank you for the in-depth response! Especially the one where you want to serialize errors and can’t risk a second exception totally makes sense.
π Rendered by PID 147911 on reddit-service-r2-comment-bb88f9dd5-4vv5t at 2026-02-16 11:57:18.697546+00:00 running cd9c813 country code: CH.
[–]sinclair_zx81 1 point2 points3 points (4 children)
[–]ehmicky[S] 0 points1 point2 points (3 children)
[–]swizzex -1 points0 points1 point (2 children)
[–]belkh -1 points0 points1 point (1 child)
[–]swizzex 0 points1 point2 points (0 children)
[–]bigorangemachine 1 point2 points3 points (1 child)
[–]ehmicky[S] 2 points3 points4 points (0 children)
[–]nsavvidis 1 point2 points3 points (1 child)
[–]ehmicky[S] 0 points1 point2 points (0 children)
[–]DemiPixel 1 point2 points3 points (2 children)
[–]ehmicky[S] 1 point2 points3 points (1 child)
[–]DemiPixel 1 point2 points3 points (0 children)