Failing to parse JSON with null value
BrerBear opened this issue · comments
Parser fails to process JSON with null value, which is allowed by spec.
That happens due to null
value is of type object
in JavaScript.
Code sample:
var parse = require("json-templates");
var spec = {
x: '{{foo}}',
y: null
};
var template = parse(spec);
console.log(template.parameters); // Prints [{ key: "foo" }]
console.log(template({ foo: "bar" })); // Expected { x: 'bar', y: null }
Stack trace:
./node_modules/json-templates/index.js:113
const children = Object.keys(object).map(key => ({
^
TypeError: Cannot convert undefined or null to object
at Function.keys (<anonymous>)
at parseObject (./node_modules/json-templates/index.js:113:27)
at parse (./node_modules/json-templates/index.js:64:14)
at Object.keys.map.key (./node_modules/json-templates/index.js:115:20)
at Array.map (<anonymous>)
at parseObject (./node_modules/json-templates/index.js:113:40)
at parse (./node_modules/json-templates/index.js:64:14)
at Object.<anonymous> (./main.js:8:16)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
Here is a patch proposal:
*** 13,18 ****
--- 13,20 ----
valueType = 'array';
} else if (value instanceof Date) {
valueType = 'date';
+ } else if (value === null) {
+ valueType = 'null';
}
return valueType;
Nice catch! I'd welcome a Pull Request that adds a test for this case and your patch.
Sure. I can do it.
Awesome. Thanks!