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

Empty array [] is decoded to nil #11

Closed
itchyny opened this Issue Mar 14, 2019 · 3 comments

Comments

Projects
None yet
2 participants
@itchyny
Copy link

commented Mar 14, 2019

It seems that [] in value is decoded to []interface{}(nil) but I expect []interface{}{}

Sample code

package main

import (
	"encoding/json"
	"fmt"

	"github.com/iancoleman/orderedmap"
)

func main() {
	src := []byte(`{"x":[]}`)

	var m interface{}
	json.Unmarshal(src, &m)
	fmt.Printf("%#v\n", m)
	bs, _ := json.Marshal(m)
	fmt.Printf("%s\n", string(bs))

	om := orderedmap.New()
	json.Unmarshal(src, om)
	fmt.Printf("%#v\n", om)
	bs, _ = json.Marshal(om)
	fmt.Printf("%s\n", string(bs))
}

Actual behavior

map[string]interface {}{"x":[]interface {}{}}
{"x":[]}
&orderedmap.OrderedMap{keys:[]string{"x"}, values:map[string]interface {}{"x":[]interface {}(nil)}}
{"x":null}

Expected behavior

map[string]interface {}{"x":[]interface {}{}}
{"x":[]}
&orderedmap.OrderedMap{keys:[]string{"x"}, values:map[string]interface {}{"x":[]interface {}{}}
{"x":[]}
@iancoleman

This comment has been minimized.

Copy link
Owner

commented Mar 18, 2019

Good find, thanks for reporting this.

@iancoleman

This comment has been minimized.

Copy link
Owner

commented Mar 18, 2019

@iancoleman iancoleman closed this Mar 18, 2019

@itchyny

This comment has been minimized.

Copy link
Author

commented Mar 19, 2019

It works fine! Thank you.

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