Skip to content

Villenny/jsonparserHelper-go

Repository files navigation

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

About

easy of use helper for buger/jsonparser

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published