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 ¶
- Variables
- func Marshal(v interface{}) ([]byte, error)
- func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error)
- func MarshalIndentWithFormatter(v interface{}, prefix, indent string, f *Formatter) ([]byte, error)
- func MarshalWithFormatter(v interface{}, f *Formatter) ([]byte, error)
- type Encoder
- type Formatter
- type SprintfFuncer
Constants ¶
This section is empty.
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.
Functions ¶
func Marshal ¶
Marshal is like encoding/json's Marshal but colorizes the output using DefaultFormatter.
func MarshalIndent ¶
MarshalIndent is like encoding/json's MarshalIndent but colorizes the output using DefaultFormatter.
func MarshalIndentWithFormatter ¶ added in v0.3.0
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
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
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
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
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
SetIndent is like encoding/json's Encoder.SetEscapeHTML.
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.
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.