json_pretty_compact/
lib.rs

1// MIT License
2//
3// Copyright (c) 2024 Robin Doer
4//
5// Permission is hereby granted, free of charge, to any person obtaining a copy
6// of this software and associated documentation files (the "Software"), to deal
7// in the Software without restriction, including without limitation the rights
8// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9// copies of the Software, and to permit persons to whom the Software is
10// furnished to do so, subject to the following conditions:
11//
12// The above copyright notice and this permission notice shall be included in all
13// copies or substantial portions of the Software.
14//
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21// SOFTWARE.
22
23//! A pretty formatter for [serde_json].
24//!
25//!
26//! The `json_pretty_compact` crate implements a pretty [serde_json] formatter
27//! which tries to be as compact as possible. This can increase the readability
28//! of formatted JSON. Look at the following comparison.
29//!
30//! * A pure pretty formatted JSON looks like this:
31//!
32//! ```json
33//! [
34//!   {
35//!     "name": {
36//!       "first": "Kobe",
37//!       "middle": "Nico",
38//!       "last": "Grimes"
39//!     },
40//!     "phoneNumber": "1-547-479-5471 x062",
41//!     "username": "Kobe-Grimes",
42//!     "emails": [
43//!       "Melyssa.Cremin4@gmail.com",
44//!       "Jayne.Green37@gmail.com"
45//!     ],
46//!     "coordinates": {
47//!       "latitude": "-66.3821",
48//!       "longitude": "127.117"
49//!     }
50//!   },
51//!   {
52//!     "name": {
53//!       "first": "Adrian",
54//!       "middle": "Finley",
55//!       "last": "Koch"
56//!     },
57//!     "phoneNumber": "1-420-853-5251 x68083",
58//!     "username": "Adrian-Koch",
59//!     "emails": [
60//!       "Andy99@gmail.com",
61//!       "Elenor.Aufderhar96@gmail.com"
62//!     ],
63//!     "coordinates": {
64//!       "latitude": "51.4003",
65//!       "longitude": "3.351"
66//!     }
67//!   }
68//! ]
69//! ```
70//!
71//! * Where the same JSON in a pretty compact format looks like this:
72//!
73//! ```json
74//! [
75//!   {
76//!     "name": { "first": "Kobe", "middle": "Nico", "last": "Grimes" },
77//!     "phoneNumber": "1-547-479-5471 x062",
78//!     "username": "Kobe-Grimes",
79//!     "emails": [ "Melyssa.Cremin4@gmail.com", "Jayne.Green37@gmail.com" ],
80//!     "coordinates": { "latitude": "-66.3821", "longitude": "127.117" }
81//!   },
82//!   {
83//!     "name": { "first": "Adrian", "middle": "Finley", "last": "Koch" },
84//!     "phoneNumber": "1-420-853-5251 x68083",
85//!     "username": "Adrian-Koch",
86//!     "emails": [ "Andy99@gmail.com", "Elenor.Aufderhar96@gmail.com" ],
87//!     "coordinates": { "latitude": "51.4003", "longitude": "3.351" }
88//!   }
89//! ]
90//! ```
91//!
92//! ## Compaction rules
93//!
94//! The formatter tries to put arrays and objects into one line, as long as the
95//! line length is still within its limit. If a line will become too large, the
96//! formatter will change into a pretty format.
97//!
98//! Check the [`PrettyCompactFormatter`] documentation to find out how to
99//! configure the formatter.
100//!
101//! ## Usage
102//!
103//! ```
104//! use json_pretty_compact::PrettyCompactFormatter;
105//! use serde::Serialize;
106//! use serde_json::{Serializer, Value};
107//!
108//! // Create a JSON value.
109//! // In this simple example it contains only the "true" value.
110//! let value: Value = serde_json::from_str("true").unwrap();
111//!
112//! // The buffer where the serialized JSON is written.
113//! let mut target = vec![];
114//!
115//! // Create the formatter.
116//! // It takes all the default values.
117//! let formatter = PrettyCompactFormatter::new();
118//!
119//! // Serialize the JSON value into the `target` buffer.
120//! let mut ser = Serializer::with_formatter(&mut target, formatter);
121//! value.serialize(&mut ser).unwrap();
122//!
123//! assert_eq!(target, b"true");
124//! ```
125//!
126//!
127//! [serde_json]: https://docs.rs/serde_json/latest/serde_json/index.html
128
129mod error;
130mod fmt;
131mod options;
132mod token;
133
134pub use crate::error::Error;
135pub use crate::fmt::PrettyCompactFormatter;