Back to godoc.org
github.com/nwidger/jsoncolor

Package jsoncolor

v0.3.0
Latest Go to latest

The highest tagged major version is .

Published: Feb 15, 2020 | License: MIT | Module: github.com/nwidger/jsoncolor

Overview

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

Index

Variables

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 = "  "
)
var DefaultFormatter = &Formatter{}

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

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

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

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

type Encoder

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

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

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

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

func (enc *Encoder) SetEscapeHTML(on bool)

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

func (*Encoder) SetIndent

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.

Package Files

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier