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

json: marshal nil slice as null #2278

Closed
gopherbot opened this issue Sep 20, 2011 · 10 comments
Closed

json: marshal nil slice as null #2278

gopherbot opened this issue Sep 20, 2011 · 10 comments

Comments

@gopherbot
Copy link
Contributor

by awreece:

What steps will reproduce the problem?
1. Marshal a struct that contains a nil []byte member.

For example,    
    type MyType struct {
        Buf []byte
        Ptr *int
        Map map[string]string
    }
    bytes, _ := json.Marshal(&MyType{nil, nil, nil})
    fmt.Println(string(bytes))

What is the expected output?
The member will be null. I expect to see:
j{"Buf":null,"Ptr":null,"Map":null}  

What do you see instead? The member is empty:
{"Buf":"","Ptr":null,"Map":null}

(A subsequent json.Unmarshal confirms this, and produces a value equivalent to
MyType{make([]byte,0,0), nil, nil} )

Which compiler are you using (5g, 6g, 8g, gccgo)?
8g

Which operating system are you using?
Ubuntu 10.04

Which revision are you using?  (hg identify)
253f02222ab0 tip

Attachments:

  1. json_test.go (1049 bytes)
@rsc
Copy link
Contributor

rsc commented Sep 20, 2011

Comment 1:

In Go, a nil byte slice *is* an empty byte slice.
We could pick off nil as a special case, of course,
but then that will break code that expects it to
be handled like any other slice.  I'm not sure
there's a way to make everyone happy.
Russ

@gopherbot
Copy link
Contributor Author

Comment 2 by awreece:

Please correct me if I'm wrong, but a nil byte slice is an empty byte slice while a nil
map is not an empty map? Why the difference?
~Alex

@rsc
Copy link
Contributor

rsc commented Sep 20, 2011

Comment 3:

Actually, a nil map is an empty map now.
Maybe I should fix its json output too.
Russ

@rsc
Copy link
Contributor

rsc commented Sep 23, 2011

Comment 4:

On second thought, maybe both should be null.  I'm not sure.

@gopherbot
Copy link
Contributor Author

Comment 5 by [email protected]:

Unless "nil" byte slice is encoded as JSON "null", it is impossible to distinguish
between missing value and empty string/slice when serializing byte slice fields. Also,
the current behavior is inconsistent:
Suppose we have a JSON object X = {"foo": null}. If we apply
json.Marshal(json.Unmarshal(X)), the current implementation will produce {"foo": ""}.
Oops!
I ran into this issue when using json.Marshal on goprotobuf-genereated types. Goprotobuf
consistently treats "nil" as missing field, while as json.Marshal generates empty string
from it.

@robpike
Copy link
Contributor

robpike commented Oct 4, 2011

Comment 6:

Today they should be null, tomorrow they should be empty. But if both are distinctly
representable in the output, doesn't it make sense to present the distinction?

@rsc
Copy link
Contributor

rsc commented Oct 6, 2011

Comment 7:

Owner changed to @rsc.

Status changed to Accepted.

@rsc
Copy link
Contributor

rsc commented Oct 6, 2011

Comment 8:

Status changed to HelpWanted.

@gopherbot
Copy link
Contributor Author

Comment 10 by awreece:

I would be willing to work on this, I was looking for something to get started on.

@rsc
Copy link
Contributor

rsc commented Oct 31, 2011

Comment 11:

This issue was closed by revision 48c75c5.

Status changed to Fixed.

This issue was closed.
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