colorjson

package module
Version: v0.0.0-...-afc6888 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2019 License: MIT Imports: 7 Imported by: 0

README

ColorJSON: The Fast Color JSON Marshaller for Go

This is a fork of https://github.com/TylerBrock/colorjson that handles the Number type from encoding/json as per this issue

ColorJSON Output What is this?

This package is based heavily on hokaccha/go-prettyjson but has some noticible differences:

  • Over twice as fast (recursive descent serialization uses buffer instead of string concatenation)
    BenchmarkColorJSONMarshall-4     500000      2498 ns/op
    BenchmarkPrettyJSON-4            200000      6145 ns/op
    
  • more customizable (ability to have zero indent, print raw json strings, etc...)
  • better defaults (less bold colors)

ColorJSON was built in order to produce fast beautiful colorized JSON output for Saw.

Installation

go get -u github.com/TylerBrock/colorjson

Usage

Setup

import "github.com/TylerBrock/colorjson"

str := `{
  "str": "foo",
  "num": 100,
  "bool": false,
  "null": null,
  "array": ["foo", "bar", "baz"],
  "obj": { "a": 1, "b": 2 }
}`

// Create an intersting JSON object to marshal in a pretty format
var obj map[string]interface{}
json.Unmarshal([]byte(str), &obj)

Vanilla Usage

s, _ := colorjson.Marshal(obj)
fmt.Println(string(s))

Customization (Custom Indent)

f := colorjson.NewFormatter()
f.Indent = 2

s, _ := f.Marshal(v)
fmt.Println(string(s))

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Marshal

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

Marshal JSON data with default options

Types

type Formatter

type Formatter struct {
	KeyColor        *color.Color
	StringColor     *color.Color
	BoolColor       *color.Color
	NumberColor     *color.Color
	NullColor       *color.Color
	StringMaxLength int
	Indent          int
	DisabledColor   bool
	RawStrings      bool
}

func NewFormatter

func NewFormatter() *Formatter

func (*Formatter) Marshal

func (f *Formatter) Marshal(jsonObj interface{}) ([]byte, error)

Source Files

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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