jsonpainter

package module
v0.0.0-...-63e2ac2 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2024 License: MIT Imports: 0 Imported by: 1

README

Fuzzy JSON highlighting

build codecov go reference play.golang.org

Description

In fact, it doesn't perform full JSON parsing. It consider spaces, quoted strings, brackets (including brackets balance), colons (in context), commas... In addition, it emphasizes quoted strings right before colons and mark them as keys.

Thanks to this, it can treat semi-JSON strings like this:

12:00:00 INFO Request: {"msg": "hellow"}, Response: {"ok": true}

Demo

JSON painter demo

You can find code of this demo in demo brunch.

You can also play with library online. However, this online sandbox doesn't allow to display colored text. Instead of painted text it you will show you control sequences (ANSI escape codes) as is.

Examples

fmt.Println(paintjson.String(`{"x":12}`))

Install

go get github.com/michurin/jsonpainter

Todo

  • The library gets to have too much heuristics. It has to be rethinking in terms of full-fledged parser.
  • Streaming: obtain io.Reader
  • CLI tool

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	Yellow    = Color("\033[33;1m")
	Brown     = Color("\033[33m")
	Red       = Color("\033[31;1m")
	Darkred   = Color("\033[31m")
	Pink      = Color("\033[35;1m")
	Darkpink  = Color("\033[35m")
	Blue      = Color("\033[34;1m")
	Darkblue  = Color("\033[34m")
	Green     = Color("\033[32;1m")
	Darkgreen = Color("\033[32m")
	Cyan      = Color("\033[36;1m")
	Darkcyan  = Color("\033[36m")
	White     = Color("\033[37;1m")
	Black     = Color("\033[30m")
	Lightgray = Color("\033[37m")
	Darkgray  = Color("\033[30;1m")
	None      = Color(nil)
	Off       = Color("\033[0m")
)

Functions

func String

func String(s string, opts ...Option) string
Example (Simplest)
package main

import (
	"fmt"

	"github.com/michurin/jsonpainter"
)

func main() {
	// Example of log line with mixture JSON and non-JSON parts
	logline := `Req: {"rc": 1}`
	// As far as go examples doesn't support escape control sequences, it has to be used %q here.
	// In real application this line might look like fmt.Println(jsonpainter.String(logline))
	fmt.Printf("%q", jsonpainter.String(logline))
}
Output:

"Req: \x1b[31;1m{\x1b[0m\x1b[33;1m\"rc\"\x1b[0m\x1b[31;1m:\x1b[0m \x1b[36;1m1\x1b[0m\x1b[31;1m}\x1b[0m"
Example (WithOptions)
package main

import (
	"fmt"

	"github.com/michurin/jsonpainter"
)

func main() {
	opts := []jsonpainter.Option{
		jsonpainter.ClrCtl(jsonpainter.None),     // We don't want to colorize JSON punctuation
		jsonpainter.ClrKey(jsonpainter.Cyan),     // Keys will be cyan
		jsonpainter.ClrSpecStr(jsonpainter.None), // Special values (true, false, null...) won't be painted
		jsonpainter.ClrStr(jsonpainter.None),     // Strings won't be painted as well
	}
	logline := `Req: {"rc": 1}`
	fmt.Printf("%q", jsonpainter.String(logline, opts...))
}
Output:

"Req: {\x1b[36;1m\"rc\"\x1b[0m: 1}"

Types

type Color

type Color []byte

type Option

type Option func(fsm *finiteStateMachine)

func ClrCtl

func ClrCtl(clr Color) Option

func ClrKey

func ClrKey(clr Color) Option

func ClrOff

func ClrOff(clr Color) Option

func ClrSpecStr

func ClrSpecStr(clr Color) Option

func ClrStr

func ClrStr(clr Color) Option

Jump to

Keyboard shortcuts

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