Expand description
colored_json crate to output colored serde json with ANSI terminal escape codes
§Examples
For everything, which implements AsRef<str>
use colored_json::prelude::*;
println!(
"{}",
r#"{
"array": [
"ele1",
"ele2"
],
"float": 3.1415926,
"integer": 4398798674962568,
"string": "string"
}
"#.to_colored_json_auto()?
);
or for serde_json::Value
use serde_json::{json, Value};
use colored_json::to_colored_json_auto;
let val : Value = json!({
"name": "John Doe",
"age": 43,
"phones": [
"+44 1234567",
"+44 2345678"
]
});
let s = to_colored_json_auto(&val)?;
println!("{}", s);
With a custom color style:
use colored_json::prelude::*;
use colored_json::{Color, Style, Styler};
println!(
"{}",
r#"{
"array": [
"ele1",
"ele2"
],
"float": 3.1415926,
"integer": 4398798674962568,
"string": "string"
}
"#.to_colored_json_with_styler(
ColorMode::default().eval(),
Styler {
key: Color::Green.foreground(),
string_value: Color::Blue.bold(),
integer_value: Color::Magenta.bold(),
float_value: Color::Magenta.italic(),
object_brackets: Color::Yellow.bold(),
array_brackets: Color::Cyan.bold(),
..Default::default()
})?
);
Ok(())
use serde_json::json;
use colored_json::{ColoredFormatter, CompactFormatter, Color, Styler, Style};
let f = ColoredFormatter::with_styler(
CompactFormatter {},
Styler {
key: Color::Green.foreground(),
string_value: Color::Blue.bold(),
..Default::default()
},
);
println!(
"{}",
f.clone().to_colored_json_auto(&json!({
"name": "John Doe",
"age": 43,
"phones": [
"+44 1234567",
"+44 2345678"
]
}))?
);
println!(
"{}",
f.to_colored_json_auto(&json!({
"name":"John", "age":31, "city":"New York"
}))?
);
Modules§
Structs§
- Colored
Formatter ColoredFormatter
decorates aFormatter
with color defined inStyler
- Compact
Formatter - This structure compacts a JSON value with no extra whitespace.
- Pretty
Formatter - This structure pretty prints a JSON value to make it human readable.
- Style
- A set of styling options.
- Styler
- Styler lets you define the look of the colored json output
Enums§
- Color
- Enum representing a terminal color.
- Color
Mode - ColorMode is a switch to enforce color mode, turn it off or auto-detect, if it should be used
- Output
- Specify the output sink, which should be used for the auto detection
Traits§
- Paint
- A trait to apply styling to any value. Implemented for all types.
- ToColored
Json - Trait to add json coloring for all
AsRef<str>
likeString
and&str
Functions§
- to_
colored_ json - to_
colored_ json_ auto - Serialize the given data structure as a pretty-color-printed String of JSON.
- write_
colored_ json - Serialize the given data structure as pretty-color-printed JSON into the IO stream.
- write_
colored_ json_ with_ mode