prettyjson

package module
v0.0.0-...-d39febd Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2019 License: MIT Imports: 6 Imported by: 0

README

prettyjson

JSON pretty print for Golang.

Example

v := map[string]interface{}{
    "str": "foo",
    "num": 100,
    "bool": false,
    "null": nil,
    "array": []string{"foo", "bar", "baz"},
    "map": map[string]interface{}{
        "foo": "bar",
    },
}
s, _ := prettyjson.Marshal(v)
fmt.Println(string(s))

Output

License

MIT

Documentation

Overview

Package prettyjson provides JSON pretty print.

Example
package main

import (
	"fmt"

	prettyjson "github.com/thycotic-rd/go-prettyjson"
)

func main() {
	v := map[string]interface{}{
		"str":   "foo",
		"num":   100,
		"bool":  false,
		"null":  nil,
		"array": []string{"foo", "bar", "baz"},
		"map": map[string]interface{}{
			"foo": "bar",
		},
	}
	s, _ := prettyjson.Marshal(v)
	fmt.Println(string(s))
}
Output:

{
  �[34;1m"array"�[0m: [
    �[32;1m"foo"�[0m,
    �[32;1m"bar"�[0m,
    �[32;1m"baz"�[0m
  ],
  �[34;1m"bool"�[0m: �[33;1mfalse�[0m,
  �[34;1m"map"�[0m: {
    �[34;1m"foo"�[0m: �[32;1m"bar"�[0m
  },
  �[34;1m"null"�[0m: �[30;1mnull�[0m,
  �[34;1m"num"�[0m: �[36;1m100�[0m,
  �[34;1m"str"�[0m: �[32;1m"foo"�[0m
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Format

func Format(data []byte) ([]byte, error)

Format JSON string with default options.

func Marshal

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

Marshal JSON data with default options.

Types

type Formatter

type Formatter struct {
	// JSON key color. Default is `color.New(color.FgBlue, color.Bold)`.
	KeyColor *color.Color

	// JSON string value color. Default is `color.New(color.FgGreen, color.Bold)`.
	StringColor *color.Color

	// JSON boolean value color. Default is `color.New(color.FgYellow, color.Bold)`.
	BoolColor *color.Color

	// JSON number value color. Default is `color.New(color.FgCyan, color.Bold)`.
	NumberColor *color.Color

	// JSON null value color. Default is `color.New(color.FgBlack, color.Bold)`.
	NullColor *color.Color

	// Max length of JSON string value. When the value is 1 and over, string is truncated to length of the value. Default is 0 (not truncated).
	StringMaxLength int

	// Boolean to disable color. Default is false.
	DisabledColor bool

	// Indent space number. Default is 2.
	Indent int

	// JsonMarshalFunc is the base json marshal func. Default is json.Marshal
	JsonMarshalFunc func(v interface{}) ([]byte, error)
}

Formatter is a struct to format JSON data. `color` is github.com/fatih/color: https://github.com/fatih/color

func NewFormatter

func NewFormatter() *Formatter

NewFormatter returns a new formatter with following default values.

func (*Formatter) Format

func (f *Formatter) Format(data []byte) ([]byte, error)

Formats JSON string.

func (*Formatter) Marshal

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

Marshals and formats JSON data.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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