• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

LICENSEH A D15-Feb-20171 KiB

README.mdH A D15-Feb-20172 KiB

jsoncolor.goH A D15-Feb-201710.2 KiB

README.md

1jsoncolor
2=========
3
4[![GoDoc](https://godoc.org/github.com/nwidger/jsoncolor?status.svg)](https://godoc.org/github.com/nwidger/jsoncolor)
5
6`jsoncolor` is a drop-in replacement for `encoding/json`'s `Marshal`
7and `MarshalIndent` functions which produce colorized output using
8fatih's [color](https://github.com/fatih/color) package.
9
10## Installation
11
12```
13go get -u github.com/nwidger/jsoncolor
14```
15
16## Usage
17
18To use as a replacement for `encoding/json`, exchange
19
20`import "encoding/json"` with `import json "github.com/nwidger/jsoncolor"`.
21
22`json.Marshal` and `json.MarshalIndent` will now produce colorized
23output.
24
25## Custom Colors
26
27The colors used for each type of token can be customized by creating a
28custom `Formatter` and changing its `XXXColor` fields.  See
29[color.New](https://godoc.org/github.com/fatih/color#New) for creating
30custom color values and the
31[GoDocs](https://godoc.org/github.com/nwidger/jsoncolor#pkg-variables)
32for the default colors.
33
34``` go
35import (
36        "bytes"
37		"encoding/json"
38		"fmt"
39		"log"
40
41        "github.com/fatih/color"
42        "github.com/nwidger/jsoncolor"
43)
44
45// marshal v into src using encoding/json
46src, err := json.Marshal(v)
47if err != nil {
48        log.Fatal(err)
49}
50
51// create custom formatter,
52f := jsoncolor.NewFormatter()
53
54// set custom colors
55f.SpaceColor = color.New(color.FgRed, color.Bold)
56f.CommaColor = color.New(color.FgWhite, color.Bold)
57f.ColonColor = color.New(color.FgBlue)
58f.ObjectColor = color.New(color.FgBlue, color.Bold)
59f.ArrayColor = color.New(color.FgWhite)
60f.FieldColor = color.New(color.FgGreen)
61f.StringColor = color.New(color.FgBlack, color.Bold)
62f.TrueColor = color.New(color.FgWhite, color.Bold)
63f.FalseColor = color.New(color.FgRed)
64f.NumberColor = color.New(color.FgWhite)
65f.NullColor = color.New(color.FgWhite, color.Bold)
66
67// colorized output is written to dst
68dst := &bytes.Buffer{}
69err := f.Format(dst, src)
70if err != nil {
71        log.Fatal(err)
72}
73
74// print colorized output to stdout
75fmt.Println(dst.String())
76```
77