flatjson

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2021 License: MIT Imports: 2 Imported by: 4

README

flatjson

Fast and dirty parsing of JSON! Decode only the parts you care about!

What is flat JSON?

This library used to support only what I called a "flat" subset of JSON. But now it supports all JSON, but you can still decide how "flat" you want to go. The flatter, the faster :).

Flat JSON is a subset of JSON where the only supported types are objects containing strings, numbers, booleans or null values. There can't be nested objects or arrays. The root element must be an object.

What's the use for that?

It's fast and you decode only what you need. The old goal was:

If you log in JSON, likely your logs respect this principle. Using a JSON parser that supports only this subset should be faster than using a general purpose one. So this is one use case, parsing logs that are in JSON.

Implementation

This is an implementation of a flatjson parser. As far as I can tell, it works well enough. Check out the tests to see if its good enough for you.

Speed

Comparing this implementation with the standard library JSON decoder. Both have their output ignored:

  • flajson's name/value pairs are ignored.
  • encoding/json is decoding into an empty struct.

The goal here is to see how fast only the decoding part is. This is not necessarly a characteristic workload since a normal use case would allocate memory for the strings of the name/value pairs.

BenchmarkFlatJSON         1000000         1970 ns/op     177.15 MB/s           0 B/op          0 allocs/op
BenchmarkEncodingJSON     100000         20962 ns/op      36.83 MB/s        2151 B/op        130 allocs/op

At this time, the API of flatjson is not nailed down, so I haven't benchmarked a real use case. However, the benchmark above at least demonstrates that the potential for greatly improved speed is there.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ScanNumber

func ScanNumber(data []byte, i int) (float64, int, error)

ScanNumber reads a JSON number value from data and advances i one past the last number component it found. It does not deal with whitespace.

Types

type Bool

type Bool struct {
	Name  Pos
	Value bool
}

type BooleanDec

type BooleanDec func(Bool)

type Callbacks

type Callbacks struct {
	OnNumber  NumberDec
	OnString  StringDec
	OnBoolean BooleanDec
	OnNull    NullDec

	OnRaw func(name, value Pos)
	// contains filtered or unexported fields
}

type Null

type Null struct{ Name Pos }

type NullDec

type NullDec func(Null)

type Number

type Number struct {
	Name  Pos
	Value float64
}

type NumberDec

type NumberDec func(Number)

type Pos

type Pos struct {
	From int
	To   int
}

func ScanArray

func ScanArray(data []byte, from int, cb *Callbacks) (pos Pos, found bool, err error)

ScanArray according to the spec at http://www.json.org/ but ignoring nested objects and arrays

func ScanObject

func ScanObject(data []byte, from int, cb *Callbacks) (pos Pos, found bool, err error)

ScanObject according to the spec at http://www.json.org/ but ignoring nested objects and arrays

func (Pos) Bytes

func (p Pos) Bytes(data []byte) []byte

func (Pos) String

func (p Pos) String(data []byte) string

type String

type String struct {
	Name  Pos
	Value Pos
}

type StringDec

type StringDec func(String)

type SyntaxError

type SyntaxError struct {
	Offset  int
	Message string

	SubErr *SyntaxError
}

func (*SyntaxError) Error

func (s *SyntaxError) Error() string

Jump to

Keyboard shortcuts

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