From 2596a0c075aeddec571cd658f748ac7a712a2b69 Mon Sep 17 00:00:00 2001 From: Ivan Bertona Date: Tue, 31 Oct 2017 13:16:38 -0700 Subject: 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 Run-TryBot: Joe Tsai TryBot-Result: Gobot Gobot --- src/encoding/json/stream.go | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'src/encoding/json/stream.go') 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. // -- cgit v1.3-5-g9baa