jsparser

package module
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: May 9, 2020 License: MIT Imports: 5 Imported by: 0

README

jsparser

jsparser is a json parser for GO. It is efficient to parse large json data with streaming fashion.

Usage

{
  "books": [{
      "title": "The Iliad and The Odyssey",
      "price": 12.95,
      "comments": [{
        "rating": 4,
        "comment": "Best translation I've read."
      }, {
        "rating": 2,
        "comment": "I like other versions better."
      }]
    },
    {
      "title": "Anthology of World Literature",
      "price": 24.95,
      "comments": [{
        "rating": 4,
        "comment": "Excellent overview of world literature."
      }, {
        "rating": 3,
        "comment": "Needs more modern literature."
      }]
    }
  ]
}

Stream over books

f, _ := os.Open("input.json")
br := bufio.NewReaderSize(f,65536)
parser := jsparser.NewJSONParser(br, "books")

for json:= range parser.Stream() {
		fmt.Println(json.ObjectVals["title"])
		fmt.Println(json.ObjectVals["price"])
		fmt.Println(json.ObjectVals["comments"].(*jsparser.JSON).ArrayVals[0].(*jsparser.JSON).ObjectVals["rating"])
}

// for relatively small size json. get all the results as slice
for json:= range parser.Parse() {
}

Skip props for efficiency

parser := pr.NewJSONParser(br, "books").SkipProps([]string{"comments", "price"})  

Error handling

for json:= range parser.Stream() {
    if json.Err !=nil {
      // handle error
    }
}

Progress of parsing

// total byte read to calculate the progress of parsing
parser.TotalReadSize

If you are interested check also xml parser which works similarly.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type JSON

type JSON struct {
	StringVal  string
	BoolVal    bool
	ArrayVals  []interface{}
	ObjectVals map[string]interface{}
	ValueType  ValueType
	Err        error
}

JSON parsed result

type JsonParser

type JsonParser struct {
	TotalReadSize uint64
	// contains filtered or unexported fields
}

func NewJSONParser

func NewJSONParser(reader *bufio.Reader, loopProp string) *JsonParser

func (*JsonParser) Parse added in v1.3.0

func (j *JsonParser) Parse() []*JSON

func (*JsonParser) SkipProps

func (j *JsonParser) SkipProps(skipProps []string) *JsonParser

func (*JsonParser) Stream

func (j *JsonParser) Stream() chan *JSON

type ValueType

type ValueType int8

ValueType of JSON value

const (
	Invalid ValueType = iota
	Null
	String
	Number
	Boolean
	Array
	Object
)

JSON types

Jump to

Keyboard shortcuts

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