json-iterator / go

A high-performance 100% compatible drop-in replacement of "encoding/json"
http://jsoniter.com/migrate-from-go-std.html
MIT License
13.46k stars 1.03k forks source link

How to stream? #453

Open yakuter opened 4 years ago

yakuter commented 4 years ago

Hello, I am sorry for asking here. Because the Token() is in progress, I couldn't convert this standart codes with jsoniter. What is the right way for streaming on jsoniter?

decoder := json.NewDecoder(jsonFile)

// Read opening file
_, err = decoder.Token()
checkErr(err)

var asset model.Asset
for decoder.More() {
    err := decoder.Decode(&asset)
    checkErr(err)

    assetJSON, err := json.Marshal(asset)
    checkErr(err)
}

// Close the file
_, err = decoder.Token()
checkErr(err)
AllenX2018 commented 4 years ago

You can try Iterator and its Readxxx functions for streaming. Since I'm not sure what your jsonField and asset look like, I don't have any concrete suggestion. But you can check this test case for example.