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

encoding/json: Unmarshal: slice handling underspecified? #8837

Closed
masiulaniec opened this issue Sep 29, 2014 · 1 comment

Comments

Projects
None yet
5 participants
@masiulaniec
Copy link

commented Sep 29, 2014

In the below code (from http://play.golang.org/p/gX3MRTQyPA), it appears impossible to
predict program's output based solely on documentation of package encoding/json.

    list := []int{-1}
    err := json.Unmarshal([]byte("[1]"), &list)
    if err != nil 
        log.Fatal(err)
    }
    log.Printf("%+v", list)

I can think of the following likely outcomes:

  [-1]
  [1]
  [-1, 1]

Package encoding/json chooses [1] but I cannot seem to find a sentence that guarantees
it.

In contrast, relevant quote from package encoding/xml: "Unmarshal maps an XML
element to a slice by extending the length of the slice and mapping the element to the
newly created value."
@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Sep 29, 2014

Comment 1:

Labels changed: added repo-main, release-go1.5, documentation.

@bradfitz bradfitz modified the milestone: Go1.5 Dec 16, 2014

@rsc rsc removed the repo-main label Apr 14, 2015

@rsc rsc closed this in 749b391 Jul 15, 2015

@golang golang locked and limited conversation to collaborators Jul 18, 2016

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.