Skip to content

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

@jaswdr

Description

@jaswdr

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions