jsons

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 2, 2016 License: MIT Imports: 1 Imported by: 0

README

jsons

Package jsons converts JSON arrays into streams of objects.

This is useful if you have a file, HTTP response or similar containing a large array of JSON objects. Instead of reading it all into memory and deserializing into a []Whatever you can wrap the reader in a jsons.Reader and then use a json.Decoder to repeatedly read and deserialize a single object into a Whatever.

Documentation

https://godoc.org/github.com/calmh/jsons

License

MIT

Documentation

Overview

Package jsons converts JSON arrays into streams of objects.

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
}

The Reader is an io.Reader that wraps another reader for the purposes of modifying a JSON stream. When the underlying reader provides a JSON array object ([obj, obj, ...]) this will be converted into a stream of newline terminated objects (obj\n obj\n obj\n) suitable for consumption by a bufio.Scanner or json.Decoder.

Example
package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"

	"github.com/calmh/jsons"
)

func main() {
	type Object struct {
		Foo string
	}

	input := bytes.NewBufferString(`[{"foo":"bar"}, {"foo":"baz"}]`)
	streamer := jsons.New(input)
	dec := json.NewDecoder(streamer)
	for {
		var res Object
		err := dec.Decode(&res)
		if err == io.EOF {
			break
		}
		if err != nil {
			fmt.Println("Error:", err)
			return
		}
		fmt.Printf("Read: %+v\n", res)
	}

}
Output:

Read: {Foo:bar}
Read: {Foo:baz}

func New

func New(r io.Reader) *Reader

func (*Reader) Read

func (s *Reader) Read(bs []byte) (int, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL