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: encoding/json: option to clear output before unmarshal #49334

Closed
coco-roll opened this issue Nov 4, 2021 · 6 comments
Closed

proposal: encoding/json: option to clear output before unmarshal #49334

coco-roll opened this issue Nov 4, 2021 · 6 comments

Comments

@coco-roll
Copy link

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

$ go version
go version go1.16.4 darwin/amd64

Does this issue reproduce with the latest release?

The lastst release is also reproduce

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

go env Output
$ go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/Library/Caches/go-build"
GOENV="/Users/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/go"
GOPRIVATE=""
GOPROXY="direct"
GOROOT="/usr/local/Cellar/go/1.16.4/libexec"
GOSUMDB=""
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.16.4/libexec/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.16.4"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/go/src/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/7p/syywyq8s4t500nf_fhcw58km0000gn/T/go-build1825247537=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

package main

import (
	"encoding/json"
	"fmt"
)

func main() {
	var testMap map[string]interface{}

	strArr := "{\"11911007\":{\"11911007\":1,\"119110010\":1,\"11911006\":1,\"11911005\":1,\"119110011\":1,\"119110012\":1,\"11911004\":1,\"119110015\":1},\"119110010\":{\"000000\":1}}"
	if err := json.Unmarshal([]byte(strArr), &testMap); err != nil {
		panic(err)
	}
	fmt.Printf("first time Unmarshal map is:%+v\n", testMap)

	if err := json.Unmarshal([]byte("{\"11911007\":{\"000001\":1}}"), &testMap); err != nil {
		panic(err)
	}
	fmt.Printf("second time Unmarshal map is:%+v\n", testMap)

	return
}

What did you expect to see?

See:

first time Unmarshal map is:map[119110010:map[000000:1] 11911007:map[119110010:1 119110011:1 119110012:1 119110015:1 11911004:1 11911005:1 11911006:1 11911007:1]]
second time Unmarshal map is:map[1911007:map[000001:1]]

What did you see instead?

See:

first time Unmarshal map is:map[119110010:map[000000:1] 11911007:map[119110010:1 119110011:1 119110012:1 119110015:1 11911004:1 11911005:1 11911006:1 11911007:1]]
second time Unmarshal map is:map[119110010:map[000000:1] 11911007:map[000001:1]]
@coco-roll
Copy link
Author

Can i add a new function like it?

// To unmarshal a JSON object into a map, Unmarshal first establishes a map to
// use. If the map is nil or not, Unmarshal allocates a new map. 
func UnmarshalDeep(data []byte, v interface{}) error {}

@seankhliao seankhliao changed the title json.Unmarshal not work good at object proposal: encoding/json: option to clear output before unmarshal Nov 4, 2021
@gopherbot gopherbot added this to the Proposal milestone Nov 4, 2021
@andig
Copy link
Contributor

andig commented Nov 4, 2021

Imho it‘s simply not how unmarshal works. More importantly, you can use the current behaviour to establish default values.

@coco-roll
Copy link
Author

Yes, i change my code for use it. Thanks all.

@ianlancetaylor ianlancetaylor added this to Incoming in Proposals (old) Nov 10, 2021
@rsc
Copy link
Contributor

rsc commented Nov 11, 2021

Sounds like this proposal has been retracted?

@mvdan
Copy link
Member

mvdan commented Nov 21, 2021

Sounds that way. Closing.

@mvdan mvdan closed this as completed Nov 21, 2021
@rsc rsc moved this from Incoming to Declined in Proposals (old) Dec 1, 2021
@rsc
Copy link
Contributor

rsc commented Dec 1, 2021

This proposal has been declined as retracted.
— rsc for the proposal review group

@golang golang locked and limited conversation to collaborators Dec 1, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
No open projects
Development

No branches or pull requests

5 participants