json - DeepEqual: Why does an unmarshalled map return DeepEqual() results differently than a seemingly identical literal -
i don't understand how compare unmarshalled json. example:
package main import ( "fmt" "reflect" "encoding/json" ) func main() { := map[string]interface{} {"foo": 1, "bar": 2} b := map[string]interface{} {"bar": 2, "foo": 1} fmt.printf("literal b %v, deepequal %v\n", b, reflect.deepequal(a, b)) err := json.unmarshal([]byte(`{"bar": 2, "foo": 1}`), &b) if err != nil { panic("could not unmarshal") } fmt.printf("umarshalled b %v, deepequal %v\n", b, reflect.deepequal(a, b)) }
prints
literal b map[bar:2 foo:1], deepequal true
umarshalled b map[bar:2 foo:1], deepequal false
what different b initialized literal , b after json unmarshalled upon it?
this answers question:
fmt.printf("%t\n", b["foo"]) // prints "float64" after unmarshaling.
json numbers 64-bit floating point numbers, while original map values integers. docs:
to unmarshal json interface value, unmarshal stores 1 of these in interface value:
bool, json booleans float64, json numbers string, json strings []interface{}, json arrays map[string]interface{}, json objects nil json null
Comments
Post a Comment