diff options
| author | Ivan Bertona <ivan.bertona@gmail.com> | 2017-10-31 13:16:38 -0700 |
|---|---|---|
| committer | Joe Tsai <thebrokentoaster@gmail.com> | 2017-10-31 22:28:36 +0000 |
| commit | 2596a0c075aeddec571cd658f748ac7a712a2b69 (patch) | |
| tree | 7703794a12c402a11fc067d3ca527d96bac658bb /src/encoding/json/stream.go | |
| parent | d58f4e9b7b503ed83641e2cd632a19011da6b50c (diff) | |
| download | go-2596a0c075aeddec571cd658f748ac7a712a2b69.tar.xz | |
encoding/json: disallow unknown fields in Decoder
Add a DisallowUnknownFields flag to Decoder.
DisallowUnknownFields causes the Decoder to return an error when
the the decoding destination is a struct and the input contains
object keys which do not match any non-ignored, public field the
destination, including keys whose value is set to null.
Note: this fix has already been worked on in 27231, which seems
to be abandoned. This version is a slightly simpler implementation
and is up to date with the master branch.
Fixes #15314
Change-Id: I987a5857c52018df334f4d1a2360649c44a7175d
Reviewed-on: https://go-review.googlesource.com/74830
Reviewed-by: Joe Tsai <joetsai@google.com>
Run-TryBot: Joe Tsai <joetsai@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'src/encoding/json/stream.go')
| -rw-r--r-- | src/encoding/json/stream.go | 5 |
1 files changed, 5 insertions, 0 deletions
diff --git a/src/encoding/json/stream.go b/src/encoding/json/stream.go index 76788f5fe7..f6b62c4cf6 100644 --- a/src/encoding/json/stream.go +++ b/src/encoding/json/stream.go @@ -35,6 +35,11 @@ func NewDecoder(r io.Reader) *Decoder { // Number instead of as a float64. func (dec *Decoder) UseNumber() { dec.d.useNumber = true } +// DisallowUnknownFields causes the Decoder to return an error when the destination +// is a struct and the input contains object keys which do not match any +// non-ignored, exported fields in the destination. +func (dec *Decoder) DisallowUnknownFields() { dec.d.disallowUnknownFields = true } + // Decode reads the next JSON-encoded value from its // input and stores it in the value pointed to by v. // |
