jsons

package module
Version: v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2022 License: MIT Imports: 3 Imported by: 0

README

jsons

Build Status GoDoc

jsons is a Go library to work with JSONS/NDJSON files.

JSONS files contain JSON objects, one per line. This storage format is more efficient than storing an array of objects since it doesn’t need to be loaded in memory when reading/writing it.

Install

go get github.com/bfontaine/jsons

Usage

Reading
func read() {
    j := jsons.NewFileReader("foo.jsons")

    if err := j.Open(); err != nil {
        log.Fatal(err)
    }

    defer j.Close()

    for {
        var m map[string]string
        if err := j.Next(&m); err != nil {
            if err == io.EOF {
                break
            }
            log.Fatal(err)
        }

        log.Printf("got %+v", m)
    }
}
Writing
func write() {
    j := jsons.NewFileWriter("foo.jsons")

    if err := j.Open(); err != nil {
        log.Fatal(err)
    }

    defer j.Close()

    j.Add(map[string]string{
        "foo": "bar",
        "qux": "abc",
    })

    j.Add(map[string]string{
        "foo": "def",
        "ghi": "jkl",
        "mno": "qpr",
    })
}

Documentation

Overview

Package jsons implements encoding and decoding of JSONS files and streams. The JSONS format is defined as compact JSON objects delimited by one newline. Each JSON object resides on its own line.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FileReader

type FileReader struct {
	// contains filtered or unexported fields
}

FileReader is a wrapper around a Reader to read JSONS data from a file.

func NewFileReader

func NewFileReader(filename string) *FileReader

NewFileReader returns a pointer on a new FileReader which will read its data from the given file.

func (*FileReader) Close

func (fr *FileReader) Close() error

Close closes the underlying file.

func (*FileReader) Next

func (fr *FileReader) Next(v interface{}) error

Next unmarshals the next JSON object in the given value, which must be a pointer.

func (*FileReader) Open

func (fr *FileReader) Open() error

Open opens the underlying file for reading. You must call this before any Close or Next call.

type FileWriter

type FileWriter struct {
	// contains filtered or unexported fields
}

FileWriter is a wrapper around a Writer which writes JSONS data in a file.

func NewFileWriter

func NewFileWriter(filename string) *FileWriter

NewFileWriter returns a pointer on a new FileWriter which will write in the given filename. The file is truncated if it already exists.

func (*FileWriter) Add

func (fw *FileWriter) Add(v interface{}) error

Add encodes the given value as a JSON object and write it in the underlying file.

func (*FileWriter) AddAll added in v1.1.0

func (fw *FileWriter) AddAll(args ...interface{}) error

AddAll is equivalent to calling Add on each of its arguments

func (*FileWriter) Close

func (fw *FileWriter) Close() error

Close closes the underlying file. You must call this when you're done adding values.

func (*FileWriter) Open

func (fw *FileWriter) Open() error

Open opens the underlying file for writing. This must be called before any Add or Close call.

type Reader

type Reader struct {
	// contains filtered or unexported fields
}

Reader is a JSONS reader

func NewReader

func NewReader(r io.Reader) *Reader

NewReader returns a pointer on a new Reader which consumes its data from the given io.Reader implementation.

func (*Reader) Next

func (r *Reader) Next(v interface{}) error

Next unmarshal the next JSON object in the given value, which must be a pointer.

type Writer

type Writer struct {
	// contains filtered or unexported fields
}

Writer is a JSONS writer

func NewWriter

func NewWriter(w io.Writer) Writer

NewWriter returns a new Writer, which writes JSONS-encoded data in the given io.Writer implementation.

func (Writer) Add

func (jw Writer) Add(v interface{}) error

Add encodes the given value and write it as a JSON object.

func (Writer) AddAll added in v1.1.0

func (jw Writer) AddAll(args ...interface{}) (err error)

AddAll is equivalent to calling Add on each of its arguments

Jump to

Keyboard shortcuts

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