0.0.5 • Published 10 years ago
json-half-parse v0.0.5
json-half-parse
npm install --save json-half-parseParses JSON, returning a partially parsed result if there are errors:
JSON.parse('[1, 2, ueauu]'); // Error
json.parse('[1, 2, ueauu]'); // [1, 2]
JSON.parse('{ "a": 1, "b":'); // Error
json.parse('{ "a": 1, "b":'); // { a: 1 }Still respects JSON syntax: no trailling commas, double quoted strings only etc.
API
const { error, value } = json.parse(string);value is an object like that returned JSON.parse.
error is an object represented as { message, remainingText, location }
- locationis represented as- { line, column, offset }, where offset is character index
const { error, value } = json.parseWithAst(string);value is an abstract syntax tree (ast) with each node being represented as { type, value, ?match, ?isComplete }.
- typeis either- null,- string,- boolean,- number,- array, or- object
- For all primitive, valueis the JSON value
- For an array, valueis an array of values represented in the ast form
- For an object, valueis a entry of[key, v], wherekeyis a string, andvis an ast value
- matchis available for primitives, and gives the string that was matched
- isCompleteis available for objects and arrays, and indicates whether they were completely parsed
Not built around efficiency: don't use it in performance sensitive scenarios.