• Stars
    star
    20
  • Rank 1,121,818 (Top 23 %)
  • Language
    Go
  • License
    MIT License
  • Created over 8 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Colorized JSON output for Go https://godoc.org/github.com/nwidger/jsoncolor

jsoncolor

GoDoc

jsoncolor is a drop-in replacement for encoding/json's Marshal and MarshalIndent functions and Encoder type which produce colorized output using fatih's color package.

Installation

go get -u github.com/nwidger/jsoncolor

Usage

To use as a replacement for encoding/json, exchange

import "encoding/json" with import json "github.com/nwidger/jsoncolor".

json.Marshal, json.MarshalIndent and json.NewEncoder will now produce colorized output.

Custom Colors

The colors used for each type of token can be customized by creating a custom Formatter, changing its XXXColor fields and then passing it to MarshalWithFormatter, MarshalIndentWithFormatter or NewEncoderWithFormatter. If a XXXColor field of the custom Formatter is not set, the corresponding DefaultXXXColor package variable is used. See color.New for creating custom color values and the GoDocs for the default colors.

import (
        "fmt"
        "log"

        "github.com/fatih/color"
        json "github.com/nwidger/jsoncolor"
)

// create custom formatter
f := json.NewFormatter()

// set custom colors
f.StringColor = color.New(color.FgBlack, color.Bold)
f.TrueColor = color.New(color.FgWhite, color.Bold)
f.FalseColor = color.New(color.FgRed)
f.NumberColor = color.New(color.FgWhite)
f.NullColor = color.New(color.FgWhite, color.Bold)

// marshal v with custom formatter,
// dst contains colorized output
dst, err := json.MarshalWithFormatter(v, f)
if err != nil {
        log.Fatal(err)
}

// print colorized output to stdout
fmt.Println(string(dst))