1.0.1 • Published 1 year ago

nvlson v1.0.1

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

nvlson

  • another json, its a simple version of nvison
  • full-support : true | false | js-number | js-string | bigint | regexp | date | array-buffer(treat--as-leaf) | array(container) | dict(container)
  • full-support : NaN | +Infinity | -Infinity

  • partly-support: symbol(USE Symbol.for to parse)

  • partly-support: undefined( in array whill be a empty-slot【which not work for forEach AND .map】, in dict will disappear)
  • not-support : circular-reference
  • not-support : multi-key
  • not-support : comments
  • not-support : user-defined-seperator/comma/colon
  • not-support : loose-scientific-number

  • its slow, coz it USE JSON.stringify/JSON.parse replacer directly, that is slow

  • its for test lson-data-structure in nvlang , normally useless
  • its about 8 times SLOW than JSON.stringify/JSON.parse

install

  • npm install nvlson

usage

  const { parse,stringify }   = require("nvlson");

example

        var x = require("nvlson")
        var y  = Symbol.for("sym");
        var ab = v8.serialize("\x00\x01abcdefg\xfe\xff").buffer; 
        /*
        ArrayBuffer {
          [Uint8Contents]: <ff 0d 22 0b 00 01 61 62 63 64 65 66 67 fe ff>,
          byteLength: 15
        }
        */

        var j = [undefined,null,true,false,NaN,+Infinity,-Infinity,11.11,"string",y,new Date,/[a-z]+/i, 12345678901234567890n,ab];

        var s = x.stringify(j,2);

        /*

        [
          "u",
          "n",
          "t",
          "f",
          " ",
          "+",
          "-",
          "N¸\u001e
        ëQ8&@",
          "Sstring",
          "Ysym",
          "D\u0000 O
        qxB",
          "R[\"[a-z]+\",\"i\"]",
          "B12345678901234567890",
          "Aÿ\r\"\u000b\u0000\u0001abcdefgþÿ"
        ]
        */

        > x.parse(s)
        /*
        [
          <1 empty item>,
          null,
          true,
          false,
          NaN,
          Infinity,
          -Infinity,
          11.11,
          'string',
          Symbol(sym),
          2023-03-25T11:02:23.584Z,
          /[a-z]+/i,
          12345678901234567890n,
          ArrayBuffer {
            [Uint8Contents]: <ff 0d 22 0b 00 01 61 62 63 64 65 66 67 fe ff>,
            byteLength: 15
          }
        ]
        > 
        */

        var j = {
            ary: [
                undefined,
                {
                    nu:null,
                    t:true,
                    f:false
                },
                NaN,
                [
                    new Date,
                    "sssssssssssssssssssssssssssssssssssssssssssssss",
                    Symbol.for("sym-using-for"),
                ]
            ],
            dict: {
                un: undefined,
                infi: [+Infinity,-Infinity],
                numlike: {
                    f64: [11.11,0,22e8,-11.11],
                    bi: [-1n,0n,123456789123456789123456789n],
                    ab: v8.serialize("\x00\x01abcdefg\xfe\xff").buffer
                }
            }
        }

        var s = x.stringify(j,2)

        /*
        {
          "ary": [
            "u",
            {
              "nu": "n",
              "t": "t",
              "f": "f"
            },
            " ",
            [
              "D\u0000p¦pqxB",
              "Ssssssssssssssssssssssssssssssssssssssssssssssss",
              "Ysym-using-for"
            ]
          ],
          "dict": {
            "un": "u",
            "infi": [
              "+",
              "-"
            ],
            "numlike": {
              "f64": [
                "N¸\u001e
        ëQ8&@",
                "N\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000",
                "N\u0000\u0000\u0000À*dàA",
                "N¸\u001e
        ëQ8&À"
              ],
              "bi": [
                "B-1",
                "B0",
                "B123456789123456789123456789"
              ],
              "ab": "Aÿ\r\"\u000b\u0000\u0001abcdefgþÿ"
            }
          }
        }
        */

         x.parse(s)
        /*
        >  x.parse(s)
        {
          ary: [
            <1 empty item>,                                // undefined become empty slot 
            { nu: null, t: true, f: false },
            NaN,
            [
              2023-03-25T11:11:07.367Z,
              'sssssssssssssssssssssssssssssssssssssssssssssss',
              Symbol(sym-using-for)
            ]
          ],
          dict: {
                                                                          //undefined disappeared
            infi: [ Infinity, -Infinity ],
            numlike: { f64: [Array], bi: [Array], ab: [ArrayBuffer] }
          }
        }
        > 
        */

        /*
        > for(let e of a) {console.log(e)}
        undefined                                            //work 
        { nu: null, t: true, f: false }
        NaN
        [
          2023-03-25T11:11:07.367Z,
          'sssssssssssssssssssssssssssssssssssssssssssssss',
          Symbol(sym-using-for)
        ]

        > a.forEach((r,i)=>console.log(r,i))           
                                                     // empty-slot cant be forEach OR Map
        { nu: null, t: true, f: false } 1
        NaN 2
        [
          2023-03-25T11:11:07.367Z,
          'sssssssssssssssssssssssssssssssssssssssssssssss',
          Symbol(sym-using-for)
        ] 3

        */

