#json #colored #color #colour #coloured

colored_json

Colorize JSON, for printing it out on the command line

4 releases

0.2.2 Nov 28, 2018
0.2.1 Nov 28, 2018
0.2.0 Nov 28, 2018
0.1.0 Nov 28, 2018

#7 in #colour

Download history 23/week @ 2018-11-28

7 downloads per month
Used in 2 crates

EPL-2.0 license

31KB
765 lines


lib.rs:

colored_json crate to output colored serde json with ANSI terminal escape codes

Note for Windows 10 users: On Windows 10, the application must enable ANSI support first:

# extern crate colored_json;
#[cfg(windows)]
let enabled = colored_json::enable_ansi_support();

Examples

For everything, which implements AsRef<str>

# extern crate serde_json;
extern crate colored_json;
use colored_json::prelude::*;

# fn main() -> ::std::result::Result<(), Box<::std::error::Error>> {
println!(
"{}",
r#"{
"array": [
"ele1",
"ele2"
],
"float": 3.1415926,
"integer": 4398798674962568,
"string": "string"
}
"#.to_colored_json_auto()?
);
# Ok(())
# }

or for serde_json::Value

# extern crate serde_json;
# extern crate colored_json;
use serde_json::{json, Value};
use colored_json::to_colored_json_auto;

# fn main() -> ::std::result::Result<(), Box<::std::error::Error>> {
let val : Value = json!({
"name": "John Doe",
"age": 43,
"phones": [
"+44 1234567",
"+44 2345678"
]
});
let s = to_colored_json_auto(&val)?;
println!("{}", s);
# Ok(())
# }

With a custom color style:

# extern crate serde_json;
extern crate colored_json;
use colored_json::prelude::*;
use colored_json::{Color, Styler};

# fn main() -> ::std::result::Result<(), Box<::std::error::Error>> {
println!(
"{}",
r#"{
"array": [
"ele1",
"ele2"
],
"float": 3.1415926,
"integer": 4398798674962568,
"string": "string"
}
"#.to_colored_json_with_styler(
ColorMode::default().eval(),
Styler {
key: Color::Green.normal(),
string_value: Color::Blue.bold(),
integer_value: Color::Purple.bold(),
float_value: Color::Purple.italic(),
object_brackets: Color::Yellow.bold(),
array_brackets: Color::Cyan.bold(),
..Default::default()
})?
);
Ok(())
# }
# extern crate serde_json;
# extern crate colored_json;

use serde_json::json;

use colored_json::{ColoredFormatter, CompactFormatter, Color, Styler, Style};

# fn main() -> ::std::result::Result<(), Box<::std::error::Error>> {
let f = ColoredFormatter::with_styler(
CompactFormatter {},
Styler {
key: Color::Green.normal(),
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"
}))?
);
# Ok(())
# }

!

Dependencies

~1.5MB
~20K SLoC