Crates.io | json-highlight-writer |
lib.rs | json-highlight-writer |
version | 1.1.0 |
source | src |
created_at | 2019-05-02 10:40:13.59843 |
updated_at | 2019-05-02 20:55:54.809632 |
description | json-highlight-writer is a writer for json-rust object which supports highlighting slices when printing JSON objects |
homepage | https://github.com/gmmorris/json-highlight-writer |
repository | https://github.com/gmmorris/json-highlight-writer |
max_upload_size | |
id | 131522 |
size | 128,746 |
A writer for json-rust object which supports highlighting slices when printing JSON objects
There are two public functions:
highlight takes a JSON object and a vector of slices you wish to highlight.
use colored::*;
use json::*;
use json_highlight_writer::{highlight_with_colors, highlight};
let res = object!{
"code" => 200,
"success" => true,
"payload" => object!{
"features" => array![
"awesome",
"easyAPI",
"lowLearningCurve"
]
}
};
println!("{:#}", highlight(&res, vec![&res["code"], &res["payload"]["features"]]));
This code will print out the entire JSON structure with the code field and array of features highlighted in red.
highlight_with_colors takes a JSON object, a vector of slices you wish to highlight and a vector of colors to cycle through when matching slices.
println!("{:#}", highlight_with_colors(&res, vec![&res["code"], &res["payload"]["features"]], Some(vec![Color::Red, Color::Green]), None));
println!("{:#}", highlight_with_colors(&res, vec![&res["payload"], &res["payload"]["features"]], Some(vec![Color::Red, Color::Green]), None));
This code will print out the entire JSON structure twice, with the slices highlighted in red, the green. Note the inner color highlighting which is used to display matching slices in different colors if a matched slice resides inside of another matched slice.
If there are more slices than there are specified colors the highlighting will cycle back through the vector.
println!("{:#}", highlight_with_colors(&res, vec![&res["code"], &res["payload"], &res["payload"]["features"]], Some(vec![Color::Red, Color::Green]), None));
highlight_with_colors can also take a color for the remainder, that is, the parts of the JSON that don't overlap with the slice.
println!("{:#}", highlight_with_colors(&res, vec![&res["code"], &res["payload"], &res["payload"]["features"]], Some(vec![Color::Red, Color::Green]), Some(Color::White)));