All Projects → epoberezkin → json-source-map

epoberezkin / json-source-map

Licence: MIT License
Parse/stringify JSON and provide source-map for JSON-pointers to all nodes - supports BigInt, Maps, Sets and Typed arrays

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to json-source-map

Qs
A querystring parser with nesting support
Stars: ✭ 6,688 (+12060%)
Mutual labels:  parse, stringify
Query String
Parse and stringify URL query strings
Stars: ✭ 5,781 (+10410.91%)
Mutual labels:  parse, stringify
Stringy
🉑 Stringy - A PHP string manipulation library with multibyte support, performance optimized
Stars: ✭ 135 (+145.45%)
Mutual labels:  stringify
html-dom-parser
📝 HTML to DOM parser.
Stars: ✭ 56 (+1.82%)
Mutual labels:  parse
expresol
Library for executing customizable script-languages in python
Stars: ✭ 11 (-80%)
Mutual labels:  parse
easy-json-parse
Parse your json safely and easily.
Stars: ✭ 33 (-40%)
Mutual labels:  parse
sbt-scala-js-map
A Sbt plugin that configures source mapping for Scala.js projects hosted on Github
Stars: ✭ 30 (-45.45%)
Mutual labels:  source-map
Android-Shortify
An Android library used for making an Android application more faster with less amount of code. Shortify for Android provides basic functionalities of view and resource binding, view customization, JSON parsing, AJAX, various readymade dialogs and much more.
Stars: ✭ 21 (-61.82%)
Mutual labels:  parse
OpenGraph-Net
.Net Open Graph Parser written in C#
Stars: ✭ 111 (+101.82%)
Mutual labels:  parse
carsBase
База автомобилей с марками и моделями JSON, CSV, XLSX и MySQL
Stars: ✭ 49 (-10.91%)
Mutual labels:  parse
exoffice
Library to parse common excel formats (xls, xlsx, csv)
Stars: ✭ 31 (-43.64%)
Mutual labels:  parse
lucene
Node.js lib to transform: lucene query → syntax tree → lucene query
Stars: ✭ 61 (+10.91%)
Mutual labels:  stringify
eval-estree-expression
Safely evaluate JavaScript (estree) expressions, sync and async.
Stars: ✭ 22 (-60%)
Mutual labels:  parse
crawler CIA CREST
R-crawler for CIA website (CREST)
Stars: ✭ 15 (-72.73%)
Mutual labels:  parse
Splain
small parser to create more interesting language/sentences
Stars: ✭ 15 (-72.73%)
Mutual labels:  parse
parse-torrent-file
DEPRECATED: Parse a .torrent file and return an object of keys/values
Stars: ✭ 62 (+12.73%)
Mutual labels:  parse
vgprompter
C# library to parse a subset of Ren'Py script syntax
Stars: ✭ 17 (-69.09%)
Mutual labels:  parse
warframe-worldstate-parser
📗 An Open parser for Warframe's Worldstate in Javascript
Stars: ✭ 50 (-9.09%)
Mutual labels:  parse
cmd-ts
💻 A type-driven command line argument parser
Stars: ✭ 92 (+67.27%)
Mutual labels:  parse
sjson-cpp
An Simplified JSON (SJSON) C++ reader and writer
Stars: ✭ 16 (-70.91%)
Mutual labels:  parse

json-source-map

Parse/stringify JSON and provide source-map for JSON-pointers to all nodes.

NEW: supports BigInt, Maps, Sets and Typed arrays.

Build Status npm version Coverage Status

Install

npm install json-source-map

Possible use cases

Source maps

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.

Editing forms/JSON

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.

Usage

Stringify

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 } } }

Parse

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 } } }

API

.parse(String json, Any _, Object options) -> Object;

Parses JSON string. Returns object with properties:

  • data: parsed data.
  • pointers: an object where each key is a JSON pointer (RFC 6901), each corresponding value is a mapping object.

Mapping object has properties:

  • key: location object (see below) of the beginning of the key in JSON string. This property is only present if parent data is an object (rather than array).
  • keyEnd: location of the end of the key in JSON string. This property is only present if parent data is an object.
  • value: location of the beginning of the value in JSON string.
  • valueEnd: location of the end of the value in JSON string.

Location object has properties (zero-based numbers):

  • line: line number in JSON file.
  • column: column number in JSON string (from the beginning of line).
  • pos: character position in JSON file (from the beginning of JSON string).

Options:

  • bigint: parse large integers as BigInt.

Whitespace:

  • the only character that increases line number in mappings is line feed ('\n'), so if your JSON string has '\r\n' sequence, it will still be counted as one line,
  • both '\r' and '\n' are counted as a character when determining pos (it is possible to slice sections of JSON string using pos property), but column counter is reset when r or n is encountered,
  • tabs ('\t') are counted as four spaces when determining column but as a single character for pos.

Comparison with the standard JSON.parse:

  • when it is not possible to parse JSON, a SyntaxError exception with exactly the same message is thrown,
  • reviver parameter of JSON.parse is not supported, but its position is reserved.
  • supports parsing large integers as BigInt (with the option bigint: true).

.stringify(Any data, Any _, String|Number|Object space) -> Object;

Stringifies JavaScript data. Returns object with properties:

  • json: JSON string - stringified data.
  • pointers: an object where each key is a JSON-pointer, each corresponding value is a mapping object (same format as in parse method).

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: same as space parameter.
  • es6: stringify ES6 Maps, Sets and Typed arrays (as JSON arrays).

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].