| 1 |
package json_test |
| 2 |
|
| 3 |
import ( |
| 4 |
stdjson "encoding/json" |
| 5 |
"math" |
| 6 |
"strconv" |
| 7 |
"testing" |
| 8 |
"time" |
| 9 |
|
| 10 |
"github.com/goccy/go-json" |
| 11 |
) |
| 12 |
|
| 13 |
// The entries of a sorted map whose values are scalars or of interface{} are written directly by one call of the |
| 14 |
// VM, up to an entry of interface{} whose value is not a scalar, which its opcodes write. The values of every kind in any order, with indent and |
| 15 |
// colors, must encode as encoding/json does. |
| 16 |
func TestEncodeSortedMapDirectEntries(t *testing.T) { |
| 17 |
one := 1 |
| 18 |
var nilInt *int |
| 19 |
values := []any{ |
| 20 |
1, -2, uint8(3), 1.5, float32(2.5), "s", "<&>", true, false, nil, &one, nilInt, |
| 21 |
[]int{1}, []any{}, map[string]any{"z": 1, "a": "b"}, struct{ A int }{1}, &struct{ B string }{"c"}, |
| 22 |
time.Date(2026, 9, 27, 0, 0, 0, 0, time.UTC), stdjson.RawMessage(`{"r":1}`), []byte("bytes"), |
| 23 |
} |
| 24 |
var maps []any |
| 25 |
for n := 1; n <= 3*len(values); n++ { |
| 26 |
m := map[string]any{} |
| 27 |
for i := 0; i < n; i++ { |
| 28 |
m["k"+strconv.Itoa(i*7919%1000)] = values[(i*31+n)%len(values)] |
| 29 |
} |
| 30 |
maps = append(maps, m, []any{m, map[string]any{"in": m}}) |
| 31 |
} |
| 32 |
maps = append(maps, |
| 33 |
map[string]int{"b": 2, "a": 1, "c": 3}, |
| 34 |
map[string]string{"b": "x", "a": "<y>"}, |
| 35 |
map[string]bool{"t": true, "f": false}, |
| 36 |
map[string][]byte{"b": []byte("x"), "a": nil}, |
| 37 |
map[string]*int{"a": &one, "b": nil}, |
| 38 |
map[string]any{}, |
| 39 |
) |
| 40 |
for _, v := range maps { |
| 41 |
want, err := stdjson.Marshal(v) |
| 42 |
if err != nil { |
| 43 |
t.Fatal(err) |
| 44 |
} |
| 45 |
got, err := json.Marshal(v) |
| 46 |
if err != nil || string(got) != string(want) { |
| 47 |
t.Fatalf("%v:\n got %s %v\nwant %s", v, got, err, want) |
| 48 |
} |
| 49 |
want, _ = stdjson.MarshalIndent(v, "", " ") |
| 50 |
got, err = json.MarshalIndent(v, "", " ") |
| 51 |
if err != nil || string(got) != string(want) { |
| 52 |
t.Fatalf("%v with indent:\n got %s %v\nwant %s", v, got, err, want) |
| 53 |
} |
| 54 |
// the colored VMs, with a scheme of no colors, write what the others write. |
| 55 |
noColor := json.Colorize(&json.ColorScheme{}) |
| 56 |
want, _ = stdjson.Marshal(v) |
| 57 |
got, err = json.MarshalWithOption(v, noColor) |
| 58 |
if err != nil || string(got) != string(want) { |
| 59 |
t.Fatalf("%v colored:\n got %s %v\nwant %s", v, got, err, want) |
| 60 |
} |
| 61 |
want, _ = stdjson.MarshalIndent(v, "", " ") |
| 62 |
got, err = json.MarshalIndentWithOption(v, "", " ", noColor) |
| 63 |
if err != nil || string(got) != string(want) { |
| 64 |
t.Fatalf("%v colored with indent:\n got %s %v\nwant %s", v, got, err, want) |
| 65 |
} |
| 66 |
} |
| 67 |
// an error of a scalar is returned. |
| 68 |
for _, v := range []any{map[string]any{"a": 1, "b": math.NaN()}, map[string]float64{"a": math.Inf(1)}} { |
| 69 |
if _, err := json.Marshal(v); err == nil { |
| 70 |
t.Fatalf("%v: expected an error", v) |
| 71 |
} |
| 72 |
} |
| 73 |
} |