Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 25x 25x 2x 1x 1x 1x 1x 1x 1x 1x 6x 6x 1x 4x 4x 1x 1x 16x 16x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 79x 79x 363x 363x 363x 118x 118x 363x 363x 363x 363x 363x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 104x 104x 104x 104x 104x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 1x 1x 1x 1x 1x 1x 2648x 97x 97x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 868x 868x 868x 868x 868x 97x 97x 97x 771x 771x 771x 771x 771x 771x 771x 80x 80x 771x 868x 868x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 97x 97x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 404x 404x 404x 404x 404x 97x 97x 97x 97x 404x 404x 288x 75x 75x 213x 213x 288x 404x 404x 404x 404x 404x 404x 404x 404x 404x 404x 404x 404x 404x 404x 404x 404x 404x 404x 404x 404x 404x 32x 32x 32x 404x 404x 404x 404x 404x 404x 172x 172x 172x 172x 172x 172x 172x 172x 16x 16x 16x 16x 172x 172x 172x 141x 141x 141x 141x 172x 172x 172x 172x 32x 32x 32x 172x 172x 404x 404x 1x 1x 157x 157x 157x 157x 157x 157x 16x 157x 32x 32x 109x 109x 157x 157x 157x 157x 157x 54x 54x 107x 107x 157x 157x 157x 157x 32x 32x 157x 157x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 30x 30x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 28x 1x 127x 127x 1x 1x 1x 1x 166x 166x 166x 166x 5x 5x 5x 5x 3x 3x 5x 161x 161x 166x 166x 166x 166x 166x 166x 166x 166x 166x 166x 166x 166x 166x 166x 166x 166x 6x 3x 3x 3x 3x 6x 166x 166x 166x 166x 166x 166x 166x 166x 166x 166x 27x 27x 12x 12x 12x 27x 160x 160x 160x 1x 1x 1x 1x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 12x 12x 12x 12x 12x 12x 12x 12x 17x 17x 17x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 196x 196x 196x 193x 193x 193x 196x 3x 3x 196x 196x 225x 225x 225x 130x 2x 2x 130x 130x 130x 130x 130x 1x 1x 62x 62x 43x 43x 43x 19x 19x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 1x 60x 60x 60x 60x 130x 130x 130x 1x 29x 29x 29x 29x 130x 130x 130x 130x 130x 130x 130x 130x 130x 19x 19x 19x 1x 19x 19x 19x 19x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 1x 1x 131x 131x 131x 131x 131x 131x 131x 131x 131x 131x 131x 131x 131x 131x 131x 131x 131x 131x 131x 131x 131x 98x 3x 98x 1x 95x 94x 5x 94x 1x 1x 88x 88x 98x 1x 1x 131x 131x 131x 131x 131x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 408x 408x 408x 408x 408x 408x 408x 408x 408x 408x 408x 408x 408x 408x 408x 408x 408x 408x 408x 408x 408x 408x 408x 408x 408x 408x 408x 408x 408x 408x 408x 408x 408x 408x 408x 408x 408x 408x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 408x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 408x 408x 408x 408x 1x 1x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 21x 21x 12x 12x 12x 1x 1x 1x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 293x 293x 293x 293x 293x 293x 293x 293x 293x 12x 12x 293x 293x 293x 293x 293x 293x 34x 34x 14x 14x 20x 20x 293x 1x 1x 1x 1x 259x 258x 258x 170x 170x 258x 3x 3x 3x 3x 88x 2x 2x 2x 2x 85x 83x 83x 83x 293x 293x 293x 293x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 10x 10x 10x 10x 59x 59x 27x 27x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 11x 11x 11x 59x 59x 9x 9x 59x 59x 93x 93x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 1x 1x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 1x 1x 1x 1x 40x 40x 4x 4x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 17x 17x 17x 17x 17x 17x 17x 17x 1x 1x 62x 62x 62x 62x 62x 5x 5x 62x 62x 62x 62x 62x 62x 62x 24x 24x 62x 62x 62x 62x 62x 62x 26x 26x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 3x 3x 62x 62x 62x 62x 62x 62x 86x 86x 19x 19x 19x 86x 103x 103x 103x 62x 62x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 27x 27x 27x 27x 27x 27x 2x 2x 2x 2x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 23x 23x 1x 1x 101x 101x 101x 101x 101x 101x 101x 101x 175x 175x 175x 175x 175x 175x 175x 1x 1x 1x 175x 187x 187x 187x 187x 1x 1x 1x 186x 186x 186x 101x 76x 76x 76x 76x 76x 101x 101x 99x 99x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 1x 1x 43x 43x 43x 43x 1x 1x 1x 43x 43x 43x 43x 43x 43x 42x 41x 41x 1x 1x | ********************************************************************** * UTILS ********************************************************************** INTERFACE lif_kind. TYPES ty_kind TYPE c LENGTH 1. CONSTANTS: any TYPE ty_kind VALUE cl_abap_typedescr=>typekind_any, date TYPE ty_kind VALUE cl_abap_typedescr=>typekind_date, time TYPE ty_kind VALUE cl_abap_typedescr=>typekind_time, packed TYPE ty_kind VALUE cl_abap_typedescr=>typekind_packed, table TYPE ty_kind VALUE cl_abap_typedescr=>typekind_table, struct_flat TYPE ty_kind VALUE cl_abap_typedescr=>typekind_struct1, struct_deep TYPE ty_kind VALUE cl_abap_typedescr=>typekind_struct2, data_ref TYPE ty_kind VALUE cl_abap_typedescr=>typekind_dref, object_ref TYPE ty_kind VALUE cl_abap_typedescr=>typekind_oref, enum TYPE ty_kind VALUE 'k'. " cl_abap_typedescr=>typekind_enum not in lower releases CONSTANTS: BEGIN OF numeric, int1 TYPE ty_kind VALUE cl_abap_typedescr=>typekind_int1, int2 TYPE ty_kind VALUE cl_abap_typedescr=>typekind_int2, int4 TYPE ty_kind VALUE cl_abap_typedescr=>typekind_int, int8 TYPE ty_kind VALUE '8', " cl_abap_typedescr=>typekind_int8 not in lower releases float TYPE ty_kind VALUE cl_abap_typedescr=>typekind_float, packed TYPE ty_kind VALUE cl_abap_typedescr=>typekind_packed, decfloat16 TYPE ty_kind VALUE cl_abap_typedescr=>typekind_decfloat16, decfloat34 TYPE ty_kind VALUE cl_abap_typedescr=>typekind_decfloat34, END OF numeric. CONSTANTS: BEGIN OF texts, char TYPE ty_kind VALUE cl_abap_typedescr=>typekind_char, numc TYPE ty_kind VALUE cl_abap_typedescr=>typekind_num, string TYPE ty_kind VALUE cl_abap_typedescr=>typekind_string, END OF texts. CONSTANTS: BEGIN OF binary, hex TYPE ty_kind VALUE cl_abap_typedescr=>typekind_hex, xstring TYPE ty_kind VALUE cl_abap_typedescr=>typekind_xstring, END OF binary. CONSTANTS: BEGIN OF deep_targets, table TYPE ty_kind VALUE cl_abap_typedescr=>typekind_table, struct_flat TYPE ty_kind VALUE cl_abap_typedescr=>typekind_struct1, struct_deep TYPE ty_kind VALUE cl_abap_typedescr=>typekind_struct2, data_ref TYPE ty_kind VALUE cl_abap_typedescr=>typekind_dref, object_ref TYPE ty_kind VALUE cl_abap_typedescr=>typekind_oref, END OF deep_targets. ENDINTERFACE. CLASS lcl_utils DEFINITION FINAL. PUBLIC SECTION. CLASS-METHODS normalize_path IMPORTING iv_path TYPE string RETURNING VALUE(rv_path) TYPE string. CLASS-METHODS split_path IMPORTING iv_path TYPE string RETURNING VALUE(rv_path_name) TYPE zif_abapgit_ajson_types=>ty_path_name. CLASS-METHODS validate_array_index IMPORTING iv_path TYPE string iv_index TYPE string RETURNING VALUE(rv_index) TYPE i RAISING zcx_abapgit_ajson_error. CLASS-METHODS string_to_xstring_utf8 IMPORTING iv_str TYPE string RETURNING VALUE(rv_xstr) TYPE xstring. CLASS-METHODS xstring_to_string_utf8 IMPORTING iv_xstr TYPE xstring RETURNING VALUE(rv_str) TYPE string. CLASS-METHODS any_to_xstring IMPORTING iv_data TYPE any RETURNING VALUE(rv_xstr) TYPE xstring RAISING zcx_abapgit_ajson_error. CLASS-METHODS any_to_string IMPORTING iv_data TYPE any RETURNING VALUE(rv_str) TYPE string RAISING zcx_abapgit_ajson_error. ENDCLASS. CLASS lcl_utils IMPLEMENTATION. METHOD string_to_xstring_utf8. DATA lo_conv TYPE REF TO object. DATA lv_out_ce TYPE string. lv_out_ce = 'CL_ABAP_CONV_OUT_CE'. TRY. CALL METHOD ('CL_ABAP_CONV_CODEPAGE')=>create_out RECEIVING instance = lo_conv. CALL METHOD lo_conv->('IF_ABAP_CONV_OUT~CONVERT') EXPORTING source = iv_str RECEIVING result = rv_xstr. CATCH cx_sy_dyn_call_illegal_class. CALL METHOD (lv_out_ce)=>create EXPORTING encoding = 'UTF-8' RECEIVING conv = lo_conv. CALL METHOD lo_conv->('CONVERT') EXPORTING data = iv_str IMPORTING buffer = rv_xstr. ENDTRY. ENDMETHOD. METHOD xstring_to_string_utf8. DATA lo_conv TYPE REF TO object. DATA lv_in_ce TYPE string. lv_in_ce = 'CL_ABAP_CONV_IN_CE'. TRY. CALL METHOD ('CL_ABAP_CONV_CODEPAGE')=>create_in RECEIVING instance = lo_conv. CALL METHOD lo_conv->('IF_ABAP_CONV_IN~CONVERT') EXPORTING source = iv_xstr RECEIVING result = rv_str. CATCH cx_sy_dyn_call_illegal_class. CALL METHOD (lv_in_ce)=>create EXPORTING encoding = 'UTF-8' RECEIVING conv = lo_conv. CALL METHOD lo_conv->('CONVERT') EXPORTING data = iv_xstr IMPORTING buffer = rv_str. ENDTRY. ENDMETHOD. METHOD validate_array_index. IF NOT iv_index CO '0123456789'. zcx_abapgit_ajson_error=>raise( |Cannot add non-numeric key [{ iv_index }] to array [{ iv_path }]| ). ENDIF. rv_index = iv_index. IF rv_index = 0. zcx_abapgit_ajson_error=>raise( |Cannot add zero key to array [{ iv_path }]| ). ENDIF. ENDMETHOD. METHOD normalize_path. rv_path = iv_path. IF strlen( rv_path ) = 0. rv_path = '/'. ENDIF. IF rv_path+0(1) <> '/'. rv_path = '/' && rv_path. ENDIF. IF substring( val = rv_path off = strlen( rv_path ) - 1 ) <> '/'. rv_path = rv_path && '/'. ENDIF. ENDMETHOD. METHOD split_path. DATA lv_offs TYPE i. DATA lv_len TYPE i. DATA lv_trim_slash TYPE i. lv_len = strlen( iv_path ). IF lv_len = 0 OR iv_path = '/'. RETURN. " empty path is the alias for root item = '' + '' ENDIF. IF substring( val = iv_path off = lv_len - 1 ) = '/'. lv_trim_slash = 1. " ignore last '/' ENDIF. lv_offs = find( val = reverse( iv_path ) sub = '/' off = lv_trim_slash ). IF lv_offs = -1. lv_offs = lv_len. " treat whole string as the 'name' part ENDIF. lv_offs = lv_len - lv_offs. rv_path_name-path = normalize_path( substring( val = iv_path len = lv_offs ) ). rv_path_name-name = substring( val = iv_path off = lv_offs len = lv_len - lv_offs - lv_trim_slash ). ENDMETHOD. METHOD any_to_xstring. " supports xstring, char, string, or string_table as input DATA lo_type TYPE REF TO cl_abap_typedescr. DATA lo_table_type TYPE REF TO cl_abap_tabledescr. DATA lv_str TYPE string. FIELD-SYMBOLS: <data> TYPE STANDARD TABLE. lo_type = cl_abap_typedescr=>describe_by_data( iv_data ). CASE lo_type->type_kind. WHEN lif_kind=>binary-xstring. rv_xstr = iv_data. WHEN lif_kind=>texts-string OR lif_kind=>texts-char. rv_xstr = string_to_xstring_utf8( iv_data ). WHEN lif_kind=>table. lo_table_type ?= lo_type. IF lo_table_type->table_kind <> cl_abap_tabledescr=>tablekind_std. zcx_abapgit_ajson_error=>raise( 'Unsupported type of input table (must be standard table)' ). ENDIF. TRY. ASSIGN iv_data TO <data>. lv_str = concat_lines_of( table = <data> sep = cl_abap_char_utilities=>newline ). rv_xstr = string_to_xstring_utf8( lv_str ). CATCH cx_root. zcx_abapgit_ajson_error=>raise( 'Error converting input table (should be string_table)' ). ENDTRY. WHEN OTHERS. zcx_abapgit_ajson_error=>raise( 'Unsupported type of input (must be char, string, string_table, or xstring)' ). ENDCASE. ENDMETHOD. METHOD any_to_string. " supports xstring, char, string, or string_table as input DATA lo_type TYPE REF TO cl_abap_typedescr. DATA lo_table_type TYPE REF TO cl_abap_tabledescr. FIELD-SYMBOLS: <data> TYPE STANDARD TABLE. lo_type = cl_abap_typedescr=>describe_by_data( iv_data ). CASE lo_type->type_kind. WHEN lif_kind=>binary-xstring. rv_str = xstring_to_string_utf8( iv_data ). WHEN lif_kind=>texts-string OR lif_kind=>texts-char. rv_str = iv_data. WHEN lif_kind=>table. lo_table_type ?= lo_type. IF lo_table_type->table_kind <> cl_abap_tabledescr=>tablekind_std. zcx_abapgit_ajson_error=>raise( 'Unsupported type of input table (must be standard table)' ). ENDIF. TRY. ASSIGN iv_data TO <data>. rv_str = concat_lines_of( table = <data> sep = cl_abap_char_utilities=>newline ). CATCH cx_root. zcx_abapgit_ajson_error=>raise( 'Error converting input table (should be string_table)' ). ENDTRY. WHEN OTHERS. zcx_abapgit_ajson_error=>raise( 'Unsupported type of input (must be char, string, string_table, or xstring)' ). ENDCASE. ENDMETHOD. ENDCLASS. ********************************************************************** * PARSER ********************************************************************** CLASS lcl_json_parser DEFINITION FINAL. PUBLIC SECTION. METHODS parse IMPORTING iv_json TYPE any iv_keep_item_order TYPE abap_bool DEFAULT abap_false RETURNING VALUE(rt_json_tree) TYPE zif_abapgit_ajson_types=>ty_nodes_tt RAISING zcx_abapgit_ajson_error. PRIVATE SECTION. TYPES: ty_stack_tt TYPE STANDARD TABLE OF REF TO zif_abapgit_ajson_types=>ty_node. DATA mt_stack TYPE ty_stack_tt. DATA mv_stack_path TYPE string. DATA mv_keep_item_order TYPE abap_bool. METHODS raise IMPORTING iv_error TYPE string RAISING zcx_abapgit_ajson_error. METHODS _parse IMPORTING iv_json TYPE xstring RETURNING VALUE(rt_json_tree) TYPE zif_abapgit_ajson_types=>ty_nodes_tt RAISING zcx_abapgit_ajson_error cx_dynamic_check. " cx_sxml_error is not released on Steampunk #153 METHODS _get_location IMPORTING iv_json TYPE string iv_offset TYPE i RETURNING VALUE(rv_location) TYPE string. ENDCLASS. CLASS lcl_json_parser IMPLEMENTATION. METHOD parse. DATA lx_sxml_parse TYPE REF TO cx_sxml_parse_error. DATA lx_sxml TYPE REF TO cx_dynamic_check. DATA lv_location TYPE string. DATA lv_json TYPE xstring. mv_keep_item_order = iv_keep_item_order. lv_json = lcl_utils=>any_to_xstring( iv_json ). TRY. " TODO sane JSON check: " JSON can be true,false,null,(-)digits " or start from " or from { rt_json_tree = _parse( lv_json ). CATCH cx_sxml_parse_error INTO lx_sxml_parse. lv_location = _get_location( iv_json = lcl_utils=>any_to_string( iv_json ) iv_offset = lx_sxml_parse->xml_offset ). zcx_abapgit_ajson_error=>raise( iv_msg = |Json parsing error (SXML): { lx_sxml_parse->get_text( ) }| iv_location = lv_location ). CATCH cx_dynamic_check INTO lx_sxml. " cx_sxml_error zcx_abapgit_ajson_error=>raise( iv_msg = |Json parsing error (SXML): { lx_sxml->get_text( ) }| iv_location = '@PARSER' ). ENDTRY. ENDMETHOD. METHOD _get_location. DATA lv_json TYPE string. DATA lv_offset TYPE i. DATA lt_text TYPE TABLE OF string. DATA lv_text TYPE string. DATA lv_line TYPE i. DATA lv_pos TYPE i. lv_offset = iv_offset. IF lv_offset < 0. lv_offset = 0. ENDIF. IF lv_offset > strlen( iv_json ). lv_offset = strlen( iv_json ). ENDIF. lv_json = iv_json(lv_offset). REPLACE ALL OCCURRENCES OF cl_abap_char_utilities=>cr_lf IN lv_json WITH cl_abap_char_utilities=>newline. SPLIT lv_json AT cl_abap_char_utilities=>newline INTO TABLE lt_text. lv_line = lines( lt_text ). IF lv_line = 0. lv_line = 1. lv_pos = 1. ELSE. READ TABLE lt_text INDEX lv_line INTO lv_text. lv_pos = strlen( lv_text ) + 1. ENDIF. rv_location = |Line { lv_line }, Offset { lv_pos }|. ENDMETHOD. METHOD _parse. DATA lo_reader TYPE REF TO if_sxml_reader. DATA lr_stack_top LIKE LINE OF mt_stack. DATA lo_node TYPE REF TO if_sxml_node. FIELD-SYMBOLS <item> LIKE LINE OF rt_json_tree. CLEAR mt_stack. CLEAR mv_stack_path. IF iv_json IS INITIAL. RETURN. ENDIF. lo_reader = cl_sxml_string_reader=>create( iv_json ). " TODO: self protection, check non-empty, check starting from object ... DO. lo_node = lo_reader->read_next_node( ). IF lo_node IS NOT BOUND. EXIT. ENDIF. CASE lo_node->type. WHEN if_sxml_node=>co_nt_element_open. DATA lt_attributes TYPE if_sxml_attribute=>attributes. DATA lo_attr LIKE LINE OF lt_attributes. DATA lo_open TYPE REF TO if_sxml_open_element. lo_open ?= lo_node. APPEND INITIAL LINE TO rt_json_tree ASSIGNING <item>. <item>-type = lo_open->qname-name. READ TABLE mt_stack INDEX 1 INTO lr_stack_top. IF sy-subrc = 0. " Using string is faster than rebuilding path from stack <item>-path = mv_stack_path. lr_stack_top->children = lr_stack_top->children + 1. IF lr_stack_top->type = `array`. " This is parser type not ajson type <item>-name = |{ lr_stack_top->children }|. <item>-index = lr_stack_top->children. ELSE. lt_attributes = lo_open->get_attributes( ). LOOP AT lt_attributes INTO lo_attr. IF lo_attr->qname-name = 'name' AND lo_attr->value_type = if_sxml_value=>co_vt_text. <item>-name = lo_attr->get_value( ). ENDIF. ENDLOOP. IF mv_keep_item_order = abap_true. <item>-order = lr_stack_top->children. ENDIF. ENDIF. IF <item>-name IS INITIAL. raise( 'Node without name (maybe not JSON)' ). ENDIF. ENDIF. GET REFERENCE OF <item> INTO lr_stack_top. INSERT lr_stack_top INTO mt_stack INDEX 1. " add path component mv_stack_path = mv_stack_path && <item>-name && '/'. WHEN if_sxml_node=>co_nt_element_close. DATA lo_close TYPE REF TO if_sxml_close_element. lo_close ?= lo_node. READ TABLE mt_stack INDEX 1 INTO lr_stack_top. DELETE mt_stack INDEX 1. IF lo_close->qname-name <> lr_stack_top->type. raise( 'Unexpected closing node type' ). ENDIF. " remove last path component mv_stack_path = substring( val = mv_stack_path len = find( val = mv_stack_path sub = '/' occ = -2 ) + 1 ). WHEN if_sxml_node=>co_nt_value. DATA lo_value TYPE REF TO if_sxml_value_node. lo_value ?= lo_node. <item>-value = lo_value->get_value( ). WHEN OTHERS. raise( 'Unexpected node type' ). ENDCASE. ENDDO. IF lines( mt_stack ) > 0. raise( 'Unexpected end of data' ). ENDIF. ENDMETHOD. METHOD raise. zcx_abapgit_ajson_error=>raise( iv_location = mv_stack_path iv_msg = |JSON PARSER: { iv_error } @ { mv_stack_path }| ). ENDMETHOD. ENDCLASS. ********************************************************************** * SERIALIZER ********************************************************************** CLASS lcl_json_serializer DEFINITION FINAL CREATE PRIVATE. PUBLIC SECTION. CLASS-METHODS stringify IMPORTING it_json_tree TYPE zif_abapgit_ajson_types=>ty_nodes_ts iv_indent TYPE i DEFAULT 0 iv_keep_item_order TYPE abap_bool DEFAULT abap_false RETURNING VALUE(rv_json_string) TYPE string RAISING zcx_abapgit_ajson_error. CLASS-METHODS class_constructor. PRIVATE SECTION. CLASS-DATA gv_comma_with_lf TYPE string. DATA mt_json_tree TYPE zif_abapgit_ajson_types=>ty_nodes_ts. DATA mv_keep_item_order TYPE abap_bool. DATA mt_buffer TYPE string_table. DATA mv_indent_step TYPE i. DATA mv_level TYPE i. CLASS-METHODS escape_string IMPORTING iv_unescaped TYPE string RETURNING VALUE(rv_escaped) TYPE string. METHODS _stringify RETURNING VALUE(rv_json_string) TYPE string RAISING zcx_abapgit_ajson_error. METHODS stringify_node IMPORTING is_node TYPE zif_abapgit_ajson_types=>ty_node RAISING zcx_abapgit_ajson_error. METHODS stringify_set IMPORTING iv_parent_path TYPE string iv_array TYPE abap_bool RAISING zcx_abapgit_ajson_error. ENDCLASS. CLASS lcl_json_serializer IMPLEMENTATION. METHOD class_constructor. gv_comma_with_lf = ',' && cl_abap_char_utilities=>newline. ENDMETHOD. METHOD stringify. DATA lo TYPE REF TO lcl_json_serializer. CREATE OBJECT lo. lo->mt_json_tree = it_json_tree. lo->mv_indent_step = iv_indent. lo->mv_keep_item_order = iv_keep_item_order. rv_json_string = lo->_stringify( ). ENDMETHOD. METHOD _stringify. FIELD-SYMBOLS <n> LIKE LINE OF mt_json_tree. READ TABLE mt_json_tree ASSIGNING <n> WITH KEY path = '' name = ''. " Root IF sy-subrc <> 0. RETURN. ENDIF. stringify_node( <n> ). rv_json_string = concat_lines_of( table = mt_buffer ). ENDMETHOD. METHOD stringify_node. DATA lv_item TYPE string. DATA lv_indent_prefix TYPE string. IF mv_indent_step > 0. lv_indent_prefix = repeat( val = ` ` occ = mv_indent_step * mv_level ). lv_item = lv_indent_prefix. ENDIF. IF is_node-name IS NOT INITIAL AND is_node-index IS INITIAL. " Not root, not array item IF mv_indent_step > 0. lv_item = lv_item && |"{ is_node-name }": |. ELSE. lv_item = |"{ is_node-name }":|. ENDIF. ENDIF. CASE is_node-type. WHEN zif_abapgit_ajson_types=>node_type-array. lv_item = lv_item && '['. WHEN zif_abapgit_ajson_types=>node_type-object. lv_item = lv_item && '{'. WHEN zif_abapgit_ajson_types=>node_type-string. lv_item = lv_item && |"{ escape_string( is_node-value ) }"|. WHEN zif_abapgit_ajson_types=>node_type-boolean OR zif_abapgit_ajson_types=>node_type-number. lv_item = lv_item && is_node-value. WHEN zif_abapgit_ajson_types=>node_type-null. lv_item = lv_item && 'null'. WHEN OTHERS. zcx_abapgit_ajson_error=>raise( iv_msg = |Unexpected type [{ is_node-type }]| iv_location = is_node-path && is_node-name ). ENDCASE. IF mv_indent_step > 0 AND ( is_node-type = zif_abapgit_ajson_types=>node_type-array OR is_node-type = zif_abapgit_ajson_types=>node_type-object ) AND is_node-children > 0. mv_level = mv_level + 1. lv_item = lv_item && cl_abap_char_utilities=>newline. ENDIF. APPEND lv_item TO mt_buffer. " finish complex item IF is_node-type = zif_abapgit_ajson_types=>node_type-array OR is_node-type = zif_abapgit_ajson_types=>node_type-object. DATA lv_children_path TYPE string. DATA lv_tail TYPE string. lv_children_path = is_node-path && is_node-name && '/'. " for root: path = '' and name = '', so result is '/' CASE is_node-type. WHEN zif_abapgit_ajson_types=>node_type-array. IF is_node-children > 0. stringify_set( iv_parent_path = lv_children_path iv_array = abap_true ). ENDIF. lv_tail = ']'. WHEN zif_abapgit_ajson_types=>node_type-object. IF is_node-children > 0. stringify_set( iv_parent_path = lv_children_path iv_array = abap_false ). ENDIF. lv_tail = '}'. ENDCASE. IF mv_indent_step > 0 AND is_node-children > 0. lv_tail = lv_indent_prefix && lv_tail. mv_level = mv_level - 1. ENDIF. APPEND lv_tail TO mt_buffer. ENDIF. ENDMETHOD. METHOD stringify_set. DATA lv_tab_key TYPE string. DATA lv_first_done TYPE abap_bool. FIELD-SYMBOLS <n> LIKE LINE OF mt_json_tree. IF iv_array = abap_true. lv_tab_key = 'array_index'. " path + index ELSEIF mv_keep_item_order = abap_true. lv_tab_key = 'item_order'. " path + order ELSE. lv_tab_key = 'primary_key'. " path + name ENDIF. LOOP AT mt_json_tree ASSIGNING <n> USING KEY (lv_tab_key) WHERE path = iv_parent_path. IF lv_first_done = abap_false. lv_first_done = abap_true. ELSEIF mv_indent_step > 0. APPEND gv_comma_with_lf TO mt_buffer. ELSE. APPEND ',' TO mt_buffer. ENDIF. stringify_node( <n> ). ENDLOOP. IF mv_indent_step > 0 AND lv_first_done = abap_true. " only of items were in the list APPEND cl_abap_char_utilities=>newline TO mt_buffer. ENDIF. ENDMETHOD. METHOD escape_string. rv_escaped = iv_unescaped. IF rv_escaped CA |"\\\t\n\r|. " TODO consider performance ... " see also https://www.json.org/json-en.html rv_escaped = replace( val = rv_escaped sub = '\' with = '\\' occ = 0 ). rv_escaped = replace( val = rv_escaped sub = |\n| with = '\n' occ = 0 ). rv_escaped = replace( val = rv_escaped sub = |\r| with = '\r' occ = 0 ). rv_escaped = replace( val = rv_escaped sub = |\t| with = '\t' occ = 0 ). rv_escaped = replace( val = rv_escaped sub = '"' with = '\"' occ = 0 ). ENDIF. ENDMETHOD. ENDCLASS. ********************************************************************** * JSON_TO_ABAP ********************************************************************** CLASS lcl_json_to_abap DEFINITION FINAL. PUBLIC SECTION. METHODS constructor IMPORTING !iv_corresponding TYPE abap_bool DEFAULT abap_false !ii_custom_mapping TYPE REF TO zif_abapgit_ajson_mapping OPTIONAL. METHODS to_abap IMPORTING it_nodes TYPE zif_abapgit_ajson_types=>ty_nodes_ts CHANGING c_container TYPE any RAISING zcx_abapgit_ajson_error. METHODS to_timestamp IMPORTING iv_value TYPE zif_abapgit_ajson_types=>ty_node-value RETURNING VALUE(rv_result) TYPE timestamp RAISING zcx_abapgit_ajson_error. METHODS to_date IMPORTING iv_value TYPE zif_abapgit_ajson_types=>ty_node-value RETURNING VALUE(rv_result) TYPE d RAISING zcx_abapgit_ajson_error. METHODS to_time IMPORTING iv_value TYPE zif_abapgit_ajson_types=>ty_node-value RETURNING VALUE(rv_result) TYPE t RAISING zcx_abapgit_ajson_error. PRIVATE SECTION. TYPES: BEGIN OF ty_type_cache, type_path TYPE string, target_field_name TYPE string, dd TYPE REF TO cl_abap_datadescr, type_kind LIKE lif_kind=>any, tab_item_buf TYPE REF TO data, END OF ty_type_cache. DATA mt_node_type_cache TYPE HASHED TABLE OF ty_type_cache WITH UNIQUE KEY type_path. DATA mr_nodes TYPE REF TO zif_abapgit_ajson_types=>ty_nodes_ts. DATA mi_custom_mapping TYPE REF TO zif_abapgit_ajson_mapping. DATA mv_corresponding TYPE abap_bool. METHODS any_to_abap IMPORTING iv_path TYPE string is_parent_type TYPE ty_type_cache OPTIONAL i_container_ref TYPE REF TO data RAISING zcx_abapgit_ajson_error. METHODS value_to_abap IMPORTING is_node TYPE zif_abapgit_ajson_types=>ty_node is_node_type TYPE ty_type_cache i_container_ref TYPE REF TO data RAISING zcx_abapgit_ajson_error cx_sy_conversion_no_number. METHODS get_node_type IMPORTING is_node TYPE zif_abapgit_ajson_types=>ty_node OPTIONAL " Empty for root is_parent_type TYPE ty_type_cache OPTIONAL i_container_ref TYPE REF TO data OPTIONAL RETURNING VALUE(rs_node_type) TYPE ty_type_cache RAISING zcx_abapgit_ajson_error. ENDCLASS. CLASS lcl_json_to_abap IMPLEMENTATION. METHOD constructor. mi_custom_mapping = ii_custom_mapping. mv_corresponding = iv_corresponding. ENDMETHOD. METHOD to_abap. DATA lr_ref TYPE REF TO data. CLEAR c_container. " what about data/obj refs ? CLEAR mt_node_type_cache. GET REFERENCE OF c_container INTO lr_ref. GET REFERENCE OF it_nodes INTO mr_nodes. get_node_type( i_container_ref = lr_ref ). " Pre-cache root node type any_to_abap( iv_path = '' i_container_ref = lr_ref ). ENDMETHOD. METHOD get_node_type. DATA lv_node_type_path TYPE string. DATA lo_sdescr TYPE REF TO cl_abap_structdescr. DATA lo_tdescr TYPE REF TO cl_abap_tabledescr. DATA lo_ddescr TYPE REF TO cl_abap_datadescr. " Calculate type path IF is_parent_type-type_kind = lif_kind=>table. lv_node_type_path = is_parent_type-type_path && '/-'. " table item type ELSEIF is_parent_type-type_kind IS NOT INITIAL. lv_node_type_path = is_parent_type-type_path && '/' && is_node-name. ENDIF. " For root node lv_node_type_path remains '' " Get or create cached READ TABLE mt_node_type_cache INTO rs_node_type WITH KEY type_path = lv_node_type_path. IF sy-subrc <> 0. rs_node_type-type_path = lv_node_type_path. IF mi_custom_mapping IS BOUND. rs_node_type-target_field_name = to_upper( mi_custom_mapping->to_abap( iv_path = is_node-path iv_name = is_node-name ) ). IF rs_node_type-target_field_name IS INITIAL. rs_node_type-target_field_name = to_upper( is_node-name ). ENDIF. ELSE. rs_node_type-target_field_name = to_upper( is_node-name ). ENDIF. CASE is_parent_type-type_kind. WHEN lif_kind=>table. lo_tdescr ?= is_parent_type-dd. rs_node_type-dd = lo_tdescr->get_table_line_type( ). WHEN lif_kind=>struct_flat OR lif_kind=>struct_deep. lo_sdescr ?= is_parent_type-dd. lo_sdescr->get_component_type( EXPORTING p_name = rs_node_type-target_field_name RECEIVING p_descr_ref = rs_node_type-dd EXCEPTIONS component_not_found = 4 ). IF sy-subrc <> 0. IF mv_corresponding = abap_false. zcx_abapgit_ajson_error=>raise( |Path not found| ). ELSE. CLEAR rs_node_type. RETURN. ENDIF. ENDIF. WHEN ''. " Root node rs_node_type-dd ?= cl_abap_typedescr=>describe_by_data_ref( i_container_ref ). WHEN OTHERS. zcx_abapgit_ajson_error=>raise( |Unexpected parent type| ). ENDCASE. rs_node_type-type_kind = rs_node_type-dd->type_kind. " for caching and cleaner uninitialized access IF rs_node_type-type_kind = lif_kind=>table. lo_tdescr ?= rs_node_type-dd. IF lo_tdescr->table_kind <> cl_abap_tabledescr=>tablekind_std. lo_ddescr = lo_tdescr->get_table_line_type( ). CREATE DATA rs_node_type-tab_item_buf TYPE HANDLE lo_ddescr. ENDIF. ENDIF. INSERT rs_node_type INTO TABLE mt_node_type_cache. ENDIF. ENDMETHOD. METHOD any_to_abap. DATA ls_node_type LIKE LINE OF mt_node_type_cache. DATA lx_ajson TYPE REF TO zcx_abapgit_ajson_error. DATA lx_root TYPE REF TO cx_root. DATA lr_target_field TYPE REF TO data. FIELD-SYMBOLS <n> TYPE zif_abapgit_ajson_types=>ty_node. FIELD-SYMBOLS <parent_stdtab> TYPE STANDARD TABLE. FIELD-SYMBOLS <parent_anytab> TYPE ANY TABLE. FIELD-SYMBOLS <parent_struc> TYPE any. FIELD-SYMBOLS <tab_item> TYPE any. " Assign container CASE is_parent_type-type_kind. WHEN lif_kind=>table. IF is_parent_type-tab_item_buf IS BOUND. " Indirect hint that table was sorted/hashed, see get_node_type. ASSIGN i_container_ref->* TO <parent_anytab>. ASSERT sy-subrc = 0. lr_target_field = is_parent_type-tab_item_buf. " For hashed/sorted table - same buffer for all children ASSIGN is_parent_type-tab_item_buf->* TO <tab_item>. ASSERT sy-subrc = 0. ELSE. ASSIGN i_container_ref->* TO <parent_stdtab>. ASSERT sy-subrc = 0. ENDIF. WHEN lif_kind=>struct_flat OR lif_kind=>struct_deep. ASSIGN i_container_ref->* TO <parent_struc>. ASSERT sy-subrc = 0. ENDCASE. TRY. " array_index because stringified index goes in wrong order [1, 10, 2 ...] LOOP AT mr_nodes->* ASSIGNING <n> USING KEY array_index WHERE path = iv_path. " Get or create type cache record IF is_parent_type-type_kind <> lif_kind=>table OR ls_node_type-type_kind IS INITIAL. " table records are the same, no need to refetch twice ls_node_type = get_node_type( is_node = <n> is_parent_type = is_parent_type ). IF mv_corresponding = abap_true AND ls_node_type IS INITIAL. CONTINUE. ENDIF. ENDIF. " Validate node type IF ls_node_type-type_kind = lif_kind=>data_ref OR ls_node_type-type_kind = lif_kind=>object_ref. " TODO maybe in future zcx_abapgit_ajson_error=>raise( 'Cannot assign to ref' ). ENDIF. " Find target field reference CASE is_parent_type-type_kind. WHEN lif_kind=>table. IF NOT ls_node_type-target_field_name CO '0123456789'. " Does not affect anything actually but for integrity zcx_abapgit_ajson_error=>raise( 'Need index to access tables' ). ENDIF. IF is_parent_type-tab_item_buf IS NOT BOUND. " Indirect hint that table was srt/hsh, see get_node_type APPEND INITIAL LINE TO <parent_stdtab> REFERENCE INTO lr_target_field. ASSERT sy-subrc = 0. ELSE. CLEAR <tab_item>. ENDIF. WHEN lif_kind=>struct_flat OR lif_kind=>struct_deep. FIELD-SYMBOLS <field> TYPE any. ASSIGN COMPONENT ls_node_type-target_field_name OF STRUCTURE <parent_struc> TO <field>. ASSERT sy-subrc = 0. GET REFERENCE OF <field> INTO lr_target_field. WHEN ''. " Root node lr_target_field = i_container_ref. WHEN OTHERS. zcx_abapgit_ajson_error=>raise( 'Unexpected parent type' ). ENDCASE. " Process value assignment CASE <n>-type. WHEN zif_abapgit_ajson_types=>node_type-object. IF ls_node_type-type_kind <> lif_kind=>struct_flat AND ls_node_type-type_kind <> lif_kind=>struct_deep. zcx_abapgit_ajson_error=>raise( 'Expected structure' ). ENDIF. any_to_abap( iv_path = <n>-path && <n>-name && '/' is_parent_type = ls_node_type i_container_ref = lr_target_field ). WHEN zif_abapgit_ajson_types=>node_type-array. IF NOT ls_node_type-type_kind = lif_kind=>table. zcx_abapgit_ajson_error=>raise( 'Expected table' ). ENDIF. any_to_abap( iv_path = <n>-path && <n>-name && '/' is_parent_type = ls_node_type i_container_ref = lr_target_field ). WHEN OTHERS. value_to_abap( is_node = <n> is_node_type = ls_node_type i_container_ref = lr_target_field ). ENDCASE. IF is_parent_type-tab_item_buf IS BOUND. " Indirect hint that table was sorted/hashed, see get_node_type. TRY. INSERT <tab_item> INTO TABLE <parent_anytab>. IF sy-subrc <> 0. zcx_abapgit_ajson_error=>raise( 'Duplicate insertion' ). ENDIF. CATCH cx_sy_itab_duplicate_key. zcx_abapgit_ajson_error=>raise( 'Duplicate insertion' ). ENDTRY. ENDIF. ENDLOOP. CATCH zcx_abapgit_ajson_error INTO lx_ajson. IF lx_ajson->location IS INITIAL. lx_ajson->set_location( <n>-path && <n>-name ). ENDIF. RAISE EXCEPTION lx_ajson. CATCH cx_sy_conversion_no_number. zcx_abapgit_ajson_error=>raise( iv_msg = 'Source is not a number' iv_location = <n>-path && <n>-name ). CATCH cx_root INTO lx_root. zcx_abapgit_ajson_error=>raise( iv_msg = lx_root->get_text( ) iv_location = <n>-path && <n>-name ). ENDTRY. ENDMETHOD. METHOD value_to_abap. FIELD-SYMBOLS <container> TYPE any. IF is_node_type-type_kind CA lif_kind=>deep_targets. zcx_abapgit_ajson_error=>raise( |Unsupported target for value [{ is_node_type-type_kind }]| ). ENDIF. ASSIGN i_container_ref->* TO <container>. ASSERT sy-subrc = 0. CASE is_node-type. WHEN zif_abapgit_ajson_types=>node_type-null. " Do nothing WHEN zif_abapgit_ajson_types=>node_type-boolean. " TODO: check type ? <container> = boolc( is_node-value = 'true' ). WHEN zif_abapgit_ajson_types=>node_type-number. " TODO: check type ? <container> = is_node-value. WHEN zif_abapgit_ajson_types=>node_type-string. " TODO: check type ? IF is_node-value IS NOT INITIAL. IF is_node_type-type_kind = lif_kind=>date. <container> = to_date( is_node-value ). ELSEIF is_node_type-type_kind = lif_kind=>time. <container> = to_time( is_node-value ). ELSEIF is_node_type-dd->absolute_name = '\TYPE=TIMESTAMP' OR is_node_type-dd->absolute_name = '\TYPE=TIMESTAMPL'. <container> = to_timestamp( is_node-value ). ELSEIF is_node_type-type_kind = lif_kind=>packed. " Number as a string, but not a timestamp <container> = is_node-value. ELSE. <container> = is_node-value. ENDIF. ELSE. <container> = is_node-value. ENDIF. WHEN OTHERS. zcx_abapgit_ajson_error=>raise( |Unexpected JSON type [{ is_node-type }]| ). ENDCASE. ENDMETHOD. METHOD to_date. DATA lv_y TYPE c LENGTH 4. DATA lv_m TYPE c LENGTH 2. DATA lv_d TYPE c LENGTH 2. FIND FIRST OCCURRENCE OF REGEX '^(\d{4})-(\d{2})-(\d{2})(T|$)' IN iv_value SUBMATCHES lv_y lv_m lv_d. IF sy-subrc <> 0. zcx_abapgit_ajson_error=>raise( 'Unexpected date format' ). ENDIF. CONCATENATE lv_y lv_m lv_d INTO rv_result. ENDMETHOD. METHOD to_timestamp. CONSTANTS lc_utc TYPE c LENGTH 6 VALUE 'UTC'. CONSTANTS lc_regex_ts_with_hour TYPE string VALUE `^(\d{4})-(\d{2})-(\d{2})(T)(\d{2}):(\d{2}):(\d{2})(\+)(\d{2}):(\d{2})`. CONSTANTS lc_regex_ts_utc TYPE string VALUE `^(\d{4})-(\d{2})-(\d{2})(T)(\d{2}):(\d{2}):(\d{2})(Z|$)`. DATA: BEGIN OF ls_timestamp, year TYPE c LENGTH 4, month TYPE c LENGTH 2, day TYPE c LENGTH 2, t TYPE c LENGTH 1, hour TYPE c LENGTH 2, minute TYPE c LENGTH 2, second TYPE c LENGTH 2, local_sign TYPE c LENGTH 1, local_hour TYPE c LENGTH 2, local_minute TYPE c LENGTH 2, END OF ls_timestamp. DATA lv_date TYPE d. DATA lv_time TYPE t. DATA lv_seconds_conv TYPE i. DATA lv_timestamp TYPE timestampl. FIND FIRST OCCURRENCE OF REGEX lc_regex_ts_with_hour IN iv_value SUBMATCHES ls_timestamp-year ls_timestamp-month ls_timestamp-day ls_timestamp-t ls_timestamp-hour ls_timestamp-minute ls_timestamp-second ls_timestamp-local_sign ls_timestamp-local_hour ls_timestamp-local_minute. IF sy-subrc = 0. lv_seconds_conv = ( ls_timestamp-local_hour * 3600 ) + ( ls_timestamp-local_minute * 60 ). ELSE. FIND FIRST OCCURRENCE OF REGEX lc_regex_ts_utc IN iv_value SUBMATCHES ls_timestamp-year ls_timestamp-month ls_timestamp-day ls_timestamp-t ls_timestamp-hour ls_timestamp-minute ls_timestamp-second. IF sy-subrc <> 0. zcx_abapgit_ajson_error=>raise( 'Unexpected timestamp format' ). ENDIF. ENDIF. CONCATENATE ls_timestamp-year ls_timestamp-month ls_timestamp-day INTO lv_date. CONCATENATE ls_timestamp-hour ls_timestamp-minute ls_timestamp-second INTO lv_time. CONVERT DATE lv_date TIME lv_time INTO TIME STAMP lv_timestamp TIME ZONE lc_utc. TRY. CASE ls_timestamp-local_sign. WHEN '-'. lv_timestamp = cl_abap_tstmp=>add( tstmp = lv_timestamp secs = lv_seconds_conv ). WHEN '+'. lv_timestamp = cl_abap_tstmp=>subtractsecs( tstmp = lv_timestamp secs = lv_seconds_conv ). ENDCASE. CATCH cx_parameter_invalid_range cx_parameter_invalid_type. zcx_abapgit_ajson_error=>raise( 'Unexpected error calculating timestamp' ). ENDTRY. IF lv_timestamp IS NOT INITIAL. cl_abap_tstmp=>move( EXPORTING tstmp_src = lv_timestamp IMPORTING tstmp_tgt = rv_result ). ENDIF. ENDMETHOD. METHOD to_time. DATA lv_h TYPE c LENGTH 2. DATA lv_m TYPE c LENGTH 2. DATA lv_s TYPE c LENGTH 2. FIND FIRST OCCURRENCE OF REGEX '^(\d{2}):(\d{2}):(\d{2})(T|$)' IN iv_value SUBMATCHES lv_h lv_m lv_s. IF sy-subrc <> 0. zcx_abapgit_ajson_error=>raise( 'Unexpected time format' ). ENDIF. CONCATENATE lv_h lv_m lv_s INTO rv_result. ENDMETHOD. ENDCLASS. ********************************************************************** * ABAP_TO_JSON ********************************************************************** CLASS lcl_abap_to_json DEFINITION FINAL. PUBLIC SECTION. CLASS-METHODS convert IMPORTING iv_data TYPE any is_prefix TYPE zif_abapgit_ajson_types=>ty_path_name OPTIONAL iv_array_index TYPE i DEFAULT 0 ii_custom_mapping TYPE REF TO zif_abapgit_ajson_mapping OPTIONAL is_opts TYPE zif_abapgit_ajson=>ty_opts OPTIONAL iv_item_order TYPE i DEFAULT 0 RETURNING VALUE(rt_nodes) TYPE zif_abapgit_ajson_types=>ty_nodes_tt RAISING zcx_abapgit_ajson_error. CLASS-METHODS insert_with_type IMPORTING iv_data TYPE any iv_type TYPE zif_abapgit_ajson_types=>ty_node_type is_prefix TYPE zif_abapgit_ajson_types=>ty_path_name OPTIONAL iv_array_index TYPE i DEFAULT 0 ii_custom_mapping TYPE REF TO zif_abapgit_ajson_mapping OPTIONAL is_opts TYPE zif_abapgit_ajson=>ty_opts OPTIONAL iv_item_order TYPE i DEFAULT 0 RETURNING VALUE(rt_nodes) TYPE zif_abapgit_ajson_types=>ty_nodes_tt RAISING zcx_abapgit_ajson_error. CLASS-METHODS format_date IMPORTING iv_date TYPE d RETURNING VALUE(rv_str) TYPE string. CLASS-METHODS format_time IMPORTING iv_time TYPE t RETURNING VALUE(rv_str) TYPE string. CLASS-METHODS format_timestamp IMPORTING iv_ts TYPE timestamp RETURNING VALUE(rv_str) TYPE string. CLASS-METHODS class_constructor. PRIVATE SECTION. CLASS-DATA gv_ajson_absolute_type_name TYPE string. DATA mi_custom_mapping TYPE REF TO zif_abapgit_ajson_mapping. DATA mv_keep_item_order TYPE abap_bool. DATA mv_format_datetime TYPE abap_bool. METHODS convert_any IMPORTING iv_data TYPE any io_type TYPE REF TO cl_abap_typedescr is_prefix TYPE zif_abapgit_ajson_types=>ty_path_name iv_index TYPE i DEFAULT 0 iv_item_order TYPE i DEFAULT 0 CHANGING ct_nodes TYPE zif_abapgit_ajson_types=>ty_nodes_tt RAISING zcx_abapgit_ajson_error. METHODS convert_ajson IMPORTING io_json TYPE REF TO zif_abapgit_ajson is_prefix TYPE zif_abapgit_ajson_types=>ty_path_name iv_index TYPE i DEFAULT 0 iv_item_order TYPE i DEFAULT 0 CHANGING ct_nodes TYPE zif_abapgit_ajson_types=>ty_nodes_tt RAISING zcx_abapgit_ajson_error. METHODS convert_value IMPORTING iv_data TYPE any io_type TYPE REF TO cl_abap_typedescr is_prefix TYPE zif_abapgit_ajson_types=>ty_path_name iv_index TYPE i DEFAULT 0 iv_item_order TYPE i DEFAULT 0 CHANGING ct_nodes TYPE zif_abapgit_ajson_types=>ty_nodes_tt RAISING zcx_abapgit_ajson_error. METHODS convert_ref IMPORTING iv_data TYPE any is_prefix TYPE zif_abapgit_ajson_types=>ty_path_name iv_index TYPE i DEFAULT 0 iv_item_order TYPE i DEFAULT 0 CHANGING ct_nodes TYPE zif_abapgit_ajson_types=>ty_nodes_tt RAISING zcx_abapgit_ajson_error. METHODS convert_struc IMPORTING iv_data TYPE any io_type TYPE REF TO cl_abap_typedescr is_prefix TYPE zif_abapgit_ajson_types=>ty_path_name iv_index TYPE i DEFAULT 0 iv_item_order TYPE i DEFAULT 0 CHANGING ct_nodes TYPE zif_abapgit_ajson_types=>ty_nodes_tt RAISING zcx_abapgit_ajson_error. METHODS convert_table IMPORTING iv_data TYPE any io_type TYPE REF TO cl_abap_typedescr is_prefix TYPE zif_abapgit_ajson_types=>ty_path_name iv_index TYPE i DEFAULT 0 iv_item_order TYPE i DEFAULT 0 CHANGING ct_nodes TYPE zif_abapgit_ajson_types=>ty_nodes_tt RAISING zcx_abapgit_ajson_error. METHODS insert_value_with_type IMPORTING iv_data TYPE any iv_type TYPE zif_abapgit_ajson_types=>ty_node_type io_type TYPE REF TO cl_abap_typedescr is_prefix TYPE zif_abapgit_ajson_types=>ty_path_name iv_index TYPE i DEFAULT 0 iv_item_order TYPE i DEFAULT 0 CHANGING ct_nodes TYPE zif_abapgit_ajson_types=>ty_nodes_tt RAISING zcx_abapgit_ajson_error. ENDCLASS. CLASS lcl_abap_to_json IMPLEMENTATION. METHOD class_constructor. DATA lo_dummy TYPE REF TO zcl_abapgit_ajson. DATA lo_type TYPE REF TO cl_abap_refdescr. lo_type ?= cl_abap_typedescr=>describe_by_data( lo_dummy ). gv_ajson_absolute_type_name = lo_type->get_referenced_type( )->absolute_name. ENDMETHOD. METHOD convert. DATA lo_type TYPE REF TO cl_abap_typedescr. DATA lo_converter TYPE REF TO lcl_abap_to_json. lo_type = cl_abap_typedescr=>describe_by_data( iv_data ). CREATE OBJECT lo_converter. lo_converter->mi_custom_mapping = ii_custom_mapping. lo_converter->mv_keep_item_order = is_opts-keep_item_order. lo_converter->mv_format_datetime = is_opts-format_datetime. lo_converter->convert_any( EXPORTING iv_data = iv_data io_type = lo_type is_prefix = is_prefix iv_index = iv_array_index iv_item_order = iv_item_order CHANGING ct_nodes = rt_nodes ). ENDMETHOD. METHOD convert_any. CASE io_type->kind. WHEN cl_abap_typedescr=>kind_elem. convert_value( EXPORTING iv_data = iv_data io_type = io_type is_prefix = is_prefix iv_index = iv_index iv_item_order = iv_item_order CHANGING ct_nodes = ct_nodes ). WHEN cl_abap_typedescr=>kind_struct. convert_struc( EXPORTING iv_data = iv_data io_type = io_type is_prefix = is_prefix iv_index = iv_index iv_item_order = iv_item_order CHANGING ct_nodes = ct_nodes ). WHEN cl_abap_typedescr=>kind_table. convert_table( EXPORTING iv_data = iv_data io_type = io_type is_prefix = is_prefix iv_index = iv_index iv_item_order = iv_item_order CHANGING ct_nodes = ct_nodes ). WHEN OTHERS. IF io_type->type_kind = lif_kind=>data_ref OR iv_data IS INITIAL. " Convert data references and initial references to other types (like ref to class or interface) " Initial references will result in "null" convert_ref( EXPORTING iv_data = iv_data is_prefix = is_prefix iv_index = iv_index iv_item_order = iv_item_order CHANGING ct_nodes = ct_nodes ). ELSEIF io_type->type_kind = lif_kind=>object_ref AND cl_abap_typedescr=>describe_by_object_ref( iv_data )->absolute_name = gv_ajson_absolute_type_name. convert_ajson( EXPORTING io_json = iv_data is_prefix = is_prefix iv_index = iv_index iv_item_order = iv_item_order CHANGING ct_nodes = ct_nodes ). ELSE. zcx_abapgit_ajson_error=>raise( |Unsupported type [{ io_type->type_kind }] @{ is_prefix-path && is_prefix-name }| ). ENDIF. ENDCASE. ENDMETHOD. METHOD convert_ajson. FIELD-SYMBOLS <src> LIKE LINE OF ct_nodes. FIELD-SYMBOLS <dst> LIKE LINE OF ct_nodes. IF io_json IS NOT BOUND. RETURN. ENDIF. LOOP AT io_json->mt_json_tree ASSIGNING <src>. APPEND <src> TO ct_nodes ASSIGNING <dst>. IF <dst>-path IS INITIAL AND <dst>-name IS INITIAL. " root node <dst>-path = is_prefix-path. <dst>-name = is_prefix-name. <dst>-index = iv_index. <dst>-order = iv_item_order. ELSE. <dst>-path = is_prefix-path && is_prefix-name && <dst>-path. ENDIF. ENDLOOP. ENDMETHOD. METHOD format_date. IF iv_date IS NOT INITIAL. rv_str = iv_date+0(4) && '-' && iv_date+4(2) && '-' && iv_date+6(2). ENDIF. ENDMETHOD. METHOD format_time. IF iv_time IS NOT INITIAL. rv_str = iv_time+0(2) && ':' && iv_time+2(2) && ':' && iv_time+4(2). ENDIF. ENDMETHOD. METHOD format_timestamp. CONSTANTS lc_utc TYPE c LENGTH 6 VALUE 'UTC'. DATA lv_date TYPE d. DATA lv_time TYPE t. CONVERT TIME STAMP iv_ts TIME ZONE lc_utc INTO DATE lv_date TIME lv_time. rv_str = lv_date+0(4) && '-' && lv_date+4(2) && '-' && lv_date+6(2) && 'T' && lv_time+0(2) && ':' && lv_time+2(2) && ':' && lv_time+4(2) && 'Z'. ENDMETHOD. METHOD convert_value. DATA ls_node LIKE LINE OF ct_nodes. ls_node-path = is_prefix-path. ls_node-name = is_prefix-name. ls_node-index = iv_index. ls_node-order = iv_item_order. IF ls_node-name IS INITIAL. ls_node-name = is_prefix-name. ENDIF. IF io_type->absolute_name = '\TYPE-POOL=ABAP\TYPE=ABAP_BOOL' OR io_type->absolute_name = '\TYPE=ABAP_BOOLEAN' OR io_type->absolute_name = '\TYPE=XSDBOOLEAN' OR io_type->absolute_name = '\TYPE=FLAG' OR io_type->absolute_name = '\TYPE=XFELD'. ls_node-type = zif_abapgit_ajson_types=>node_type-boolean. IF iv_data IS NOT INITIAL. ls_node-value = 'true'. ELSE. ls_node-value = 'false'. ENDIF. ELSEIF io_type->absolute_name = '\TYPE=TIMESTAMP'. IF mv_format_datetime = abap_true. ls_node-type = zif_abapgit_ajson_types=>node_type-string. ls_node-value = format_timestamp( iv_data ). ELSE. ls_node-type = zif_abapgit_ajson_types=>node_type-number. ls_node-value = |{ iv_data }|. ENDIF. ELSEIF io_type->type_kind CO lif_kind=>texts OR io_type->type_kind CO lif_kind=>binary OR io_type->type_kind CO lif_kind=>enum. ls_node-type = zif_abapgit_ajson_types=>node_type-string. ls_node-value = |{ iv_data }|. ELSEIF io_type->type_kind = lif_kind=>date. ls_node-type = zif_abapgit_ajson_types=>node_type-string. IF mv_format_datetime = abap_true. ls_node-value = format_date( iv_data ). ELSE. ls_node-value = |{ iv_data }|. ENDIF. ELSEIF io_type->type_kind = lif_kind=>time. ls_node-type = zif_abapgit_ajson_types=>node_type-string. IF mv_format_datetime = abap_true. ls_node-value = format_time( iv_data ). ELSE. ls_node-value = |{ iv_data }|. ENDIF. ELSEIF io_type->type_kind CO lif_kind=>numeric. ls_node-type = zif_abapgit_ajson_types=>node_type-number. ls_node-value = |{ iv_data }|. ELSE. zcx_abapgit_ajson_error=>raise( |Unexpected elementary type [{ io_type->type_kind }] @{ is_prefix-path && is_prefix-name }| ). ENDIF. APPEND ls_node TO ct_nodes. ENDMETHOD. METHOD convert_ref. DATA ls_node LIKE LINE OF ct_nodes. ls_node-path = is_prefix-path. ls_node-name = is_prefix-name. ls_node-index = iv_index. ls_node-order = iv_item_order. IF mi_custom_mapping IS BOUND. ls_node-name = mi_custom_mapping->to_json( iv_path = is_prefix-path iv_name = is_prefix-name ). ENDIF. IF ls_node-name IS INITIAL. ls_node-name = is_prefix-name. ENDIF. IF iv_data IS INITIAL. ls_node-type = zif_abapgit_ajson_types=>node_type-null. ls_node-value = 'null'. ELSE. " TODO support data references zcx_abapgit_ajson_error=>raise( |Unexpected reference @{ is_prefix-path && is_prefix-name }| ). ENDIF. APPEND ls_node TO ct_nodes. ENDMETHOD. METHOD convert_struc. DATA lo_struc TYPE REF TO cl_abap_structdescr. DATA lt_comps TYPE cl_abap_structdescr=>included_view. DATA ls_next_prefix LIKE is_prefix. DATA lv_mapping_prefix_name LIKE is_prefix-name. DATA lv_item_order TYPE i. DATA ls_root LIKE LINE OF ct_nodes. FIELD-SYMBOLS <root> LIKE ls_root. FIELD-SYMBOLS <c> LIKE LINE OF lt_comps. FIELD-SYMBOLS <val> TYPE any. " Object root ls_root-path = is_prefix-path. ls_root-name = is_prefix-name. ls_root-type = zif_abapgit_ajson_types=>node_type-object. ls_root-index = iv_index. IF mi_custom_mapping IS BOUND. ls_root-name = mi_custom_mapping->to_json( iv_path = is_prefix-path iv_name = is_prefix-name ). ENDIF. IF ls_root-name IS INITIAL. ls_root-name = is_prefix-name. ENDIF. ls_root-order = iv_item_order. APPEND ls_root TO ct_nodes ASSIGNING <root>. " Object attributes lo_struc ?= io_type. lt_comps = lo_struc->get_included_view( ). " replaced call to get_components() with get_included_view() to avoid problems with suffixes in includes. " get_components is potentially much slower than lo_struc->components " but ! we still need it to identify booleans " and rtti seems to cache type descriptions really well (https://github.com/sbcgua/benchmarks.git) " the structures will be repeated in real life ls_next_prefix-path = is_prefix-path && <root>-name && '/'. LOOP AT lt_comps ASSIGNING <c>. CLEAR lv_mapping_prefix_name. <root>-children = <root>-children + 1. ls_next_prefix-name = to_lower( <c>-name ). ASSIGN COMPONENT <c>-name OF STRUCTURE iv_data TO <val>. ASSERT sy-subrc = 0. IF mi_custom_mapping IS BOUND AND <c>-type->kind = cl_abap_typedescr=>kind_elem. lv_mapping_prefix_name = mi_custom_mapping->to_json( iv_path = ls_next_prefix-path iv_name = ls_next_prefix-name ). ENDIF. IF lv_mapping_prefix_name IS NOT INITIAL. ls_next_prefix-name = lv_mapping_prefix_name. ENDIF. IF mv_keep_item_order = abap_true. lv_item_order = <root>-children. ENDIF. convert_any( EXPORTING iv_data = <val> io_type = <c>-type is_prefix = ls_next_prefix iv_item_order = lv_item_order CHANGING ct_nodes = ct_nodes ). ENDLOOP. ENDMETHOD. METHOD convert_table. DATA lo_table TYPE REF TO cl_abap_tabledescr. DATA lo_ltype TYPE REF TO cl_abap_typedescr. DATA ls_next_prefix LIKE is_prefix. DATA lv_tabix TYPE sy-tabix. DATA ls_root LIKE LINE OF ct_nodes. FIELD-SYMBOLS <root> LIKE ls_root. FIELD-SYMBOLS <tab> TYPE ANY TABLE. FIELD-SYMBOLS <val> TYPE any. " Array root ls_root-path = is_prefix-path. ls_root-name = is_prefix-name. ls_root-type = zif_abapgit_ajson_types=>node_type-array. ls_root-index = iv_index. ls_root-order = iv_item_order. IF mi_custom_mapping IS BOUND. ls_root-name = mi_custom_mapping->to_json( iv_path = is_prefix-path iv_name = is_prefix-name ). ENDIF. IF ls_root-name IS INITIAL. ls_root-name = is_prefix-name. ENDIF. APPEND ls_root TO ct_nodes ASSIGNING <root>. " Array items lo_table ?= io_type. lo_ltype = lo_table->get_table_line_type( ). ls_next_prefix-path = is_prefix-path && <root>-name && '/'. ASSIGN iv_data TO <tab>. lv_tabix = 1. LOOP AT <tab> ASSIGNING <val>. ls_next_prefix-name = to_lower( |{ lv_tabix }| ). convert_any( EXPORTING iv_data = <val> io_type = lo_ltype is_prefix = ls_next_prefix iv_index = <root>-children + 1 CHANGING ct_nodes = ct_nodes ). <root>-children = <root>-children + 1. lv_tabix = lv_tabix + 1. ENDLOOP. ENDMETHOD. METHOD insert_with_type. DATA lo_type TYPE REF TO cl_abap_typedescr. DATA lo_converter TYPE REF TO lcl_abap_to_json. lo_type = cl_abap_typedescr=>describe_by_data( iv_data ). CREATE OBJECT lo_converter. lo_converter->mi_custom_mapping = ii_custom_mapping. lo_converter->mv_keep_item_order = is_opts-keep_item_order. lo_converter->mv_format_datetime = is_opts-format_datetime. lo_converter->insert_value_with_type( EXPORTING iv_data = iv_data iv_type = iv_type io_type = lo_type is_prefix = is_prefix iv_index = iv_array_index iv_item_order = iv_item_order CHANGING ct_nodes = rt_nodes ). ENDMETHOD. METHOD insert_value_with_type. DATA lv_prefix TYPE string. DATA ls_node LIKE LINE OF ct_nodes. lv_prefix = is_prefix-path && is_prefix-name. IF io_type->type_kind CO lif_kind=>texts OR io_type->type_kind CO lif_kind=>date OR io_type->type_kind CO lif_kind=>time. IF iv_type = zif_abapgit_ajson_types=>node_type-boolean AND iv_data <> 'true' AND iv_data <> 'false'. zcx_abapgit_ajson_error=>raise( |Unexpected boolean value [{ iv_data }] @{ lv_prefix }| ). ELSEIF iv_type = zif_abapgit_ajson_types=>node_type-null AND iv_data IS NOT INITIAL. zcx_abapgit_ajson_error=>raise( |Unexpected null value [{ iv_data }] @{ lv_prefix }| ). ELSEIF iv_type = zif_abapgit_ajson_types=>node_type-number AND iv_data CN '0123456789. E+-'. zcx_abapgit_ajson_error=>raise( |Unexpected numeric value [{ iv_data }] @{ lv_prefix }| ). ELSEIF iv_type <> zif_abapgit_ajson_types=>node_type-string AND iv_type <> zif_abapgit_ajson_types=>node_type-boolean AND iv_type <> zif_abapgit_ajson_types=>node_type-null AND iv_type <> zif_abapgit_ajson_types=>node_type-number. zcx_abapgit_ajson_error=>raise( |Unexpected type for value [{ iv_type },{ iv_data }] @{ lv_prefix }| ). ENDIF. ELSEIF io_type->type_kind CO lif_kind=>numeric. IF iv_type <> zif_abapgit_ajson_types=>node_type-number. zcx_abapgit_ajson_error=>raise( |Unexpected value for numeric [{ iv_data }] @{ lv_prefix }| ). ENDIF. ELSE. zcx_abapgit_ajson_error=>raise( |Unexpected type [{ io_type->type_kind }] @{ lv_prefix }| ). ENDIF. ls_node-path = is_prefix-path. ls_node-name = is_prefix-name. ls_node-index = iv_index. ls_node-value = iv_data. ls_node-type = iv_type. ls_node-order = iv_item_order. IF mi_custom_mapping IS BOUND. ls_node-name = mi_custom_mapping->to_json( iv_path = is_prefix-path iv_name = is_prefix-name ). ENDIF. IF ls_node-name IS INITIAL. ls_node-name = is_prefix-name. ENDIF. APPEND ls_node TO ct_nodes. ENDMETHOD. ENDCLASS. ********************************************************************** * MUTATOR INTERFACE ********************************************************************** INTERFACE lif_mutator_runner. METHODS run IMPORTING it_source_tree TYPE zif_abapgit_ajson_types=>ty_nodes_ts EXPORTING et_dest_tree TYPE zif_abapgit_ajson_types=>ty_nodes_ts RAISING zcx_abapgit_ajson_error. ENDINTERFACE. ********************************************************************** * FILTER RUNNER ********************************************************************** CLASS lcl_filter_runner DEFINITION FINAL. PUBLIC SECTION. INTERFACES lif_mutator_runner. CLASS-METHODS new IMPORTING ii_filter TYPE REF TO zif_abapgit_ajson_filter RETURNING VALUE(ro_instance) TYPE REF TO lcl_filter_runner. METHODS constructor IMPORTING ii_filter TYPE REF TO zif_abapgit_ajson_filter. PRIVATE SECTION. DATA mi_filter TYPE REF TO zif_abapgit_ajson_filter. DATA mr_source_tree TYPE REF TO zif_abapgit_ajson_types=>ty_nodes_ts. DATA mr_dest_tree TYPE REF TO zif_abapgit_ajson_types=>ty_nodes_ts. METHODS walk IMPORTING iv_path TYPE string CHANGING cs_parent TYPE zif_abapgit_ajson_types=>ty_node OPTIONAL RAISING zcx_abapgit_ajson_error. ENDCLASS. CLASS lcl_filter_runner IMPLEMENTATION. METHOD new. CREATE OBJECT ro_instance EXPORTING ii_filter = ii_filter. ENDMETHOD. METHOD constructor. ASSERT ii_filter IS BOUND. mi_filter = ii_filter. ENDMETHOD. METHOD lif_mutator_runner~run. CLEAR et_dest_tree. GET REFERENCE OF it_source_tree INTO mr_source_tree. GET REFERENCE OF et_dest_tree INTO mr_dest_tree. walk( iv_path = '' ). ENDMETHOD. METHOD walk. DATA ls_node TYPE zif_abapgit_ajson_types=>ty_node. DATA lv_tab_key TYPE string. IF cs_parent-type = zif_abapgit_ajson_types=>node_type-array. lv_tab_key = 'array_index'. " path + index ENDIF. LOOP AT mr_source_tree->* INTO ls_node USING KEY (lv_tab_key) WHERE path = iv_path. CASE ls_node-type. WHEN zif_abapgit_ajson_types=>node_type-boolean OR zif_abapgit_ajson_types=>node_type-null OR zif_abapgit_ajson_types=>node_type-number OR zif_abapgit_ajson_types=>node_type-string. IF mi_filter->keep_node( ls_node ) = abap_false. CONTINUE. ENDIF. WHEN zif_abapgit_ajson_types=>node_type-array OR zif_abapgit_ajson_types=>node_type-object. IF mi_filter->keep_node( is_node = ls_node iv_visit = zif_abapgit_ajson_filter=>visit_type-open ) = abap_false. CONTINUE. ENDIF. " Intentionally clear AFTER "open" CLEAR ls_node-children. walk( EXPORTING iv_path = iv_path && ls_node-name && `/` CHANGING cs_parent = ls_node ). IF mi_filter->keep_node( is_node = ls_node iv_visit = zif_abapgit_ajson_filter=>visit_type-close ) = abap_false. CONTINUE. ENDIF. WHEN OTHERS. zcx_abapgit_ajson_error=>raise( |Unexpected node type { ls_node-type }| ). ENDCASE. IF cs_parent IS SUPPLIED. cs_parent-children = cs_parent-children + 1. IF cs_parent-type = zif_abapgit_ajson_types=>node_type-array. ls_node-name = |{ cs_parent-children }|. ls_node-index = cs_parent-children. ENDIF. ENDIF. INSERT ls_node INTO TABLE mr_dest_tree->*. ENDLOOP. ENDMETHOD. ENDCLASS. ********************************************************************** * MAPPER RUNNER ********************************************************************** CLASS lcl_mapper_runner DEFINITION FINAL. PUBLIC SECTION. INTERFACES lif_mutator_runner. CLASS-METHODS new IMPORTING ii_mapper TYPE REF TO zif_abapgit_ajson_mapping RETURNING VALUE(ro_instance) TYPE REF TO lcl_mapper_runner. METHODS constructor IMPORTING ii_mapper TYPE REF TO zif_abapgit_ajson_mapping. PRIVATE SECTION. DATA mi_mapper TYPE REF TO zif_abapgit_ajson_mapping. DATA mr_source_tree TYPE REF TO zif_abapgit_ajson_types=>ty_nodes_ts. DATA mr_dest_tree TYPE REF TO zif_abapgit_ajson_types=>ty_nodes_ts. METHODS process_deep_node IMPORTING iv_path TYPE string iv_renamed_path TYPE string iv_node_type TYPE zif_abapgit_ajson_types=>ty_node-type RAISING zcx_abapgit_ajson_error. ENDCLASS. CLASS lcl_mapper_runner IMPLEMENTATION. METHOD new. CREATE OBJECT ro_instance EXPORTING ii_mapper = ii_mapper. ENDMETHOD. METHOD constructor. ASSERT ii_mapper IS BOUND. mi_mapper = ii_mapper. ENDMETHOD. METHOD lif_mutator_runner~run. FIELD-SYMBOLS <root> LIKE LINE OF it_source_tree. READ TABLE it_source_tree WITH KEY path = `` name = `` ASSIGNING <root>. IF sy-subrc <> 0 OR NOT ( <root>-type = zif_abapgit_ajson_types=>node_type-array OR <root>-type = zif_abapgit_ajson_types=>node_type-object ). " empty or one-value-only tree et_dest_tree = it_source_tree. RETURN. ENDIF. CLEAR et_dest_tree. GET REFERENCE OF it_source_tree INTO mr_source_tree. GET REFERENCE OF et_dest_tree INTO mr_dest_tree. INSERT <root> INTO TABLE et_dest_tree. process_deep_node( iv_path = `/` iv_renamed_path = `/` iv_node_type = <root>-type ). ENDMETHOD. METHOD process_deep_node. FIELD-SYMBOLS <item> LIKE LINE OF mr_source_tree->*. DATA ls_renamed_node LIKE <item>. LOOP AT mr_source_tree->* ASSIGNING <item> WHERE path = iv_path. ls_renamed_node = <item>. IF iv_node_type <> zif_abapgit_ajson_types=>node_type-array. " don't rename array item names -> they are numeric index mi_mapper->rename_node( EXPORTING is_node = <item> CHANGING cv_name = ls_renamed_node-name ). IF ls_renamed_node-name IS INITIAL. zcx_abapgit_ajson_error=>raise( iv_msg = 'Renamed node name cannot be empty' is_node = <item> ). ENDIF. ENDIF. ls_renamed_node-path = iv_renamed_path. INSERT ls_renamed_node INTO TABLE mr_dest_tree->*. IF sy-subrc <> 0. " = 4 ? zcx_abapgit_ajson_error=>raise( iv_msg = 'Renamed node has a duplicate' is_node = ls_renamed_node ). ENDIF. " maybe also catch CX_SY_ITAB_DUPLICATE_KEY but secondary keys are not changed here, so not for now IF <item>-type = zif_abapgit_ajson_types=>node_type-array OR <item>-type = zif_abapgit_ajson_types=>node_type-object. process_deep_node( iv_path = iv_path && <item>-name && `/` iv_renamed_path = iv_renamed_path && ls_renamed_node-name && `/` iv_node_type = <item>-type ). ENDIF. ENDLOOP. ENDMETHOD. ENDCLASS. ********************************************************************** * MUTATOR QUEUE ********************************************************************** CLASS lcl_mutator_queue DEFINITION FINAL. PUBLIC SECTION. INTERFACES lif_mutator_runner. CLASS-METHODS new RETURNING VALUE(ro_instance) TYPE REF TO lcl_mutator_queue. METHODS add IMPORTING ii_mutator TYPE REF TO lif_mutator_runner RETURNING VALUE(ro_self) TYPE REF TO lcl_mutator_queue. PRIVATE SECTION. DATA mt_queue TYPE STANDARD TABLE OF REF TO lif_mutator_runner. ENDCLASS. CLASS lcl_mutator_queue IMPLEMENTATION. METHOD add. IF ii_mutator IS BOUND. APPEND ii_mutator TO mt_queue. ENDIF. ro_self = me. ENDMETHOD. METHOD new. CREATE OBJECT ro_instance. ENDMETHOD. METHOD lif_mutator_runner~run. DATA li_mutator TYPE REF TO lif_mutator_runner. DATA lv_qsize TYPE i. FIELD-SYMBOLS <from> LIKE it_source_tree. FIELD-SYMBOLS <to> LIKE it_source_tree. DATA lr_buf TYPE REF TO zif_abapgit_ajson_types=>ty_nodes_ts. lv_qsize = lines( mt_queue ). IF lv_qsize = 0. et_dest_tree = it_source_tree. RETURN. ENDIF. LOOP AT mt_queue INTO li_mutator. IF sy-tabix = 1. ASSIGN it_source_tree TO <from>. ELSE. ASSIGN lr_buf->* TO <from>. ENDIF. IF sy-tabix = lv_qsize. ASSIGN et_dest_tree TO <to>. ELSE. CREATE DATA lr_buf. ASSIGN lr_buf->* TO <to>. ENDIF. li_mutator->run( EXPORTING it_source_tree = <from> IMPORTING et_dest_tree = <to> ). ENDLOOP. ENDMETHOD. ENDCLASS. |