New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: encoding/json: add error var to compare the returned error when using json.Decoder.DisallowUnknownFields() #29035

Open
jaswdr opened this Issue Nov 30, 2018 · 2 comments

Comments

Projects
None yet
3 participants
@jaswdr

jaswdr commented Nov 30, 2018

What version of Go are you using (go version)?

$ go version

go version devel +ffc7bc55f3 Tue Oct 9 10:35:08 2018 +0000 linux/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env

GOARCH="amd64"
GOBIN=""
GOCACHE="/home/jaswdr/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/jaswdr"
GOPROXY=""
GORACE=""
GOROOT="/home/jaswdr/go"
GOTMPDIR=""
GOTOOLDIR="/home/jaswdr/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build749620831=/tmp/go-build -gno-record-gcc-switches"

What did you do?

package main

import (
	"log"
	"bytes"
	"encoding/json"
)

type T struct {
	Bar string `json:"bar"`
}

func main() {
	str := []byte(`{"foo":"bar"}`)
	obj := new(T)
	
	dec := json.NewDecoder(bytes.NewReader(str))
	dec.DisallowUnknownFields()
	err := dec.Decode(&obj)
	if err != nil { // Whant to check if the specific unkown fields error happen
		log.Fatal(err)
	}
	
	log.Println(obj)
}

What did you expect to see?

Some way to check if the error happen.

...
if err == json.UnknownFieldsError {
 ...
}
...

What did you see instead?

No way to do this instead of checking the error string


If this is accepted I really want to work on it.

@gopherbot gopherbot added this to the Proposal milestone Nov 30, 2018

@gopherbot gopherbot added the Proposal label Nov 30, 2018

@ianlancetaylor ianlancetaylor changed the title from proposal: add error var to compare to the returned error when using json.Decoder.DisallowUnknownFields() to proposal: encoding/json: add error var to compare to the returned error when using json.Decoder.DisallowUnknownFields() Nov 30, 2018

@jaswdr jaswdr changed the title from proposal: encoding/json: add error var to compare to the returned error when using json.Decoder.DisallowUnknownFields() to proposal: encoding/json: add error var to compare the returned error when using json.Decoder.DisallowUnknownFields() Dec 1, 2018

@gopherbot

This comment has been minimized.

gopherbot commented Dec 3, 2018

Change https://golang.org/cl/152297 mentions this issue: encoding/json: created json.UnknownFieldsError

@andybons

This comment has been minimized.

Member

andybons commented Dec 5, 2018

Per discussion with @golang/proposal-review, putting on hold until we can review JSON issues en masse.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment