You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
28 lines
570 B
JavaScript
28 lines
570 B
JavaScript
"use strict";
|
|
|
|
const { chain } = require("error-chain");
|
|
const ParseError = require("../text-parser");
|
|
|
|
module.exports = function createJsonParser(resultMapper) {
|
|
return {
|
|
supportsStreams: false,
|
|
parse: function (text) {
|
|
try {
|
|
let parsed = JSON.parse(text);
|
|
|
|
if (resultMapper != null) {
|
|
return resultMapper(parsed);
|
|
} else {
|
|
return parsed;
|
|
}
|
|
} catch (error) {
|
|
if (error instanceof SyntaxError) {
|
|
throw chain(error, ParseError, "JSON parsing failed with a syntax error");
|
|
} else {
|
|
throw error;
|
|
}
|
|
}
|
|
}
|
|
};
|
|
};
|