jsonutil

package module
v0.0.0-...-61d497d Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2022 License: Apache-2.0 Imports: 6 Imported by: 2

README

Fast JSON encoding

See this blog post for the rational and benchmarks.

tldr; when writing an array of objects this library can speed things up. Especially as the number of objects in the array and the number of columns in the object grow.

Writing a whole array at once

If you have a whole large array at once, there is a helper function for writing it all out at once.

package main

import (
	"os"
	
	"github.com/multiprocessio/go-json"
)

func main() {
	// Uses stdlib's encoding/json
	data := []interface{}{
		map[string]interface{}{"a": 1, "b": 2},
		map[string]interface{}{"a": 5, "c": 3, "d": "xyz"},
	}

	out := os.Stdout // Can be any io.Writer
	
	err := jsonutil.Encode(out, data)
	if err != nil {
		panic(err)
	}
}

Streaming encoding an array

If you are streaming data, there is also support for stream encoding with this library:

package main

import (
	"os"
	
	"github.com/multiprocessio/go-json"
)

func main() {
	// Uses stdlib's encoding/json
	data := []interface{}{
		map[string]interface{}{"a": 1, "b": 2},
		map[string]interface{}{"a": 5, "c": 3, "d": "xyz"},
	}

	out := os.Stdout // Can be any io.Writer

	encoder := jsonutil.NewStreamEncoder(out, true)
	for _, row := range data {
		err := encoder.EncodeRow(row)
		if err != nil{
			panic(err)
		}
	}

	err := encoder.Close()
	if err != nil {
		panic(err)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Encode

func Encode(out io.Writer, obj interface{}) error

func EncodeGeneric

func EncodeGeneric(out io.Writer, obj interface{}, marshalFn Marshaler) error

func EncodeStdlib

func EncodeStdlib(out io.Writer, obj interface{}) error

Types

type Marshaler

type Marshaler func(interface{}) ([]byte, error)

type StreamEncoder

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

func NewGenericStreamEncoder

func NewGenericStreamEncoder(w io.Writer, marshalFn Marshaler, array bool) *StreamEncoder

func NewStdlibStreamEncoder

func NewStdlibStreamEncoder(out io.Writer, array bool) *StreamEncoder

func NewStreamEncoder

func NewStreamEncoder(out io.Writer, array bool) *StreamEncoder

func (*StreamEncoder) Close

func (sw *StreamEncoder) Close() error

func (*StreamEncoder) EncodeRow

func (sw *StreamEncoder) EncodeRow(row interface{}) error

Jump to

Keyboard shortcuts

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