jsonparserHelper

package module
v0.0.0-...-0c0d8cf Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2021 License: BSD-2-Clause Imports: 6 Imported by: 0

README

GitHub issues GitHub forks GitHub stars GitHub license Go Codecov branch Go Report Card Documentation

jsonparserHelper-go

Install

go get -u github.com/Villenny/jsonparserHelper-go

Notable members:

MakeParser, RunParser,

The expected use case:

  • declare your parser somewhere
	import (
		"github.com/buger/jsonparser"
		"github.com/villenny/jsonparserHelper-go"
	)

	type Fubar struct {
		Id       string
		BidId    string
		BidImpId string
		BidPrice float64
		BidAdm   string
		Cur      string
		Foo      string
	}

	var fubarParser = func() []Parser {
		parser := MakeParser(make([]Parser, 32)[:0]).
			Add(UnsafeStringValue(unsafe.Offsetof(Fubar{}.Id)), "id").
			Add(UnsafeStringValue(unsafe.Offsetof(Fubar{}.BidId)), "seatbid", "[0]", "bid", "[0]", "id").
			Add(UnsafeStringValue(unsafe.Offsetof(Fubar{}.BidImpId)), "seatbid", "[0]", "bid", "[0]", "impid").
			Add(Float64Value(unsafe.Offsetof(Fubar{}.BidPrice)), "seatbid", "[0]", "bid", "[0]", "price").
			Add(UnsafeStringValue(unsafe.Offsetof(Fubar{}.BidAdm)), "seatbid", "[0]", "bid", "[0]", "adm").
			Add(UnsafeStringValue(unsafe.Offsetof(Fubar{}.Cur)), "cur").
			Add(UnsafeStringValue(unsafe.Offsetof(Fubar{}.Foo)), "foo")
		return parser
	}()

Use your parser. Yay zero allocations (almost, buger/jsonparser still has a few but I assume those will be fixed soon)

		fubar := Fubar{}
		err := RunParser(bodyBytes, fubarParser, unsafe.Pointer(&fubar))

Benchmark

  • Still have 3 allocs inside buger/jsonparser EachKey function, once those are fixed (I contributed a PR which is under review), will be zero
  • Unfortunately, still significantly slower than the json iterator package (https://github.com/json-iterator/go)
$ ./bench.sh
=== RUN   TestRunParser
=== RUN   TestRunParser/simple_test
--- PASS: TestRunParser (0.00s)
    --- PASS: TestRunParser/simple_test (0.00s)
goos: windows
goarch: amd64
pkg: github.com/villenny/jsonparserHelper-go
BenchmarkRunParser
BenchmarkRunParser-8              923974              3944 ns/op              96 B/op          3 allocs/op
BenchmarkJsonIterator
BenchmarkJsonIterator-8          1611687              2219 ns/op             832 B/op         18 allocs/op
BenchmarkEachKey
BenchmarkEachKey-8                948354              3724 ns/op              96 B/op          3 allocs/op
PASS
ok      github.com/villenny/jsonparserHelper-go 13.311s
01s

Contact

Ryan Haksi [ryan.haksi@gmail.com]

License

Available under the BSD License. Or any license really, do what you like

Documentation

Index

Constants

View Source
const JsonparserValueType_ByteArray = 5
View Source
const JsonparserValueType_Float64 = 4
View Source
const JsonparserValueType_Int = 3
View Source
const JsonparserValueType_Int64 = 2
View Source
const JsonparserValueType_UnsafeString = 1

Variables

This section is empty.

Functions

func BytesAsString

func BytesAsString(bs []byte) string

func RunParser

func RunParser(bodyBytes []byte, parser []Parser, dest unsafe.Pointer) error

func StringAsBytes

func StringAsBytes(s string) []byte

Types

type JsonparserResult

type JsonparserResult struct {
	Key   string
	Idx   int
	Value []byte
	Vt    jsonparser.ValueType
	Err   error
}

func MakeJsonparserResult

func MakeJsonparserResult(paths [][]string, idx int, value []byte, vt jsonparser.ValueType, err error) JsonparserResult

func (JsonparserResult) GetFloatOrZero

func (t JsonparserResult) GetFloatOrZero() float64

func (JsonparserResult) GetInt64OrZero

func (t JsonparserResult) GetInt64OrZero() int64

func (JsonparserResult) GetIntOrZero

func (t JsonparserResult) GetIntOrZero() int

func (JsonparserResult) GetUnsafeStringOrEmpty

func (t JsonparserResult) GetUnsafeStringOrEmpty() string

type JsonparserValue

type JsonparserValue struct {
	Type   int
	Offset uintptr
}

func ByteArrayValue

func ByteArrayValue(p uintptr) JsonparserValue

func Float64Value

func Float64Value(p uintptr) JsonparserValue

func Int64Value

func Int64Value(p uintptr) JsonparserValue

func IntValue

func IntValue(p uintptr) JsonparserValue

func UnsafeStringValue

func UnsafeStringValue(p uintptr) JsonparserValue

type Parser

type Parser struct {
	Path  []string
	Value JsonparserValue
}

type Parsers

type Parsers []Parser

func MakeParser

func MakeParser(storage []Parser) Parsers

parser = append(parser, Parser{[]string{"foo"}, UnsafeStringValue(unsafe.Offsetof(Fubar{}.Foo))})

func (Parsers) Add

func (t Parsers) Add(v JsonparserValue, keys ...string) Parsers

Jump to

Keyboard shortcuts

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