jsoncolor

package module
v0.3.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 21, 2023 License: MIT Imports: 6 Imported by: 651

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

Documentation

Overview

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

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultSpaceColor is the default color for whitespace
	// characters.
	DefaultSpaceColor = color.New()
	// DefaultCommaColor is the default color for the comma
	// character ',' delimiting object and array fields.
	DefaultCommaColor = color.New(color.Bold)
	// DefaultColonColor is the default color the colon character
	// ':' separating object field names and values.
	DefaultColonColor = color.New(color.Bold)
	// DefaultObjectColor is the default color for the object
	// delimiter characters '{' and '}'.
	DefaultObjectColor = color.New(color.Bold)
	// DefaultArrayColor is the default color for the array
	// delimiter characters '[' and ']'.
	DefaultArrayColor = color.New(color.Bold)
	// DefaultFieldQuoteColor is the default color for quotes '"'
	// surrounding object field names.
	DefaultFieldQuoteColor = color.New(color.FgBlue, color.Bold)
	// DefaultFieldColor is the default color for object field
	// names.
	DefaultFieldColor = color.New(color.FgBlue, color.Bold)
	// DefaultStringQuoteColor is the default color for quotes '"'
	// surrounding string values.
	DefaultStringQuoteColor = color.New(color.FgGreen)
	// DefaultStringColor is the default color for string values.
	DefaultStringColor = color.New(color.FgGreen)
	// DefaultTrueColor is the default color for 'true' boolean
	// values.
	DefaultTrueColor = color.New()
	// DefaultFalseColor is the default color for 'false' boolean
	// values.
	DefaultFalseColor = color.New()
	// DefaultNumberColor is the default color for number values.
	DefaultNumberColor = color.New()
	// DefaultNullColor is the default color for null values.
	DefaultNullColor = color.New(color.FgBlack, color.Bold)

	// By default, no prefix is used.
	DefaultPrefix = ""
	// By default, an indentation of two spaces is used.
	DefaultIndent = "  "
)
View Source
var DefaultFormatter = &Formatter{}

DefaultFormatter is the Formatter used by Marshal, MarshalIndent and NewEncoder.

Functions

func Marshal

func Marshal(v interface{}) ([]byte, error)

Marshal is like encoding/json's Marshal but colorizes the output using DefaultFormatter.

func MarshalIndent

func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error)

MarshalIndent is like encoding/json's MarshalIndent but colorizes the output using DefaultFormatter.

func MarshalIndentWithFormatter added in v0.3.0

func MarshalIndentWithFormatter(v interface{}, prefix, indent string, f *Formatter) ([]byte, error)

MarshalIndentWithFormatter is like MarshalIndent but using the Formatter f. MarshalIndentWithFormatter's prefix and indent arguments override f's Prefix and Indent fields, which are therefore ignored. MarshalIndentWithFormatter replaces problematic characters and therefore ignores f's EscapeHTML field. This replacement can be disabled when using an Encoder, by calling SetEscapeHTML(false).

func MarshalWithFormatter added in v0.3.0

func MarshalWithFormatter(v interface{}, f *Formatter) ([]byte, error)

MarshalWithFormatter is like Marshal but using the Formatter f. MarshalWithFormatter does not indent its output and thus ignores the values of f's Prefix and Indent fields. MarshalWithFormatter replaces problematic characters and thus ignores the value of f's EscapeHTML field. This replacement can be disabled when using an Encoder, by calling SetEscapeHTML(false).

Types

type Encoder added in v0.2.0

type Encoder struct {
	// contains filtered or unexported fields
}

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

func NewEncoder added in v0.2.0

func NewEncoder(w io.Writer) *Encoder

NewEncoder is like encoding/json's NewEncoder but returns an encoder that writes colorized output to w using DefaultFormatter.

func NewEncoderWithFormatter added in v0.3.0

func NewEncoderWithFormatter(w io.Writer, f *Formatter) *Encoder

NewEncoderFormatter is like NewEncoder but using the Formatter f. Note that the value of f's EscapeHTML field is ignored, callers wishing to disable the default behavior of escaping HTML should call SetEscapeHTML(false) after creating the encoder.

func (*Encoder) Encode added in v0.2.0

func (enc *Encoder) Encode(v interface{}) error

Encode is like encoding/json's Encoder.Encode but writes a colorized JSON encoding of v to the stream.

func (*Encoder) SetEscapeHTML added in v0.2.0

func (enc *Encoder) SetEscapeHTML(on bool)

SetIndent is like encoding/json's Encoder.SetEscapeHTML.

func (*Encoder) SetIndent added in v0.2.0

func (enc *Encoder) SetIndent(prefix, indent string)

SetIndent is like encoding/json's Encoder.SetIndent.

type Formatter

type Formatter struct {
	// Color for whitespace characters.  If nil, DefaultSpaceColor
	// is used.
	SpaceColor SprintfFuncer
	// Color for the comma character ',' delimiting object and
	// array fields.  If nil, DefaultCommaColor is used.
	CommaColor SprintfFuncer
	// Color for the colon character ':' separating object field
	// names and values.  If nil, DefaultColonColor is used.
	ColonColor SprintfFuncer
	// Color for object delimiter characters '{' and '}'.  If nil,
	// DefaultObjectColor is used.
	ObjectColor SprintfFuncer
	// Color for array delimiter characters '[' and ']'.  If nil,
	// DefaultArrayColor is used.
	ArrayColor SprintfFuncer
	// Color for quotes '"' surrounding object field names.  If
	// nil, DefaultFieldQuoteColor is used.
	FieldQuoteColor SprintfFuncer
	// Color for object field names.  If nil, DefaultFieldColor is
	// used.
	FieldColor SprintfFuncer
	// Color for quotes '"' surrounding string values.  If nil,
	// DefaultStringQuoteColor is used.
	StringQuoteColor SprintfFuncer
	// Color for string values.  If nil, DefaultStringColor is
	// used.
	StringColor SprintfFuncer
	// Color for 'true' boolean values.  If nil, DefaultTrueColor
	// is used.
	TrueColor SprintfFuncer
	// Color for 'false' boolean values.  If nil,
	// DefaultFalseColor is used.
	FalseColor SprintfFuncer
	// Color for number values.  If nil, DefaultNumberColor is
	// used.
	NumberColor SprintfFuncer
	// Color for null values.  If nil, DefaultNullColor is used.
	NullColor SprintfFuncer

	// Prefix is prepended before indentation to newlines.
	Prefix string
	// Indent is prepended to newlines one or more times according
	// to indentation nesting.
	Indent string

	// EscapeHTML specifies whether problematic HTML characters
	// should be escaped inside JSON quoted strings.  See
	// json.Encoder.SetEscapeHTML's comment for more details.
	EscapeHTML bool
}

Formatter colorizes buffers containing JSON.

func NewFormatter

func NewFormatter() *Formatter

NewFormatter returns a new formatter.

func (*Formatter) Format

func (f *Formatter) Format(dst io.Writer, src []byte) error

Format appends to dst a colorized form of the JSON-encoded src.

type SprintfFuncer

type SprintfFuncer interface {
	// SprintfFunc returns a new function that returns colorized
	// strings for the given arguments with fmt.Sprintf().
	SprintfFunc() func(format string, a ...interface{}) string
}

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL