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 (*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"}