SublimePrettyJson icon indicating copy to clipboard operation
SublimePrettyJson copied to clipboard

Enable single-line declarations

Open webron opened this issue 11 years ago • 7 comments

I'm not sure this is a feature you'd want to support (should of course be configurable), but I'll raise it.

I'm in the process of writing a large json-schema file, now almost a 1000 line long and it will grow even more.

I'd want to have the ability to have one-liners where possible.

For example, instead of having this:

"description": {
      "$ref": "http://json-schema.org/draft-04/schema#/properties/description"
}

I'd hope to see this:

"description": { "$ref": "http://json-schema.org/draft-04/schema#/properties/description" }

I have plenty one-liner objects (mostly json references) and it would really compress the json and make it more readable (in my opinion at least).

I would love to see a similar option for arrays of primitives, though that's less important. This means that this:

"enum": [
        "csv",
        "ssv",
        "tsv",
        "pipes",
        "multi"
]

would become this:

"enum": [ "csv", "ssv", "tsv", "pipes", "multi" ]

I understand that this may be an issue even with primitives as string values (and numbers for that matter) can end up having pretty long values.

webron avatar Oct 07 '14 14:10 webron

I'd love to see this too, since this is how a human would write a JSON file to be readable. For example, I often work with GeoJSON and TopoJSON formats, and pretty-printing them looks like this:

    [
        [
          [
            0
          ]
        ],
        [
          [
            1390,
            228
          ],
          [
            1770,
            1181,
            -119,
            -960,
            -549
          ],
          [
            3,
            1104
          ],
          [
            1084
          ],
          [
            1086,
            545
          ]
      ...

To do this programmatically, I run a array/object regexp on the pretty-printed output, strip all returns and see if the resulting line is less than a threshold (say 80), and return the stripped version instead of the full one if so.

mourner avatar Dec 16 '14 17:12 mourner

Hi, any news about this feature? It could be very useful for me.

Thanks

alexcarrega avatar Jul 21 '15 19:07 alexcarrega

can you please try adding this

"keep_arrays_single_line": true

into your user settings file and try formatting and let me know what you think

it works only for arrays [...] and extremely experimental :)

dzhibas avatar Jul 14 '16 16:07 dzhibas

@dzhibas looks great, thanks for looking into this! The next step would be to handle nested arrays, e.g. stuff like this:

  {
    "geometry": [
      [
        [3415, -64],
        [3323, -15],
        [3300, -64],
        [3415, -64]
      ]
    ],

mourner avatar Jul 14 '16 16:07 mourner

I am also in favor of this feature. And I have come up with an idea to solve nested ones. In Pretty Json we have a minify JSON and a format JSON, so can we pretty a file like this:

function pretty(JsonObject A){
    if(A.length>max_length_minify){
        A.a = pretty(A.a);
        A.b = pretty(A.b);
        A.c = pretty(A.c);
        /* other first level nested objects*/
        // format first level of A, with formatted A.a, A.b, A.c ...
        format(A, level=1, [A.a, A.b, A.c]);
    }
    return minify(A);
}
// where format(A) refer to its format JSON, and minify(A) refer to its minify JSON.
// Also max_length_minify is a parameter like max_arrays_line_length,
// and can be changed by user in configuration file

I have never programmed in Python yet so I do not know if this approach match our case. And sorry for not able to help with a pull request. Thank you.

qimuxi1990 avatar Jan 12 '17 16:01 qimuxi1990

Believe this will now be possible in #111 with the following settings

"brace_newline": true "bracket_newline": true

TerminalFi avatar Apr 16 '20 12:04 TerminalFi

Investigating

"description": { "$ref": "http://json-schema.org/draft-04/schema#/properties/description" }

TerminalFi avatar Apr 16 '20 12:04 TerminalFi