Categorygithub.com/nwidger/jsoncolor
modulepackage
0.3.2
Repository: https://github.com/nwidger/jsoncolor.git
Documentation: pkg.go.dev

# README

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))

# Functions

Marshal is like encoding/json's Marshal but colorizes the output using DefaultFormatter.
MarshalIndent is like encoding/json's MarshalIndent but colorizes the output using DefaultFormatter.
MarshalIndentWithFormatter is like MarshalIndent but using the Formatter f.
MarshalWithFormatter is like Marshal but using the Formatter f.
NewEncoder is like encoding/json's NewEncoder but returns an encoder that writes colorized output to w using DefaultFormatter.
NewEncoderFormatter is like NewEncoder but using the Formatter f.
NewFormatter returns a new formatter.

# Variables

DefaultArrayColor is the default color for the array delimiter characters '[' and ']'.
DefaultColonColor is the default color the colon character ':' separating object field names and values.
DefaultCommaColor is the default color for the comma character ',' delimiting object and array fields.
DefaultFalseColor is the default color for 'false' boolean values.
DefaultFieldColor is the default color for object field names.
DefaultFieldQuoteColor is the default color for quotes '"' surrounding object field names.
DefaultFormatter is the Formatter used by Marshal, MarshalIndent and NewEncoder.
By default, an indentation of two spaces is used.
DefaultNullColor is the default color for null values.
DefaultNumberColor is the default color for number values.
DefaultObjectColor is the default color for the object delimiter characters '{' and '}'.
By default, no prefix is used.
DefaultSpaceColor is the default color for whitespace characters.
DefaultStringColor is the default color for string values.
DefaultStringQuoteColor is the default color for quotes '"' surrounding string values.
DefaultTrueColor is the default color for 'true' boolean values.

# Structs

Encoder is like encoding/json's Encoder but colorizes the output written to the stream using a Formatter.
Formatter colorizes buffers containing JSON.

# Interfaces

SprintfFuncer is implemented by any value that has a SprintfFunc method.