GithubHelp home page GithubHelp logo

bserdar / jsonstream Goto Github PK

View Code? Open in Web Editor NEW
8.0 3.0 0.0 15 KB

Go library to deal with streaming JSON documents with line-delimited JSON (NDJSON), record separator-delimited JSON, concatenated JSON, and length-prefixed JSON

License: Apache License 2.0

Go 100.00%
go json ndjson jsonstream

jsonstream's Introduction

JSON Streams

GoDoc

This library supports streaming JSON streaming conventions described in this Wikipedia page.

This is for the concatenated JSON streams, where each JSON document is concatenated one after the other:

rd:=jsonstream.NewConcatReader(reader)
var entry MyEntry
for {
  err:=rd.Unmarshal(&entry)
  if err==io.EOF {
    break
  }
  if err!=nil {
    return err
  }
  processEntry(entry)
}

This is for JSON streams with a separate JSON document in each line (NDJSON):

ndLinesReader:=jsonstream.NewLineReader(reader)
ndLinesWriter:=jsonstream.NewLineWriter(writer)

This is for JSON streams separated with record separator delimiter:

seqReader:=jsonstream.NewSeqReader(reader) // 0x1e record separator
seqWriter:=jsonstream.NewSeqWriter(writer) 
seqReader:=jsonstream.NewSeqReaderWithSep(reader,'\n') // Line-separated JSON
seqWriter:=jsonstream.NewSeqWriterWithSep(writer,'\n')

This is for JSON streams with length prefixed JSON stream, where each JSON document is prefixed by its byte length:

lpReader:=jsonstream.NewLenPrefixedReader(reader)
lpWriter:=jsonstream.NewLenPrefixesWriter(writer)

APIs

All four stream readers/writers use the same APIs.

Readers

data, err:=reader.ReadRaw()

ReadRaw reads the next JSON document. Only the ConcatReader validates that the JSON document is a valid document, the remaining readers simply read until the next delimiter. The returned byte array is a newly allocated copy of the underlying read buffer. Some of the readers use buffered input, so the state of the underlying reader is unknown.

var data myStruct
err:=reader.Unmarshal(&data)

Unmarshals the next entry from the input. For concatenated JSON, errors invalidate the rest of the stream. For others stream processing can continue.

Writers

err:=writer.WriteRaw(data)

WriteRaw simply writes the []byte data to the output, with the correct delimiter. For NDJSON, WriteRaw removes the newline characters from data.

err:=writer.Marshal(data)

Marshal first encodes data to JSON, and then writes it to the output.

jsonstream's People

Contributors

bserdar avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.