pjson

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2021 License: MIT Imports: 0 Imported by: 0

README

pjson

GoDoc

A JSON stream parser for Go and (Rust)

Example

The example below prints all string values from a JSON document.

package main

import "github.com/tidwall/pjson"

func main() {
	var json = `
	{
	  "name": {"first": "Tom", "last": "Anderson"},
	  "age":37,
	  "children": ["Sara","Alex","Jack"],
	  "fav.movie": "Deer Hunter",
	  "friends": [
		{"first": "Dale", "last": "Murphy", "age": 44, "nets": ["ig", "fb", "tw"]},
		{"first": "Roger", "last": "Craig", "age": 68, "nets": ["fb", "tw"]},
		{"first": "Jane", "last": "Murphy", "age": 47, "nets": ["ig", "tw"]}
	  ]
	}
	`
	pjson.Parse([]byte(json), 0, func(start, end, info int) int {
		if info&(pjson.String|pjson.Value) == pjson.String|pjson.Value {
			println(json[start:end])
		}
		return 1
	})
}

// output:
// "Tom"
// "Anderson"
// "Sara"
// "Alex"
// "Jack"
// "Deer Hunter"
// "Dale"
// "Murphy"
// "ig"
// "fb"
// "tw"
// "Roger"
// "Craig"
// "fb"
// "tw"
// "Jane"
// "Murphy"
// "ig"
// "tw"

Contact

Josh Baker @tidwall

License

pjson source code is available under the MIT License.

Documentation

Index

Constants

View Source
const (
	String  // the data is a JSON String
	Number  // the data is a JSON Number
	True    // the data is a JSON True
	False   // the data is a JSON False
	Null    // the data is a JSON NUll
	Object  // the data is a JSON Object (open or close character)
	Array   // the data is a JSON Array (open or close character)
	Comma   // the data is a JSON comma character ','
	Colon   // the data is a JSON colon character ':'
	Start   // the data is the start of the JSON document
	End     // the data is the end of the JSON document
	Open    // the data is an open character (Object or Array, '{' or '[')
	Close   // the data is an close character (Object or Array, '}' or ']')
	Key     // the data is a JSON Object key
	Value   // the data is a JSON Object or Array value
	Escaped // the data is a String with at least one escape character ('\')
	Sign    // the data is a signed Number (has a '-' prefix)
	Dot     // the data is a Number has a dot (radix point)
	E       // the data is a Number in scientific notation (has 'E' or 'e')
)

Bit flags passed to the "info" parameter of the iter function which provides additional information about the

Variables

This section is empty.

Functions

func Parse

func Parse(json []byte, opts int, iter func(start, end, info int) int) int

Parse JSON. The iter function is a callback that fires for every element in the JSON document. Elements include all values and tokens. The 'start' and 'end' params are the start and end indexes of their respective element, such that json[start:end] will equal the complete element data. The 'info' param provides extra information about the element data. Returning 0 from 'iter' will stop the parsing. Returning 1 from 'iter' will continue the parsing. Returning -1 from 'iter' will skip all children elements in the current Object or Array, which only applies when the 'info' for current element has the Open bit set, otherwise it effectively works like returning 1. This operation returns zero or a negative value if an error occured. This value represents the position that the parser was at when it discovered the error. To get the true offset multiple this value by -1.

e := Parse(json, iter)
if e < 0 {
    pos := e * -1
    return fmt.Errorf("parsing error at position %d", pos)
}

This operation returns a positive value when successful. If the 'iter' stopped early then this value will be the position the parser was at when it stopped, otherwise the value will be equal the length of the original json document.

Types

This section is empty.

Jump to

Keyboard shortcuts

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