|
16 | 16 | import java.util.stream.Stream; |
17 | 17 |
|
18 | 18 | import static org.junit.Assert.assertEquals; |
| 19 | +import static org.junit.Assert.assertNotSame; |
19 | 20 | import static org.junit.Assert.assertSame; |
20 | 21 | import static org.junit.Assert.assertThrows; |
21 | 22 | import static org.junit.Assert.assertTrue; |
| 23 | +import static org.junit.Assert.assertFalse; |
22 | 24 | import static org.junit.Assert.fail; |
23 | 25 |
|
24 | 26 | public class JSONParserConfigurationTest { |
@@ -708,4 +710,140 @@ private List<String> getNonCompliantJSONObjectList() { |
708 | 710 | ); |
709 | 711 | } |
710 | 712 |
|
| 713 | + private static final String SHARED_KEYS_SOURCE = "{\"rows\": [" |
| 714 | + + "{\"name\": \"a\", \"count\": 1, \"big\": 12345678901, \"ratio\": 1.5, \"flag\": true, \"missing\": null," |
| 715 | + + " \"nested\": {\"name\": \"inner\", \"list\": [1, \"name\", null]}}," |
| 716 | + + "{\"name\": \"name\", \"count\": 2, \"nested\": {\"name\": \"inner2\", \"list\": []}}," |
| 717 | + + "{\"name\": \"third\", \"count\": -3}" |
| 718 | + + "]}"; |
| 719 | + |
| 720 | + @Test |
| 721 | + public void testMaxSharedKeysConfig() { |
| 722 | + JSONParserConfiguration config = new JSONParserConfiguration(); |
| 723 | + assertEquals(JSONParserConfiguration.DEFAULT_MAXIMUM_SHARED_KEYS, config.getMaxSharedKeys()); |
| 724 | + assertEquals(1000, config.getMaxSharedKeys()); |
| 725 | + assertEquals(0, config.withMaxSharedKeys(0).getMaxSharedKeys()); |
| 726 | + assertEquals(JSONParserConfiguration.UNLIMITED_SHARED_KEYS, config.withMaxSharedKeys(-1).getMaxSharedKeys()); |
| 727 | + assertEquals(JSONParserConfiguration.UNLIMITED_SHARED_KEYS, config.withMaxSharedKeys(-5).getMaxSharedKeys()); |
| 728 | + |
| 729 | + JSONParserConfiguration chained = config.withMaxSharedKeys(7).withOverwriteDuplicateKey(true).withStrictMode(false); |
| 730 | + assertEquals(7, chained.getMaxSharedKeys()); |
| 731 | + assertTrue(chained.isOverwriteDuplicateKey()); |
| 732 | + assertEquals(1000, config.getMaxSharedKeys()); |
| 733 | + } |
| 734 | + |
| 735 | + @Test |
| 736 | + public void testSharedKeysByDefault() { |
| 737 | + JSONArray rows = new JSONObject(SHARED_KEYS_SOURCE).getJSONArray("rows"); |
| 738 | + String nameKey = key(rows.getJSONObject(0), "name"); |
| 739 | + for (int i = 1; i < rows.length(); i++) { |
| 740 | + assertSame(nameKey, key(rows.getJSONObject(i), "name")); |
| 741 | + } |
| 742 | + assertSame(nameKey, key(rows.getJSONObject(0).getJSONObject("nested"), "name")); |
| 743 | + assertSame(key(rows.getJSONObject(0), "count"), key(rows.getJSONObject(2), "count")); |
| 744 | + assertSame(key(rows.getJSONObject(0).getJSONObject("nested"), "list"), |
| 745 | + key(rows.getJSONObject(1).getJSONObject("nested"), "list")); |
| 746 | + |
| 747 | + // string values equal to a key are not shared |
| 748 | + assertNotSame(nameKey, rows.getJSONObject(1).get("name")); |
| 749 | + assertNotSame(nameKey, rows.getJSONObject(0).getJSONObject("nested").getJSONArray("list").get(1)); |
| 750 | + } |
| 751 | + |
| 752 | + @Test |
| 753 | + public void testSharedKeysDisabled() { |
| 754 | + JSONArray rows = new JSONObject(SHARED_KEYS_SOURCE, new JSONParserConfiguration().withMaxSharedKeys(0)) |
| 755 | + .getJSONArray("rows"); |
| 756 | + assertNotSame(key(rows.getJSONObject(0), "name"), key(rows.getJSONObject(1), "name")); |
| 757 | + assertNotSame(key(rows.getJSONObject(0), "count"), key(rows.getJSONObject(1), "count")); |
| 758 | + } |
| 759 | + |
| 760 | + @Test |
| 761 | + public void testSharedKeysLimit() { |
| 762 | + JSONArray rows = new JSONArray("[{\"a\": 1, \"b\": 2, \"c\": 3}, {\"a\": 4, \"b\": 5, \"c\": 6}]", |
| 763 | + new JSONParserConfiguration().withMaxSharedKeys(2)); |
| 764 | + assertSame(key(rows.getJSONObject(0), "a"), key(rows.getJSONObject(1), "a")); |
| 765 | + assertSame(key(rows.getJSONObject(0), "b"), key(rows.getJSONObject(1), "b")); |
| 766 | + assertNotSame(key(rows.getJSONObject(0), "c"), key(rows.getJSONObject(1), "c")); |
| 767 | + } |
| 768 | + |
| 769 | + @Test |
| 770 | + public void testSharedKeysUnlimited() { |
| 771 | + int keyCount = JSONParserConfiguration.DEFAULT_MAXIMUM_SHARED_KEYS + 500; |
| 772 | + StringBuilder row = new StringBuilder("{"); |
| 773 | + for (int i = 0; i < keyCount; i++) { |
| 774 | + row.append(i == 0 ? "" : ",").append("\"k").append(i).append("\":").append(i); |
| 775 | + } |
| 776 | + row.append('}'); |
| 777 | + String source = "[" + row + "," + row + "]"; |
| 778 | + |
| 779 | + JSONArray unlimited = new JSONArray(source, |
| 780 | + new JSONParserConfiguration().withMaxSharedKeys(JSONParserConfiguration.UNLIMITED_SHARED_KEYS)); |
| 781 | + for (int i = 0; i < keyCount; i++) { |
| 782 | + assertSame(key(unlimited.getJSONObject(0), "k" + i), key(unlimited.getJSONObject(1), "k" + i)); |
| 783 | + } |
| 784 | + |
| 785 | + JSONArray limited = new JSONArray(source); |
| 786 | + String lastKey = "k" + (keyCount - 1); |
| 787 | + assertSame(key(limited.getJSONObject(0), "k0"), key(limited.getJSONObject(1), "k0")); |
| 788 | + assertNotSame(key(limited.getJSONObject(0), lastKey), key(limited.getJSONObject(1), lastKey)); |
| 789 | + assertTrue(unlimited.similar(limited)); |
| 790 | + } |
| 791 | + |
| 792 | + @Test |
| 793 | + public void testSharedKeysNotSharedAcrossParses() { |
| 794 | + JSONObject first = new JSONObject("{\"name\": 1}"); |
| 795 | + JSONObject second = new JSONObject("{\"name\": 2}"); |
| 796 | + assertNotSame(key(first, "name"), key(second, "name")); |
| 797 | + } |
| 798 | + |
| 799 | + @Test |
| 800 | + public void testSharedKeysParseUnchanged() { |
| 801 | + JSONObject unshared = new JSONObject(SHARED_KEYS_SOURCE, new JSONParserConfiguration().withMaxSharedKeys(0)); |
| 802 | + JSONObject shared = new JSONObject(SHARED_KEYS_SOURCE); |
| 803 | + JSONObject unlimited = new JSONObject(SHARED_KEYS_SOURCE, |
| 804 | + new JSONParserConfiguration().withMaxSharedKeys(JSONParserConfiguration.UNLIMITED_SHARED_KEYS)); |
| 805 | + assertTrue(unshared.similar(shared)); |
| 806 | + assertTrue(unshared.similar(unlimited)); |
| 807 | + assertEquals(unshared.toString(), shared.toString()); |
| 808 | + assertEquals(unshared.toString(), unlimited.toString()); |
| 809 | + |
| 810 | + JSONObject row = shared.getJSONArray("rows").getJSONObject(0); |
| 811 | + assertEquals(Integer.class, row.get("count").getClass()); |
| 812 | + assertEquals(Long.class, row.get("big").getClass()); |
| 813 | + assertEquals(java.math.BigDecimal.class, row.get("ratio").getClass()); |
| 814 | + assertEquals(Boolean.TRUE, row.get("flag")); |
| 815 | + assertSame(JSONObject.NULL, row.get("missing")); |
| 816 | + assertSame(JSONObject.NULL, row.getJSONObject("nested").getJSONArray("list").get(2)); |
| 817 | + assertEquals("third", shared.getJSONArray("rows").getJSONObject(2).get("name")); |
| 818 | + } |
| 819 | + |
| 820 | + @Test |
| 821 | + public void testSharedKeysLenientSyntax() { |
| 822 | + JSONArray rows = new JSONArray("[{\"name\": 1, \"count\": 2}, {'name': 'single', count: 3}]", |
| 823 | + new JSONParserConfiguration().withStrictMode(false)); |
| 824 | + assertSame(key(rows.getJSONObject(0), "name"), key(rows.getJSONObject(1), "name")); |
| 825 | + assertSame(key(rows.getJSONObject(0), "count"), key(rows.getJSONObject(1), "count")); |
| 826 | + assertEquals("single", rows.getJSONObject(1).get("name")); |
| 827 | + } |
| 828 | + |
| 829 | + @Test |
| 830 | + public void testSharedKeysStrictMode() { |
| 831 | + JSONArray rows = new JSONArray("[{\"a\": 1}, {\"a\": 2}]", new JSONParserConfiguration().withStrictMode()); |
| 832 | + assertSame(key(rows.getJSONObject(0), "a"), key(rows.getJSONObject(1), "a")); |
| 833 | + assertFalse(rows.getJSONObject(0).similar(rows.getJSONObject(1))); |
| 834 | + } |
| 835 | + |
| 836 | + /** |
| 837 | + * Returns the key instance a JSONObject holds, rather than the caller's equal String. |
| 838 | + */ |
| 839 | + private static String key(JSONObject jsonObject, String name) { |
| 840 | + for (String key : jsonObject.keySet()) { |
| 841 | + if (key.equals(name)) { |
| 842 | + return key; |
| 843 | + } |
| 844 | + } |
| 845 | + fail("missing key " + name); |
| 846 | + return null; |
| 847 | + } |
| 848 | + |
711 | 849 | } |
0 commit comments