| 1 |
package json_test |
| 2 |
|
| 3 |
import ( |
| 4 |
stdjson "encoding/json" |
| 5 |
"errors" |
| 6 |
"reflect" |
| 7 |
"strings" |
| 8 |
"testing" |
| 9 |
"time" |
| 10 |
"unsafe" |
| 11 |
|
| 12 |
json "github.com/goccy/go-json" |
| 13 |
) |
| 14 |
|
| 15 |
type unmarshalOfSmall struct { |
| 16 |
St int |
| 17 |
Sid int |
| 18 |
Tt string |
| 19 |
Gr int |
| 20 |
Uuid string |
| 21 |
Ip string |
| 22 |
Ua string |
| 23 |
Tz int |
| 24 |
V int |
| 25 |
} |
| 26 |
|
| 27 |
const unmarshalOfSmallInput = `{"st": 1,"sid": 486,"tt": "active","gr": 0,"uuid": "de305d54-75b4-431b-adb2-eb6b9e546014","ip": "127.0.0.1","ua": "user_agent","tz": -6,"v": 1}` |
| 28 |
|
| 29 |
// unmarshalOfCase decodes the input by UnmarshalOf and by Unmarshal into two values which start the same. |
| 30 |
func unmarshalOfCase[T any](t *testing.T, input string, start T) { |
| 31 |
t.Helper() |
| 32 |
got, want := start, start |
| 33 |
errOf := json.UnmarshalOf([]byte(input), &got) |
| 34 |
err := json.Unmarshal([]byte(input), &want) |
| 35 |
if (errOf == nil) != (err == nil) || (err != nil && errOf.Error() != err.Error()) { |
| 36 |
t.Fatalf("%s: UnmarshalOf: %v, Unmarshal: %v", input, errOf, err) |
| 37 |
} |
| 38 |
if !reflect.DeepEqual(got, want) { |
| 39 |
t.Fatalf("%s: UnmarshalOf: %#v, Unmarshal: %#v", input, got, want) |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
type unmarshalOfLarge struct { |
| 44 |
A [600]int |
| 45 |
Name string |
| 46 |
} |
| 47 |
|
| 48 |
func TestUnmarshalOfAsUnmarshal(t *testing.T) { |
| 49 |
unmarshalOfCase(t, unmarshalOfSmallInput, unmarshalOfSmall{}) |
| 50 |
// into a value which exists: the fields absent from the input are kept. |
| 51 |
unmarshalOfCase(t, `{"st":5}`, unmarshalOfSmall{Tt: "kept", V: 9}) |
| 52 |
unmarshalOfCase(t, `{"a":[1,2],"m":{"x":"y"},"p":{"n":3}}`, struct { |
| 53 |
A []int |
| 54 |
M map[string]string |
| 55 |
P *struct{ N int } |
| 56 |
}{}) |
| 57 |
unmarshalOfCase(t, `[1,"a",{"b":null}]`, any(nil)) |
| 58 |
unmarshalOfCase(t, `"2026-09-24T00:00:00Z"`, time.Time{}) |
| 59 |
unmarshalOfCase(t, `123`, 0) |
| 60 |
unmarshalOfCase(t, `"s"`, "") |
| 61 |
unmarshalOfCase(t, `{"x":1}`, map[string]int{"y": 2}) |
| 62 |
unmarshalOfCase(t, `[1,2,3]`, []int{}) |
| 63 |
unmarshalOfCase(t, `{"Name":"large","A":[1,2,3]}`, unmarshalOfLarge{}) |
| 64 |
// errors, with what was decoded before them |
| 65 |
unmarshalOfCase(t, `{"st":1,"tt":2}`, unmarshalOfSmall{}) |
| 66 |
unmarshalOfCase(t, `{"st":1} x`, unmarshalOfSmall{}) |
| 67 |
unmarshalOfCase(t, `{"st":`, unmarshalOfSmall{}) |
| 68 |
|
| 69 |
var nilPtr *unmarshalOfSmall |
| 70 |
var invalid *json.InvalidUnmarshalError |
| 71 |
if err := json.UnmarshalOf([]byte(`{}`), nilPtr); !errors.As(err, &invalid) { |
| 72 |
t.Fatalf("expected InvalidUnmarshalError, got %v", err) |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
func TestUnmarshalOfOtherTypesByTurns(t *testing.T) { |
| 77 |
// The values reused by a context are of the type decoded last: other types come by turns. |
| 78 |
for i := 0; i < 20; i++ { |
| 79 |
var a unmarshalOfSmall |
| 80 |
if err := json.UnmarshalOf([]byte(unmarshalOfSmallInput), &a); err != nil || a.Uuid != "de305d54-75b4-431b-adb2-eb6b9e546014" { |
| 81 |
t.Fatalf("got %+v, %v", a, err) |
| 82 |
} |
| 83 |
var b struct{ X []string } |
| 84 |
if err := json.UnmarshalOf([]byte(`{"X":["p","q"]}`), &b); err != nil || !reflect.DeepEqual(b.X, []string{"p", "q"}) { |
| 85 |
t.Fatalf("got %+v, %v", b, err) |
| 86 |
} |
| 87 |
// the reused value is zero: nothing of the previous call is decoded into |
| 88 |
var c unmarshalOfSmall |
| 89 |
if err := json.UnmarshalOf([]byte(`{"st":1}`), &c); err != nil || c != (unmarshalOfSmall{St: 1}) { |
| 90 |
t.Fatalf("got %+v, %v", c, err) |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
func TestUnmarshalOfZeroAllocation(t *testing.T) { |
| 96 |
if raceEnabled { |
| 97 |
t.Skip("sync.Pool drops items at random under the race detector") |
| 98 |
} |
| 99 |
data := []byte(unmarshalOfSmallInput) |
| 100 |
var v unmarshalOfSmall |
| 101 |
for _, opts := range [][]json.DecodeOptionFunc{nil, {json.DecodeNoCopyString()}} { |
| 102 |
allocs := testing.AllocsPerRun(1000, func() { |
| 103 |
var w unmarshalOfSmall |
| 104 |
if err := json.UnmarshalOf(data, &w, opts...); err != nil { |
| 105 |
t.Fatal(err) |
| 106 |
} |
| 107 |
v = w |
| 108 |
}) |
| 109 |
// The strings share a buffer of the context, which is allocated once in a while. |
| 110 |
if allocs >= 0.1 { |
| 111 |
t.Fatalf("options %d: %v allocations per call", len(opts), allocs) |
| 112 |
} |
| 113 |
} |
| 114 |
if v.Ua != "user_agent" { |
| 115 |
t.Fatalf("got %+v", v) |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
func TestDecodedStringsOwnership(t *testing.T) { |
| 120 |
type pair struct { |
| 121 |
A string |
| 122 |
B string |
| 123 |
M map[string]any |
| 124 |
} |
| 125 |
input := `{"A":"plain","B":"esc\"aped","M":{"key":"value"}}` |
| 126 |
decoders := map[string]func([]byte, *pair, ...json.DecodeOptionFunc) error{ |
| 127 |
"Unmarshal": func(data []byte, v *pair, opts ...json.DecodeOptionFunc) error { |
| 128 |
return json.UnmarshalWithOption(data, v, opts...) |
| 129 |
}, |
| 130 |
"UnmarshalOf": func(data []byte, v *pair, opts ...json.DecodeOptionFunc) error { |
| 131 |
return json.UnmarshalOf(data, v, opts...) |
| 132 |
}, |
| 133 |
} |
| 134 |
for name, decode := range decoders { |
| 135 |
// By default the strings are copies: the input may be modified after the call, |
| 136 |
// and the buffer of the input is reused by the next call. |
| 137 |
data := []byte(input) |
| 138 |
var v pair |
| 139 |
if err := decode(data, &v); err != nil { |
| 140 |
t.Fatal(err) |
| 141 |
} |
| 142 |
for i := range data { |
| 143 |
data[i] = 'X' |
| 144 |
} |
| 145 |
var other pair |
| 146 |
if err := decode([]byte(strings.ReplaceAll(input, "plain", "PLAIN")), &other); err != nil { |
| 147 |
t.Fatal(err) |
| 148 |
} |
| 149 |
if v.A != "plain" || v.B != `esc"aped` || v.M["key"] != "value" { |
| 150 |
t.Fatalf("%s: the strings changed with the input: %+v", name, v) |
| 151 |
} |
| 152 |
// With DecodeNoCopyString a string without an escape refers to the input. |
| 153 |
data = []byte(input) |
| 154 |
v = pair{} |
| 155 |
if err := decode(data, &v, json.DecodeNoCopyString()); err != nil { |
| 156 |
t.Fatal(err) |
| 157 |
} |
| 158 |
if v.A != "plain" || v.B != `esc"aped` || v.M["key"] != "value" { |
| 159 |
t.Fatalf("%s: got %+v", name, v) |
| 160 |
} |
| 161 |
start := uintptr(unsafe.Pointer(&data[0])) |
| 162 |
within := func(s string) bool { |
| 163 |
p := uintptr(unsafe.Pointer(unsafe.StringData(s))) |
| 164 |
return p >= start && p < start+uintptr(len(data)) |
| 165 |
} |
| 166 |
if !within(v.A) || !within(v.M["key"].(string)) { |
| 167 |
t.Fatalf("%s: a string without an escape doesn't refer to the input", name) |
| 168 |
} |
| 169 |
if within(v.B) { |
| 170 |
t.Fatalf("%s: an escaped string refers to the input, which has other bytes", name) |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
func TestDecodedEscapedStringsAsEncodingJSON(t *testing.T) { |
| 176 |
inputs := []string{ |
| 177 |
`"plain"`, `"tab\there"`, `"quote\"and\\backslash\/slash"`, `"\u00e9\u3042"`, `"\ud83d\ude00 surrogate"`, |
| 178 |
`"\ud83d alone"`, "\"bad \xff byte\"", "\"bad \xff with \\n escape\"", `"` + strings.Repeat(`a\n`, 400) + `"`, |
| 179 |
`""`, `"\u0000"`, |
| 180 |
} |
| 181 |
type holder struct { |
| 182 |
S string |
| 183 |
M map[string]string |
| 184 |
A any |
| 185 |
} |
| 186 |
for _, in := range inputs { |
| 187 |
doc := `{"S":` + in + `,"M":{` + in + `:` + in + `},"A":[` + in + `]}` |
| 188 |
var want holder |
| 189 |
if err := stdjsonUnmarshal([]byte(doc), &want); err != nil { |
| 190 |
t.Fatalf("%s: %v", doc, err) |
| 191 |
} |
| 192 |
for name, decode := range map[string]func([]byte, *holder) error{ |
| 193 |
"Unmarshal": func(d []byte, v *holder) error { return json.Unmarshal(d, v) }, |
| 194 |
"UnmarshalOf": func(d []byte, v *holder) error { return json.UnmarshalOf(d, v) }, |
| 195 |
"NoCopyString": func(d []byte, v *holder) error { |
| 196 |
return json.UnmarshalWithOption(d, v, json.DecodeNoCopyString()) |
| 197 |
}, |
| 198 |
"Decoder": func(d []byte, v *holder) error { return json.NewDecoder(strings.NewReader(string(d))).Decode(v) }, |
| 199 |
} { |
| 200 |
var got holder |
| 201 |
if err := decode([]byte(doc), &got); err != nil { |
| 202 |
t.Fatalf("%s %s: %v", name, doc, err) |
| 203 |
} |
| 204 |
if !reflect.DeepEqual(got, want) { |
| 205 |
t.Fatalf("%s %s:\n got %q\nwant %q", name, doc, got, want) |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
func stdjsonUnmarshal(data []byte, v any) error { |
| 212 |
return stdjson.Unmarshal(data, v) |
| 213 |
} |