Crate colored_json

source ·
Expand description

colored_json crate to output colored serde json with ANSI terminal escape codes

Examples

For everything, which implements AsRef

    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!(
        "\n{}",
        r#"{
              "array": [
                "ele1",
                "ele2"
              ],
              "float": 3.1415926,
              "integer": 4398798674962568,
              "string": "string"
           }
    "#.to_colored_json_with_styler(
        ColorMode::Auto,
        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 serde_json::ser::CompactFormatter;

    use colored_json::{ColoredFormatter, 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

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

Enums

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

Traits

Functions

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.