json

package module
v0.0.0-...-752ee0d Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2020 License: BSD-2-Clause Imports: 5 Imported by: 2 modules, 2 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

pkg/json GoDoc

An alternative JSON decoder for Go.

Features

  • json.Scanner, when provided an external buffer, does not allocate.
  • io.Reader friendly; you don't need to buffer your input in memory.
  • json.Decoder.Token() is almost allocation free. It allocates a heck of a lot less than encoding/json.Decoder.Token()

Is it faster than fastjson/ultrajson/megajson/fujson?

Honestly, I don't know. I have some benchmarks that show that pkg/json is faster than encoding/json for tokenisation, but this package isn't finished yet.

BenchmarkDecoder/pkgjson/canada.json.gz-4                    151           7678709 ns/op         293.16 MB/s         152 B/op          3 allocs/op
BenchmarkDecoder/encodingjson/canada.json.gz-4                12         102050197 ns/op          22.06 MB/s    17740358 B/op     889106 allocs/op
BenchmarkDecoder/pkgjson/citm_catalog.json.gz-4              346           3396931 ns/op         508.46 MB/s         152 B/op          3 allocs/op
BenchmarkDecoder/encodingjson/citm_catalog.json.gz-4          40          28496966 ns/op          60.61 MB/s     5665615 B/op     324799 allocs/op
BenchmarkDecoder/pkgjson/twitter.json.gz-4                   708           1676174 ns/op         376.76 MB/s         168 B/op          4 allocs/op
BenchmarkDecoder/encodingjson/twitter.json.gz-4               66          17879604 ns/op          35.32 MB/s     3660255 B/op     187815 allocs/op
BenchmarkDecoder/pkgjson/code.json.gz-4                      144           8403582 ns/op         230.91 MB/s         264 B/op          6 allocs/op
BenchmarkDecoder/encodingjson/code.json.gz-4                   9         123089228 ns/op          15.76 MB/s    23355896 B/op    1319125 allocs/op
BenchmarkDecoder/pkgjson/example.json.gz-4                 31387             37471 ns/op         347.58 MB/s         168 B/op          4 allocs/op
BenchmarkDecoder/encodingjson/example.json.gz-4             2779            415232 ns/op          31.37 MB/s       82416 B/op       4325 allocs/op

Should I use this?

Right now? No. In the future, maybe.

I've found a bug!

Great! Please follow this two step process:

  1. Raise an issue
  2. Send a PR (unless I fix it first)

Standing on the shoulders of giants

This project is heavily influenced by Steven Schveighoffer's iopipe.

Documentation

Overview

Package json decodes JSON.

Index

Examples

Constants

View Source
const (
	ObjectStart = '{' // {
	ObjectEnd   = '}' // }
	String      = '"' // "
	Colon       = ':' // :
	Comma       = ',' // ,
	ArrayStart  = '[' // [
	ArrayEnd    = ']' // ]
	True        = 't' // t
	False       = 'f' // f
	Null        = 'n' // n
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Decoder

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

A Decoder decodes JSON values from an input stream.

func NewDecoder

func NewDecoder(r io.Reader) *Decoder

NewDecoder returns a new Decoder for the supplied Reader r.

func NewDecoderBuffer

func NewDecoderBuffer(r io.Reader, buf []byte) *Decoder

NewDecoderBuffer returns a new Decoder for the supplier Reader r, using the []byte buf provided for working storage.

func (*Decoder) Decode

func (d *Decoder) Decode(v interface{}) error

Decode reads the next JSON-encoded value from its input and stores it in the value pointed to by v.

func (*Decoder) Token

func (d *Decoder) Token() ([]byte, error)

Token returns a []byte referencing the next logical token in the stream. The []byte is valid until Token is called again. Ad the end of the input stream, Token returns nil, io.EOF.

Token guarantees that the delimiters [ ] { } it returns are properly nested and matched: if Token encounters an unexpected delimiter in the input, it will return an error.

A valid token begins with one of the following:

{ Object start
[ Array start
} Object end
] Array End
t JSON true
f JSON false
n JSON null
" A string, possibly containing backslash escaped entites.
-, 0-9 A number

Commas and colons are elided.

Example
package main

import (
	"fmt"
	"io"
	"log"
	"strings"

	"github.com/pkg/json"
)

func main() {
	input := `{"a": 1,"b": 123.456, "c": [null]}`
	dec := json.NewDecoder(strings.NewReader(input))
	for {
		tok, err := dec.Token()
		if err == io.EOF {
			break
		}
		if err != nil {
			log.Fatal(err)
		}
		fmt.Printf("%s\n", tok)
	}

}
Output:
{
"a"
1
"b"
123.456
"c"
[
null
]
}

type Scanner

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

Scanner implements a JSON scanner as defined in RFC 7159. A Scanner reads from the supplied io.Reader and produces via Next a stream of tokens, expressed as []byte slices.

func NewScanner

func NewScanner(r io.Reader) *Scanner

NewScanner returns a new Scanner for the io.Reader r.

func (*Scanner) Error

func (s *Scanner) Error() error

Error returns the first error encountered. When underlying reader is exhausted, Error returns io.EOF.

func (*Scanner) Next

func (s *Scanner) Next() []byte

Next returns a []byte referencing the the next lexical token in the stream. The []byte is valid until Next is called again. If the stream is at its end, or an error has occured, Next returns a zero length []byte slice.

A valid token begins with one of the following:

{ Object start
[ Array start
} Object end
] Array End
, Literal comma
: Literal colon
t JSON true
f JSON false
n JSON null
" A string, possibly containing backslash escaped entites.
-, 0-9 A number
Example
package main

import (
	"fmt"
	"io"
	"log"
	"strings"

	"github.com/pkg/json"
)

func main() {
	input := `{"a": 1,"b": 123.456, "c": [null]}`
	sc := json.NewScanner(strings.NewReader(input))
	for {
		tok := sc.Next()
		if len(tok) < 1 {
			break
		}
		fmt.Printf("%s\n", tok)
	}
	if err := sc.Error(); err != nil && err != io.EOF {
		log.Fatal(err)
	}

}
Output:
{
"a"
:
1
,
"b"
:
123.456
,
"c"
:
[
null
]
}

Jump to

Keyboard shortcuts

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