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>

    extern crate colored_json;
    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:

    extern crate colored_json;
    use colored_json::prelude::*;
    use colored_json::{Color, 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.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(())

    use serde_json::json;

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

    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"
        }))?
    );

!

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.
A style is a collection of properties that can format a string using ANSI escape codes.
Styler lets you define the look of the colored json output

Enums

A colour is one specific type of ANSI escape code, and can refer to either the foreground or background colour.
ColorMode is a switch to enforce color mode, turn it off or auto-detect, if it should be used
A colour is one specific type of ANSI escape code, and can refer to either the foreground or background colour.
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

Enables ANSI code support on Windows 10.
Serialize the given data structure as a pretty-color-printed String of JSON.
Serialize the given data structure as pretty-color-printed JSON into the IO stream.