GitHub - nwidger/jsoncolor: Colorized JSON output for Go https://godoc.org/github.com/nwidger/jsoncolor (original) (raw)

jsoncolor

GoDoc

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

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 orNewEncoderWithFormatter. If a XXXColor field of the customFormatter is not set, the corresponding DefaultXXXColor package variable is used. Seecolor.New for creating custom color values and theGoDocsfor 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))