fastjson icon indicating copy to clipboard operation
fastjson copied to clipboard

Can I use one `Parser` repeatedly?

Open liucaisi opened this issue 6 years ago • 3 comments

I want to define a Parser only one time, and use it to parse different string several times. But it seems that the variables only are assigned with the latest parsing result, even I use different variable. My demo code as follows:

package main

import "fmt"
import "github.com/valyala/fastjson"

func main() {

        var p fastjson.Parser

        s0 := `{"hello": "world"}`
        s1 := `{"hello": "dlrow"}`

        v, _ := p.Parse(s0)
        v1, _ := p.Parse(s1)

        fmt.Printf("%v\n", v.Get("hello"))
        fmt.Printf("%v\n", v1.Get("hello"))
}

liucaisi avatar Oct 25 '19 08:10 liucaisi

Parser can be used repeatedly. Moreover - this is the most optimal usage for Parser. But all the objects returned from the Parse call become invalid after the next Parse call.

valyala avatar Oct 25 '19 21:10 valyala

Any methods to keep it valid?

liucaisi avatar Oct 26 '19 02:10 liucaisi

ParserPool may be used for pooling Parsers for similarly typed JSONs.

https://godoc.org/github.com/valyala/fastjson#ParserPool

What are the disadvantages of reusing a parser pool for infinitely dissimilar json?
Boundless memory consumption?

kevburnsjr avatar Nov 01 '19 21:11 kevburnsjr