JSONedit: formatting/postprocessing

There is no formatting standard for JSON and different implementation may produce different outputs. This is little problematic when one need to compare two files as text or store JSON in some kind of version control system - if file was generated with different tool and edited with JSONedit later diff tool would report multiple differences.

Although JSONedit has two configurable parameters for JSON formatting: indentation string and string separating name from value in objects this may be not enough to match precisely other tool formatting style. As I don't know how many configuration parameters might be needed - generalized solution seems to be hard to create.

My proposed way of handling this is to use separate command-line format tool. This tool would be run in background after each file save. For this purpose Lua events mechanism was copied from tSIP:
JSONedit Lua events

First, unzip jq-win32.exe and format.bat to directory of your choice (with no spaces in name). Update format.bat location in following Lua script.

Create /scripts subdirectory for JSONedit and place this script - reformat.lua - (after updating format.bat location) there:

    -- Reformat saved file
    -- Purpose: reduce number of VCS diffs by keeping same formatting as other tool
    
    -- [CONFIGURATION]
    local format_path = "D:\Dokumenty\_PROJEKTY\json_editor\_devel\jq\format.bat"
    -- [END OF CONFIGURATION]
    
    local filename = GetCurrentFileName()
    --ShowMessage(string.format("filename = %s", filename))
    local winapi = require("jsonedit_winapi")
    local cmd = string.format("/c %s \"%s\"", format_path, filename)
    winapi.ShellExecute("open", "cmd.exe", cmd, nil, 0)  
  

Note: beware that jq outputs empty file if its source file is not valid as JSON.

Note 2. Sadly, while jq seems to be most recommended command line JSON tool its Windows support is really poor. In particular it crashes on my PC when used with long file names, i.e. I can convert file "D:\test.json", "D:\test\test.json" but it crashes with more nested directory structure. This seems to be confirmed problem: https://github.com/stedolan/jq/issues/1072.

Back to JSONedit.


 "Cookie monsters": 7732704    Parse time: 0.001 s