| 1 |
package json_test |
| 2 |
|
| 3 |
import ( |
| 4 |
stdjson "encoding/json" |
| 5 |
"sort" |
| 6 |
"strings" |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/goccy/go-json" |
| 10 |
) |
| 11 |
|
| 12 |
// A map whose key is a plain string is encoded by fewer opcodes than a map with a key of another kind, which |
| 13 |
// its own opcode writes: both must encode as encoding/json does, sorted and with an indent, and the option |
| 14 |
// to leave the map unsorted must write every entry. |
| 15 |
|
| 16 |
type mapKeyString string |
| 17 |
|
| 18 |
type mapKeyText struct{ V int } |
| 19 |
|
| 20 |
func (k mapKeyText) MarshalText() ([]byte, error) { return []byte(strings.Repeat("t", k.V)), nil } |
| 21 |
|
| 22 |
func TestEncodeMapKeys(t *testing.T) { |
| 23 |
values := []any{ |
| 24 |
map[string]int{}, |
| 25 |
map[string]int{"": 0}, |
| 26 |
map[string]int{"b": 2, "a": 1, "c": 3}, |
| 27 |
map[string]any{"a": 1, "b": 2.5, "c": "s", "d": nil, "e": []int{1}, "f": map[string]int{"x": 1}}, |
| 28 |
map[string]string{"k\"q": "v", "<html>": "&", "日本": "語", "\x00": "c"}, |
| 29 |
map[mapKeyString]int{"y": 1, "x": 2}, |
| 30 |
map[string]*int{"nil": nil}, |
| 31 |
map[string]map[string]int{"outer": {"inner": 1}, "empty": {}, "nil": nil}, |
| 32 |
map[int]string{2: "b", 10: "j", 1: "a"}, |
| 33 |
map[mapKeyText]int{{1}: 1, {3}: 3, {2}: 2}, |
| 34 |
struct { |
| 35 |
M map[string]int `json:"m"` |
| 36 |
P *map[string]int `json:"p"` |
| 37 |
N map[string]int `json:"n,omitempty"` |
| 38 |
MM map[string]bool `json:"mm"` |
| 39 |
}{M: map[string]int{"b": 1, "a": 2}, P: &map[string]int{"z": 0}, MM: map[string]bool{"t": true, "f": false}}, |
| 40 |
[]map[string]int{{"a": 1}, nil, {}}, |
| 41 |
json.Number("1"), |
| 42 |
} |
| 43 |
for _, v := range values { |
| 44 |
expected, err := stdjson.Marshal(v) |
| 45 |
if err != nil { |
| 46 |
t.Fatal(err) |
| 47 |
} |
| 48 |
got, err := json.Marshal(v) |
| 49 |
if err != nil { |
| 50 |
t.Fatalf("%T: %v", v, err) |
| 51 |
} |
| 52 |
if string(got) != string(expected) { |
| 53 |
t.Errorf("%T:\n got %s\nwant %s", v, got, expected) |
| 54 |
} |
| 55 |
expected, _ = stdjson.MarshalIndent(v, "", " ") |
| 56 |
got, err = json.MarshalIndent(v, "", " ") |
| 57 |
if err != nil { |
| 58 |
t.Fatalf("%T: %v", v, err) |
| 59 |
} |
| 60 |
if string(got) != string(expected) { |
| 61 |
t.Errorf("%T with indent:\n got %s\nwant %s", v, got, expected) |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
func TestEncodeMapUnordered(t *testing.T) { |
| 67 |
m := map[string]any{"a": 1, "b": 2.5, "c": "s", "d": nil, "e": []int{1}, "f": map[string]int{"x": 1}} |
| 68 |
got, err := json.MarshalWithOption(m, json.UnorderedMap()) |
| 69 |
if err != nil { |
| 70 |
t.Fatal(err) |
| 71 |
} |
| 72 |
// every entry is written, in the order of the map: sorted here to compare. |
| 73 |
var decoded map[string]any |
| 74 |
if err := stdjson.Unmarshal(got, &decoded); err != nil { |
| 75 |
t.Fatalf("%s: %v", got, err) |
| 76 |
} |
| 77 |
keys := make([]string, 0, len(decoded)) |
| 78 |
for k := range decoded { |
| 79 |
keys = append(keys, k) |
| 80 |
} |
| 81 |
sort.Strings(keys) |
| 82 |
if strings.Join(keys, ",") != "a,b,c,d,e,f" { |
| 83 |
t.Fatalf("unexpected keys %v in %s", keys, got) |
| 84 |
} |
| 85 |
indented, err := json.MarshalIndentWithOption(m, "", " ", json.UnorderedMap()) |
| 86 |
if err != nil { |
| 87 |
t.Fatal(err) |
| 88 |
} |
| 89 |
if err := stdjson.Unmarshal(indented, &decoded); err != nil || len(decoded) != 6 { |
| 90 |
t.Fatalf("%s: %v", indented, err) |
| 91 |
} |
| 92 |
} |