Skip to content

Enhancement: add jprint(1) tool to output valid JSON in canonical format #5

@lcn2

Description

@lcn2

Is there an existing issue for this?

  • I have searched for existing issues and did not find anything like this

Describe the feature

We recommend adding a jprint(1) tool that will output JSON that has been parsed and found to be valid, in canonical format.

For the canonical output format model, we recommend that bin/jprint-wrapper.sh prints.

Relevant images, screenshots or other files

We recommend the following command line for jprint(1):

usage: jprint [-h] [-v level] [-V] [--use-jparse] [-S] [-i ext] file.json

    -h		    print help message and exit
    -v level	    set verbosity level (def level: 0)
    -V		    print version string and exit

    --use-jparse    Do nothing: this option is a simple way to verify this tool came from the jparse toolset.

    -S              Reserved for a future print JSON semantic strings facility (def: print values)

    -i  ext         Edit files in-place, saving backups with the specified ext extension, and do not write to stdout.
                    If a zero-length ext is given, no backup will be saved.  (def: do not edit files)
                    If file.json is - (dash) or . (dot) then this option is ignored.

    -c              Compare new contents with the old contents and only update is different
                    The use of -c requires the use of -i ext
                    If file.json is - (dash) or . (dot) then this option is ignored.

    file.json       JSON input file
                    If file.json is - (dash) read from standard input.
                    If file.json is . (dot) do not read any file, perform an internal test and exit accordingly.

Exit codes:
     0         all OK
     1         file is not valid JSON 
     2         cannot read file.json, or file.json does not exist, or file.json is not a file
     3         internal test failed
     4         invoked a reserved facility that is not yet implemented=
 >= 10         internal error 

Relevant links

The bin/jprint-wrapper.sh tool is the model for how to print JSON canonically.

Anything else?

We would not expect another jparse command to be able to undestant --use-jparse as a command line option. Again, the --use-jparse command line option does nothing (other than to be parsed as a command line option).

When file.json is - (dash), read from standard input as in:

    /some/command | jprint -

When file.json is . (dot), perform an internal test by parsing an internal (static array in memory) JSON. If the test of the internal (static array in memory) JSON fails, when jprint(1) must exit 3.

HINT: While the internal (static array in memory) JSON should NOT be huge, the contents should be a mix of various JSON elements. Something fun along the lines of jparse/test_jparse/test_JSON/good/party.json might serve well.

The purposes of jprint --use-jparse is to "verify" that the jprint(1) tool came from this jparse repo toolset. There are likely to be more than 1 jprint command out there. To "verify" (somewhat) that the jprint tool found on $PATH is the proper jparse tool version we use:

    jprint --use-jparse .

If there is someone else's jprint found (such as along the $PATH), then one may expect that jprint --use-jparse . will fail, either because -use-jparse is NOT a valid argument for that command, or because . is not a file.

The -i ext allow one to update a file with the canonically formatted JSON and save the old contents of the file. For example:

    jprint -i .old file.json

The previous contents is saved as file.json.old and file.json is replaced with the canonically formatted JSON contents. The actions to move files must be performed using the rename(2) system call. The effect of the above command is as if this was performed:

    jprint file.json > .tmp.file.json
    ln -f file.json file.json.old
    mv -f .tmp.file.json file.json

NOTE: In these examples, files of the form .tmp.file.json represent a temporary file. One should use mkstemp(3) call to crate a temporary file in the same directory as file.json so that the rename(2) system call will be atomic.

If the ext is an empty string, no old file contents is saved:

    jprint -i "" file.json

The file.json contents is replaced with the canonical JSON contents canonically formatted JSON contents. The actions to move files must be performed using the rename(2) system call. The effect of the above command is as if this was performed:

    jprint file.json > .tmp.file.json
    mv -f .tmp.file.json file.json

When the file.json is - (dash) or . (dot), the -i ext is ignored.

The -c option must be used with -i ext. The file.json file is ONLY touched if the contents of the file is updated. If the contents of the file.json file is not changed, no old file is saved.

This command:

    jprint -i .old -c file.json

has the effect of:

    jprint file.json > .tmp.file.json
    if cmp -s file.json .tmp.file.json; then
        rm -f .tmp.file.json
    else
        ln -f file.json file.json.old
        mv -f .tmp.file.json file.json
    fi

This command:

    jprint -i "" -c file.json

has the effect of:

    jprint file.json > .tmp.file.json
    if cmp -s file.json .tmp.file.json; then
        rm -f .tmp.file.json
    else
        mv -f .tmp.file.json file.json
    fi

The contents of file.json should be read into memory. If the file.json is not a file, or if file.json does not exist, or if there is a read error while reading the file into memory, or if the command runs out of memory, then the jprint(1) command must exit 2.

Once the contents of the file.json is in memory, then jparse_json(3) (or related function) should be called to parse the contents. If the contents is NOT valid JSON (or the JSON parser function returns an error), then the jprint(1) command must exit 1.

UPDATE 0

We will save this issue now, and return in the morning to review and if need edit/revise it. This is just a draft.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions