Giter VIP home page Giter VIP logo

go-jsonstream's Introduction

go-jsonstream

JSON Stream (JSON Lines, JSON Array) Go Library

Installation

go get github.com/Wing924/go-jsonstream

How to use

Encode

// encode as JSON Lines stream
out := bytes.NewBuffer(nil)

sender := NewJSONLinesEncoder(out)
sender.Send("foo")
sender.Send(map[string]string{"bar": "baz"})
sender.Send(123)
sender.Done()

fmt.Println(out.String())
// Will output:
// "foo"
// {"bar":"baz"}
// 123
// encode as JSON Array stream
out := bytes.NewBuffer(nil)

sender := NewJSONArrayEncoder(out)
sender.Send("foo")
sender.Send(map[string]string{"bar": "baz"})
sender.Send(123)
sender.Done()

fmt.Println(out.String())
// Will output:
// [
// "foo"
// ,{"bar":"baz"}
// ,123
// ]

Decode

// encode as JSON Lines stream
in := bytes.NewBufferString(`
{"name":"Barbie"}
{"name":"Barbie"}
{"name":"Ken"}
`)

var data struct{
    Name string `json:"name"`
}

receiver := jsonstream.NewJSONLinesDecoder(in)
for {
    err := receiver.Receive(&data)
    if err != nil {
        if err == io.EOF {
            break
        }
        panic(err)
    }
    fmt.Printf("%+v\n", data)
}

// will output
// {Name:Barbie}
// {Name:Barbie}
// {Name:Ken}
// encode as JSON Array stream
in := bytes.NewBufferString(`[{"name":"Barbie"},{"name":"Barbie"},{"name":"Ken"}]`)

var data struct{
Name string `json:"name"`
}

receiver := jsonstream.NewJSONArrayDecoder(in)
for {
    err := receiver.Receive(&data)
    if err != nil {
        if err == io.EOF {
            break
        }
        panic(err)
    }
    fmt.Printf("%+v\n", data)
}

// will output
// {Name:Barbie}
// {Name:Barbie}
// {Name:Ken}

Use go-json instead of encoding/json

The go-json is fast JSON encoder/decoder compatible with encoding/json for Go.

// Change the global
jsonstream.DefaultConfig = jsonstream.GoJSONConfig

// now use go-json to encode/decode JSON
jsonstream.NewJSONArrayEncoder(out)
jsonstream.NewJSONArrayDecoder(in)
jsonstream.NewJSONLinesEncoder(out)
jsonstream.NewJSONLinesDecoder(in)
// Change individual
jsonstream.NewJSONArrayEncoderWithConfig(out, jsonstream.GoJSONConfig)
jsonstream.NewJSONArrayDecoderWithConfig(in, jsonstream.GoJSONConfig)
jsonstream.NewJSONLinesEncoderWithConfig(out, jsonstream.GoJSONConfig)
jsonstream.NewJSONLinesDecoderWithConfig(in, jsonstream.GoJSONConfig)

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.