Skip to content
This repository has been archived by the owner on Mar 29, 2023. It is now read-only.

Latest commit

 

History

History
309 lines (238 loc) · 3.72 KB

json.md

File metadata and controls

309 lines (238 loc) · 3.72 KB

JSON

Prettify supports beautification of JSON structures. The exposed rules are forwarded to the script lexer for handling. Below is the different styles that can be produced with the available formatting options.

Rule Configuration

{
  json: {
    arrayFormat: 'default',
    braceAllman: false,
    bracePadding: false,
    braceNewline: false,
    objectIndent: false,
    objectSort: false
  }
}

Beautification Options

Array Format

The arrayFormat rule controls how arrays on objects are formatted. This rules will determines if all array indexes should be indented, never indented, or left to the default.

Rule

This defaults to default, ie: left intact and format according to the input style.

{
  json: {
    arrayFormat?: 'default' | 'indent' | 'inline';
  }
}

Default

{
  "array": [ 1, 2,
    3,
    4,
    5 ]
}

Indent

{
  "array": [
    1,
    2,
    3,
    4,
    5
  ]
}

Inline

{
  "array": [ 1, 2, 3, 4, 5 ]
}

Brace Allman

The braceAllman rule puts JSON braces onto new lines, producing an Allman Style output.

Rule

This defaults to false, ie: disabled.

{
  json: {
    braceAllman?: boolean;
  }
}

Disabled

[
  { "prop": "value" },
  { "prop": "value" },
  { "prop": "value" }
]

Enabled

[
  {
    "prop": "value"
  },
  {
    "prop": "value"
  },
  {
    "prop": "value"
  }
]

Brace Padding

The bracePadding rule applies additional spacing to structures.

Rule

This defaults to false, ie: disabled.

{
  json: {
    bracePadding?:: boolean;
  }
}

Disabled

[
  {"prop": "value"},
  {"prop": "value"},
  {"prop": "value"}
]

Enabled

[
  { "prop": "value" },
  { "prop": "value" },
  { "prop": "value" }
]

Brace Newline

The braceNewline rule will insert newlines at the top and bottom of nested properties.

Rule

This defaults to false, ie: disabled.

{
  json: {
    braceNewline?: boolean;
  }
}

Disabled

{
 "one": {
    "xx": {
      "xx": false
    }
  },
  "two": {
    "xx": {
      "xx": false
    }
  }
}

Enabled

{
 "one": {

    "xx": {

      "xx": false

    }

  },
  "two": {

    "xx": {

      "xx": false

    }

  }
}

Object Indent

The objectSort rule will control how object keys should be handled. You can apply indented, never indented, or left to the default. Typically, you will want to leave this option to the default to prevent unreadable objects.

Rule

This defaults to false, ie: disabled.

{
  json: {
    objectSort?:: 'default' | 'indent' | 'inline'
  }
}

Default

{
  "foo": {
    "bar": { "bax": true }
  }
}

Indent

{
  "foo": {
    "bar": {
      "bax": true
    }
  }
}

Inline

{
  "foo": { "bar": { "bax": true } }
}

Object Sort

The objectSort rules will alphanumerically sort object properties.

Rule

This defaults to false, ie: disabled.

{
  json: {
    objectSort?:: boolean;
  }
}

Input

{
  "e": "5",
  "b": "2",
  "d": "4",
  "a": "1",
  "f": "6",
  "c": "3"
}

Output

{
  "a": "1",
  "b": "2",
  "c": "3",
  "d": "4",
  "e": "5",
  "f": "6"
}