Source File
decode_stream.go
Belonging Package
github.com/francoispqt/gojay
package gojayimport ()// UnmarshalerStream is the interface to implement for a slice, an array or a slice// to decode a line delimited JSON to.type UnmarshalerStream interface {UnmarshalStream(*StreamDecoder) error}// Stream is a struct holding the Stream apivar Stream = stream{}type stream struct{}// A StreamDecoder reads and decodes JSON values from an input stream.//// It implements conext.Context and provide a channel to notify interruption.type StreamDecoder struct {mux sync.RWMutex*Decoderdone chan struct{}deadline *time.Time}// DecodeStream reads the next line delimited JSON-encoded value from the decoder's input (io.Reader) and stores it in the value pointed to by c.//// c must implement UnmarshalerStream. Ideally c is a channel. See example for implementation.//// See the documentation for Unmarshal for details about the conversion of JSON into a Go value.func ( *StreamDecoder) ( UnmarshalerStream) error {if .isPooled == 1 {panic(InvalidUsagePooledDecoderError("Invalid usage of pooled decoder"))}if .r == nil {.err = NoReaderError("No reader given to decode stream")close(.done)return .err}for ; .cursor < .length || .read(); .cursor++ {switch .data[.cursor] {case ' ', '\n', '\t', '\r', ',':continuedefault:// char is not space start readingfor .nextChar() != 0 {// calling unmarshal stream:= .UnmarshalStream()if != nil {.err =close(.done)return}// garbage collects buffer// we don't want the buffer to grow extensively.data = .data[.cursor:].length = .length - .cursor.cursor = 0}// close the done channel to signal the end of the jobclose(.done)return nil}}close(.done).mux.Lock():= .raiseInvalidJSONErr(.cursor).mux.Unlock()return}// context.Context implementation// Done returns a channel that's closed when work is done.// It implements context.Contextfunc ( *StreamDecoder) () <-chan struct{} {return .done}// Deadline returns the time when work done on behalf of this context// should be canceled. Deadline returns ok==false when no deadline is// set. Successive calls to Deadline return the same results.func ( *StreamDecoder) () (time.Time, bool) {if .deadline != nil {return *.deadline, true}return time.Time{}, false}// SetDeadline sets the deadlinefunc ( *StreamDecoder) ( time.Time) {.deadline = &}// Err returns nil if Done is not yet closed.// If Done is closed, Err returns a non-nil error explaining why.// It implements context.Contextfunc ( *StreamDecoder) () error {select {case <-.done:.mux.RLock()defer .mux.RUnlock()return .errdefault:return nil}}// Value implements context.Contextfunc ( *StreamDecoder) ( interface{}) interface{} {return nil}
![]() |
The pages are generated with Golds v0.8.2. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds. |