| 1 | # Colored JSON output for Rust [](https://github.com/ctron/colored_json) [](https://docs.rs/colored_json/latest/colored_json/) [](https://crates.io/crates/colored_json) |
| 2 | |
| 3 |  |
| 4 | |
| 5 | ## Using |
| 6 | |
| 7 | Add it to your project: |
| 8 | |
| 9 | ~~~toml |
| 10 | [dependencies] |
| 11 | colored_json = "4" |
| 12 | ~~~ |
| 13 | |
| 14 | And then color your JSON output: |
| 15 | |
| 16 | ~~~rust |
| 17 | use colored_json::prelude::*; |
| 18 | |
| 19 | fn main() -> ::std::result::Result<(), Box<::std::error::Error>> { |
| 20 | println!( |
| 21 | "{}", |
| 22 | r#" |
| 23 | { |
| 24 | "array": [ |
| 25 | "ele1", |
| 26 | "ele2" |
| 27 | ], |
| 28 | "float": 3.1415926, |
| 29 | "integer": 4398798674962568, |
| 30 | "string": "string" |
| 31 | } |
| 32 | "#.to_colored_json_auto()? |
| 33 | ); |
| 34 | Ok(()) |
| 35 | } |
| 36 | ~~~ |
| 37 | |
| 38 | Or directly write it out: |
| 39 | |
| 40 | ~~~rust |
| 41 | use serde_json::{from_str, Value}; |
| 42 | use std::io::stdout; |
| 43 | use std::io::Write; |
| 44 | |
| 45 | pub fn main() -> ::std::result::Result<(), Box<::std::error::Error>> { |
| 46 | let value: Value = from_str(r#" |
| 47 | { |
| 48 | "array": [ |
| 49 | "ele1", |
| 50 | "ele2" |
| 51 | ], |
| 52 | "float": 3.1415926, |
| 53 | "integer": 4398798674962568, |
| 54 | "string": "string" |
| 55 | } |
| 56 | "#)?; |
| 57 | let out = stdout(); |
| 58 | { |
| 59 | let mut out = out.lock(); |
| 60 | colored_json::write_colored_json(&value, &mut out)?; |
| 61 | out.flush()?; |
| 62 | } |
| 63 | Ok(()) |
| 64 | } |
| 65 | ~~~ |
| 66 |