dzhibas / SublimePrettyJson

Prettify/Minify/Query/Goto/Validate/Lint JSON plugin for Sublime Text 3 & 4
MIT License
2.02k stars 181 forks source link

Enable single-line declarations #47

Open webron opened 10 years ago

webron commented 10 years ago

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.

mourner commented 9 years ago

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.

alexcarrega commented 9 years ago

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

Thanks

dzhibas commented 8 years ago

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 :)

mourner commented 8 years ago

@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]
      ]
    ],
qimuxi1990 commented 7 years ago

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.

TerminalFi commented 4 years ago

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

"brace_newline": true "bracket_newline": true

TerminalFi commented 4 years ago

Investigating

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