| 1 | package json_test |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/google/go-cmp/cmp" |
| 9 | "github.com/tomwright/dasel/v3/model" |
| 10 | "github.com/tomwright/dasel/v3/parsing" |
| 11 | "github.com/tomwright/dasel/v3/parsing/json" |
| 12 | ) |
| 13 | |
| 14 | func TestJson(t *testing.T) { |
| 15 | doc := []byte(`{ |
| 16 | "string": "foo", |
| 17 | "int": 1, |
| 18 | "float": 1.1, |
| 19 | "bool": true, |
| 20 | "null": null, |
| 21 | "array": [ |
| 22 | 1, |
| 23 | 2, |
| 24 | 3 |
| 25 | ], |
| 26 | "object": { |
| 27 | "key": "value" |
| 28 | } |
| 29 | } |
| 30 | `) |
| 31 | reader, err := json.JSON.NewReader(parsing.DefaultReaderOptions()) |
| 32 | if err != nil { |
| 33 | t.Fatal(err) |
| 34 | } |
| 35 | writer, err := json.JSON.NewWriter(parsing.DefaultWriterOptions()) |
| 36 | if err != nil { |
| 37 | t.Fatal(err) |
| 38 | } |
| 39 | |
| 40 | value, err := reader.Read(doc) |
| 41 | if err != nil { |
| 42 | t.Fatal(err) |
| 43 | } |
| 44 | |
| 45 | newDoc, err := writer.Write(value) |
| 46 | if err != nil { |
| 47 | t.Fatal(err) |
| 48 | } |
| 49 | |
| 50 | if string(doc) != string(newDoc) { |
| 51 | t.Fatalf("expected %s, got %s...\n%s", string(doc), string(newDoc), cmp.Diff(string(doc), string(newDoc))) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | func TestJsonCompact(t *testing.T) { |
| 56 | doc := []byte(`{ |
| 57 | "string": "foo", |
| 58 | "int": 1, |
| 59 | "float": 1.1, |
| 60 | "bool": true, |
| 61 | "null": null, |
| 62 | "array": [ |
| 63 | 1, |
| 64 | 2, |
| 65 | 3 |
| 66 | ], |
| 67 | "object": { |
| 68 | "key": "value" |
| 69 | } |
| 70 | } |
| 71 | `) |
| 72 | reader, err := json.JSON.NewReader(parsing.DefaultReaderOptions()) |
| 73 | if err != nil { |
| 74 | t.Fatal(err) |
| 75 | } |
| 76 | opts := parsing.DefaultWriterOptions() |
| 77 | opts.Compact = true |
| 78 | writer, err := json.JSON.NewWriter(opts) |
| 79 | if err != nil { |
| 80 | t.Fatal(err) |
| 81 | } |
| 82 | |
| 83 | value, err := reader.Read(doc) |
| 84 | if err != nil { |
| 85 | t.Fatal(err) |
| 86 | } |
| 87 | |
| 88 | newDoc, err := writer.Write(value) |
| 89 | if err != nil { |
| 90 | t.Fatal(err) |
| 91 | } |
| 92 | |
| 93 | expected := `{"string":"foo","int":1,"float":1.1,"bool":true,"null":null,"array":[1,2,3],"object":{"key":"value"}}` + "\n" |
| 94 | if string(newDoc) != expected { |
| 95 | t.Fatalf("expected %s, got %s...\n%s", expected, string(newDoc), cmp.Diff(expected, string(newDoc))) |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | func TestNDJSON(t *testing.T) { |
| 100 | newReader := func(t *testing.T) parsing.Reader { |
| 101 | t.Helper() |
| 102 | r, err := json.JSON.NewReader(parsing.DefaultReaderOptions()) |
| 103 | if err != nil { |
| 104 | t.Fatal(err) |
| 105 | } |
| 106 | return r |
| 107 | } |
| 108 | |
| 109 | newWriter := func(t *testing.T, compact bool) parsing.Writer { |
| 110 | t.Helper() |
| 111 | opts := parsing.DefaultWriterOptions() |
| 112 | opts.Compact = compact |
| 113 | w, err := json.JSON.NewWriter(opts) |
| 114 | if err != nil { |
| 115 | t.Fatal(err) |
| 116 | } |
| 117 | return parsing.MultiDocumentWriter(w) |
| 118 | } |
| 119 | |
| 120 | // readWrite is a helper that reads input, writes it back via MultiDocumentWriter, |
| 121 | // and compares the output to the expected string. |
| 122 | readWrite := func(t *testing.T, input, expected string, compact bool) { |
| 123 | t.Helper() |
| 124 | reader := newReader(t) |
| 125 | writer := newWriter(t, compact) |
| 126 | |
| 127 | value, err := reader.Read([]byte(input)) |
| 128 | if err != nil { |
| 129 | t.Fatalf("read error: %v", err) |
| 130 | } |
| 131 | |
| 132 | out, err := writer.Write(value) |
| 133 | if err != nil { |
| 134 | t.Fatalf("write error: %v", err) |
| 135 | } |
| 136 | |
| 137 | if string(out) != expected { |
| 138 | t.Fatalf("unexpected output:\n%s", cmp.Diff(expected, string(out))) |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | // assertValue is a helper that reads input and runs an assertion function against |
| 143 | // the parsed model.Value. |
| 144 | assertValue := func(t *testing.T, input string, assert func(t *testing.T, v *model.Value)) { |
| 145 | t.Helper() |
| 146 | reader := newReader(t) |
| 147 | value, err := reader.Read([]byte(input)) |
| 148 | if err != nil { |
| 149 | t.Fatalf("read error: %v", err) |
| 150 | } |
| 151 | assert(t, value) |
| 152 | } |
| 153 | |
| 154 | t.Run("single object unchanged", func(t *testing.T) { |
| 155 | input := "{\"name\":\"Tom\"}\n" |
| 156 | assertValue(t, input, func(t *testing.T, v *model.Value) { |
| 157 | if v.IsBranch() { |
| 158 | t.Fatal("single document should not be a branch") |
| 159 | } |
| 160 | got, err := v.GetMapKey("name") |
| 161 | if err != nil { |
| 162 | t.Fatal(err) |
| 163 | } |
| 164 | s, err := got.StringValue() |
| 165 | if err != nil { |
| 166 | t.Fatal(err) |
| 167 | } |
| 168 | if s != "Tom" { |
| 169 | t.Fatalf("expected Tom, got %s", s) |
| 170 | } |
| 171 | }) |
| 172 | }) |
| 173 | |
| 174 | t.Run("single array unchanged", func(t *testing.T) { |
| 175 | input := "[1,2,3]\n" |
| 176 | assertValue(t, input, func(t *testing.T, v *model.Value) { |
| 177 | if v.IsBranch() { |
| 178 | t.Fatal("single document should not be a branch") |
| 179 | } |
| 180 | length, err := v.SliceLen() |
| 181 | if err != nil { |
| 182 | t.Fatal(err) |
| 183 | } |
| 184 | if length != 3 { |
| 185 | t.Fatalf("expected length 3, got %d", length) |
| 186 | } |
| 187 | }) |
| 188 | }) |
| 189 | |
| 190 | t.Run("single scalar unchanged", func(t *testing.T) { |
| 191 | input := "42\n" |
| 192 | assertValue(t, input, func(t *testing.T, v *model.Value) { |
| 193 | if v.IsBranch() { |
| 194 | t.Fatal("single document should not be a branch") |
| 195 | } |
| 196 | i, err := v.IntValue() |
| 197 | if err != nil { |
| 198 | t.Fatal(err) |
| 199 | } |
| 200 | if i != 42 { |
| 201 | t.Fatalf("expected 42, got %d", i) |
| 202 | } |
| 203 | }) |
| 204 | }) |
| 205 | |
| 206 | t.Run("two objects", func(t *testing.T) { |
| 207 | input := "{\"name\":\"Tom\"}\n{\"name\":\"Jim\"}\n" |
| 208 | assertValue(t, input, func(t *testing.T, v *model.Value) { |
| 209 | if !v.IsBranch() { |
| 210 | t.Fatal("multiple documents should be a branch") |
| 211 | } |
| 212 | length, err := v.SliceLen() |
| 213 | if err != nil { |
| 214 | t.Fatal(err) |
| 215 | } |
| 216 | if length != 2 { |
| 217 | t.Fatalf("expected 2 documents, got %d", length) |
| 218 | } |
| 219 | |
| 220 | for i, expected := range []string{"Tom", "Jim"} { |
| 221 | doc, err := v.GetSliceIndex(i) |
| 222 | if err != nil { |
| 223 | t.Fatal(err) |
| 224 | } |
| 225 | got, err := doc.GetMapKey("name") |
| 226 | if err != nil { |
| 227 | t.Fatal(err) |
| 228 | } |
| 229 | s, err := got.StringValue() |
| 230 | if err != nil { |
| 231 | t.Fatal(err) |
| 232 | } |
| 233 | if s != expected { |
| 234 | t.Fatalf("doc %d: expected %s, got %s", i, expected, s) |
| 235 | } |
| 236 | } |
| 237 | }) |
| 238 | }) |
| 239 | |
| 240 | t.Run("three objects", func(t *testing.T) { |
| 241 | input := "{\"a\":1}\n{\"b\":2}\n{\"c\":3}\n" |
| 242 | assertValue(t, input, func(t *testing.T, v *model.Value) { |
| 243 | if !v.IsBranch() { |
| 244 | t.Fatal("expected branch") |
| 245 | } |
| 246 | length, err := v.SliceLen() |
| 247 | if err != nil { |
| 248 | t.Fatal(err) |
| 249 | } |
| 250 | if length != 3 { |
| 251 | t.Fatalf("expected 3 documents, got %d", length) |
| 252 | } |
| 253 | }) |
| 254 | }) |
| 255 | |
| 256 | t.Run("mixed types", func(t *testing.T) { |
| 257 | input := "{\"name\":\"Tom\"}\n[1,2,3]\n\"hello\"\n42\ntrue\nnull\n" |
| 258 | assertValue(t, input, func(t *testing.T, v *model.Value) { |
| 259 | if !v.IsBranch() { |
| 260 | t.Fatal("expected branch") |
| 261 | } |
| 262 | length, err := v.SliceLen() |
| 263 | if err != nil { |
| 264 | t.Fatal(err) |
| 265 | } |
| 266 | if length != 6 { |
| 267 | t.Fatalf("expected 6 documents, got %d", length) |
| 268 | } |
| 269 | |
| 270 | // Check object |
| 271 | doc0, _ := v.GetSliceIndex(0) |
| 272 | name, _ := doc0.GetMapKey("name") |
| 273 | s, _ := name.StringValue() |
| 274 | if s != "Tom" { |
| 275 | t.Fatalf("doc 0: expected Tom, got %s", s) |
| 276 | } |
| 277 | |
| 278 | // Check array |
| 279 | doc1, _ := v.GetSliceIndex(1) |
| 280 | arrLen, _ := doc1.SliceLen() |
| 281 | if arrLen != 3 { |
| 282 | t.Fatalf("doc 1: expected length 3, got %d", arrLen) |
| 283 | } |
| 284 | |
| 285 | // Check string |
| 286 | doc2, _ := v.GetSliceIndex(2) |
| 287 | s2, _ := doc2.StringValue() |
| 288 | if s2 != "hello" { |
| 289 | t.Fatalf("doc 2: expected hello, got %s", s2) |
| 290 | } |
| 291 | |
| 292 | // Check int |
| 293 | doc3, _ := v.GetSliceIndex(3) |
| 294 | i, _ := doc3.IntValue() |
| 295 | if i != 42 { |
| 296 | t.Fatalf("doc 3: expected 42, got %d", i) |
| 297 | } |
| 298 | |
| 299 | // Check bool |
| 300 | doc4, _ := v.GetSliceIndex(4) |
| 301 | b, _ := doc4.BoolValue() |
| 302 | if !b { |
| 303 | t.Fatal("doc 4: expected true") |
| 304 | } |
| 305 | |
| 306 | // Check null |
| 307 | doc5, _ := v.GetSliceIndex(5) |
| 308 | if doc5.Type() != model.TypeNull { |
| 309 | t.Fatalf("doc 5: expected null, got %s", doc5.Type()) |
| 310 | } |
| 311 | }) |
| 312 | }) |
| 313 | |
| 314 | t.Run("objects with no trailing newline", func(t *testing.T) { |
| 315 | input := "{\"a\":1}\n{\"b\":2}" |
| 316 | assertValue(t, input, func(t *testing.T, v *model.Value) { |
| 317 | if !v.IsBranch() { |
| 318 | t.Fatal("expected branch") |
| 319 | } |
| 320 | length, err := v.SliceLen() |
| 321 | if err != nil { |
| 322 | t.Fatal(err) |
| 323 | } |
| 324 | if length != 2 { |
| 325 | t.Fatalf("expected 2 documents, got %d", length) |
| 326 | } |
| 327 | }) |
| 328 | }) |
| 329 | |
| 330 | t.Run("extra whitespace between documents", func(t *testing.T) { |
| 331 | input := "{\"a\":1}\n\n\n{\"b\":2}\n" |
| 332 | assertValue(t, input, func(t *testing.T, v *model.Value) { |
| 333 | if !v.IsBranch() { |
| 334 | t.Fatal("expected branch") |
| 335 | } |
| 336 | length, err := v.SliceLen() |
| 337 | if err != nil { |
| 338 | t.Fatal(err) |
| 339 | } |
| 340 | if length != 2 { |
| 341 | t.Fatalf("expected 2 documents, got %d", length) |
| 342 | } |
| 343 | }) |
| 344 | }) |
| 345 | |
| 346 | t.Run("pretty printed objects on separate lines", func(t *testing.T) { |
| 347 | input := "{\n \"name\": \"Tom\"\n}\n{\n \"name\": \"Jim\"\n}\n" |
| 348 | assertValue(t, input, func(t *testing.T, v *model.Value) { |
| 349 | if !v.IsBranch() { |
| 350 | t.Fatal("expected branch") |
| 351 | } |
| 352 | length, err := v.SliceLen() |
| 353 | if err != nil { |
| 354 | t.Fatal(err) |
| 355 | } |
| 356 | if length != 2 { |
| 357 | t.Fatalf("expected 2 documents, got %d", length) |
| 358 | } |
| 359 | }) |
| 360 | }) |
| 361 | |
| 362 | t.Run("nested objects", func(t *testing.T) { |
| 363 | input := "{\"user\":{\"name\":\"Tom\",\"age\":30}}\n{\"user\":{\"name\":\"Jim\",\"age\":25}}\n" |
| 364 | assertValue(t, input, func(t *testing.T, v *model.Value) { |
| 365 | if !v.IsBranch() { |
| 366 | t.Fatal("expected branch") |
| 367 | } |
| 368 | |
| 369 | doc0, _ := v.GetSliceIndex(0) |
| 370 | user0, _ := doc0.GetMapKey("user") |
| 371 | name0, _ := user0.GetMapKey("name") |
| 372 | s0, _ := name0.StringValue() |
| 373 | if s0 != "Tom" { |
| 374 | t.Fatalf("expected Tom, got %s", s0) |
| 375 | } |
| 376 | age0, _ := user0.GetMapKey("age") |
| 377 | a0, _ := age0.IntValue() |
| 378 | if a0 != 30 { |
| 379 | t.Fatalf("expected 30, got %d", a0) |
| 380 | } |
| 381 | |
| 382 | doc1, _ := v.GetSliceIndex(1) |
| 383 | user1, _ := doc1.GetMapKey("user") |
| 384 | name1, _ := user1.GetMapKey("name") |
| 385 | s1, _ := name1.StringValue() |
| 386 | if s1 != "Jim" { |
| 387 | t.Fatalf("expected Jim, got %s", s1) |
| 388 | } |
| 389 | }) |
| 390 | }) |
| 391 | |
| 392 | t.Run("empty input", func(t *testing.T) { |
| 393 | assertValue(t, "", func(t *testing.T, v *model.Value) { |
| 394 | if v.IsBranch() { |
| 395 | t.Fatal("empty input should not be a branch") |
| 396 | } |
| 397 | if v.Type() != model.TypeNull { |
| 398 | t.Fatalf("expected null, got %s", v.Type()) |
| 399 | } |
| 400 | }) |
| 401 | }) |
| 402 | |
| 403 | t.Run("whitespace only input", func(t *testing.T) { |
| 404 | assertValue(t, " \n\n \n", func(t *testing.T, v *model.Value) { |
| 405 | if v.IsBranch() { |
| 406 | t.Fatal("whitespace-only input should not be a branch") |
| 407 | } |
| 408 | if v.Type() != model.TypeNull { |
| 409 | t.Fatalf("expected null, got %s", v.Type()) |
| 410 | } |
| 411 | }) |
| 412 | }) |
| 413 | |
| 414 | t.Run("two scalar strings", func(t *testing.T) { |
| 415 | input := "\"hello\"\n\"world\"\n" |
| 416 | assertValue(t, input, func(t *testing.T, v *model.Value) { |
| 417 | if !v.IsBranch() { |
| 418 | t.Fatal("expected branch") |
| 419 | } |
| 420 | doc0, _ := v.GetSliceIndex(0) |
| 421 | s0, _ := doc0.StringValue() |
| 422 | if s0 != "hello" { |
| 423 | t.Fatalf("expected hello, got %s", s0) |
| 424 | } |
| 425 | doc1, _ := v.GetSliceIndex(1) |
| 426 | s1, _ := doc1.StringValue() |
| 427 | if s1 != "world" { |
| 428 | t.Fatalf("expected world, got %s", s1) |
| 429 | } |
| 430 | }) |
| 431 | }) |
| 432 | |
| 433 | t.Run("two scalar ints", func(t *testing.T) { |
| 434 | input := "1\n2\n" |
| 435 | assertValue(t, input, func(t *testing.T, v *model.Value) { |
| 436 | if !v.IsBranch() { |
| 437 | t.Fatal("expected branch") |
| 438 | } |
| 439 | doc0, _ := v.GetSliceIndex(0) |
| 440 | i0, _ := doc0.IntValue() |
| 441 | if i0 != 1 { |
| 442 | t.Fatalf("expected 1, got %d", i0) |
| 443 | } |
| 444 | doc1, _ := v.GetSliceIndex(1) |
| 445 | i1, _ := doc1.IntValue() |
| 446 | if i1 != 2 { |
| 447 | t.Fatalf("expected 2, got %d", i1) |
| 448 | } |
| 449 | }) |
| 450 | }) |
| 451 | |
| 452 | t.Run("two arrays", func(t *testing.T) { |
| 453 | input := "[1,2]\n[3,4]\n" |
| 454 | assertValue(t, input, func(t *testing.T, v *model.Value) { |
| 455 | if !v.IsBranch() { |
| 456 | t.Fatal("expected branch") |
| 457 | } |
| 458 | length, _ := v.SliceLen() |
| 459 | if length != 2 { |
| 460 | t.Fatalf("expected 2 documents, got %d", length) |
| 461 | } |
| 462 | |
| 463 | doc0, _ := v.GetSliceIndex(0) |
| 464 | l0, _ := doc0.SliceLen() |
| 465 | if l0 != 2 { |
| 466 | t.Fatalf("doc 0: expected length 2, got %d", l0) |
| 467 | } |
| 468 | |
| 469 | doc1, _ := v.GetSliceIndex(1) |
| 470 | el, _ := doc1.GetSliceIndex(0) |
| 471 | i, _ := el.IntValue() |
| 472 | if i != 3 { |
| 473 | t.Fatalf("doc 1[0]: expected 3, got %d", i) |
| 474 | } |
| 475 | }) |
| 476 | }) |
| 477 | |
| 478 | t.Run("round trip compact two objects", func(t *testing.T) { |
| 479 | input := "{\"name\":\"Tom\"}\n{\"name\":\"Jim\"}\n" |
| 480 | // Each document gets a trailing \n from the writer, plus \n separator between docs. |
| 481 | expected := "{\"name\":\"Tom\"}\n\n{\"name\":\"Jim\"}\n" |
| 482 | readWrite(t, input, expected, true) |
| 483 | }) |
| 484 | |
| 485 | t.Run("round trip compact single object", func(t *testing.T) { |
| 486 | input := "{\"name\":\"Tom\"}\n" |
| 487 | expected := "{\"name\":\"Tom\"}\n" |
| 488 | readWrite(t, input, expected, true) |
| 489 | }) |
| 490 | |
| 491 | t.Run("round trip pretty two objects", func(t *testing.T) { |
| 492 | input := "{\"name\":\"Tom\"}\n{\"name\":\"Jim\"}\n" |
| 493 | expected := "{\n \"name\": \"Tom\"\n}\n\n{\n \"name\": \"Jim\"\n}\n" |
| 494 | readWrite(t, input, expected, false) |
| 495 | }) |
| 496 | |
| 497 | t.Run("round trip compact mixed types", func(t *testing.T) { |
| 498 | input := "{\"a\":1}\n[1,2]\n\"str\"\n42\ntrue\nnull\n" |
| 499 | expected := "{\"a\":1}\n\n[1,2]\n\n\"str\"\n\n42\n\ntrue\n\nnull\n" |
| 500 | readWrite(t, input, expected, true) |
| 501 | }) |
| 502 | |
| 503 | t.Run("objects separated by spaces", func(t *testing.T) { |
| 504 | input := "{\"a\":1} {\"b\":2}" |
| 505 | assertValue(t, input, func(t *testing.T, v *model.Value) { |
| 506 | if !v.IsBranch() { |
| 507 | t.Fatal("expected branch") |
| 508 | } |
| 509 | length, _ := v.SliceLen() |
| 510 | if length != 2 { |
| 511 | t.Fatalf("expected 2 documents, got %d", length) |
| 512 | } |
| 513 | }) |
| 514 | }) |
| 515 | |
| 516 | t.Run("documents with float values", func(t *testing.T) { |
| 517 | input := "{\"val\":1.5}\n{\"val\":2.5}\n" |
| 518 | assertValue(t, input, func(t *testing.T, v *model.Value) { |
| 519 | if !v.IsBranch() { |
| 520 | t.Fatal("expected branch") |
| 521 | } |
| 522 | doc0, _ := v.GetSliceIndex(0) |
| 523 | val0, _ := doc0.GetMapKey("val") |
| 524 | f0, _ := val0.FloatValue() |
| 525 | if f0 != 1.5 { |
| 526 | t.Fatalf("expected 1.5, got %f", f0) |
| 527 | } |
| 528 | doc1, _ := v.GetSliceIndex(1) |
| 529 | val1, _ := doc1.GetMapKey("val") |
| 530 | f1, _ := val1.FloatValue() |
| 531 | if f1 != 2.5 { |
| 532 | t.Fatalf("expected 2.5, got %f", f1) |
| 533 | } |
| 534 | }) |
| 535 | }) |
| 536 | |
| 537 | t.Run("documents with null values", func(t *testing.T) { |
| 538 | input := "{\"a\":null}\n{\"b\":null}\n" |
| 539 | assertValue(t, input, func(t *testing.T, v *model.Value) { |
| 540 | if !v.IsBranch() { |
| 541 | t.Fatal("expected branch") |
| 542 | } |
| 543 | doc0, _ := v.GetSliceIndex(0) |
| 544 | val0, _ := doc0.GetMapKey("a") |
| 545 | if val0.Type() != model.TypeNull { |
| 546 | t.Fatalf("expected null, got %s", val0.Type()) |
| 547 | } |
| 548 | }) |
| 549 | }) |
| 550 | |
| 551 | t.Run("documents with boolean values", func(t *testing.T) { |
| 552 | input := "{\"ok\":true}\n{\"ok\":false}\n" |
| 553 | assertValue(t, input, func(t *testing.T, v *model.Value) { |
| 554 | if !v.IsBranch() { |
| 555 | t.Fatal("expected branch") |
| 556 | } |
| 557 | doc0, _ := v.GetSliceIndex(0) |
| 558 | val0, _ := doc0.GetMapKey("ok") |
| 559 | b0, _ := val0.BoolValue() |
| 560 | if !b0 { |
| 561 | t.Fatal("expected true") |
| 562 | } |
| 563 | doc1, _ := v.GetSliceIndex(1) |
| 564 | val1, _ := doc1.GetMapKey("ok") |
| 565 | b1, _ := val1.BoolValue() |
| 566 | if b1 { |
| 567 | t.Fatal("expected false") |
| 568 | } |
| 569 | }) |
| 570 | }) |
| 571 | |
| 572 | t.Run("documents with empty objects", func(t *testing.T) { |
| 573 | input := "{}\n{}\n" |
| 574 | assertValue(t, input, func(t *testing.T, v *model.Value) { |
| 575 | if !v.IsBranch() { |
| 576 | t.Fatal("expected branch") |
| 577 | } |
| 578 | length, _ := v.SliceLen() |
| 579 | if length != 2 { |
| 580 | t.Fatalf("expected 2 documents, got %d", length) |
| 581 | } |
| 582 | }) |
| 583 | }) |
| 584 | |
| 585 | t.Run("documents with empty arrays", func(t *testing.T) { |
| 586 | input := "[]\n[]\n" |
| 587 | assertValue(t, input, func(t *testing.T, v *model.Value) { |
| 588 | if !v.IsBranch() { |
| 589 | t.Fatal("expected branch") |
| 590 | } |
| 591 | length, _ := v.SliceLen() |
| 592 | if length != 2 { |
| 593 | t.Fatalf("expected 2 documents, got %d", length) |
| 594 | } |
| 595 | }) |
| 596 | }) |
| 597 | |
| 598 | t.Run("large number of documents", func(t *testing.T) { |
| 599 | input := "" |
| 600 | for i := 0; i < 100; i++ { |
| 601 | input += "{\"i\":" + string(rune('0'+i%10)) + "}\n" |
| 602 | } |
| 603 | // Use a simpler approach - just build manually |
| 604 | input = "" |
| 605 | for i := 0; i < 100; i++ { |
| 606 | input += "{\"i\":1}\n" |
| 607 | } |
| 608 | assertValue(t, input, func(t *testing.T, v *model.Value) { |
| 609 | if !v.IsBranch() { |
| 610 | t.Fatal("expected branch") |
| 611 | } |
| 612 | length, err := v.SliceLen() |
| 613 | if err != nil { |
| 614 | t.Fatal(err) |
| 615 | } |
| 616 | if length != 100 { |
| 617 | t.Fatalf("expected 100 documents, got %d", length) |
| 618 | } |
| 619 | }) |
| 620 | }) |
| 621 | } |
| 622 | |
| 623 | func TestJSONReader_DepthLimit(t *testing.T) { |
| 624 | reader, err := json.JSON.NewReader(parsing.DefaultReaderOptions()) |
| 625 | if err != nil { |
| 626 | t.Fatal(err) |
| 627 | } |
| 628 | |
| 629 | t.Run("nested arrays exceeding limit return error", func(t *testing.T) { |
| 630 | // 10_001 levels of nested arrays — one past the 10_000 limit. |
| 631 | const depth = 10_001 |
| 632 | data := []byte(strings.Repeat("[", depth) + strings.Repeat("]", depth)) |
| 633 | _, err := reader.Read(data) |
| 634 | if !errors.Is(err, json.ErrJSONMaxDepthExceeded) { |
| 635 | t.Fatalf("expected ErrJSONMaxDepthExceeded, got %v", err) |
| 636 | } |
| 637 | }) |
| 638 | |
| 639 | t.Run("nested objects exceeding limit return error", func(t *testing.T) { |
| 640 | const depth = 10_001 |
| 641 | data := []byte(strings.Repeat(`{"x":`, depth) + `"v"` + strings.Repeat("}", depth)) |
| 642 | _, err := reader.Read(data) |
| 643 | if !errors.Is(err, json.ErrJSONMaxDepthExceeded) { |
| 644 | t.Fatalf("expected ErrJSONMaxDepthExceeded, got %v", err) |
| 645 | } |
| 646 | }) |
| 647 | |
| 648 | t.Run("normal nesting within limit succeeds", func(t *testing.T) { |
| 649 | data := []byte(`{"a":{"b":{"c":"d"}}}`) |
| 650 | _, err := reader.Read(data) |
| 651 | if err != nil { |
| 652 | t.Fatalf("unexpected error: %v", err) |
| 653 | } |
| 654 | }) |
| 655 | } |