File
Blob: util/json.go
| 1 | package util |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "errors" |
| 6 | "io" |
| 7 | ) |
| 8 | |
| 9 | // DecodeJSON reads one JSON value, rejecting unknown fields and trailing data. |
| 10 | func DecodeJSON(input io.Reader, value any) error { |
| 11 | decoder := json.NewDecoder(input) |
| 12 | decoder.DisallowUnknownFields() |
| 13 | if err := decoder.Decode(value); err != nil { |
| 14 | return err |
| 15 | } |
| 16 | if decoder.Decode(new(any)) != io.EOF { |
| 17 | return errors.New("must contain one JSON value") |
| 18 | } |
| 19 | return nil |
| 20 | } |