Skip to content

json: marshal nil slice as null #2278

Closed
@gopherbot

Description

@gopherbot

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)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions