Crate colored_json

source ·
Expand description

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:

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

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: Style::new(Color::Green),
            string_value: Style::new(Color::Blue).bold(),
            integer_value: Style::new(Color::Magenta).bold(),
            float_value: Style::new(Color::Magenta).italic(),
            object_brackets: Style::new(Color::Yellow).bold(),
            array_brackets: Style::new(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: Style::new(Color::Green),
            string_value: Style::new(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

  • ColoredFormatter decorates a Formatter with color defined in Styler
  • This structure compacts a JSON value with no extra whitespace.
  • This structure pretty prints a JSON value to make it human readable.
  • Represents a set of styling options.
  • Styler lets you define the look of the colored json output

Enums

  • An enum representing an ANSI color code.
  • ColorMode is a switch to enforce color mode, turn it off or auto-detect, if it should be used
  • Specify the output sink, which should be used for the auto detection

Traits

  • Trait to add json coloring for all AsRef<str> like String and &str

Functions