碎裂的JSON
FracturedJson

原始链接: https://github.com/j-brooke/FracturedJson/wiki

## FracturedJson:可读性JSON格式化 FracturedJson是一系列工具,旨在格式化JSON数据,以提高人类可读性,同时保持相对紧凑。与传统的压缩或深度缩进JSON不同,FracturedJson智能地使用四种主要格式来构建输出:内联、紧凑多行数组、表格对齐和扩展多行结构。 它优先考虑在可行的情况下使用单行表示对象和数组,并对齐相似的结构(如表格)以便于比较。长数组被分解为每行多个条目。 众多设置允许自定义,但默认设置通常会自动产生出色的结果。 FracturedJson可作为浏览器格式化工具、.NET库、JavaScript/TypeScript包和Visual Studio Code扩展使用,Python选项也可提供。它甚至处理注释(非标准JSON),并尝试将其与相关元素关联。 目标是以一种自然且高效的方式呈现JSON,方便人类阅读。

## FracturedJson:一个新的 JSON 库 一个名为 FracturedJson 的 GitHub 库允许 JSON 文件包含注释和其他非标准元素。该工具在 Hacker News 上分享,引发了关于其潜在用途的讨论。 一些用户认为 TOML 和 YAML 等标准格式更适合人类可读的配置文件(因为可能存在 Git diff 问题),而另一些人则认为 FracturedJson 在调试方面具有价值——尤其是在 API 响应中嵌入注释或游戏开发 JSON 数据等场景中。 一位用户强调了支持注释的好处,批评了最初将注释排除在 JSON 标准之外的理由没有根据。 另一位用户提到希望具有管道输入功能,类似于 `jq` 命令行工具的流行功能。 总的来说,该库被认为是一个“很酷”且可能对特定用例有帮助的工具。
相关文章

原文

FracturedJson is a family of utilities that format JSON data in a way that's easy for humans to read, but fairly compact. Arrays and objects are written on single lines, as long as they're neither too long nor too complex. When several such lines are similar in structure, they're written with fields aligned like a table. Long arrays are written with multiple items per line across multiple lines.

There are lots of settings available to control the output, but usually you can ignore most of them. FracturedJson produces nice-looking output from any set of JSON data automatically.

You can try it out with the browser formatter page. It's also available as a .NET library, a JavaScript/Typescript package, and a Visual Studio Code extension. See here for Python options.

Here's a sample of output using default settings. See the Options page for examples of what you different settings do.

{
    "BasicObject"   : {
        "ModuleId"   : "armor",
        "Name"       : "",
        "Locations"  : [
            [11,  2], [11,  3], [11,  4], [11,  5], [11,  6], [11,  7], [11,  8], [11,  9],
            [11, 10], [11, 11], [11, 12], [11, 13], [11, 14], [ 1, 14], [ 1, 13], [ 1, 12],
            [ 1, 11], [ 1, 10], [ 1,  9], [ 1,  8], [ 1,  7], [ 1,  6], [ 1,  5], [ 1,  4],
            [ 1,  3], [ 1,  2], [ 4,  2], [ 5,  2], [ 6,  2], [ 7,  2], [ 8,  2], [ 8,  3],
            [ 7,  3], [ 6,  3], [ 5,  3], [ 4,  3], [ 0,  4], [ 0,  5], [ 0,  6], [ 0,  7],
            [ 0,  8], [12,  8], [12,  7], [12,  6], [12,  5], [12,  4]
        ],
        "Orientation": "Fore",
        "Seed"       : 272691529
    },
    "SimilarArrays" : {
        "Katherine": ["blue",       "lightblue", "black"       ],
        "Logan"    : ["yellow",     "blue",      "black", "red"],
        "Erik"     : ["red",        "purple"                   ],
        "Jean"     : ["lightgreen", "yellow",    "black"       ]
    },
    "SimilarObjects": [
        { "type": "turret",    "hp": 400, "loc": {"x": 47, "y":  -4}, "flags": "S"   },
        { "type": "assassin",  "hp":  80, "loc": {"x": 12, "y":   6}, "flags": "Q"   },
        { "type": "berserker", "hp": 150, "loc": {"x":  0, "y":   0}                 },
        { "type": "pittrap",              "loc": {"x": 10, "y": -14}, "flags": "S,I" }
    ]
}

Optionally, comments can be preserved. Comments aren't allowed by the official JSON standard, but they're ubiquitous, so it's nice to have the option. FracturedJson tries to keep the comments together with whatever elements they seem to relate to.

Most JSON libraries give you a choice between two formatting options. Minified JSON is very efficient, but difficult for a person to read.

{"AttackPlans":[{"TeamId":1,"Spawns":[{"Time":0.0,"UnitType":"Grunt","SpawnPointIndex":0},{"Time":0.0,"UnitType":"Grunt","SpawnPointIndex":0},{"Time":0.0,"UnitType":"Grunt","SpawnPointIndex":0}]}],"DefensePlans":[{"TeamId":2,"Placements":[{"UnitType":"Archer","Position":[41,7]},{"UnitType":"Pikeman","Position":[40,7]},{"UnitType":"Barricade","Position":[39,7]}]}]}

Most beautified/indented JSON, on the other hand, is too spread out, often making it difficult to take in quickly or to scan for specific information.

{
    "AttackPlans": [
        {
            "TeamId": 1,
            "Spawns": [
                {
                    "Time": 0,
                    "UnitType": "Grunt",
                    "SpawnPointIndex": 0
                },
                {
                    "Time": 0,
                    "UnitType": "Grunt",
                    "SpawnPointIndex": 0
                },
                {
                    "Time": 0,
                    "UnitType": "Grunt",
                    "SpawnPointIndex": 0
                }
            ]
        }
    ],
    "DefensePlans": [
        {
            "TeamId": 2,
            "Placements": [
                {
                    "UnitType": "Archer",
                    "Position": [
                        41,
                        7
                    ]
                },
                {
                    "UnitType": "Pikeman",
                    "Position": [
                        40,
                        7
                    ]
                },
                {
                    "UnitType": "Barricade",
                    "Position": [
                        39,
                        7
                    ]
                }
            ]
        }
    ]
}

FracturedJson tries to format data like a person would. Containers are kept to single lines as long as they're not too complex and not too long. If several successive inline arrays or objects are similar enough, they will be formatted as a table.

{
    "AttackPlans" : [
        {
            "TeamId": 1,
            "Spawns": [
                {"Time": 0, "UnitType": "Grunt", "SpawnPointIndex": 0},
                {"Time": 0, "UnitType": "Grunt", "SpawnPointIndex": 0},
                {"Time": 0, "UnitType": "Grunt", "SpawnPointIndex": 0}
            ]
        }
    ],
    "DefensePlans": [
        {
            "TeamId"    : 2,
            "Placements": [
                { "UnitType": "Archer",    "Position": [41, 7] },
                { "UnitType": "Pikeman",   "Position": [40, 7] },
                { "UnitType": "Barricade", "Position": [39, 7] }
            ]
        }
    ]
}

FracturedJson uses four types of formatting: inlined, compact multiline array, table and expanded.

Layout Types

When possible, sections of the document are written inlined, as long as that doesn't make them too long or too complex (as determined by the settings). "Complexity" refers to how deeply nested an array or object's contents are.

Example:

{ "UnitType": "Archer", "Position": [41, 7] }

Use the setting MaxInlineComplexity to control how much nesting is allowed on one line.

The next option, for arrays, is to write them with multiple items per line, across multiple lines.

Example:

[
    [19,  2], [ 3,  8], [14,  0], [ 9,  9], [ 9,  9], [ 0,  3], [10,  1],
    [ 9,  1], [ 9,  2], [ 6, 13], [18,  5], [ 4, 11], [12,  2]
]

Use the setting MaxCompactArrayComplexity to control how deeply nested items can be when arranged this way, or use -1 to disable this feature.

If an array or object contains inlineable items of the same type, they can be formatted in a tabular format. With enough room, all fields at any depth are lined up (and reordered if necessary).

Example:

{
    "Rect" : { "position": {"x": -44, "y":  3.4         }, "color": [0, 255, 255] },
    "Point": { "position": {          "y": 22   , "z": 3}                         },
    "Oval" : { "position": {"x": 140, "y":  0.04        }, "color": "#7f3e96"     },
    "Plane": { "position": null,                           "color": [0, 64, 64]   }
}

If the table wouldn't fit with all of the elements and subelements aligned, the inner-most containers are collapsed while keeping the outer ones still aligned.

{
    "Rect" : { "position": {"x": -44, "y": 3.4},  "color": [0, 255, 255] },
    "Point": { "position": {"y": 22, "z": 3}                             },
    "Oval" : { "position": {"x": 140, "y": 0.04}, "color": "#7f3e96"     },
    "Plane": { "position": null,                  "color": [0, 64, 64]   }
}

Use the setting MaxTableRowComplexity to control how how deeply nested rows can be, or use -1 to disable this feature. Use TableCommaPlacement to adjust where commas are placed.

If none of those options work for an element, it will be written across multiple lines, with child items indented and starting on its own line.

Example:

[
    {
        "type" : "turret",
        "hp"   : 400,
        "loc"  : {"x": 47, "y": -4},
        "flags": ["stationary"]
    },
    {
        "type" : "assassin",
        "hp"   : 80,
        "loc"  : {"x": 102, "y": 6},
        "flags": ["stealth"]
    },
    { "type": "berserker", "hp": 150, "loc": {"x": 0, "y": 0} },
    {
        "type" : "pittrap",
        "loc"  : {"x": 10, "y": -14},
        "flags": ["invulnerable", "stationary"]
    }
]

If you have any questions, comments, requests, advice, or whatever, feel free to check out the discussions area.

联系我们 contact @ memedata.com