typestar

Streaming JSON in Go

Decoder and Encoder work over a reader and a writer, one value at a time.

type Run struct {
    Lang string `json:"lang"`
    TPM  int    `json:"tpm"`
}

func decodeStream(input string) ([]Run, error) {
    decoder := json.NewDecoder(strings.NewReader(input))
    var runs []Run
    for {
        var run Run
        err := decoder.Decode(&run)
        if errors.Is(err, io.EOF) {
            return runs, nil
        }
        if err != nil {
            return nil, fmt.Errorf("decode: %w", err)
        }
        runs = append(runs, run)
    }
}

func strictDecode(input string) error {
    decoder := json.NewDecoder(strings.NewReader(input))
    decoder.DisallowUnknownFields()
    var run Run
    return decoder.Decode(&run)
}

How it works

  1. A Decoder reads several values from one stream.
  2. DisallowUnknownFields turns a typo into an error.
  3. This is what you use for a request body or a large file.

Keywords and builtins used here

The run, in numbers

Lines
27
Characters to type
538
Tokens
139
Three-star pace
100 tpm

At the three-star pace of 100 tokens a minute, this run takes about 83 seconds.

Type this snippet

Step 3 of 3 in Encoding, step 3 of 26 in Standard library & project layout.

← Previous Next →