pjson

package
v0.0.0-...-642df0c Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2024 License: BSD-3-Clause, 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  // token is a JSON String
	Number  // token is a JSON Number
	True    // token is a JSON True
	False   // token is a JSON False
	Null    // token is a JSON NUll
	Object  // token is a JSON Object (open or close character)
	Array   // token is a JSON Array (open or close character)
	Comma   // token is a JSON comma character ','
	Colon   // token is a JSON colon character ':'
	Start   // token is the start of the JSON document
	End     // token is the end of the JSON document
	Open    // token is an open character (Object or Array, '{' or '[')
	Close   // token is a close character (Object or Array, '}' or ']')
	Key     // token is a JSON Object key
	Value   // token is a JSON Object or Array value
	Escaped // token is a String with at least one escape character ('\')
	Sign    // token is a signed Number (has a '-' prefix)
	Dot     // token is a Number that has a dot (radix point)
	E       // token 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 current JSON Element.

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