fracturedjson
A Rust port of FracturedJson — a JSON formatter that produces human-readable output with smart line breaks, table-like alignment, and optional comment support.
Overview
FracturedJson formats JSON data in a way that's easy for humans to read while remaining fairly compact. Arrays and objects are written on single lines when they're short and simple enough. When several lines have similar structure, their fields are aligned like a table. Long arrays are written with multiple items per line.
This crate is part of the FracturedJson family:
- Home Page and Browser-based Formatter
- FracturedJson (C#)
- FracturedJsonJs (JavaScript/npm)
- VS Code Extension
Installation
Add to your Cargo.toml:
[]
= "0.1"
Usage
Reformat JSON Text
use Formatter;
Serialize Rust Values
use Formatter;
use Serialize;
Serialize serde_json::Value
use Formatter;
use json;
Minify JSON
use Formatter;
Configuration
Customize formatting behavior via FracturedJsonOptions:
use ;
Available Options
| Option | Type | Default | Description |
|---|---|---|---|
max_total_line_length |
usize |
120 | Maximum line length before wrapping |
max_inline_complexity |
isize |
2 | Max nesting depth for inline formatting |
max_compact_array_complexity |
isize |
2 | Max complexity for compact array rows |
max_table_row_complexity |
isize |
2 | Max complexity for table row formatting |
indent_spaces |
usize |
4 | Spaces per indentation level |
use_tab_to_indent |
bool |
false | Use tabs instead of spaces |
json_eol_style |
EolStyle |
Lf |
Line ending style (Lf or Crlf) |
number_list_alignment |
NumberListAlignment |
Decimal |
Number alignment in arrays |
comment_policy |
CommentPolicy |
TreatAsError |
How to handle comments |
preserve_blank_lines |
bool |
false | Keep blank lines from input |
allow_trailing_commas |
bool |
false | Allow trailing commas in input |
Example Output
With comment support enabled (CommentPolicy::Preserve):
{
/*
* Multi-line comments
* are fun!
*/
"NumbersWithHex": [
254 /*00FE*/, 1450 /*5AA*/, 0 /*0000*/, 36000 /*8CA0*/, 10 /*000A*/,
199 /*00C7*/, 15001 /*3A99*/, 6540 /*198C*/
],
/* Elements are keen */
"Elements" : [
{ /*Carbon*/ "Symbol": "C", "Number": 6, "Isotopes": [11, 12, 13, 14] },
{ /*Oxygen*/ "Symbol": "O", "Number": 8, "Isotopes": [16, 18, 17 ] },
{ /*Hydrogen*/ "Symbol": "H", "Number": 1, "Isotopes": [ 1, 2, 3 ] },
{ /*Iron*/ "Symbol": "Fe", "Number": 26, "Isotopes": [56, 54, 57, 58] }
// Not a complete list...
]
}
License
MIT