Documentation ¶
Overview ¶
Package ndjson implements reading and writing files according to the ndjson specification (http://ndjson.org/).
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Reader ¶
type Reader struct {
// contains filtered or unexported fields
}
Reader allows reading line-oriented JSON data following the ndjson spec at http://ndjson.org/.
Example ¶
package main import ( "fmt" "os" "strings" "github.com/olivere/ndjson" ) type Location struct { City string `json:"city"` } func main() { r := ndjson.NewReader(strings.NewReader(`{"city":"Munich"} {"city":"Berlin"} {"city":"London"}`)) for r.Next() { var loc Location if err := r.Decode(&loc); err != nil { fmt.Fprintf(os.Stderr, "Decode failed: %v", err) return } fmt.Println(loc.City) } if err := r.Err(); err != nil { fmt.Fprintf(os.Stderr, "Reader failed: %v", err) return } }
Output: Munich Berlin London
func NewReaderSize ¶ added in v1.0.0
NewReaderSize returns a new reader whose buffer has the specified max size, using the underlying io.Reader as input.
func (*Reader) Bytes ¶ added in v1.0.1
Bytes returns the most recent buffer generated by a call to Next. The underlying array may point to data that will be overwritten by a subsequent call to Next. It does no allocation.
Example ¶
package main import ( "fmt" "os" "strings" "github.com/olivere/ndjson" ) type Location struct { City string `json:"city"` } func main() { r := ndjson.NewReader(strings.NewReader(`{"city":"Munich"} {"city":"Invalid" {"city":"London"}`)) for r.Next() { var loc Location if err := r.Decode(&loc); err != nil { fmt.Printf("Decode failed: %v. Last read: %s\n", err, string(r.Bytes())) return } fmt.Println(loc.City) } if err := r.Err(); err != nil { fmt.Fprintf(os.Stderr, "Reader failed: %v", err) return } }
Output: Munich Decode failed: unexpected end of JSON input. Last read: {"city":"Invalid"
func (*Reader) Decode ¶
Decode decodes the bytes read after the last call to Next into the specified value.
func (*Reader) Next ¶
Next advances the Reader to the next line, which will then be available through the Decode method. It returns false when the reader stops, either by reaching the end of the input or an error. After Next returns false, the Err method will return any error that occured while reading, except if it was io.EOF, Err will return nil.
Next might panic if the underlying split function returns too many tokens without advancing the input.
type Writer ¶
type Writer struct {
// contains filtered or unexported fields
}
Writer implements writing line-oriented JSON data following the ndjson spec at http://ndjson.org/.
Example ¶
package main import ( "fmt" "os" "github.com/olivere/ndjson" ) type Location struct { City string `json:"city"` } func main() { locations := []Location{ {City: "Munich"}, {City: "Berlin"}, {City: "London"}, } r := ndjson.NewWriter(os.Stdout) for _, loc := range locations { if err := r.Encode(loc); err != nil { fmt.Fprintf(os.Stderr, "Encode failed: %v", err) return } } }
Output: {"city":"Munich"} {"city":"Berlin"} {"city":"London"}