|
12 | 12 | import com.fasterxml.jackson.annotation.JsonTypeInfo.As; |
13 | 13 |
|
14 | 14 | import tools.jackson.core.*; |
| 15 | +import tools.jackson.core.json.JsonReadFeature; |
15 | 16 | import tools.jackson.core.type.TypeReference; |
| 17 | + |
16 | 18 | import tools.jackson.databind.*; |
17 | 19 | import tools.jackson.databind.deser.std.StdDeserializer; |
18 | 20 | import tools.jackson.databind.deser.std.StdScalarDeserializer; |
| 21 | +import tools.jackson.databind.json.JsonMapper; |
19 | 22 | import tools.jackson.databind.module.SimpleModule; |
20 | 23 | import tools.jackson.databind.testutil.NoCheckSubTypeValidator; |
21 | 24 |
|
@@ -131,7 +134,7 @@ static class SerContainer { |
131 | 134 | /********************************************************** |
132 | 135 | */ |
133 | 136 |
|
134 | | - private final ObjectMapper MAPPER = newJsonMapper(); |
| 137 | + private final JsonMapper MAPPER = newJsonMapper(); |
135 | 138 |
|
136 | 139 | @SuppressWarnings("unchecked") |
137 | 140 | @Test |
@@ -556,4 +559,196 @@ public void testPolymorphicUntypedCustom() throws IOException |
556 | 559 | WrappedPolymorphicUntyped result = rDefault.readValue(json); |
557 | 560 | assertEquals(SHORT, result.value); |
558 | 561 | } |
| 562 | + |
| 563 | + /* |
| 564 | + /********************************************************** |
| 565 | + /* Test methods, additional coverage |
| 566 | + /********************************************************** |
| 567 | + */ |
| 568 | + |
| 569 | + @Test |
| 570 | + public void testEmptyArrayAndObject() throws Exception |
| 571 | + { |
| 572 | + // Empty array |
| 573 | + Object result = MAPPER.readValue("[]", Object.class); |
| 574 | + assertNotNull(result); |
| 575 | + assertTrue(result instanceof List); |
| 576 | + assertEquals(0, ((List<?>) result).size()); |
| 577 | + |
| 578 | + // Empty object |
| 579 | + result = MAPPER.readValue("{}", Object.class); |
| 580 | + assertNotNull(result); |
| 581 | + assertTrue(result instanceof Map); |
| 582 | + assertEquals(0, ((Map<?,?>) result).size()); |
| 583 | + } |
| 584 | + |
| 585 | + @Test |
| 586 | + public void testSingleElementArrayAndObject() throws Exception |
| 587 | + { |
| 588 | + // Single element array |
| 589 | + Object result = MAPPER.readValue("[42]", Object.class); |
| 590 | + assertNotNull(result); |
| 591 | + assertTrue(result instanceof List); |
| 592 | + List<?> list = (List<?>) result; |
| 593 | + assertEquals(1, list.size()); |
| 594 | + assertEquals(Integer.valueOf(42), list.get(0)); |
| 595 | + |
| 596 | + // Single property object |
| 597 | + result = MAPPER.readValue("{\"key\":\"value\"}", Object.class); |
| 598 | + assertNotNull(result); |
| 599 | + assertTrue(result instanceof Map); |
| 600 | + Map<?,?> map = (Map<?,?>) result; |
| 601 | + assertEquals(1, map.size()); |
| 602 | + assertEquals("value", map.get("key")); |
| 603 | + } |
| 604 | + |
| 605 | + @Test |
| 606 | + public void testFloatDeserializationWithBigDecimal() throws Exception |
| 607 | + { |
| 608 | + ObjectMapper mapper = jsonMapperBuilder() |
| 609 | + .enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS) |
| 610 | + .build(); |
| 611 | + |
| 612 | + Object result = mapper.readValue("3.14159", Object.class); |
| 613 | + assertNotNull(result); |
| 614 | + assertTrue(result instanceof BigDecimal); |
| 615 | + assertEquals(new BigDecimal("3.14159"), result); |
| 616 | + } |
| 617 | + |
| 618 | + @Test |
| 619 | + public void testFloatTypesFloat32AndFloat64() throws Exception |
| 620 | + { |
| 621 | + // Test Float (32-bit) |
| 622 | + Object result = MAPPER.readValue("1.5", Object.class); |
| 623 | + assertTrue(result instanceof Double); |
| 624 | + result = MAPPER.readValue("2.718281828", Object.class); |
| 625 | + assertTrue(result instanceof Double); |
| 626 | + } |
| 627 | + |
| 628 | + @Test |
| 629 | + public void testNaNHandling() throws Exception |
| 630 | + { |
| 631 | + // NaN values should be handled specially |
| 632 | + ObjectMapper mapper = jsonMapperBuilder() |
| 633 | + .enable(JsonReadFeature.ALLOW_NON_NUMERIC_NUMBERS) |
| 634 | + .build(); |
| 635 | + |
| 636 | + Object result = mapper.readValue("NaN", Object.class); |
| 637 | + assertTrue(result instanceof Double); |
| 638 | + assertTrue(((Double) result).isNaN()); |
| 639 | + } |
| 640 | + |
| 641 | + @Test |
| 642 | + public void testNullInDifferentContexts() throws Exception |
| 643 | + { |
| 644 | + // Null as root value |
| 645 | + Object result = MAPPER.readValue("null", Object.class); |
| 646 | + assertNull(result); |
| 647 | + |
| 648 | + // Null in array |
| 649 | + List<?> list = (List<?>) MAPPER.readValue("[null, 1, null]", Object.class); |
| 650 | + assertEquals(3, list.size()); |
| 651 | + assertNull(list.get(0)); |
| 652 | + assertEquals(Integer.valueOf(1), list.get(1)); |
| 653 | + assertNull(list.get(2)); |
| 654 | + |
| 655 | + // Null in object |
| 656 | + Map<?,?> map = (Map<?,?>) MAPPER.readValue("{\"a\":null,\"b\":2}", Object.class); |
| 657 | + assertEquals(2, map.size()); |
| 658 | + assertTrue(map.containsKey("a")); |
| 659 | + assertNull(map.get("a")); |
| 660 | + assertEquals(Integer.valueOf(2), map.get("b")); |
| 661 | + } |
| 662 | + |
| 663 | + @Test |
| 664 | + public void testBigIntegerCoercion() throws Exception |
| 665 | + { |
| 666 | + ObjectMapper mapper = jsonMapperBuilder() |
| 667 | + .enable(DeserializationFeature.USE_BIG_INTEGER_FOR_INTS) |
| 668 | + .build(); |
| 669 | + |
| 670 | + Object result = mapper.readValue("12345678901234567890", Object.class); |
| 671 | + assertNotNull(result); |
| 672 | + assertTrue(result instanceof BigInteger); |
| 673 | + assertEquals(new BigInteger("12345678901234567890"), result); |
| 674 | + } |
| 675 | + |
| 676 | + @Test |
| 677 | + public void testLargeArray() throws Exception |
| 678 | + { |
| 679 | + // Test array with more than 2 elements to trigger ObjectBuffer usage |
| 680 | + StringBuilder json = new StringBuilder("["); |
| 681 | + for (int i = 0; i < 100; i++) { |
| 682 | + if (i > 0) json.append(","); |
| 683 | + json.append(i); |
| 684 | + } |
| 685 | + json.append("]"); |
| 686 | + |
| 687 | + List<?> result = (List<?>) MAPPER.readValue(json.toString(), Object.class); |
| 688 | + assertEquals(100, result.size()); |
| 689 | + assertEquals(Integer.valueOf(0), result.get(0)); |
| 690 | + assertEquals(Integer.valueOf(99), result.get(99)); |
| 691 | + } |
| 692 | + |
| 693 | + @Test |
| 694 | + public void testLargeObject() throws Exception |
| 695 | + { |
| 696 | + // Test object with more than 2 properties |
| 697 | + StringBuilder json = new StringBuilder("{"); |
| 698 | + for (int i = 0; i < 50; i++) { |
| 699 | + if (i > 0) json.append(","); |
| 700 | + json.append("\"key").append(i).append("\":").append(i); |
| 701 | + } |
| 702 | + json.append("}"); |
| 703 | + |
| 704 | + Map<?,?> result = (Map<?,?>) MAPPER.readValue(json.toString(), Object.class); |
| 705 | + assertEquals(50, result.size()); |
| 706 | + assertEquals(Integer.valueOf(0), result.get("key0")); |
| 707 | + assertEquals(Integer.valueOf(49), result.get("key49")); |
| 708 | + } |
| 709 | + |
| 710 | + @Test |
| 711 | + public void testMixedTypesInArray() throws Exception |
| 712 | + { |
| 713 | + String json = "[1, \"text\", true, null, 3.14, {\"nested\":\"object\"}, [1,2,3]]"; |
| 714 | + List<?> result = (List<?>) MAPPER.readValue(json, Object.class); |
| 715 | + |
| 716 | + assertEquals(7, result.size()); |
| 717 | + assertEquals(Integer.valueOf(1), result.get(0)); |
| 718 | + assertEquals("text", result.get(1)); |
| 719 | + assertEquals(Boolean.TRUE, result.get(2)); |
| 720 | + assertNull(result.get(3)); |
| 721 | + assertEquals(Double.valueOf(3.14), result.get(4)); |
| 722 | + assertTrue(result.get(5) instanceof Map); |
| 723 | + assertTrue(result.get(6) instanceof List); |
| 724 | + } |
| 725 | + |
| 726 | + @Test |
| 727 | + public void testDeeplyNestedStructures() throws Exception |
| 728 | + { |
| 729 | + String json = "{\"level1\":{\"level2\":{\"level3\":{\"level4\":{\"value\":\"deep\"}}}}}"; |
| 730 | + Map<?,?> result = (Map<?,?>) MAPPER.readValue(json, Object.class); |
| 731 | + |
| 732 | + Map<?,?> level1 = (Map<?,?>) result.get("level1"); |
| 733 | + Map<?,?> level2 = (Map<?,?>) level1.get("level2"); |
| 734 | + Map<?,?> level3 = (Map<?,?>) level2.get("level3"); |
| 735 | + Map<?,?> level4 = (Map<?,?>) level3.get("level4"); |
| 736 | + assertEquals("deep", level4.get("value")); |
| 737 | + } |
| 738 | + |
| 739 | + @Test |
| 740 | + public void testObjectWithArraysAndObjects() throws Exception |
| 741 | + { |
| 742 | + String json = "{\"numbers\":[1,2,3],\"nested\":{\"flag\":true},\"text\":\"hello\"}"; |
| 743 | + Map<?,?> result = (Map<?,?>) MAPPER.readValue(json, Object.class); |
| 744 | + |
| 745 | + assertEquals(3, result.size()); |
| 746 | + List<?> numbers = (List<?>) result.get("numbers"); |
| 747 | + assertEquals(3, numbers.size()); |
| 748 | + |
| 749 | + Map<?,?> nested = (Map<?,?>) result.get("nested"); |
| 750 | + assertEquals(Boolean.TRUE, nested.get("flag")); |
| 751 | + |
| 752 | + assertEquals("hello", result.get("text")); |
| 753 | + } |
559 | 754 | } |
0 commit comments