nvison
- another json
- undefined
- bin
- hex
- oct
- scientific-notation
- bigInt
- Infinity
- NaN
- concatable-string
- comments
- hash
- ref
- optional-quote
- configurable-colons
- configurable-commas
- configurable-quotes
- configurable-array-blk
- configurable-object-blk
- post-dfs-generator
install
requirements
- node need V15+ , coz it USE Event-Target
usage
const ison = require("nvison");
parse
examples
undefined
var code = `[undefined,null,true,false]`
var j = ison.parse_from_str(code)
/*
> j
[ undefined, null, true, false ]
>
*/
number
var code = `
[
175 ,0x1101, 0b1101, 0o1101,
-123.12e3, -1.1e2.5, .1E3.13,
1234567891234567889123456789n,
+Infinity, -Infinity, NaN
]
`
var j = ison.parse_from_str(code)
/*
> j
[
175,
4353,
13,
577,
-123120,
-347.85054261852173,
134.89628825916535,
1234567891234567889123456789n,
Infinity,
-Infinity,
NaN
]
>
*/
string
var code = `
[
abc-def,
'def\tsquoted',
"d quoted",
\`tock-quoted-line\ntock-quoted-line\`,
str"@auto-concat@"str
]
`
var j = ison.parse_from_str(code)
/*
[
'abc-def',
'def\tsquoted',
'd quoted',
'tock-quoted-line\ntock-quoted-line',
'str@auto-concat@str'
]
*/
commas
- commas is optional
- support mixed-style
- by default it is "," ";" and white-spaces
- its configurable: must-be-one-char
var code = `
[1 2 3 a b c]
[1,2,3,a,b,c]
[1;2;3;a;b;c]
[1 2,3;a b,c]
`
var j = ison.parse_from_str(code)
/*
[
[ 1, 2, 3, 'a', 'b', 'c' ],
[ 1, 2, 3, 'a', 'b', 'c' ],
[ 1, 2, 3, 'a', 'b', 'c' ],
[ 1, 2, 3, 'a', 'b', 'c' ]
]
*/
self-define
//for example, I add a chinese period "。" as a comma
> ison.CFG.commas
Set(2) { ',', ';' }
>
> ison.CFG.commas.add("。")
> ison.CFG.commas
Set(3) { ',', ';', '。' }
>
var code = `[i,you,he; yo,tú,Él; 我。你。他;]`
var j = ison.parse_from_str(code)
/*
[
'i', 'you', 'he',
'yo', 'tú', 'Él',
'我', '你', '他'
]
*/
colons
- colons is mandatory
- support mixed-style
- by default it is ":" and "="
- its configurable,must-be-one-char
var code = `
{a:b,c:d,e:f}
{a=b,c=d,e=f}
{a:b c=d,e=f; g:h}
`
var j = ison.parse_from_str(code)
/*
[
{ a: 'b', c: 'd', e: 'f' },
{ a: 'b', c: 'd', e: 'f' },
{ a: 'b', c: 'd', e: 'f', g: 'h' }
]
*/
self-define
ison.CFG.colons
Set(2) { ':', '=' }
ison.CFG.colons.add("|")
Set(3) { ':', '=', '|' }
var code = `
{ a|b,c|d,e|f }
`
var j = ison.parse_from_str(code)
/*
{ a: 'b', c: 'd', e: 'f' }
*/
Error auto recover
- colon before key will be treated as whitespace
- multi-colons before value will be treated as one
- colons following value will be dropped
- unfinished key:value pair will be dropped
//colon before key will be treated as whitespace
{ a:b, : key : value} -> { a: 'b', key: 'value' }
//multi-colons before value will be treated as one
{ a:b, key ::::value } -> { a: 'b', key: 'value' }
[100, ::::xy] -> [ 100, 'xy' ]
//colons following value will be dropped
[abc::: 123] -> [ 'abc', 123 ],
{k:abc:::, k2:v2} -> { k: 'abc', k2: 'v2' }
[abc : 666] -> [ 'abc', 666 ]
{k:v, key:value : 100:200} -> { '100': 200, k: 'v', key: 'value' }
//unfinished k:v will be dropped
{k:abc:, k2, k3:v3} -> { k: 'abc', k3: 'v3' }
{k:abc:, k2:, k3:v3} -> { k: 'abc', k3: 'v3' }
{k:abc:, k2 :,k3:v3} -> { k: 'abc', k3: 'v3' }
quotes
- quotes is optional if without escape
- support mixed-style
- by default it is "'" and '"' and '`'
- its configurable,must-be-one-char, except '
', '
' is reservered for future-using
- currently NOT support string-literal-template
- auto concat
> ison.CFG.quotes
Set(3) { '"', "'", '`' }
> ison.CFG.reserved
[ '/', '*', '#', '&', '`' ]
> ison.CFG.quotes.add("%")
> ison.CFG.quotes
Set(4) { '"', "'", '`', '%' }
>
var code = `
[
a"\t\v\t"b,
"cde",
fgh,
'ijk',
%lmnopq%,
\`rst \n uvw \n xyz\`
]
`
var j = ison.parse_from_str(code)
/*
[ 'a\t\x0B\tb', 'cde', 'fgh', 'ijk', 'lmnopq', 'rst \n uvw \n xyz' ]
*/
unclosed quotes will be dropped
{ k0:v0, key : "val-\t-.... -> {k0:v0}
if you want the lefted parted for continue, use
require("nvison-parse-internal")
D {
....
lefted: {
type: 3,
data: { rslt: 'val-\t-....\n\n\n', lefted: '', state: 3, quote: '"' }
},
....
}
comments
- two kind : line-comment(//....\n) and block-comment(/...../)
- four position supported: @after_key, @before_val(properties), @after_val, @before_val(elements)
valid and invalid comment
// VALID
{
.....
/*text-node-comment*/
key /*key-comment*/ : /* before-val-comment*/ value /*after-val-comment*/ ,
/*text-node-comment*/
key1 : value1,
/*text-node-comment*/
.....
}
[
.....
/*text-node-comment*/
element0 /*after-val-comment*/ ,
/*text-node-comment*/
....
]
//VALID
//INVALID
[ abc/*in-value-comment-not-supported*/def, ....]
it will be treated as [ abc, def ....]
{ kkkk/*in-key-comment-not-supported*/ey : value, ...}
it will be treadted as { ey:value,....} ,
the pre-part of kkkkey(kkkk) will be dropped
//INVALID
var code = `
{
"total_rows": 129 , //line comment here
/*
block comment here
*/
"offset": 0, //line comment here
"rows": [
{
/*text comment*/
"id": /*@before_val comment*/ "change1_0.6995461115147918",
/*text comment*/
"key" : "change1_0.6995461115147918" /*@after_val comment*/ ,
/*text comment*/
"value" /*key comment*/ : {
"rev": "1-e240bae28c7bb3667f02760f6398d508"
},
/*text comment*/
"doc" /*key comment*/ : {
"_id": "change1_0.6995461115147918",
"_rev": "1-e240bae28c7bb3667f02760f6398d508",
"hello": 1
}
}
]
}
`
var j = ison.parse_from_str(code)
/*
{
total_rows: 129,
offset: 0,
rows: [
{
id: 'change1_0.6995461115147918',
key: 'change1_0.6995461115147918',
value: [Object],
doc: [Object]
}
]
}
*/
array
- support mixed-style
- by default it is "[]" and '()' and '<>'
- its configurable,must-be-one-char-pair
ison.CFG.array_blks
> Map(3) { '[' => ']', '(' => ')', '<' => '>' }
ison.CFG.array_blks.add("【","】")
ison.CFG.array_blks.add("《","》")
> Map(5) { '[' => ']', '(' => ')', '<' => '>', '【' => '】', '《' => '》' }
var code = `
defun last-state <rewindable>
【
let
[
(size <rewind-count rewindable>)
]
[
if<zerop size> <values nil nil>
(
values
<aref 《rewind-store rewindable》 《1 - size》>
t
)
]
】
`
var j = ison.parse_from_str(code)
/*
[
'defun',
'last-state',
[ 'rewindable' ],
[ 'let', [ [Array] ], [ 'if', [Array], [Array], [Array] ] ]
]
*/
dict(object)
- support mixed-style
- by default it is "{}"
- its configurable,must-be-one-char-pair
ison.CFG.obj_blks
ison.CFG.obj_blks.add('^','