jsoncolor

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2017 License: MIT Imports: 6 Imported by: 651

README

jsoncolor

GoDoc

jsoncolor is a drop-in replacement for encoding/json's Marshal and MarshalIndent functions 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 and json.MarshalIndent will now produce colorized output.

Custom Colors

The colors used for each type of token can be customized by creating a custom Formatter and changing its XXXColor fields. See color.New for creating custom color values and the GoDocs for the default colors.

import (
        "bytes"
		"encoding/json"
		"fmt"
		"log"

        "github.com/fatih/color"
        "github.com/nwidger/jsoncolor"
)

// marshal v into src using encoding/json
src, err := json.Marshal(v)
if err != nil {
        log.Fatal(err)
}

// create custom formatter,
f := jsoncolor.NewFormatter()

// set custom colors
f.SpaceColor = color.New(color.FgRed, color.Bold)
f.CommaColor = color.New(color.FgWhite, color.Bold)
f.ColonColor = color.New(color.FgBlue)
f.ObjectColor = color.New(color.FgBlue, color.Bold)
f.ArrayColor = color.New(color.FgWhite)
f.FieldColor = color.New(color.FgGreen)
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)

// colorized output is written to dst
dst := &bytes.Buffer{}
err := f.Format(dst, src)
if err != nil {
        log.Fatal(err)
}

// print colorized output to stdout
fmt.Println(dst.String())

Documentation

Overview

Package jsoncolor is a replacement for encoding/json's Marshal and MarshalIndent producing colorized output.

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultSpaceColor       = color.New()
	DefaultCommaColor       = color.New(color.Bold)
	DefaultColonColor       = color.New(color.Bold)
	DefaultObjectColor      = color.New(color.Bold)
	DefaultArrayColor       = color.New(color.Bold)
	DefaultFieldQuoteColor  = color.New(color.FgBlue, color.Bold)
	DefaultFieldColor       = color.New(color.FgBlue, color.Bold)
	DefaultStringQuoteColor = color.New(color.FgGreen)
	DefaultStringColor      = color.New(color.FgGreen)
	DefaultTrueColor        = color.New()
	DefaultFalseColor       = color.New()
	DefaultNumberColor      = color.New()
	DefaultNullColor        = color.New(color.FgBlack, color.Bold)

	// By default, no prefix is used.
	DefaultPrefix = ""
	// By default, an indentation of two spaces is used.
	DefaultIndent = "  "
)

Functions

func Marshal

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

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

func MarshalIndent

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

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

Types

type Formatter

type Formatter struct {
	// Color for whitespace characters.
	SpaceColor SprintfFuncer
	// Color for comma character ',' delimiting object and array
	// fields.
	CommaColor SprintfFuncer
	// Color for colon character ':' separating object field names
	// and values.
	ColonColor SprintfFuncer
	// Color for object delimiter characters '{' and '}'.
	ObjectColor SprintfFuncer
	// Color for array delimiter characters '[' and ']'.
	ArrayColor SprintfFuncer
	// Color for quotes '" surrounding object field names.
	FieldQuoteColor SprintfFuncer
	// Color for object field names.
	FieldColor SprintfFuncer
	// Color for quotes '"' surrounding string values.
	StringQuoteColor SprintfFuncer
	// Color for string values.
	StringColor SprintfFuncer
	// Color for 'true' boolean values.
	TrueColor SprintfFuncer
	// Color for 'false' boolean values.
	FalseColor SprintfFuncer
	// Color for number values.
	NumberColor SprintfFuncer
	// Color for null values.
	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() func(format string, a ...interface{}) string
}

Jump to

Keyboard shortcuts

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