Skip to content

amterp/jsoncolor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

amterp/jsoncolor

Forked

This repo is a fork of nwidger's jsoncolor library.

This fork exists because the original is no longer maintained, and there's a bug where a space character is omitted after colons for non-collection values.

For example, the original library prints like this:

{
  "foo":"bar", << notice missing space
  "quz": []    << notice *correct* space
}

This fork fixes that bug so the output becomes:

{
  "foo": "bar",
  "quz": []
}

Additionally, this fork also updates Go to 1.24 and switches from fatih/color to amterp/color as the latter similarly contains bug fixes + improvements not yet merged upstream (though I'm hoping this changes).

Thanks to the original contributors for their work! 🙏

Original README

Not quite original, contains updated references to correct libraries, etc.

GoDoc

jsoncolor is a drop-in replacement for encoding/json's Marshal and MarshalIndent functions and Encoder type which produce colorized output using amterp's color package.

Installation

go get -u github.com/amterp/jsoncolor

Usage

To use as a replacement for encoding/json, exchange

import "encoding/json" with import json "github.com/amterp/jsoncolor".

json.Marshal, json.MarshalIndent and json.NewEncoder will now produce colorized output.

Custom Colors

The colors used for each type of token can be customized by creating a custom Formatter, changing its XXXColor fields and then passing it to MarshalWithFormatter, MarshalIndentWithFormatter or NewEncoderWithFormatter. If a XXXColor field of the custom Formatter is not set, the corresponding DefaultXXXColor package variable is used. See color.New for creating custom color values and the GoDocs for the default colors.

import (
        "fmt"
        "log"

        "github.com/amterp/color"
        json "github.com/amterp/jsoncolor"
)

// create custom formatter
f := json.NewFormatter()

// set custom colors
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)

// marshal v with custom formatter,
// dst contains colorized output
dst, err := json.MarshalWithFormatter(v, f)
if err != nil {
        log.Fatal(err)
}

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

About

Go library for colored JSON output.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages