Skip to content

jsoniter.Marshal makes ordered maps unordered #676

Open
@xiaozhiliaoo

Description

@xiaozhiliaoo
func Test_jsoniterMarshal(t *testing.T) {
	m := map[string]int{"c": 3, "a": 1, "b": 2}
	keys := make([]string, 0, len(m))
	for key := range m {
		keys = append(keys, key)
	}
	sort.Strings(keys)
	fmt.Println(m) // map[a:1 b:2 c:3]
	marshal, _ := jsoniter.Marshal(m) // jsoniter "github.com/json-iterator/go" package
	fmt.Println(string(marshal)) // {"c":3,"a":1,"b":2}
}

the map is ordered by sort. but jsoniter.Marshal make the map unordered. but json.Marshal json.Marshal

func Test_jsonMarshal(t *testing.T) {
	m := map[string]int{"c": 3, "a": 1, "b": 2}
	keys := make([]string, 0, len(m))
	for key := range m {
		keys = append(keys, key)
	}
	sort.Strings(keys)
	marshal, _ := json.Marshal(m) // "encoding/json" package
	fmt.Println(string(marshal)) //{"a":1,"b":2,"c":3}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions