https://github.com/tada/jsonstream
Helper functions to enable true JSON streaming capabilities.
https://github.com/tada/jsonstream
Last synced: 6 months ago
JSON representation
Helper functions to enable true JSON streaming capabilities.
- Host: GitHub
- URL: https://github.com/tada/jsonstream
- Owner: tada
- License: apache-2.0
- Created: 2020-04-14T05:58:32.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-05-01T14:15:28.000Z (about 6 years ago)
- Last Synced: 2024-05-07T18:15:40.717Z (about 2 years ago)
- Language: Go
- Size: 25.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
JSONStream provides helper functions to enable true JSON streaming capabilities.
[](https://opensource.org/licenses/Apache-2.0)
[](https://goreportcard.com/report/github.com/tada/jsonstream)
[](https://godoc.org/github.com/tada/jsonstream)
[](https://github.com/tada/jsonstream/actions)
[](https://coveralls.io/github/tada/jsonstream)
### How to get:
```sh
go get github.com/tada/jsonstream
```
### Sample usage
Since jsonstream uses the github.com/tada/catch module, all error handling is baked into the support functions. If any
error is encountered, it will result in a panic that is recovered in the top level functions `Marshal` and `Unmarshal`.
Code using the support functions, like in the example below, is compact since no error propagation is needed.
```go
package tst
import (
"encoding/json"
"io"
"time"
"github.com/tada/catch/pio"
"github.com/tada/jsonstream"
)
type ts struct {
v time.Duration
}
// MarshalJSON is from the json.Marshaller interface
func (t *ts) MarshalJSON() ([]byte, error) {
// Dispatch to the helper function
return jsonstream.Marshal(t)
}
// UnmarshalJSON is from the json.Marshaller interface
func (t *ts) UnmarshalJSON(bs []byte) error {
// Dispatch to the helper function
return jsonstream.Unmarshal(t, bs)
}
// MarshalToJSON encode as json and stream result to the writer
func (t *ts) MarshalToJSON(w io.Writer) {
pio.WriteByte('{', w)
jsonstream.WriteString("v", w)
pio.WriteByte(':', w)
pio.WriteInt(int64(t.v/time.Millisecond), w)
pio.WriteByte('}', w)
}
// UnmarshalToJSON decodes using the given decoder
func (t *ts) UnmarshalFromJSON(js jsonstream.Decoder, firstToken json.Token) {
jsonstream.AssertDelim(firstToken, '{')
for {
s, ok := js.ReadStringOrEnd('}')
if !ok {
break
}
if s == "v" {
t.v = time.Duration(js.ReadInt()) * time.Millisecond
}
}
}
```