| 1 |
package json_test |
| 2 |
|
| 3 |
import ( |
| 4 |
stdjson "encoding/json" |
| 5 |
"fmt" |
| 6 |
"math/rand" |
| 7 |
"reflect" |
| 8 |
"strings" |
| 9 |
"testing" |
| 10 |
|
| 11 |
json "github.com/goccy/go-json" |
| 12 |
) |
| 13 |
|
| 14 |
// randomEscapedString returns the JSON literal of a random string of n runes, some of which are escaped in the |
| 15 |
// ways JSON has, with runs of plain bytes of every length between the escapes. |
| 16 |
func randomEscapedString(r *rand.Rand, n int) string { |
| 17 |
var b strings.Builder |
| 18 |
b.WriteByte('"') |
| 19 |
for i := 0; i < n; i++ { |
| 20 |
switch k := r.Intn(20); { |
| 21 |
case k == 0: |
| 22 |
b.WriteString([]string{`\n`, `\r`, `\t`, `\"`, `\\`, `\/`, `\b`, `\f`}[r.Intn(8)]) |
| 23 |
case k == 1: |
| 24 |
fmt.Fprintf(&b, `\u%04x`, []rune{'a', 'é', 'あ', 0x2028, 0}[r.Intn(5)]) |
| 25 |
case k == 2: |
| 26 |
b.WriteString(`\ud83d\ude00`) // a surrogate pair |
| 27 |
case k == 3: |
| 28 |
b.WriteString([]string{"é", "あ", "😀"}[r.Intn(3)]) |
| 29 |
default: |
| 30 |
b.WriteByte(byte('a' + r.Intn(26))) |
| 31 |
} |
| 32 |
} |
| 33 |
b.WriteByte('"') |
| 34 |
return b.String() |
| 35 |
} |
| 36 |
|
| 37 |
func TestDecodeEscapedStrings(t *testing.T) { |
| 38 |
// Escaped strings of every length, decoded into a string copied out of the buffer ( up to 512 bytes ) and |
| 39 |
// decoded in place ( longer ones ), into an interface{} and a map key, as encoding/json does. |
| 40 |
r := rand.New(rand.NewSource(1)) |
| 41 |
for i := 0; i < 3000; i++ { |
| 42 |
lit := randomEscapedString(r, r.Intn(i%700+1)) |
| 43 |
for _, in := range []string{lit, `{"k":` + lit + `}`, `{` + lit + `:1}`, `[` + lit + `,` + lit + `]`} { |
| 44 |
var want, got any |
| 45 |
if err := stdjson.Unmarshal([]byte(in), &want); err != nil { |
| 46 |
t.Fatal(err) |
| 47 |
} |
| 48 |
if err := json.Unmarshal([]byte(in), &got); err != nil { |
| 49 |
t.Fatalf("%s: %v", in, err) |
| 50 |
} |
| 51 |
if fmt.Sprint(got) != fmt.Sprint(want) { |
| 52 |
t.Fatalf("%s:\n got %q\nwant %q", in, got, want) |
| 53 |
} |
| 54 |
} |
| 55 |
var want, got string |
| 56 |
stdjson.Unmarshal([]byte(lit), &want) |
| 57 |
if err := json.Unmarshal([]byte(lit), &got); err != nil || got != want { |
| 58 |
t.Fatalf("%s: got %q %v, want %q", lit, got, err, want) |
| 59 |
} |
| 60 |
var s struct{ S string } |
| 61 |
if err := json.Unmarshal([]byte(`{"S":`+lit+`}`), &s); err != nil || s.S != want { |
| 62 |
t.Fatalf("%s: got %q %v, want %q", lit, s.S, err, want) |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
func TestDecodeLongStringsMalformed(t *testing.T) { |
| 68 |
// A long string, whose rest is scanned apart ( see scanStringRest ), is valid or not as encoding/json has it, |
| 69 |
// whatever follows its runs of plain bytes. |
| 70 |
plain := strings.Repeat("abcdefgh", 12) |
| 71 |
for _, tail := range []string{`"`, `\n"`, `\u00e9"`, `\ud83d\ude00"`, `\q"`, "\x01\"", `\u12"`, `\u12zz"`, `\`, ``, `é"`, "\xff\""} { |
| 72 |
for _, n := range []int{64, 65, 70, 96} { |
| 73 |
in := `"` + plain[:n] + tail |
| 74 |
for _, doc := range []string{in, `{"k":` + in + `}`, `[` + in + `,1]`} { |
| 75 |
var want, got any |
| 76 |
errStd := stdjson.Unmarshal([]byte(doc), &want) |
| 77 |
err := json.Unmarshal([]byte(doc), &got) |
| 78 |
if (errStd == nil) != (err == nil) { |
| 79 |
t.Fatalf("%q: encoding/json: %v, go-json: %v", doc, errStd, err) |
| 80 |
} |
| 81 |
if errStd == nil && fmt.Sprint(got) != fmt.Sprint(want) { |
| 82 |
t.Fatalf("%q:\n got %q\nwant %q", doc, got, want) |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
func TestDecodeSkippedStrings(t *testing.T) { |
| 90 |
// The strings of the keys of no field are skipped: they are validated as encoding/json does, whatever their |
| 91 |
// length and wherever an escape, a control character or a byte which is not ASCII is in them. Each is |
| 92 |
// decoded from a buffer of its own and from a stream, which have different ends. |
| 93 |
type target struct { |
| 94 |
A int `json:"a"` |
| 95 |
} |
| 96 |
specials := []string{`\n`, `\u00e9`, `\ud83d\ude00`, "\x01", "é", "\xff", `\x`, `\u12`} |
| 97 |
for n := 0; n < 140; n++ { |
| 98 |
for pos := -1; pos < n; pos++ { |
| 99 |
for _, special := range specials { |
| 100 |
s := strings.Repeat("a", n) |
| 101 |
if pos >= 0 { |
| 102 |
s = s[:pos] + special + s[pos:] |
| 103 |
} else if special != specials[0] { |
| 104 |
continue |
| 105 |
} |
| 106 |
for _, doc := range []string{`{"unknown":"` + s + `","a":1}`, `{"a":1,"unknown":"` + s + `"}`} { |
| 107 |
var want, got, gotStream target |
| 108 |
wantErr := stdjson.Unmarshal([]byte(doc), &want) |
| 109 |
gotErr := json.Unmarshal([]byte(doc), &got) |
| 110 |
streamErr := json.NewDecoder(strings.NewReader(doc)).Decode(&gotStream) |
| 111 |
if (gotErr != nil) != (wantErr != nil) || (streamErr != nil) != (wantErr != nil) { |
| 112 |
t.Fatalf("%q: got the errors %v and %v, want %v", doc, gotErr, streamErr, wantErr) |
| 113 |
} |
| 114 |
if wantErr == nil && (got != want || gotStream != want) { |
| 115 |
t.Fatalf("%q: got %+v and %+v, want %+v", doc, got, gotStream, want) |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
func TestDecodeEscapedStringRuns(t *testing.T) { |
| 124 |
// A string with escapes between runs of every length, short ones and ones longer than the words which the |
| 125 |
// escapes are decoded by, is decoded as encoding/json decodes it, in every mode of the strings. |
| 126 |
escapes := []string{`\n`, `\"`, `\\`, `\/`, `\t`, `\u00e9`, `\u3042`, `\ud83d\ude01`, `\ud83d`, `\u0000`, `\ud83d\ude00`, `\uD83D\uDE00x`, "\xff", "\xe3\x81"} |
| 127 |
r := rand.New(rand.NewSource(13)) |
| 128 |
for i := 0; i < 3000; i++ { |
| 129 |
var b strings.Builder |
| 130 |
b.WriteByte('"') |
| 131 |
for n := r.Intn(8); n >= 0; n-- { |
| 132 |
run := r.Intn(4) |
| 133 |
switch run { |
| 134 |
case 0: |
| 135 |
run = r.Intn(8) |
| 136 |
case 1: |
| 137 |
run = 8 + r.Intn(16) |
| 138 |
default: |
| 139 |
run = r.Intn(200) |
| 140 |
} |
| 141 |
for j := 0; j < run; j++ { |
| 142 |
b.WriteByte("abcdefghij é"[r.Intn(12)]) |
| 143 |
} |
| 144 |
b.WriteString(escapes[r.Intn(len(escapes))]) |
| 145 |
} |
| 146 |
b.WriteByte('"') |
| 147 |
doc := b.String() |
| 148 |
var want string |
| 149 |
if err := stdjson.Unmarshal([]byte(doc), &want); err != nil { |
| 150 |
t.Fatalf("encoding/json: %q: %v", doc, err) |
| 151 |
} |
| 152 |
for _, opts := range [][]json.DecodeOptionFunc{nil, {json.DecodeNoCopyString()}} { |
| 153 |
var got string |
| 154 |
if err := json.UnmarshalWithOption([]byte(doc), &got, opts...); err != nil || got != want { |
| 155 |
t.Fatalf("%q: got %q, %v, want %q", doc, got, err, want) |
| 156 |
} |
| 157 |
var gotOf string |
| 158 |
if err := json.UnmarshalOf([]byte(doc), &gotOf, opts...); err != nil || gotOf != want { |
| 159 |
t.Fatalf("UnmarshalOf %q: got %q, %v, want %q", doc, gotOf, err, want) |
| 160 |
} |
| 161 |
} |
| 162 |
var any1 any |
| 163 |
if err := json.NewDecoder(strings.NewReader(doc)).Decode(&any1); err != nil || any1 != want { |
| 164 |
t.Fatalf("Decode %q: got %q, %v, want %q", doc, any1, err, want) |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
func TestDecodeEscapedStringRunsInvalid(t *testing.T) { |
| 170 |
// A long string with an invalid escape or byte after its runs is a syntax error, as encoding/json reports it, |
| 171 |
// and a valid value after it in an array is not taken for its end. |
| 172 |
bad := []string{`\x`, `\u12g4`, `\u12`, "\x01", `\ud83d\u12`, `\`, "\x00"} |
| 173 |
r := rand.New(rand.NewSource(17)) |
| 174 |
for i := 0; i < 2000; i++ { |
| 175 |
var b strings.Builder |
| 176 |
b.WriteString(`[{"s":"`) |
| 177 |
for n := r.Intn(4); n >= 0; n-- { |
| 178 |
for j := r.Intn(120); j > 0; j-- { |
| 179 |
b.WriteByte("abcdefghij"[r.Intn(10)]) |
| 180 |
} |
| 181 |
if r.Intn(2) == 0 { |
| 182 |
b.WriteString(`\n`) |
| 183 |
} |
| 184 |
} |
| 185 |
b.WriteString(bad[r.Intn(len(bad))]) |
| 186 |
b.WriteString(`abc"}]`) |
| 187 |
if r.Intn(4) == 0 { |
| 188 |
doc := b.String() |
| 189 |
b.Reset() |
| 190 |
b.WriteString(doc[:len(doc)-r.Intn(8)-1]) |
| 191 |
} |
| 192 |
doc := b.String() |
| 193 |
var want []struct{ S string } |
| 194 |
stdErr := stdjson.Unmarshal([]byte(doc), &want) |
| 195 |
var got []struct{ S string } |
| 196 |
err := json.Unmarshal([]byte(doc), &got) |
| 197 |
if (err == nil) != (stdErr == nil) { |
| 198 |
t.Fatalf("%q: got %v, encoding/json %v", doc, err, stdErr) |
| 199 |
} |
| 200 |
if err != nil && err.Error() != stdErr.Error() { |
| 201 |
t.Fatalf("%q: got %q, encoding/json %q", doc, err, stdErr) |
| 202 |
} |
| 203 |
var gotOf []struct{ S string } |
| 204 |
if err := json.UnmarshalOf([]byte(doc), &gotOf, json.DecodeNoCopyString()); (err == nil) != (stdErr == nil) { |
| 205 |
t.Fatalf("UnmarshalOf %q: got %v, encoding/json %v", doc, err, stdErr) |
| 206 |
} |
| 207 |
var gotStream []struct{ S string } |
| 208 |
if err := json.NewDecoder(strings.NewReader(doc)).Decode(&gotStream); (err == nil) != (stdErr == nil) { |
| 209 |
t.Fatalf("Decode %q: got %v, encoding/json %v", doc, err, stdErr) |
| 210 |
} |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
func TestDecodeInterfaceStrings(t *testing.T) { |
| 215 |
// The keys and the string values decoded into interface{}, and the strings decoded into string, are scanned by |
| 216 |
// words when they are plain ASCII: a string of any length, plain or with an escape, a byte which is not ASCII or |
| 217 |
// a control byte, anywhere in it and at the end of the input, is decoded or rejected as encoding/json does. |
| 218 |
pieces := []string{"a", "bcdefgh", "ijklmnopqrstuvw", `\n`, `\"`, `\u00e9`, "é", "あ", "\xff", "\x01", " "} |
| 219 |
r := rand.New(rand.NewSource(19)) |
| 220 |
str := func() string { |
| 221 |
var b strings.Builder |
| 222 |
b.WriteByte('"') |
| 223 |
for n := r.Intn(6); n > 0; n-- { |
| 224 |
p := pieces[r.Intn(len(pieces))] |
| 225 |
if r.Intn(3) > 0 { |
| 226 |
p = pieces[r.Intn(3)] |
| 227 |
} |
| 228 |
b.WriteString(p) |
| 229 |
} |
| 230 |
b.WriteByte('"') |
| 231 |
return b.String() |
| 232 |
} |
| 233 |
for i := 0; i < 5000; i++ { |
| 234 |
var b strings.Builder |
| 235 |
switch r.Intn(3) { |
| 236 |
case 0: |
| 237 |
b.WriteString(str()) |
| 238 |
case 1: |
| 239 |
b.WriteString("[") |
| 240 |
for n := r.Intn(4); n >= 0; n-- { |
| 241 |
b.WriteString(str()) |
| 242 |
if n > 0 { |
| 243 |
b.WriteString(",") |
| 244 |
} |
| 245 |
} |
| 246 |
b.WriteString("]") |
| 247 |
default: |
| 248 |
b.WriteString("{") |
| 249 |
for n := r.Intn(4); n >= 0; n-- { |
| 250 |
b.WriteString(str() + ":" + str()) |
| 251 |
if n > 0 { |
| 252 |
b.WriteString(",") |
| 253 |
} |
| 254 |
} |
| 255 |
b.WriteString("}") |
| 256 |
} |
| 257 |
doc := b.String() |
| 258 |
var want any |
| 259 |
stdErr := stdjson.Unmarshal([]byte(doc), &want) |
| 260 |
for _, opts := range [][]json.DecodeOptionFunc{nil, {json.DecodeNoCopyString()}} { |
| 261 |
var got any |
| 262 |
err := json.UnmarshalWithOption([]byte(doc), &got, opts...) |
| 263 |
if (err == nil) != (stdErr == nil) { |
| 264 |
t.Fatalf("%q: got %v, encoding/json %v", doc, err, stdErr) |
| 265 |
} |
| 266 |
if err == nil && !reflect.DeepEqual(got, want) { |
| 267 |
t.Fatalf("%q: got %#v, want %#v", doc, got, want) |
| 268 |
} |
| 269 |
} |
| 270 |
var got any |
| 271 |
err := json.NewDecoder(strings.NewReader(doc)).Decode(&got) |
| 272 |
if (err == nil) != (stdErr == nil) { |
| 273 |
t.Fatalf("Decode %q: got %v, encoding/json %v", doc, err, stdErr) |
| 274 |
} |
| 275 |
if err == nil && !reflect.DeepEqual(got, want) { |
| 276 |
t.Fatalf("Decode %q: got %#v, want %#v", doc, got, want) |
| 277 |
} |
| 278 |
// the same strings into their types, by the decoder of strings |
| 279 |
var typed any |
| 280 |
switch doc[0] { |
| 281 |
case '"': |
| 282 |
typed = new(string) |
| 283 |
case '[': |
| 284 |
typed = new([]string) |
| 285 |
default: |
| 286 |
continue |
| 287 |
} |
| 288 |
wantTyped := reflect.New(reflect.TypeOf(typed).Elem()).Interface() |
| 289 |
stdTypedErr := stdjson.Unmarshal([]byte(doc), wantTyped) |
| 290 |
for _, opts := range [][]json.DecodeOptionFunc{nil, {json.DecodeNoCopyString()}} { |
| 291 |
gotTyped := reflect.New(reflect.TypeOf(typed).Elem()).Interface() |
| 292 |
err := json.UnmarshalWithOption([]byte(doc), gotTyped, opts...) |
| 293 |
if (err == nil) != (stdTypedErr == nil) { |
| 294 |
t.Fatalf("%T %q: got %v, encoding/json %v", gotTyped, doc, err, stdTypedErr) |
| 295 |
} |
| 296 |
if err == nil && !reflect.DeepEqual(gotTyped, wantTyped) { |
| 297 |
t.Fatalf("%T %q: got %#v, want %#v", gotTyped, doc, gotTyped, wantTyped) |
| 298 |
} |
| 299 |
} |
| 300 |
} |
| 301 |
} |