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: Maybe a bug of encoding/json. #11283

Closed
conc opened this issue Jun 19, 2015 · 2 comments
Closed

encoding/json: Maybe a bug of encoding/json. #11283

conc opened this issue Jun 19, 2015 · 2 comments
Milestone

Comments

@conc
Copy link

conc commented Jun 19, 2015

package main

import (
    "encoding/json"
    "fmt"
)

func main() {
    // jsonDeal([]byte(`{"name":"xiaoming","course":["1111","2222"]}`))
    // jsonDeal([]byte(`{"name":"xiaoming","course":["1111"]}`))
    jsonDeal([]byte(`{"name":"xiaoming","course":[null]}`))
    // jsonDeal([]byte(`{"name":"xiaoming","course":[]}`))
    // jsonDeal([]byte(`{"name":"xiaoming","course":null}`))
}

type User struct {
    Name   string   `json:"name"`
    Course []string `json:"course"`
}

func jsonDeal(data []byte) {
    var oneUser User
    if err := json.Unmarshal(data, &oneUser); err != nil {
        panic(err)
        return
    }
    fmt.Println("==============")
    fmt.Println("oridata:", string(data))
    fmt.Println("json result:", oneUser)
    fmt.Println("course num:", len(oneUser.Course))
    return
}

I think len(oneUser.Course) should be 0,but now it is 1.

@ianlancetaylor ianlancetaylor changed the title Maybe a bug of encoding/json. encoding/json: Maybe a bug of encoding/json. Jun 19, 2015
@ianlancetaylor ianlancetaylor added this to the Go1.6 milestone Jun 19, 2015
@dspezia
Copy link
Contributor

dspezia commented Jun 19, 2015

I don't see why you think it is a bug. IMO, a [null] array is not the same as a [] array ...

@conc
Copy link
Author

conc commented Jun 19, 2015

@dspezia ,I understand, [null]string equals [""]string.
thanks.

@conc conc closed this as completed Jun 19, 2015
@golang golang locked and limited conversation to collaborators Jun 25, 2016
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

4 participants