Skip to content
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: Allow specifying a field which takes all unexpected fields when parsing JSON #42417

Closed
mitar opened this issue Nov 6, 2020 · 2 comments

Comments

@mitar
Copy link
Contributor

mitar commented Nov 6, 2020

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

go version go1.14.4 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
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/mitar/.cache/go-build"
GOENV="/home/mitar/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/mitar/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
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-build926066688=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I am trying to parse JSON which has some known/expected fields and some uknown.

What did you expect to see?

Single-pass parsiing of both known and unknown fields.

What did you see instead?

Existing approaches all have downsides. It seems that with standard encoding/json package the best approach is to parse JSON twice, once with the struct with known fields, and once into map[string]interface{}. I find this ugly, redundant, and slow. I would propose to use symbol + to mark a field into which unknown fields should go. So struct could be defined like:

type Message struct {
  Name  string                 `json:"name"`
  Type  string                 `json:"type"`
  Skip  string                 `json:"-"`
  Extra map[string]interface{} `json:"+"`
}
@gopherbot gopherbot added this to the Proposal milestone Nov 6, 2020
@mvdan
Copy link
Member

mvdan commented Nov 6, 2020

Duplicate of #22533.

@mvdan mvdan closed this as completed Nov 6, 2020
@mitar
Copy link
Contributor Author

mitar commented Nov 6, 2020

I think it is more a duplicate of #6213.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants