从映射和结构体中编组的JSON中的排序差异

huangapple go评论102阅读模式
英文:

Ordering Difference in JSON Marshaled from Map and Struct

问题

map[string]interface{}和等效的struct进行编组时,返回的JSON在键的内部顺序上是相似但不同的,如下所示:

var arg1 = map[string]interface{}{
    "foo": "bar",
    "baz": map[string]interface{}{
        "bee": "boo",
    },
}

type Arg struct {
    Foo string                 `json:"foo"`
    Baz map[string]interface{} `json:"baz"`
}

var arg2 = &Arg{
    Foo: "bar",
    Baz: map[string]interface{}{
        "bee": "boo",
    },
}

func main() {
    result1, _ := json.Marshal(arg1)
    result2, _ := json.Marshal(arg2)

    fmt.Println(reflect.DeepEqual(result1, result2))
    fmt.Println(string(result1))
    fmt.Println(string(result2))

    // false
    // {"baz":{"bee":"boo"},"foo":"bar"}
    // {"foo":"bar","baz":{"bee":"boo"}}
}

这里的playground上进行了检查。

尽管在JSON中顺序无关紧要,但我想知道为什么结果的顺序不同?

英文:

When marshaling from a map[string]interface{} and an equivalent struct, the JSON returned are similar but different in the internal order of the keys likewise:

var arg1 = map[string]interface{}{
    "foo": "bar",
    "baz": map[string]interface{}{
        "bee": "boo",
    },
}

type Arg struct {
	Foo string                 `json:"foo"`
	Baz map[string]interface{} `json:"baz"`
}

var arg2 = &Arg{
	Foo: "bar",
	Baz: map[string]interface{}{
		"bee": "boo",
	},
}

func main() {
    result1, _ := json.Marshal(arg1)
	result2, _ := json.Marshal(arg2)

    fmt.Println(reflect.DeepEqual(result1, result2)
    fmt.Println(string(result1))
    fmt.Println(string(result2))

    // false
    // {"baz":{"bee":"boo"},"foo":"bar"}
    // {"foo":"bar","baz":{"bee":"boo"}}

}

Check the playground here.

Although in JSON the order doesn't matter, I'd like to know why are the results ordered differently?

答案1

得分: 8

encoding/json包将映射按排序键顺序进行编组,而结构体按字段声明顺序进行编组。

尽管encoding/json包使用的顺序没有记录在案,但可以安全地假设映射按排序键顺序进行编组,而结构体按字段声明顺序进行编组。标准库和其他地方有许多依赖于这些顺序的测试。

英文:

The encoding/json package marshals maps in sorted key order and structs in the order that the fields are declared.

Although the order used by the encoding/json package is not documented, it's safe to assume that maps are marshaled in sorted key order and structs are marshaled in field declaration order. There are many tests in the standard library and elsewhere that depend on these orders.

答案2

得分: 6

映射中元素的顺序在Go中是不确定的,因为Go使用哈希表来实现它们。你应该预期元素以任意顺序进行编组。

在编组结构时,JSON字段的顺序应该与结构中字段的顺序相匹配。这是通过反射访问字段的副作用。但是你不应该依赖这个副作用。

在JSON中,元素的顺序是不重要的。比较JSON结构的问题与比较具有无序子节点的树的问题相同。

英文:

The order of elements in maps is not defined as Go uses a hash table to implement them. You should expect the elements to be marshalled in any order.

When marshalling a structure, the order of JSON fields should match the order of fields in structure. This is a side effect of accessing fields through reflection. Still you should not rely on this side effect.

The order of elements is not significant in JSON. The problem of comparing JSON structures is the same as the problem of comparing trees with unordered child nodes.

huangapple
  • 本文由 发表于 2016年4月18日 23:16:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/36698065.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定