The best way to catch invalid JSON parsing errors is to put the calls to JSON.parse()
to a try/catch
block.
You really do not have any other option - the built-in implementation throws an exception on invalid JSON data and the only way to prevent that exception from halting your application is to catch it. Even using a 3rd party library will not avoid that - they must do a try/catch
on a JSON.parse()
call somewhere.
The only alternative is to implement your own JSON parsing algorithm that could be more forgiving on invalid data structures, but that feels like digging a 1 cubic metre hole with a small nuke.
Note about performance
The v8 JavaScript engine used by Node.js cannot optimise functions which contain a try/catch
block.
Update: v8 4.5 and above can optimise try/catch. For older releases, see below.
A simple workaround is to put the safe-parsing logic into a separate function so that the main function can still be optimised:
function safelyParseJSON (json) {
// This function cannot be optimised, it's best to
// keep it small!
var parsed
try {
parsed = JSON.parse(json)
} catch (e) {
// Oh well, but whatever...
}
return parsed // Could be undefined!
}
function doAlotOfStuff () {
// ... stuff stuff stuff
var json = safelyParseJSON(data)
// Tadaa, I just got rid of an optimisation killer!
}
If the JSON parsing is done sporadically, this might not have a noticeable effect on performance, but if used improperly in usage-heavy function it could lead to dramatic increase in response times.
Note about try/catch being blocking
It should be noted that every.single.statement of JavaScript code in Node.js is executed only one-at-a-time, no matter if it's called from the main function or from a callback or from a different module or whatever. As such, every single statement will block the process. This is not necessarily a bad thing - a well designed application will spend most of its time waiting for an external resource (database response, HTTP communication, filesystem operations etc.). It is therefore of great importance that frequently executed JavaScript code can be optimised by the v8 engine so it takes as little time as possible in this blocked state - see the note about performance.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…