speed test

        var jlson = {
            ary: [
                undefined,
                {
                    nu:null,
                    t:true,
                    f:false
                },
                NaN,
                [
                    new Date,
                    "sssssssssssssssssssssssssssssssssssssssssssssss",
                    Symbol.for("sym-using-for"),
                ]
            ],
            dict: {
                un: undefined,
                infi: [+Infinity,-Infinity],
                numlike: {
                    f64: [11.11,0,22e8,-11.11],
                    bi: [-1n,0n,123456789123456789123456789n],
                    ab: v8.serialize("\x00\x01abcdefg\xfe\xff").buffer
                }
            }
        }


        var jjson = {
            ary: [
                undefined,
                {
                    nu:null,
                    t:true,
                    f:false
                },
                -0,
                [
                    new Date,
                    "sssssssssssssssssssssssssssssssssssssssssssssss",
                    "sym-using-for",
                ]
            ],
            dict: {
                un: undefined,
                infi: [Number.MAX_VALUE,-Number.MAX_VALUE],
                numlike: {
                    f64: [11.11,0,22e8,-11.11],
                    bi: [-1,0,1.2345678912345679e+26],
                    ab: v8.serialize("\x00\x01abcdefg\xfe\xff").toString("latin1")
                }
            }
        }


            > sync(100000,(j)=>JSON.stringify(j),jjson)
            {
              rounds: 100000,
              f: [Function (anonymous)],
              costed: 411.23624181747437
            }
            >

            > sync(100000,(j)=>x.stringify(j),jlson)                   //7 times slow than JSON.stringify
            {
              rounds: 100000,
              f: [Function (anonymous)],
              costed: 2852.3511419296265
            }
            >

            var sjson =JSON.stringify(jjson);
            var slson = x.stringify(jlson);


            >sync(100000,(s)=>JSON.parse(s),sjson);
            {
              rounds: 100000,
              f: [Function (anonymous)],
              costed: 265.8316650390625
            }
            >

            > sync(100000,(s)=>x.parse(s),slson);                         // 8 times slow than JSON.parse
            {
              rounds: 100000,
              f: [Function (anonymous)],
              costed: 1864.9141840934753
            }
            >

METHODS

APIS

    {
      T2NM: {
        u: 'undefined',
        n: 'null',
        t: 'true',
        f: 'false',
        ' ': 'NaN',
        '+': '+Infinity',
        '-': '-Infinity',
        N: 'number',
        S: 'string',
        B: 'bigint',
        Y: 'symbol',
        R: 'RegExp',
        D: 'Date',
        A: 'ArrayBuffer'
      },
      encd_rgx: [Function: encd_rgx],
      decd_rgx: [Function: decd_rgx],
      encd_num: [Function: encd_num],
      decd_num: [Function: decd_num],
      encd_dt: [Function: encd_dt],
      decd_dt: [Function: decd_dt],
      stringify: [Function: stringify],
      parse: [Function: parse]
    }

LICENSE

  • ISC