[][src]Crate colored_json

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

prelude

Structs

ColoredFormatter

ColoredFormatter decorates a Formatter with color defined in Styler

CompactFormatter

This structure compacts a JSON value with no extra whitespace.

PrettyFormatter

This structure pretty prints a JSON value to make it human readable.

Style

A style is a collection of properties that can format a string using ANSI escape codes.

Styler

Styler lets you define the look of the colored json output

Enums

Color

A colour is one specific type of ANSI escape code, and can refer to either the foreground or background colour.

ColorMode

ColorMode is a switch to enforce color mode, turn it off or auto-detect, if it should be used

Colour

A colour is one specific type of ANSI escape code, and can refer to either the foreground or background colour.

Output

Specify the output sink, which should be used for the auto detection

Traits

ToColoredJson

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

Functions

to_colored_json
to_colored_json_auto

Serialize the given data structure as a pretty-color-printed String of JSON.

write_colored_json

Serialize the given data structure as pretty-color-printed JSON into the IO stream.

write_colored_json_with_mode