Go "encoding/json/v2" Unmarshall Stream
Exploring Go json v2 unmarshall in depth
Go encoding/json/v2 Unmarshall Stream
How to Efficiently unmarshall a stream of data ⁇
Old Ways with encoding/json
- Method 1
- Read all the stream data with
io.ReadAll - Unmarshall it to a custom type with
json.Unmarshall
- Read all the stream data with
func OldJsonStreamUnmarshal(dataStream io.Reader, v interface{}) error {
data, err := io.ReadAll(dataStream)
if err != nil {
return err
}
return json.Unmarshal(data, v)
}
- Method 2
- use relatively modern
NewDecoderto create a Decocer for the input streamio.Reader - Decode it to custom type with
Decode
- use relatively modern
func OldJsonStreamUnmarshalWithDecoder(dataStream io.Reader, v interface{}) error {
return json.NewDecoder(dataStream).Decode(v)
}
New Ways with encoding/json/v2
Method 1
- Read all the stream data with
io.ReadAll - Unmarshall it to a custom type with
jsonv2.Unmarshall - consider import as
- Read all the stream data with
Import
import (
"encoding/json"
jsonv2 "encoding/json/v2"
"io"
)
- Function
func NewJsonV2StreamUnmarshal(dataStream io.Reader, v interface{}) error {
data, err := io.ReadAll(dataStream)
if err != nil {
return err
}
return jsonv2.Unmarshal(data, v)
}
Method 2
- using newer function
UnmarshalReadfromgo1.27->encoding/json/v2 - which handles
Readstream data andUnmarshallto our Custom Type all in one step. - consider import as
- using newer function
Import
import (
"encoding/json"
jsonv2 "encoding/json/v2"
"io"
)
- Function
func NewJsonV2StreamUnmarshalWithDecoder(dataStream io.Reader, v interface{}) error {
return jsonv2.UnmarshalRead(dataStream, v)
}
Comparision
when ran each of these respective functions
ref :- https://gist.github.com/Sandy10247/ded8c4bd81cde7e0cbeb45f40c968fad
Conclusion
- The main result is that NewJsonV2StreamUnmarshalWithDecoder is the best default for streaming JSON in this benchmark.
func NewJsonV2StreamUnmarshalWithDecoder(dataStream io.Reader, v interface{}) error {
return jsonv2.UnmarshalRead(dataStream, v)
}
// NewJsonV2StreamUnmarshalWithDecoder
var person4 Person
err = NewJsonV2StreamUnmarshalWithDecoder(getNewStream(), &person4)
if err != nil {
fmt.Println("Error unmarshalling JSON v2 with decoder:", err)
return
}
fmt.Printf("New JSON v2 Unmarshal with Decoder: %+v\n", person4)