$ gnpm install json-source-map
Parse/stringify JSON and provide source-map for JSON-pointers to all nodes.
NEW: supports BigInt, Maps, Sets and Typed arrays.
npm install json-source-map
When a domain-specific language that compiles to JavaScript uses JSON as a format, this module can be used as a replacement for standard JSON to simplify generation of source maps.
When a form also allows to edit JSON representation of data on the same screen, this module can be used to sinchronise navigation in JSON and in the form.
var jsonMap = require('json-source-map');
var result = jsonMap.stringify({ foo: 'bar' }, null, 2);
console.log('json:');
console.log(result.json);
console.log('\npointers:');
console.log(result.pointers);
output:
json:
{
"foo": "bar"
}
pointers:
{ '':
{ value: { line: 0, column: 0, pos: 0 },
valueEnd: { line: 2, column: 1, pos: 18 } },
'/foo':
{ key: { line: 1, column: 2, pos: 4 },
keyEnd: { line: 1, column: 7, pos: 9 },
value: { line: 1, column: 9, pos: 11 },
valueEnd: { line: 1, column: 14, pos: 16 } } }
var result = jsonMap.parse('{ "foo": "bar" }');
console.log('data:')
console.log(result.data);
console.log('\npointers:');
console.log(result.pointers);
output:
data:
{ foo: 'bar' }
pointers:
{ '':
{ value: { line: 0, column: 0, pos: 0 },
valueEnd: { line: 0, column: 16, pos: 16 } },
'/foo':
{ key: { line: 0, column: 2, pos: 2 },
keyEnd: { line: 0, column: 7, pos: 7 },
value: { line: 0, column: 9, pos: 9 },
valueEnd: { line: 0, column: 14, pos: 14 } } }
Parses JSON string. Returns object with properties:
Mapping object has properties:
Location object has properties (zero-based numbers):
Options:
Whitespace:
pos
(it is possible to slice sections of JSON string using pos
property), but column
counter is reset when r
or n
is encountered,column
but as a single character for pos
.Comparison with the standard JSON.parse
:
reviver
parameter of JSON.parse is not supported, but its position is reserved.bigint: true
).Stringifies JavaScript data. Returns object with properties:
Comparison with the standard JSON.stringify
:
replacer
parameter of JSON.stringify is not supported, but its position is reserved.space
parameter is supported, but if it is a string, it may only contain characters space, tab ('\t'), caret return ('\r') and line feed ('\n') - using any other caracter throws an exception. If this parameter is an object, it is options.Options:
space
parameter.Copyright 2013 - present © cnpmjs.org | Home |