Skip to content

json package does not handle nil values #400

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

Closed
zombiezen opened this issue Dec 9, 2009 · 3 comments
Closed

json package does not handle nil values #400

zombiezen opened this issue Dec 9, 2009 · 3 comments

Comments

@zombiezen
Copy link
Contributor

What steps will reproduce the problem?
1. Run this Go source code:

package main 
import 
( 
    "fmt"; 
    "json"; 
    "os"; 
) 
func main() 
{ 
    jsonData := make(map[string]interface{}); 
    jsonData["foo"] = "bar"; 
    jsonData["fail"] = nil; 
    err := json.Marshal(os.Stdout, jsonData); 
    if err != nil 
    { 
        fmt.Fprintf(os.Stderr, "Error while marshalling: %s\n", err); 
        os.Exit(1); 
    } 
    fmt.Printf("\n"); 
} 

2. *OR* Run this source code:

package main 
import 
( 
    "fmt"; 
    "json"; 
    "os"; 
) 
func main() 
{ 
    var jsonData interface{} = nil; 
    err := json.Marshal(os.Stdout, jsonData); 
    if err != nil 
    { 
        fmt.Fprintf(os.Stderr, "Error while marshalling: %s\n", err); 
        os.Exit(1); 
    } 
    fmt.Printf("\n"); 
} 

What is the expected output? What do you see instead?

I would expect the first case to print
`{"foo":"bar","fail":null}`, but it gives an error: `Error
while 
marshalling: json cannot encode value of type interface { }`

The second case should just print `null`, but it SEGSEGVs.

What is your $GOOS?  $GOARCH?

darwin amd64

Which revision are you using?  (hg identify)
c36376f7c054+ tip

Please provide any additional information below.

I originally reported this on the go-nuts mailing list:
http://groups.google.com/group/golang-
nuts/browse_thread/thread/4a7972add964dd7/feba4a6e90bcf1e0

I do have a patch for this, I will post it momentarily.
@zombiezen
Copy link
Contributor Author

Comment 1:

Uploaded to Code Review: http://golang.org/cl/167058

@zombiezen
Copy link
Contributor Author

Comment 2:

This is also my first contribution, so here is the contributing change list: 
http://golang.org/cl/165101
I have electronically signed the Contributor Agreement as well.

@rsc
Copy link
Contributor

rsc commented Dec 9, 2009

Comment 3:

This issue was closed by revision 8a5b76c.

Status changed to Fixed.

Merged into issue #-.

@zombiezen zombiezen added the fixed label Dec 9, 2009
@golang golang locked and limited conversation to collaborators Jun 24, 2016
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