iancoleman / orderedmap

orderedmap is a golang map where the keys keep the order that they're added. It can be de/serialized from/to JSON. It's based closely on the python collections.OrderedDict.
MIT License
360 stars 55 forks source link

Empty array [] is decoded to nil #11

Closed itchyny closed 5 years ago

itchyny commented 5 years ago

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 commented 5 years ago

Good find, thanks for reporting this.

iancoleman commented 5 years ago
itchyny commented 5 years ago

It works fine! Thank you.