projson

package module
v0.0.0-...-3503bd2 Latest Latest
Warning

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

Go to latest
Published: May 10, 2021 License: BSD-3-Clause Imports: 8 Imported by: 5

README

go-projson

Procedural JSON printer for Go with pretty and compact output formatting.

projson sample output

Installation

$ go get github.com/hayamiz/go-projson

Basic Usage

Basic usage of go-projson is:

  1. Create JsonPrinter object by calling projson.NewPrinter() function.
  2. Put JSON elements (int, float, string, object, array) one by one with following APIs:
  • PutInt, PutFloat, PutString ... functions for putting JSON primitive data.
  • BeginArray, FinishArray ... functions for putting arrays. Elements of an array are constructed by projson API calls between corresponding BeginArray and FinishArray.
  • BeginObject, FinishObject ... functions for putting objects. Members of an object are constructed by projson API calls between corresponding BeginObject and FinishObject, and each member must be keyed by a preceding PutKey API call.
  1. Get JSON output string with String function

Example 1: basic usage

package main

import (
    projson "github.com/hayamiz/go-projson"
)

func main() {
    printer := projson.NewPrinter()

    // Building JSON output by calling Put*/Begin*/Finish* API functions
    printer.BeginObject()

    printer.PutKey("key1")
    printer.PutInt(12345)  // => "key1":12345

    printer.PutKey("key2")
    printer.BeginArray()
    printer.PutInt(12)
    printer.PutFloat(345.67)
    printer.PutString("hello, go-projson")
    printer.FinishArray()  // => "key2":[12, 345.67, "hello, go-projson"]

    printer.FinishObject()

    if str, err := printer.String(); err != nil {
        panic(err)
    } else {
        fmt.Println(str) // prints {"key1":12345,"key2":"key2":[12,345.67,"hello, go-projson"]}
    }
}

Example 2: just print int, float, or string

    printer := projson.NewPrinter()
    printer.PutInt(42)
    str, _ := printer.String() // => 42
    printer := projson.NewPrinter()
    printer.PutFloat(123.45)

    str, _ := printer.String() // => 123.45
    printer := projson.NewPrinter()
    printer.PutString("hello, projson")

    str, _ := printer.String() // => "hello, projson"

Example 3: nested array and nested object

    printer := projson.NewPrinter()

    printer.BeginArray()
      printer.PutInt(123)
      printer.BeginArray()
        printer.PutFloat(456.7)
        printer.BeginArray()
          printer.PutString("nest, nest, and nest")
        printer.FinishArray()
        printer.PutString("nest depth = two here")
      printer.FinishArray()
    printer.FinishArray()

    str, _ := printer.String() // => [123,[456.7,["nest, nest and nest"],"nest depth = two here"]]
    printer := projson.NewPrinter()

    printer.BeginObject()
      printer.PutKey("key1")
      printer.BeginObject()
        printer.PutKey("nested key1")
        printer.PutInt(12345)

        printer.PutKey("nested key2")
        printer.BeginObject()
          printer.PutKey("double nested key1")
          printer.PutFloat(678.9)
        printer.FinishObject()
      printer.FinishObject()
    printer.FinishObject()

    str, _ := printer.String() // => {"key1":{"nested key1":12345,"nested key2":{"double nested key1":678.9}}}

Example 4: formatting styles and coloring

Default formatting
    printer := projson.NewPrinter()
    // build JSON output here ...
    str, _ := printer.String()
    fmt.Println(str)

default output formatting

SmartStyle formatting
    printer := projson.NewPrinter()
    printer.SetStyle(projson.SmartStyle)
    // build JSON output here ...
    str, _ := printer.String()
    fmt.Println(str)

SmartStyle output formatting

Colored SmartStyle formatting
    printer := projson.NewPrinter()
    printer.SetStyle(projson.SmartStyle)
    printer.SetColor(true)
    // build JSON output here ...
    str, _ := printer.String()
    fmt.Println(str)

Colored SmartStyle output formatting

License

MIT license

Author

Yuto Hayamizu (hayamiz)

Documentation

Index

Constants

View Source
const (
	SimpleStyle int = iota
	SmartStyle
	PrettyStyle
)

Variables

This section is empty.

Functions

This section is empty.

Types

type JsonPrinter

type JsonPrinter struct {
	// contains filtered or unexported fields
}

func NewPrinter

func NewPrinter() *JsonPrinter

func (*JsonPrinter) BeginArray

func (printer *JsonPrinter) BeginArray() error

func (*JsonPrinter) BeginObject

func (printer *JsonPrinter) BeginObject() error

func (*JsonPrinter) Error

func (printer *JsonPrinter) Error() error

func (*JsonPrinter) FinishArray

func (printer *JsonPrinter) FinishArray() error

func (*JsonPrinter) FinishObject

func (printer *JsonPrinter) FinishObject() error

func (*JsonPrinter) PutArray

func (printer *JsonPrinter) PutArray(arr []interface{}) error

func (*JsonPrinter) PutFloat

func (printer *JsonPrinter) PutFloat(v float64) error

func (*JsonPrinter) PutFloatFmt

func (printer *JsonPrinter) PutFloatFmt(v float64, fmtstr string) error

func (*JsonPrinter) PutInt

func (printer *JsonPrinter) PutInt(v int) error

func (*JsonPrinter) PutInt64

func (printer *JsonPrinter) PutInt64(v int64) error

func (*JsonPrinter) PutKey

func (printer *JsonPrinter) PutKey(v string) error

func (*JsonPrinter) PutObject

func (printer *JsonPrinter) PutObject(m map[string]interface{}) error

func (*JsonPrinter) PutString

func (printer *JsonPrinter) PutString(v string) error

func (*JsonPrinter) Reset

func (printer *JsonPrinter) Reset()

func (*JsonPrinter) SetColor

func (printer *JsonPrinter) SetColor(color bool) error

func (*JsonPrinter) SetStyle

func (printer *JsonPrinter) SetStyle(style int) error

func (*JsonPrinter) SetTermWidth

func (printer *JsonPrinter) SetTermWidth(termwid int) error

func (*JsonPrinter) String

func (printer *JsonPrinter) String() (string, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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