fracturedjson/lib.rs
1//! # FracturedJson
2//!
3//! A JSON formatter that produces human-readable output with smart line breaks,
4//! table-like alignment, and optional comment support.
5//!
6//! FracturedJson formats JSON data in a way that's easy for humans to read while
7//! remaining fairly compact:
8//!
9//! - Arrays and objects are written on single lines when they're short and simple enough
10//! - When several lines have similar structure, their fields are aligned like a table
11//! - Long arrays are written with multiple items per line
12//! - Comments (non-standard JSON) can be preserved if enabled
13//!
14//! ## Command-Line Tool
15//!
16//! This crate includes the `fjson` CLI tool for formatting JSON from the terminal:
17//!
18//! ```sh
19//! # Install
20//! cargo install fracturedjson
21//!
22//! # Format JSON from stdin
23//! echo '{"a":1,"b":2}' | fjson
24//!
25//! # Format a file
26//! fjson input.json -o output.json
27//!
28//! # Minify
29//! fjson --compact < input.json
30//! ```
31//!
32//! Run `fjson --help` for all options.
33//!
34//! ## Quick Start
35//!
36//! ```rust
37//! use fracturedjson::Formatter;
38//!
39//! let input = r#"{"name":"Alice","scores":[95,87,92],"active":true}"#;
40//!
41//! let mut formatter = Formatter::new();
42//! let output = formatter.reformat(input, 0).unwrap();
43//!
44//! println!("{}", output);
45//! ```
46//!
47//! ## Serializing Rust Types
48//!
49//! Any type implementing [`serde::Serialize`] can be formatted directly:
50//!
51//! ```rust
52//! use fracturedjson::Formatter;
53//! use serde::Serialize;
54//!
55//! #[derive(Serialize)]
56//! struct Player {
57//! name: String,
58//! scores: Vec<i32>,
59//! }
60//!
61//! let player = Player {
62//! name: "Alice".into(),
63//! scores: vec![95, 87, 92],
64//! };
65//!
66//! let mut formatter = Formatter::new();
67//! let output = formatter.serialize(&player, 0, 100).unwrap();
68//! ```
69//!
70//! ## Configuration
71//!
72//! Customize formatting behavior through [`FracturedJsonOptions`]:
73//!
74//! ```rust
75//! use fracturedjson::{Formatter, EolStyle, NumberListAlignment};
76//!
77//! let mut formatter = Formatter::new();
78//! formatter.options.max_total_line_length = 80;
79//! formatter.options.indent_spaces = 2;
80//! formatter.options.json_eol_style = EolStyle::Lf;
81//! formatter.options.number_list_alignment = NumberListAlignment::Decimal;
82//!
83//! let output = formatter.reformat(r#"{"values":[1,2,3]}"#, 0).unwrap();
84//! ```
85//!
86//! ## Comment Support
87//!
88//! FracturedJson can handle JSON with comments (non-standard) when enabled:
89//!
90//! ```rust
91//! use fracturedjson::{Formatter, CommentPolicy};
92//!
93//! let input = r#"{
94//! // This is a comment
95//! "name": "Alice"
96//! }"#;
97//!
98//! let mut formatter = Formatter::new();
99//! formatter.options.comment_policy = CommentPolicy::Preserve;
100//!
101//! let output = formatter.reformat(input, 0).unwrap();
102//! ```
103//!
104//! ## Example Output
105//!
106//! Given appropriate input, FracturedJson produces output like:
107//!
108//! ```json
109//! {
110//! "SimilarObjects": [
111//! { "type": "turret", "hp": 400, "loc": {"x": 47, "y": -4} },
112//! { "type": "assassin", "hp": 80, "loc": {"x": 12, "y": 6} },
113//! { "type": "berserker", "hp": 150, "loc": {"x": 0, "y": 0} }
114//! ]
115//! }
116//! ```
117//!
118//! Notice how:
119//! - Similar objects are aligned in a table format
120//! - Numbers are right-aligned within their columns
121//! - The structure remains compact while being highly readable
122
123mod buffer;
124mod convert;
125mod error;
126mod formatter;
127mod model;
128mod options;
129mod parser;
130mod table_template;
131mod tokenizer;
132
133pub use crate::error::FracturedJsonError;
134pub use crate::formatter::Formatter;
135pub use crate::model::{InputPosition, JsonItemType};
136pub use crate::options::{
137 CommentPolicy, EolStyle, FracturedJsonOptions, NumberListAlignment, TableCommaPlacement,
138};