JSONStream provides helper functions to enable true JSON streaming capabilities.
go get github.com/tada/jsonstream
Since jsonstream uses the github.com/tada/catch module, all error handling is baked into the support functions. If any
error is encountered, it will result in a panic that is recovered in the top level functions Marshal
and Unmarshal
.
Code using the support functions, like in the example below, is compact since no error propagation is needed.
package tst
import (
"encoding/json"
"io"
"time"
"github.com/tada/catch/pio"
"github.com/tada/jsonstream"
)
type ts struct {
v time.Duration
}
// MarshalJSON is from the json.Marshaller interface
func (t *ts) MarshalJSON() ([]byte, error) {
// Dispatch to the helper function
return jsonstream.Marshal(t)
}
// UnmarshalJSON is from the json.Marshaller interface
func (t *ts) UnmarshalJSON(bs []byte) error {
// Dispatch to the helper function
return jsonstream.Unmarshal(t, bs)
}
// MarshalToJSON encode as json and stream result to the writer
func (t *ts) MarshalToJSON(w io.Writer) {
pio.WriteByte('{', w)
jsonstream.WriteString("v", w)
pio.WriteByte(':', w)
pio.WriteInt(int64(t.v/time.Millisecond), w)
pio.WriteByte('}', w)
}
// UnmarshalToJSON decodes using the given decoder
func (t *ts) UnmarshalFromJSON(js jsonstream.Decoder, firstToken json.Token) {
jsonstream.AssertDelim(firstToken, '{')
for {
s, ok := js.ReadStringOrEnd('}')
if !ok {
break
}
if s == "v" {
t.v = time.Duration(js.ReadInt()) * time.Millisecond
}
}
}