j2n

1.2.0 • Public • Published

J2N

J2N is a .json and .j2n parser. It also stringifies into .j2n format.

Main purpose of J2N is to shorten on json file size by removing any unecessary characters

It also can serialize Date $D<ms> and RegExp $E{F:<flags>,S:<source>}

It can serialize circular references $R<order>

Strings can begin with:

  • ~ - where , or array or object end is the end of the string terminator. These characters have to be backslashed inside the string.
  • ' - where ' is the string terminator. It has to be bachslashed inside the string.
  • " - where " is the string terminator. It has to be bachslashed inside the string.

Strings use \t, \n and \r. If you use an actual tab or newline, it will be ignored. You can use this for prettying your j2n.

Keywords:

  • / - undefined
  • - - false
  • + - true
  • $<k> - special. k:
    • $ - null
    • R<order> - reference to an already existing object or function
    • F<string> - Function
    • D<ms> - Date
    • E{F:<flags>,S:<source>} - RegExp

In Keys , doesn't end the key name, but : does. This character has to be bachslashed to be included in the name.

Keys behave like strings, but don't begin with ~ if they don't have to.

You can comment the file as well:

  • # opens a comment or closes it. New line closes a comment as well.
  • If you enabled tabs in the stringify method, it will comment all objects with their respective $R<order> order.

The stringify method chooses the most space efficient metod of storing a string.

The parse method accepts J2N as well as regular JSON.

Docs: J2N.stringify( value: any[, tabs: boolean[, tokenize: ( text: string, type: J2N.types, secondaryType: J2N.types ) => string ] ] ): string

  • value: Any js value to be stringified.
  • tabs: Whether to make the text formated ( default: false - single line ).
    • As of 1.2.0 the stringify method accepts a third parameter, which supplied with text and event type has to return a tokenized string. You might use this to color code your J2N files.
    • tokenize: A callback that modyfies the string for each value and keyword. See the J2N.types enum for a list of types.
  • Returns a string in J2N format.

J2N.parse( string: string ): any

  • string A string in J2N or JSON format.
  • Returns any.

J2N.types: enum

  • An enum of all value and keyword types.

Example:

const J2N = require( 'j2n' );

let value = '\n\
[\n\
    +,\n\
    -,\n\
    /,\n\
    $$,\n\
    $E{F:~g,S:~I am a regular expression}, # also this is a comment\n\
    $F~( v ) => { return v; },\n\
    $D100,\n\
    83# I can comment even there #566,\n\
    ~ Stringy   ,\n\
    \',,,,,,,,,,,,,,\',\n\
    true,\n\
    undefined,\n\
    {\n\
        key: ~value,\n\
        N: null,\n\
        F: false,\n\
        Parent: $R0,\n\
        Me: $R4,\n\
        Date: $R3,\n\
        PerentFunc: $R2,\n\
        RegExp: $R1\n\
    },\n\
    ~# but this is a part of the string #,\n\
    $R0\n\
]';

console.log( J2N.parse( value ) );

Output:

[
    true,
    false,
    undefined,
    null,
    /I am a regular expression/g,
    function (),
    Date 1970-01-01T00:00:00.100Z,
    83566,
    " Stringy   ",
    ",,,,,,,,,,,,,,",
    true,
    undefined,
    {
        key: "value",
        N: null,
        F: false,
        Parent: [ ... ],
        Me: { ... },
        Date: Date 1970-01-01T00:00:00.100Z,
        ParentFunc: function (),
        RegExp: /I am a regular expression/g
    },
    "# but this is a part of the string #",
    [ ... ]
]

Package Sidebar

Install

npm i j2n

Weekly Downloads

0

Version

1.2.0

License

GPL-3.0-only

Unpacked Size

15.6 kB

Total Files

3

Last publish

Collaborators

  • flutterish