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
| | /* JSON parsing and serialization.
Copyright (C) 2017-2024 Free Software Foundation, Inc.
This file is part of GNU Emacs.
GNU Emacs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
#include <config.h>
#include <errno.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <math.h>
#include <jansson.h>
#include "lisp.h"
#include "buffer.h"
#include "coding.h"
#ifdef WINDOWSNT
# include <windows.h>
# include "w32common.h"
# include "w32.h"
DEF_DLL_FN (void, json_set_alloc_funcs,
(json_malloc_t malloc_fn, json_free_t free_fn));
DEF_DLL_FN (void, json_delete, (json_t *json));
DEF_DLL_FN (json_t *, json_array, (void));
DEF_DLL_FN (int, json_array_append_new, (json_t *array, json_t *value));
DEF_DLL_FN (size_t, json_array_size, (const json_t *array));
DEF_DLL_FN (json_t *, json_object, (void));
DEF_DLL_FN (int, json_object_set_new,
(json_t *object, const char *key, json_t *value));
DEF_DLL_FN (json_t *, json_null, (void));
DEF_DLL_FN (json_t *, json_true, (void));
DEF_DLL_FN (json_t *, json_false, (void));
DEF_DLL_FN (json_t *, json_integer, (json_int_t value));
DEF_DLL_FN (json_t *, json_real, (double value));
DEF_DLL_FN (json_t *, json_stringn, (const char *value, size_t len));
DEF_DLL_FN (char *, json_dumps, (const json_t *json, size_t flags));
DEF_DLL_FN (int, json_dump_callback,
(const json_t *json, json_dump_callback_t callback, void *data,
size_t flags));
DEF_DLL_FN (json_t *, json_object_get, (const json_t *object, const char *key));
/* This is called by json_decref, which is an inline function. */
void json_delete(json_t *json)
{
fn_json_delete (json);
}
static bool json_initialized;
static bool
init_json_functions (void)
{
HMODULE library = w32_delayed_load (Qjson);
if (!library)
return false;
LOAD_DLL_FN (library, json_set_alloc_funcs);
LOAD_DLL_FN (library, json_delete);
LOAD_DLL_FN (library, json_array);
LOAD_DLL_FN (library, json_array_append_new);
LOAD_DLL_FN (library, json_array_size);
LOAD_DLL_FN (library, json_object);
LOAD_DLL_FN (library, json_object_set_new);
LOAD_DLL_FN (library, json_null);
LOAD_DLL_FN (library, json_true);
LOAD_DLL_FN (library, json_false);
LOAD_DLL_FN (library, json_integer);
LOAD_DLL_FN (library, json_real);
LOAD_DLL_FN (library, json_stringn);
LOAD_DLL_FN (library, json_dumps);
LOAD_DLL_FN (library, json_dump_callback);
LOAD_DLL_FN (library, json_object_get);
init_json ();
return true;
}
#define json_set_alloc_funcs fn_json_set_alloc_funcs
#define json_array fn_json_array
#define json_array_append_new fn_json_array_append_new
#define json_array_size fn_json_array_size
#define json_object fn_json_object
#define json_object_set_new fn_json_object_set_new
#define json_null fn_json_null
#define json_true fn_json_true
#define json_false fn_json_false
#define json_integer fn_json_integer
#define json_real fn_json_real
#define json_stringn fn_json_stringn
#define json_dumps fn_json_dumps
#define json_dump_callback fn_json_dump_callback
#define json_object_get fn_json_object_get
#endif /* WINDOWSNT */
/* We install a custom allocator so that we can avoid objects larger
than PTRDIFF_MAX. Such objects wouldn't play well with the rest of
Emacs's codebase, which generally uses ptrdiff_t for sizes and
indices. The other functions in this file also generally assume
that size_t values never exceed PTRDIFF_MAX.
In addition, we need to use a custom allocator because on
MS-Windows we replace malloc/free with our own functions, see
w32heap.c, so we must force the library to use our allocator, or
else we won't be able to free storage allocated by the library. */
static void *
json_malloc (size_t size)
{
if (size > PTRDIFF_MAX)
{
errno = ENOMEM;
return NULL;
}
return malloc (size);
}
static void
json_free (void *ptr)
{
free (ptr);
}
void
init_json (void)
{
json_set_alloc_funcs (json_malloc, json_free);
}
/* Note that all callers of make_string_from_utf8 and build_string_from_utf8
below either pass only value UTF-8 strings or use the functionf for
formatting error messages; in the latter case correctness isn't
critical. */
/* Return a unibyte string containing the sequence of UTF-8 encoding
units of the UTF-8 representation of STRING. If STRING does not
represent a sequence of Unicode scalar values, return a string with
unspecified contents. */
static Lisp_Object
json_encode (Lisp_Object string)
{
/* FIXME: Raise an error if STRING is not a scalar value
sequence. */
return encode_string_utf_8 (string, Qnil, false, Qt, Qt);
}
static AVOID
json_out_of_memory (void)
{
xsignal0 (Qjson_out_of_memory);
}
static void
json_release_object (void *object)
{
json_decref (object);
}
/* Signal an error if OBJECT is not a string, or if OBJECT contains
embedded null characters. */
static void
check_string_without_embedded_nulls (Lisp_Object object)
{
CHECK_STRING (object);
CHECK_TYPE (memchr (SDATA (object), '\0', SBYTES (object)) == NULL,
Qstring_without_embedded_nulls_p, object);
}
/* Signal an error of type `json-out-of-memory' if OBJECT is
NULL. */
static json_t *
json_check (json_t *object)
{
if (object == NULL)
json_out_of_memory ();
return object;
}
/* If STRING is not a valid UTF-8 string, signal an error of type
`wrong-type-argument'. STRING must be a unibyte string. */
static void
json_check_utf8 (Lisp_Object string)
{
CHECK_TYPE (utf8_string_p (string), Qutf_8_string_p, string);
}
enum json_object_type {
json_object_hashtable,
json_object_alist,
json_object_plist
};
enum json_array_type {
json_array_array,
json_array_list
};
struct json_configuration {
enum json_object_type object_type;
enum json_array_type array_type;
Lisp_Object null_object;
Lisp_Object false_object;
};
static json_t *lisp_to_json (Lisp_Object,
const struct json_configuration *conf);
/* Convert a Lisp object to a nonscalar JSON object (array or object). */
static json_t *
lisp_to_json_nonscalar_1 (Lisp_Object lisp,
const struct json_configuration *conf)
{
json_t *json;
specpdl_ref count;
if (VECTORP (lisp))
{
ptrdiff_t size = ASIZE (lisp);
json = json_check (json_array ());
count = SPECPDL_INDEX ();
record_unwind_protect_ptr (json_release_object, json);
for (ptrdiff_t i = 0; i < size; ++i)
{
int status
= json_array_append_new (json, lisp_to_json (AREF (lisp, i),
conf));
if (status == -1)
json_out_of_memory ();
}
eassert (json_array_size (json) == size);
}
else if (HASH_TABLE_P (lisp))
{
struct Lisp_Hash_Table *h = XHASH_TABLE (lisp);
json = json_check (json_object ());
count = SPECPDL_INDEX ();
record_unwind_protect_ptr (json_release_object, json);
DOHASH (h, key, v)
{
CHECK_STRING (key);
Lisp_Object ekey = json_encode (key);
/* We can't specify the length, so the string must be
null-terminated. */
check_string_without_embedded_nulls (ekey);
const char *key_str = SSDATA (ekey);
/* Reject duplicate keys. These are possible if the hash
table test is not `equal'. */
if (json_object_get (json, key_str) != NULL)
wrong_type_argument (Qjson_value_p, lisp);
int status
= json_object_set_new (json, key_str,
lisp_to_json (v, conf));
if (status == -1)
{
/* A failure can be caused either by an invalid key or
by low memory. */
json_check_utf8 (ekey);
json_out_of_memory ();
}
}
}
else if (NILP (lisp))
return json_check (json_object ());
else if (CONSP (lisp))
{
Lisp_Object tail = lisp;
json = json_check (json_object ());
count = SPECPDL_INDEX ();
record_unwind_protect_ptr (json_release_object, json);
bool is_plist = !CONSP (XCAR (tail));
FOR_EACH_TAIL (tail)
{
const char *key_str;
Lisp_Object value;
Lisp_Object key_symbol;
if (is_plist)
{
key_symbol = XCAR (tail);
tail = XCDR (tail);
CHECK_CONS (tail);
value = XCAR (tail);
}
else
{
Lisp_Object pair = XCAR (tail);
CHECK_CONS (pair);
key_symbol = XCAR (pair);
value = XCDR (pair);
}
CHECK_SYMBOL (key_symbol);
Lisp_Object key = SYMBOL_NAME (key_symbol);
/* We can't specify the length, so the string must be
null-terminated. */
check_string_without_embedded_nulls (key);
key_str = SSDATA (key);
/* In plists, ensure leading ":" in keys is stripped. It
will be reconstructed later in `json_to_lisp'.*/
if (is_plist && ':' == key_str[0] && key_str[1])
{
key_str = &key_str[1];
}
/* Only add element if key is not already present. */
if (json_object_get (json, key_str) == NULL)
{
int status
= json_object_set_new (json, key_str, lisp_to_json (value,
conf));
if (status == -1)
json_out_of_memory ();
}
}
CHECK_LIST_END (tail, lisp);
}
else
wrong_type_argument (Qjson_value_p, lisp);
clear_unwind_protect (count);
unbind_to (count, Qnil);
return json;
}
/* Convert LISP to a nonscalar JSON object (array or object). Signal
an error of type `wrong-type-argument' if LISP is not a vector,
hashtable, alist, or plist. */
static json_t *
lisp_to_json_nonscalar (Lisp_Object lisp,
const struct json_configuration *conf)
{
if (++lisp_eval_depth > max_lisp_eval_depth)
xsignal0 (Qjson_object_too_deep);
json_t *json = lisp_to_json_nonscalar_1 (lisp, conf);
--lisp_eval_depth;
return json;
}
/* Convert LISP to any JSON object. Signal an error of type
`wrong-type-argument' if the type of LISP can't be converted to a
JSON object. */
static json_t *
lisp_to_json (Lisp_Object lisp, const struct json_configuration *conf)
{
if (EQ (lisp, conf->null_object))
return json_check (json_null ());
else if (EQ (lisp, conf->false_object))
return json_check (json_false ());
else if (EQ (lisp, Qt))
return json_check (json_true ());
else if (INTEGERP (lisp))
{
intmax_t low = TYPE_MINIMUM (json_int_t);
intmax_t high = TYPE_MAXIMUM (json_int_t);
intmax_t value = check_integer_range (lisp, low, high);
return json_check (json_integer (value));
}
else if (FLOATP (lisp))
return json_check (json_real (XFLOAT_DATA (lisp)));
else if (STRINGP (lisp))
{
Lisp_Object encoded = json_encode (lisp);
json_t *json = json_stringn (SSDATA (encoded), SBYTES (encoded));
if (json == NULL)
{
/* A failure can be caused either by an invalid string or by
low memory. */
json_check_utf8 (encoded);
json_out_of_memory ();
}
return json;
}
/* LISP now must be a vector, hashtable, alist, or plist. */
return lisp_to_json_nonscalar (lisp, conf);
}
static void
json_parse_args (ptrdiff_t nargs,
Lisp_Object *args,
struct json_configuration *conf,
bool parse_object_types)
{
if ((nargs % 2) != 0)
wrong_type_argument (Qplistp, Flist (nargs, args));
/* Start from the back so keyword values appearing
first take precedence. */
for (ptrdiff_t i = nargs; i > 0; i -= 2) {
Lisp_Object key = args[i - 2];
Lisp_Object value = args[i - 1];
if (parse_object_types && EQ (key, QCobject_type))
{
if (EQ (value, Qhash_table))
conf->object_type = json_object_hashtable;
else if (EQ (value, Qalist))
conf->object_type = json_object_alist;
else if (EQ (value, Qplist))
conf->object_type = json_object_plist;
else
wrong_choice (list3 (Qhash_table, Qalist, Qplist), value);
}
else if (parse_object_types && EQ (key, QCarray_type))
{
if (EQ (value, Qarray))
conf->array_type = json_array_array;
else if (EQ (value, Qlist))
conf->array_type = json_array_list;
else
wrong_choice (list2 (Qarray, Qlist), value);
}
else if (EQ (key, QCnull_object))
conf->null_object = value;
else if (EQ (key, QCfalse_object))
conf->false_object = value;
else if (parse_object_types)
wrong_choice (list4 (QCobject_type,
QCarray_type,
QCnull_object,
QCfalse_object),
value);
else
wrong_choice (list2 (QCnull_object,
QCfalse_object),
value);
}
}
static bool
json_available_p (void)
{
#ifdef WINDOWSNT
if (!json_initialized)
{
Lisp_Object status;
json_initialized = init_json_functions ();
status = json_initialized ? Qt : Qnil;
Vlibrary_cache = Fcons (Fcons (Qjson, status), Vlibrary_cache);
}
return json_initialized;
#else /* !WINDOWSNT */
return true;
#endif
}
#ifdef WINDOWSNT
static void
ensure_json_available (void)
{
if (!json_available_p ())
Fsignal (Qjson_unavailable,
list1 (build_unibyte_string ("jansson library not found")));
}
#endif
DEFUN ("json--available-p", Fjson__available_p, Sjson__available_p, 0, 0, NULL,
doc: /* Return non-nil if libjansson is available (internal use only). */)
(void)
{
return json_available_p () ? Qt : Qnil;
}
DEFUN ("json-serialize", Fjson_serialize, Sjson_serialize, 1, MANY,
NULL,
doc: /* Return the JSON representation of OBJECT as a string.
OBJECT must be t, a number, string, vector, hashtable, alist, plist,
or the Lisp equivalents to the JSON null and false values, and its
elements must recursively consist of the same kinds of values. t will
be converted to the JSON true value. Vectors will be converted to
JSON arrays, whereas hashtables, alists and plists are converted to
JSON objects. Hashtable keys must be strings without embedded null
characters and must be unique within each object. Alist and plist
keys must be symbols; if a key is duplicate, the first instance is
used.
The Lisp equivalents to the JSON null and false values are
configurable in the arguments ARGS, a list of keyword/argument pairs:
The keyword argument `:null-object' specifies which object to use
to represent a JSON null value. It defaults to `:null'.
The keyword argument `:false-object' specifies which object to use to
represent a JSON false value. It defaults to `:false'.
In you specify the same value for `:null-object' and `:false-object',
a potentially ambiguous situation, the JSON output will not contain
any JSON false values.
usage: (json-serialize OBJECT &rest ARGS) */)
(ptrdiff_t nargs, Lisp_Object *args)
{
specpdl_ref count = SPECPDL_INDEX ();
#ifdef WINDOWSNT
ensure_json_available ();
#endif
struct json_configuration conf =
{json_object_hashtable, json_array_array, QCnull, QCfalse};
json_parse_args (nargs - 1, args + 1, &conf, false);
json_t *json = lisp_to_json (args[0], &conf);
record_unwind_protect_ptr (json_release_object, json);
char *string = json_dumps (json, JSON_COMPACT | JSON_ENCODE_ANY);
if (string == NULL)
json_out_of_memory ();
record_unwind_protect_ptr (json_free, string);
return unbind_to (count, build_string_from_utf8 (string));
}
struct json_buffer_and_size
{
const char *buffer;
ptrdiff_t size;
/* This tracks how many bytes were inserted by the callback since
json_dump_callback was called. */
ptrdiff_t inserted_bytes;
};
static Lisp_Object
json_insert (void *data)
{
struct json_buffer_and_size *buffer_and_size = data;
ptrdiff_t len = buffer_and_size->size;
ptrdiff_t inserted_bytes = buffer_and_size->inserted_bytes;
ptrdiff_t gap_size = GAP_SIZE - inserted_bytes;
/* Enlarge the gap if necessary. */
if (gap_size < len)
make_gap (len - gap_size);
/* Copy this chunk of data into the gap. */
memcpy ((char *) BEG_ADDR + PT_BYTE - BEG_BYTE + inserted_bytes,
buffer_and_size->buffer, len);
buffer_and_size->inserted_bytes += len;
return Qnil;
}
static Lisp_Object
json_handle_nonlocal_exit (enum nonlocal_exit type, Lisp_Object data)
{
switch (type)
{
case NONLOCAL_EXIT_SIGNAL:
return data;
case NONLOCAL_EXIT_THROW:
return Fcons (Qno_catch, data);
default:
eassume (false);
}
}
struct json_insert_data
{
/* This tracks how many bytes were inserted by the callback since
json_dump_callback was called. */
ptrdiff_t inserted_bytes;
/* nil if json_insert succeeded, otherwise the symbol
Qcatch_all_memory_full or a cons (ERROR-SYMBOL . ERROR-DATA). */
Lisp_Object error;
};
/* Callback for json_dump_callback that inserts a JSON representation
as a unibyte string into the gap. DATA must point to a structure
of type json_insert_data. This function may not exit nonlocally.
It catches all nonlocal exits and stores them in data->error for
reraising. */
static int
json_insert_callback (const char *buffer, size_t size, void *data)
{
struct json_insert_data *d = data;
struct json_buffer_and_size buffer_and_size
= {.buffer = buffer, .size = size, .inserted_bytes = d->inserted_bytes};
d->error = internal_catch_all (json_insert, &buffer_and_size,
json_handle_nonlocal_exit);
d->inserted_bytes = buffer_and_size.inserted_bytes;
return NILP (d->error) ? 0 : -1;
}
DEFUN ("json-insert", Fjson_insert, Sjson_insert, 1, MANY,
NULL,
doc: /* Insert the JSON representation of OBJECT before point.
This is the same as (insert (json-serialize OBJECT)), but potentially
faster. See the function `json-serialize' for allowed values of
OBJECT.
usage: (json-insert OBJECT &rest ARGS) */)
(ptrdiff_t nargs, Lisp_Object *args)
{
specpdl_ref count = SPECPDL_INDEX ();
#ifdef WINDOWSNT
ensure_json_available ();
#endif
struct json_configuration conf =
{json_object_hashtable, json_array_array, QCnull, QCfalse};
json_parse_args (nargs - 1, args + 1, &conf, false);
json_t *json = lisp_to_json (args[0], &conf);
record_unwind_protect_ptr (json_release_object, json);
prepare_to_modify_buffer (PT, PT, NULL);
move_gap_both (PT, PT_BYTE);
struct json_insert_data data;
data.inserted_bytes = 0;
/* Could have used json_dumpb, but that became available only in
Jansson 2.10, whereas we want to support 2.7 and upward. */
int status = json_dump_callback (json, json_insert_callback, &data,
JSON_COMPACT | JSON_ENCODE_ANY);
if (status == -1)
{
if (CONSP (data.error))
xsignal (XCAR (data.error), XCDR (data.error));
else
json_out_of_memory ();
}
ptrdiff_t inserted = 0;
ptrdiff_t inserted_bytes = data.inserted_bytes;
if (inserted_bytes > 0)
{
/* If required, decode the stuff we've read into the gap. */
struct coding_system coding;
/* JSON strings are UTF-8 encoded strings. If for some reason
the text returned by the Jansson library includes invalid
byte sequences, they will be represented by raw bytes in the
buffer text. */
setup_coding_system (Qutf_8_unix, &coding);
coding.dst_multibyte =
!NILP (BVAR (current_buffer, enable_multibyte_characters));
if (CODING_MAY_REQUIRE_DECODING (&coding))
{
/* Now we have all the new bytes at the beginning of the gap,
but `decode_coding_gap` needs them at the end of the gap, so
we need to move them. */
memmove (GAP_END_ADDR - inserted_bytes, GPT_ADDR, inserted_bytes);
decode_coding_gap (&coding, inserted_bytes);
inserted = coding.produced_char;
}
else
{
/* Make the inserted text part of the buffer, as unibyte text. */
eassert (NILP (BVAR (current_buffer, enable_multibyte_characters)));
insert_from_gap_1 (inserted_bytes, inserted_bytes, false);
/* The target buffer is unibyte, so we don't need to decode. */
invalidate_buffer_caches (current_buffer,
PT, PT + inserted_bytes);
adjust_after_insert (PT, PT_BYTE,
PT + inserted_bytes,
PT_BYTE + inserted_bytes,
inserted_bytes);
inserted = inserted_bytes;
}
}
/* Call after-change hooks. */
signal_after_change (PT, 0, inserted);
if (inserted > 0)
{
update_compositions (PT, PT, CHECK_BORDER);
/* Move point to after the inserted text. */
SET_PT_BOTH (PT + inserted, PT_BYTE + inserted_bytes);
}
return unbind_to (count, Qnil);
}
#define JSON_PARSER_INTERNAL_OBJECT_WORKSPACE_SIZE 64
#define JSON_PARSER_INTERNAL_BYTE_WORKSPACE_SIZE 512
struct json_parser
{
/* Because of a possible gap in the input (an emacs buffer can have
a gap), the input is described by [input_begin;input_end) and
[secondary_input_begin;secondary_input_end). If the input is
continuous, then secondary_input_begin and secondary_input_end
should be NULL */
const unsigned char *input_current;
const unsigned char *input_begin;
const unsigned char *input_end;
const unsigned char *secondary_input_begin;
const unsigned char *secondary_input_end;
ptrdiff_t current_line;
ptrdiff_t current_column;
ptrdiff_t point_of_current_line;
/* The parser has a maximum allowed depth. available_depth
decreases at each object/array begin. If reaches zero, then an
error is generated */
int available_depth;
struct json_configuration conf;
size_t additional_bytes_count;
/* Lisp_Objects are collected in this area during object/array
parsing. To avoid allocations, initially
internal_object_workspace is used. If it runs out of space then
we switch to allocated space. Important note: with this design,
GC must not run during JSON parsing, otherwise Lisp_Objects in
the workspace may get incorrectly collected. */
Lisp_Object internal_object_workspace
[JSON_PARSER_INTERNAL_OBJECT_WORKSPACE_SIZE];
Lisp_Object *object_workspace;
size_t object_workspace_size;
size_t object_workspace_current;
/* String and number parsing uses this workspace. The idea behind
internal_byte_workspace is the same as the idea behind
internal_object_workspace */
unsigned char
internal_byte_workspace[JSON_PARSER_INTERNAL_BYTE_WORKSPACE_SIZE];
unsigned char *byte_workspace;
unsigned char *byte_workspace_end;
unsigned char *byte_workspace_current;
};
static AVOID
json_signal_error (struct json_parser *parser, Lisp_Object error)
{
xsignal3 (error, INT_TO_INTEGER (parser->current_line),
INT_TO_INTEGER (parser->current_column),
INT_TO_INTEGER (parser->point_of_current_line
+ parser->current_column));
}
static void
json_parser_init (struct json_parser *parser,
struct json_configuration conf,
const unsigned char *input,
const unsigned char *input_end,
const unsigned char *secondary_input,
const unsigned char *secondary_input_end)
{
if (secondary_input >= secondary_input_end)
{
secondary_input = NULL;
secondary_input_end = NULL;
}
if (input < input_end)
{
parser->input_begin = input;
parser->input_end = input_end;
parser->secondary_input_begin = secondary_input;
parser->secondary_input_end = secondary_input_end;
}
else
{
parser->input_begin = secondary_input;
parser->input_end = secondary_input_end;
parser->secondary_input_begin = NULL;
parser->secondary_input_end = NULL;
}
parser->input_current = parser->input_begin;
parser->current_line = 1;
parser->current_column = 0;
parser->point_of_current_line = 0;
parser->available_depth = 10000;
parser->conf = conf;
parser->additional_bytes_count = 0;
parser->object_workspace = parser->internal_object_workspace;
parser->object_workspace_size
= JSON_PARSER_INTERNAL_OBJECT_WORKSPACE_SIZE;
parser->object_workspace_current = 0;
parser->byte_workspace = parser->internal_byte_workspace;
parser->byte_workspace_end
= (parser->byte_workspace
+ JSON_PARSER_INTERNAL_BYTE_WORKSPACE_SIZE);
}
static void
json_parser_done (void *parser)
{
struct json_parser *p = (struct json_parser *) parser;
if (p->object_workspace != p->internal_object_workspace)
xfree (p->object_workspace);
if (p->byte_workspace != p->internal_byte_workspace)
xfree (p->byte_workspace);
}
/* Makes sure that the object_workspace has 'size' available space for
Lisp_Objects */
NO_INLINE static void
json_make_object_workspace_for_slow_path (struct json_parser *parser,
size_t size)
{
size_t needed_workspace_size
= (parser->object_workspace_current + size);
size_t new_workspace_size = parser->object_workspace_size;
while (new_workspace_size < needed_workspace_size)
{
if (ckd_mul (&new_workspace_size, new_workspace_size, 2))
{
json_signal_error (parser, Qjson_out_of_memory);
}
}
Lisp_Object *new_workspace_ptr;
if (parser->object_workspace_size
== JSON_PARSER_INTERNAL_OBJECT_WORKSPACE_SIZE)
{
new_workspace_ptr
= xnmalloc (new_workspace_size, sizeof (Lisp_Object));
memcpy (new_workspace_ptr, parser->object_workspace,
(sizeof (Lisp_Object)
* parser->object_workspace_current));
}
else
{
new_workspace_ptr
= xnrealloc (parser->object_workspace, new_workspace_size,
sizeof (Lisp_Object));
}
parser->object_workspace = new_workspace_ptr;
parser->object_workspace_size = new_workspace_size;
}
INLINE void
json_make_object_workspace_for (struct json_parser *parser,
size_t size)
{
if (parser->object_workspace_size - parser->object_workspace_current
< size)
{
json_make_object_workspace_for_slow_path (parser, size);
}
}
static void
json_byte_workspace_reset (struct json_parser *parser)
{
parser->byte_workspace_current = parser->byte_workspace;
}
/* Puts 'value' into the byte_workspace. If there is no space
available, it allocates space */
NO_INLINE static void
json_byte_workspace_put_slow_path (struct json_parser *parser,
unsigned char value)
{
size_t new_workspace_size
= parser->byte_workspace_end - parser->byte_workspace;
if (ckd_mul (&new_workspace_size, new_workspace_size, 2))
{
json_signal_error (parser, Qjson_out_of_memory);
}
size_t offset
= parser->byte_workspace_current - parser->byte_workspace;
if (parser->byte_workspace == parser->internal_byte_workspace)
{
parser->byte_workspace = xmalloc (new_workspace_size);
memcpy (parser->byte_workspace, parser->internal_byte_workspace,
offset);
}
else
{
parser->byte_workspace
= xrealloc (parser->byte_workspace, new_workspace_size);
}
parser->byte_workspace_end
= parser->byte_workspace + new_workspace_size;
parser->byte_workspace_current = parser->byte_workspace + offset;
*parser->byte_workspace_current++ = value;
}
INLINE void
json_byte_workspace_put (struct json_parser *parser,
unsigned char value)
{
if (parser->byte_workspace_current < parser->byte_workspace_end)
{
*parser->byte_workspace_current++ = value;
}
else
{
json_byte_workspace_put_slow_path (parser, value);
}
}
static bool
json_input_at_eof (struct json_parser *parser)
{
if (parser->input_current < parser->input_end)
return false;
return parser->secondary_input_end == NULL;
}
/* If there is a secondary buffer, this switches to it */
static int
json_input_switch_to_secondary (struct json_parser *parser)
{
if (parser->secondary_input_begin < parser->secondary_input_end)
{
parser->additional_bytes_count
= parser->input_end - parser->input_begin;
parser->input_begin = parser->secondary_input_begin;
parser->input_end = parser->secondary_input_end;
parser->input_current = parser->secondary_input_begin;
parser->secondary_input_begin = NULL;
parser->secondary_input_end = NULL;
return 0;
}
else
return -1;
}
/* Reads a byte from the JSON input stream */
NO_INLINE static unsigned char
json_input_get_slow_path (struct json_parser *parser)
{
if (json_input_switch_to_secondary (parser) < 0)
json_signal_error (parser, Qjson_end_of_file);
return *parser->input_current++;
}
static unsigned char
json_input_get (struct json_parser *parser)
{
if (parser->input_current < parser->input_end)
return *parser->input_current++;
return json_input_get_slow_path (parser);
}
/* Reads a byte from the JSON input stream, if the stream is not at
* eof. At eof, returns -1 */
static int
json_input_get_if_possible (struct json_parser *parser)
{
if (parser->input_current >= parser->input_end
&& json_input_switch_to_secondary (parser) < 0)
return -1;
return *parser->input_current++;
}
/* Puts back the last read input byte. Only one byte can be put back,
because otherwise this code would need to handle switching from
the secondary buffer to the initial */
static void
json_input_put_back (struct json_parser *parser)
{
parser->input_current--;
}
static bool
json_skip_whitespace_internal (struct json_parser *parser, int c)
{
parser->current_column++;
if (c == 0x20 || c == 0x09 || c == 0x0d)
return false;
else if (c == 0x0a)
{
parser->current_line++;
parser->point_of_current_line += parser->current_column;
parser->current_column = 0;
return false;
}
else
return true;
}
/* Skips JSON whitespace, and returns with the first non-whitespace
* character */
static int
json_skip_whitespace (struct json_parser *parser)
{
for (;;)
{
int c = json_input_get (parser);
if (json_skip_whitespace_internal (parser, c))
return c;
}
}
/* Skips JSON whitespace, and returns with the first non-whitespace
* character, if possible. If there is no non-whitespace character
* (because we reached the end), it returns -1 */
static int
json_skip_whitespace_if_possible (struct json_parser *parser)
{
for (;;)
{
int c = json_input_get_if_possible (parser);
if (c < 0)
return c;
if (json_skip_whitespace_internal (parser, c))
return c;
}
}
static int
json_hex_value (int c)
{
if (c >= '0' && c <= '9')
return c - '0';
else if (c >= 'A' && c <= 'F')
return c - 'A' + 10;
else if (c >= 'a' && c <= 'f')
return c - 'a' + 10;
else
return -1;
}
/* Parses the CCCC part of the unicode escape sequence \uCCCC */
static int
json_parse_unicode (struct json_parser *parser)
{
unsigned char v[4];
for (int i = 0; i < 4; i++)
{
int c = json_hex_value (json_input_get (parser));
parser->current_column++;
if (c < 0)
json_signal_error (parser, Qjson_escape_sequence_error);
v[i] = c;
}
return v[0] << 12 | v[1] << 8 | v[2] << 4 | v[3];
}
/* Parses an utf-8 code-point encoding (except the first byte), and
returns the numeric value of the code-point (without considering
the first byte) */
static int
json_handle_utf8_tail_bytes (struct json_parser *parser, int n)
{
int v = 0;
for (int i = 0; i < n; i++)
{
int c = json_input_get (parser);
json_byte_workspace_put (parser, c);
if ((c & 0xc0) != 0x80)
json_signal_error (parser, Qjson_utf8_decode_error);
v = (v << 6) | (c & 0x3f);
}
return v;
}
/* Reads a JSON string, and puts the result into the byte workspace */
static void
json_parse_string (struct json_parser *parser)
{
/* a single_uninteresting byte can be simply copied from the input
to output, it doesn't need any extra care. This means all the
characters between [0x20;0x7f], except the double quote and
the backslash */
static const char is_single_uninteresting[256] = {
/* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
/* 0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 1 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 2 */ 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
/* 3 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
/* 4 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
/* 5 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1,
/* 6 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
/* 7 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
/* 8 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 9 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* a */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* b */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* c */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* d */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* e */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* f */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};
for (;;)
{
/* This if is only here for a possible speedup. If there are 4
bytes available, and all of them are single_uninteresting,
then we can just copy these 4 bytes to output */
if (parser->input_end - parser->input_current >= 4)
{
int c0 = parser->input_current[0];
int c1 = parser->input_current[1];
int c2 = parser->input_current[2];
int c3 = parser->input_current[3];
bool v0 = is_single_uninteresting[c0];
bool v1 = is_single_uninteresting[c1];
bool v2 = is_single_uninteresting[c2];
bool v3 = is_single_uninteresting[c3];
if (v0 && v1 && v2 && v3)
{
json_byte_workspace_put (parser, c0);
json_byte_workspace_put (parser, c1);
json_byte_workspace_put (parser, c2);
json_byte_workspace_put (parser, c3);
parser->input_current += 4;
parser->current_column += 4;
continue;
}
}
int c = json_input_get (parser);
parser->current_column++;
if (is_single_uninteresting[c])
{
json_byte_workspace_put (parser, c);
continue;
}
if (c == '"')
return;
else if (c & 0x80)
{
/* Handle utf-8 encoding */
json_byte_workspace_put (parser, c);
if (c < 0xc0)
json_signal_error (parser, Qjson_utf8_decode_error);
else if (c < 0xe0)
{
int n = ((c & 0x1f) << 6
| json_handle_utf8_tail_bytes (parser, 1));
if (n < 0x80)
json_signal_error (parser, Qjson_utf8_decode_error);
}
else if (c < 0xf0)
{
int n = ((c & 0xf) << 12
| json_handle_utf8_tail_bytes (parser, 2));
if (n < 0x800 || (n >= 0xd800 && n < 0xe000))
json_signal_error (parser, Qjson_utf8_decode_error);
}
else if (c < 0xf8)
{
int n = ((c & 0x7) << 18
| json_handle_utf8_tail_bytes (parser, 3));
if (n < 0x10000 || n > 0x10ffff)
json_signal_error (parser, Qjson_utf8_decode_error);
}
else
json_signal_error (parser, Qjson_utf8_decode_error);
}
else if (c == '\\')
{
/* Handle escape sequences */
c = json_input_get (parser);
parser->current_column++;
if (c == '"')
json_byte_workspace_put (parser, '"');
else if (c == '\\')
json_byte_workspace_put (parser, '\\');
else if (c == '/')
json_byte_workspace_put (parser, '/');
else if (c == 'b')
json_byte_workspace_put (parser, '\b');
else if (c == 'f')
json_byte_workspace_put (parser, '\f');
else if (c == 'n')
json_byte_workspace_put (parser, '\n');
else if (c == 'r')
json_byte_workspace_put (parser, '\r');
else if (c == 't')
json_byte_workspace_put (parser, '\t');
else if (c == 'u')
{
int num = json_parse_unicode (parser);
/* is the first half of the surrogate pair */
if (num >= 0xd800 && num < 0xdc00)
{
parser->current_column++;
if (json_input_get (parser) != '\\')
json_signal_error (parser,
Qjson_invalid_surrogate_error);
parser->current_column++;
if (json_input_get (parser) != 'u')
json_signal_error (parser,
Qjson_invalid_surrogate_error);
int num2 = json_parse_unicode (parser);
if (num2 < 0xdc00 || num2 >= 0xe000)
json_signal_error (parser,
Qjson_invalid_surrogate_error);
num = (0x10000
+ ((num - 0xd800) << 10 | (num2 - 0xdc00)));
}
else if (num >= 0xdc00 && num < 0xe000)
/* is the second half of the surrogate pair without
the first half */
json_signal_error (parser,
Qjson_invalid_surrogate_error);
/* utf-8 encode the code-point */
if (num < 0x80)
json_byte_workspace_put (parser, num);
else if (num < 0x800)
{
json_byte_workspace_put (parser, 0xc0 | num >> 6);
json_byte_workspace_put (parser,
0x80 | (num & 0x3f));
}
else if (num < 0x10000)
{
json_byte_workspace_put (parser, 0xe0 | num >> 12);
json_byte_workspace_put (parser,
(0x80
| ((num >> 6) & 0x3f)));
json_byte_workspace_put (parser,
0x80 | (num & 0x3f));
}
else
{
json_byte_workspace_put (parser, 0xf0 | num >> 18);
json_byte_workspace_put (parser,
(0x80
| ((num >> 12) & 0x3f)));
json_byte_workspace_put (parser,
(0x80
| ((num >> 6) & 0x3f)));
json_byte_workspace_put (parser,
0x80 | (num & 0x3f));
}
}
else
json_signal_error (parser, Qjson_escape_sequence_error);
}
else
json_signal_error (parser, Qjson_parse_error);
}
}
/* If there was no integer overflow during parsing the integer, this
puts 'value' to the output. Otherwise this calls string_to_number
to parse integer on the byte workspace. This could just always
call string_to_number, but for performance reasons, during parsing
the code tries to calculate the value, so in most cases, we can
save call of string_to_number */
static Lisp_Object
json_create_integer (struct json_parser *parser,
bool integer_overflow, bool negative,
EMACS_UINT value)
{
if (!integer_overflow)
{
if (negative)
{
uintmax_t v = value;
if (v <= (uintmax_t) INTMAX_MAX + 1)
return INT_TO_INTEGER ((intmax_t) -v);
}
else
return INT_TO_INTEGER (value);
}
json_byte_workspace_put (parser, 0);
ptrdiff_t len;
Lisp_Object result
= string_to_number ((const char *) parser->byte_workspace, 10,
&len);
if (len
!= parser->byte_workspace_current - parser->byte_workspace - 1)
json_signal_error (parser, Qjson_error);
return result;
}
/* Parses a float using the byte workspace */
static Lisp_Object
json_create_float (struct json_parser *parser)
{
json_byte_workspace_put (parser, 0);
errno = 0;
char *e;
double value = strtod ((const char *) parser->byte_workspace, &e);
bool out_of_range
= (errno != 0 && (value == HUGE_VAL || value == -HUGE_VAL));
if (out_of_range)
json_signal_error (parser, Qjson_number_out_of_range);
else if ((const unsigned char *) e
!= parser->byte_workspace_current - 1)
json_signal_error (parser, Qjson_error);
else
return make_float (value);
}
/* Parses a number. The first character is the input parameter 'c'.
*/
static Lisp_Object
json_parse_number (struct json_parser *parser, int c)
{
json_byte_workspace_reset (parser);
json_byte_workspace_put (parser, c);
bool negative = false;
if (c == '-')
{
negative = true;
c = json_input_get (parser);
json_byte_workspace_put (parser, c);
parser->current_column++;
}
if (c < '0' || c > '9')
json_signal_error (parser, Qjson_parse_error);
/* The idea is that during finding the last character of the
number, the for loop below also tries to calculate the value. If
the parsed number is an integer which fits into unsigned long,
then the parser can use the value of 'integer' right away,
instead of having to re-parse the byte workspace later.
Ideally, this integer should have the same size as a CPU general
purpose register. */
EMACS_UINT integer = c - '0';
bool integer_overflow = false;
if (integer == 0)
{
if (json_input_at_eof (parser))
return INT_TO_INTEGER (0);
c = json_input_get (parser);
}
else
{
for (;;)
{
if (json_input_at_eof (parser))
return json_create_integer (parser, integer_overflow,
negative, integer);
c = json_input_get (parser);
if (c < '0' || c > '9')
break;
json_byte_workspace_put (parser, c);
parser->current_column++;
integer_overflow |= ckd_mul (&integer, integer, 10);
integer_overflow |= ckd_add (&integer, integer, c - '0');
}
}
bool is_float = false;
if (c == '.')
{
json_byte_workspace_put (parser, c);
parser->current_column++;
is_float = true;
c = json_input_get (parser);
json_byte_workspace_put (parser, c);
parser->current_column++;
if (c < '0' || c > '9')
json_signal_error (parser, Qjson_parse_error);
for (;;)
{
if (json_input_at_eof (parser))
return json_create_float (parser);
c = json_input_get (parser);
if (c < '0' || c > '9')
break;
json_byte_workspace_put (parser, c);
parser->current_column++;
}
}
if (c == 'e' || c == 'E')
{
json_byte_workspace_put (parser, c);
parser->current_column++;
is_float = true;
c = json_input_get (parser);
json_byte_workspace_put (parser, c);
parser->current_column++;
if (c == '-' || c == '+')
{
c = json_input_get (parser);
json_byte_workspace_put (parser, c);
parser->current_column++;
}
if (c < '0' || c > '9')
json_signal_error (parser, Qjson_parse_error);
for (;;)
{
if (json_input_at_eof (parser))
return json_create_float (parser);
c = json_input_get (parser);
if (c < '0' || c > '9')
break;
json_byte_workspace_put (parser, c);
parser->current_column++;
}
}
/* 'c' contains a character which is not part of the number,
so it is need to be put back */
json_input_put_back (parser);
if (is_float)
return json_create_float (parser);
else
return json_create_integer (parser, integer_overflow, negative,
integer);
}
static Lisp_Object json_parse_value (struct json_parser *parser,
int c);
/* Parses a JSON array. */
static Lisp_Object
json_parse_array (struct json_parser *parser)
{
int c = json_skip_whitespace (parser);
const size_t first = parser->object_workspace_current;
Lisp_Object result = Qnil;
if (c != ']')
{
parser->available_depth--;
if (parser->available_depth < 0)
json_signal_error (parser, Qjson_object_too_deep);
size_t number_of_elements = 0;
Lisp_Object *cdr = &result;
/* This loop collects the array elements in the object workspace
*/
for (;;)
{
Lisp_Object element = json_parse_value (parser, c);
switch (parser->conf.array_type)
{
case json_array_array:
json_make_object_workspace_for (parser, 1);
parser->object_workspace[parser->object_workspace_current]
= element;
parser->object_workspace_current++;
break;
case json_array_list:
{
Lisp_Object nc = Fcons (element, Qnil);
*cdr = nc;
cdr = xcdr_addr (nc);
break;
}
default:
emacs_abort ();
}
c = json_skip_whitespace (parser);
number_of_elements++;
if (c == ']')
{
parser->available_depth++;
break;
}
if (c != ',')
json_signal_error (parser, Qjson_parse_error);
c = json_skip_whitespace (parser);
}
}
switch (parser->conf.array_type)
{
case json_array_array:
{
size_t number_of_elements
= parser->object_workspace_current - first;
result = make_vector (number_of_elements, Qnil);
for (size_t i = 0; i < number_of_elements; i++)
{
rarely_quit (i);
ASET (result, i, parser->object_workspace[first + i]);
}
parser->object_workspace_current = first;
break;
}
case json_array_list:
break;
default:
emacs_abort ();
}
return result;
}
/* Parses the ": value" part of a JSON object member. */
static Lisp_Object
json_parse_object_member_value (struct json_parser *parser)
{
int c = json_skip_whitespace (parser);
if (c != ':')
json_signal_error (parser, Qjson_parse_error);
c = json_skip_whitespace (parser);
return json_parse_value (parser, c);
}
/* Parses a JSON object. */
static Lisp_Object
json_parse_object (struct json_parser *parser)
{
int c = json_skip_whitespace (parser);
const size_t first = parser->object_workspace_current;
Lisp_Object result = Qnil;
if (c != '}')
{
parser->available_depth--;
if (parser->available_depth < 0)
json_signal_error (parser, Qjson_object_too_deep);
Lisp_Object *cdr = &result;
/* This loop collects the object members (key/value pairs) in
* the object workspace */
for (;;)
{
if (c != '"')
json_signal_error (parser, Qjson_parse_error);
json_byte_workspace_reset (parser);
switch (parser->conf.object_type)
{
case json_object_hashtable:
{
json_parse_string (parser);
Lisp_Object key
= make_string_from_utf8 ((char *)
parser->byte_workspace,
(parser->byte_workspace_current
- parser->byte_workspace));
Lisp_Object value
= json_parse_object_member_value (parser);
json_make_object_workspace_for (parser, 2);
parser->object_workspace[parser->object_workspace_current]
= key;
parser->object_workspace_current++;
parser->object_workspace[parser->object_workspace_current]
= value;
parser->object_workspace_current++;
break;
}
case json_object_alist:
{
json_parse_string (parser);
Lisp_Object key
= Fintern (make_string_from_utf8 (
(char *) parser->byte_workspace,
(parser->byte_workspace_current
- parser->byte_workspace)),
Qnil);
Lisp_Object value
= json_parse_object_member_value (parser);
Lisp_Object nc = Fcons (Fcons (key, value), Qnil);
*cdr = nc;
cdr = xcdr_addr (nc);
break;
}
case json_object_plist:
{
json_byte_workspace_put (parser, ':');
json_parse_string (parser);
Lisp_Object key
= intern_1 ((char *) parser->byte_workspace,
(parser->byte_workspace_current
- parser->byte_workspace));
Lisp_Object value
= json_parse_object_member_value (parser);
Lisp_Object nc = Fcons (key, Qnil);
*cdr = nc;
cdr = xcdr_addr (nc);
nc = Fcons (value, Qnil);
*cdr = nc;
cdr = xcdr_addr (nc);
break;
}
default:
emacs_abort ();
}
c = json_skip_whitespace (parser);
if (c == '}')
{
parser->available_depth++;
break;
}
if (c != ',')
json_signal_error (parser, Qjson_parse_error);
c = json_skip_whitespace (parser);
}
}
switch (parser->conf.object_type)
{
case json_object_hashtable:
{
result
= CALLN (Fmake_hash_table, QCtest, Qequal, QCsize,
make_fixed_natnum (
(parser->object_workspace_current - first) / 2));
struct Lisp_Hash_Table *h = XHASH_TABLE (result);
for (size_t i = first; i < parser->object_workspace_current;
i += 2)
{
hash_hash_t hash;
Lisp_Object key = parser->object_workspace[i];
Lisp_Object value = parser->object_workspace[i + 1];
ptrdiff_t i = hash_lookup_get_hash (h, key, &hash);
if (i < 0)
hash_put (h, key, value, hash);
else
set_hash_value_slot (h, i, value);
}
parser->object_workspace_current = first;
break;
}
case json_object_alist:
case json_object_plist:
break;
default:
emacs_abort ();
}
return result;
}
/* Token-char is not a JSON terminology. When parsing
null/false/true, this function tells the character set that is need
to be considered as part of a token. For example, if the input is
"truesomething", then the parser shouldn't consider it as "true",
and an additional later "something" token. An additional example:
if the input is "truetrue", then calling (json-parse-buffer) twice
shouldn't produce two successful calls which return t, but a
parsing error */
static bool
json_is_token_char (int c)
{
return ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')
|| (c >= '0' && c <= '9') || (c == '-'));
}
/* This is the entry point to the value parser, this parses a JSON
* value */
Lisp_Object
json_parse_value (struct json_parser *parser, int c)
{
if (c == '{')
return json_parse_object (parser);
else if (c == '[')
return json_parse_array (parser);
else if (c == '"')
{
json_byte_workspace_reset (parser);
json_parse_string (parser);
Lisp_Object result
= make_string_from_utf8 ((const char *)
parser->byte_workspace,
(parser->byte_workspace_current
- parser->byte_workspace));
return result;
}
else if ((c >= '0' && c <= '9') || (c == '-'))
return json_parse_number (parser, c);
else
{
int c2 = json_input_get (parser);
int c3 = json_input_get (parser);
int c4 = json_input_get (parser);
int c5 = json_input_get_if_possible (parser);
if (c == 't' && c2 == 'r' && c3 == 'u' && c4 == 'e'
&& (c5 < 0 || !json_is_token_char (c5)))
{
if (c5 >= 0)
json_input_put_back (parser);
parser->current_column += 3;
return Qt;
}
if (c == 'n' && c2 == 'u' && c3 == 'l' && c4 == 'l'
&& (c5 < 0 || !json_is_token_char (c5)))
{
if (c5 >= 0)
json_input_put_back (parser);
parser->current_column += 3;
return parser->conf.null_object;
}
if (c == 'f' && c2 == 'a' && c3 == 'l' && c4 == 's'
&& c5 == 'e')
{
int c6 = json_input_get_if_possible (parser);
if (c6 < 0 || !json_is_token_char (c6))
{
if (c6 >= 0)
json_input_put_back (parser);
parser->current_column += 4;
return parser->conf.false_object;
}
}
json_signal_error (parser, Qjson_parse_error);
}
}
enum ParseEndBehavior
{
PARSEENDBEHAVIOR_CheckForGarbage,
PARSEENDBEHAVIOR_MovePoint
};
static Lisp_Object
json_parse (struct json_parser *parser,
enum ParseEndBehavior parse_end_behavior)
{
int c = json_skip_whitespace (parser);
Lisp_Object result = json_parse_value (parser, c);
switch (parse_end_behavior)
{
case PARSEENDBEHAVIOR_CheckForGarbage:
c = json_skip_whitespace_if_possible (parser);
if (c >= 0)
json_signal_error (parser, Qjson_trailing_content);
break;
case PARSEENDBEHAVIOR_MovePoint:
{
ptrdiff_t byte
= (PT_BYTE + parser->input_current - parser->input_begin
+ parser->additional_bytes_count);
ptrdiff_t position;
if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
position = byte;
else
position
= PT + parser->point_of_current_line + parser->current_column;
SET_PT_BOTH (position, byte);
break;
}
}
return result;
}
DEFUN ("json-parse-string", Fjson_parse_string, Sjson_parse_string, 1, MANY,
NULL,
doc: /* Parse the JSON STRING into a Lisp object.
This is essentially the reverse operation of `json-serialize', which
see. The returned object will be the JSON null value, the JSON false
value, t, a number, a string, a vector, a list, a hashtable, an alist,
or a plist. Its elements will be further objects of these types. If
there are duplicate keys in an object, all but the last one are
ignored. If STRING doesn't contain a valid JSON object, this function
signals an error of type `json-parse-error'.
The arguments ARGS are a list of keyword/argument pairs:
The keyword argument `:object-type' specifies which Lisp type is used
to represent objects; it can be `hash-table', `alist' or `plist'. It
defaults to `hash-table'. If an object has members with the same
key, `hash-table' keeps only the last value of such keys, while
`alist' and `plist' keep all the members.
The keyword argument `:array-type' specifies which Lisp type is used
to represent arrays; it can be `array' (the default) or `list'.
The keyword argument `:null-object' specifies which object to use
to represent a JSON null value. It defaults to `:null'.
The keyword argument `:false-object' specifies which object to use to
represent a JSON false value. It defaults to `:false'.
usage: (json-parse-string STRING &rest ARGS) */)
(ptrdiff_t nargs, Lisp_Object *args)
{
specpdl_ref count = SPECPDL_INDEX ();
Lisp_Object string = args[0];
CHECK_STRING (string);
Lisp_Object encoded = json_encode (string);
struct json_configuration conf
= { json_object_hashtable, json_array_array, QCnull, QCfalse };
json_parse_args (nargs - 1, args + 1, &conf, true);
struct json_parser p;
const unsigned char *begin
= (const unsigned char *) SSDATA (encoded);
json_parser_init (&p, conf, begin, begin + SBYTES (encoded), NULL,
NULL);
record_unwind_protect_ptr (json_parser_done, &p);
return unbind_to (count,
json_parse (&p,
PARSEENDBEHAVIOR_CheckForGarbage));
}
DEFUN ("json-parse-buffer", Fjson_parse_buffer, Sjson_parse_buffer,
0, MANY, NULL,
doc: /* Read JSON object from current buffer starting at point.
Move point after the end of the object if parsing was successful.
On error, don't move point.
The returned object will be a vector, list, hashtable, alist, or
plist. Its elements will be the JSON null value, the JSON false
value, t, numbers, strings, or further vectors, lists, hashtables,
alists, or plists. If there are duplicate keys in an object, all
but the last one are ignored.
If the current buffer doesn't contain a valid JSON object, the
function signals an error of type `json-parse-error'.
The arguments ARGS are a list of keyword/argument pairs:
The keyword argument `:object-type' specifies which Lisp type is used
to represent objects; it can be `hash-table', `alist' or `plist'. It
defaults to `hash-table'. If an object has members with the same
key, `hash-table' keeps only the last value of such keys, while
`alist' and `plist' keep all the members.
The keyword argument `:array-type' specifies which Lisp type is used
to represent arrays; it can be `array' (the default) or `list'.
The keyword argument `:null-object' specifies which object to use
to represent a JSON null value. It defaults to `:null'.
The keyword argument `:false-object' specifies which object to use to
represent a JSON false value. It defaults to `:false'.
usage: (json-parse-buffer &rest args) */)
(ptrdiff_t nargs, Lisp_Object *args)
{
specpdl_ref count = SPECPDL_INDEX ();
struct json_configuration conf
= { json_object_hashtable, json_array_array, QCnull, QCfalse };
json_parse_args (nargs, args, &conf, true);
struct json_parser p;
unsigned char *begin = PT_ADDR;
unsigned char *end = GPT_ADDR;
unsigned char *secondary_begin = NULL;
unsigned char *secondary_end = NULL;
if (GPT_ADDR < Z_ADDR)
{
secondary_begin = GAP_END_ADDR;
if (secondary_begin < PT_ADDR)
secondary_begin = PT_ADDR;
secondary_end = Z_ADDR;
}
json_parser_init (&p, conf, begin, end, secondary_begin,
secondary_end);
record_unwind_protect_ptr (json_parser_done, &p);
return unbind_to (count,
json_parse (&p, PARSEENDBEHAVIOR_MovePoint));
}
void
syms_of_json (void)
{
DEFSYM (QCnull, ":null");
DEFSYM (QCfalse, ":false");
DEFSYM (Qstring_without_embedded_nulls_p, "string-without-embedded-nulls-p");
DEFSYM (Qjson_value_p, "json-value-p");
DEFSYM (Qjson_error, "json-error");
DEFSYM (Qjson_out_of_memory, "json-out-of-memory");
DEFSYM (Qjson_parse_error, "json-parse-error");
DEFSYM (Qjson_end_of_file, "json-end-of-file");
DEFSYM (Qjson_trailing_content, "json-trailing-content");
DEFSYM (Qjson_object_too_deep, "json-object-too-deep");
DEFSYM (Qjson_utf8_decode_error, "json-utf8-decode-error")
DEFSYM (Qjson_invalid_surrogate_error, "json-invalid-surrogate-error")
DEFSYM (Qjson_number_out_of_range, "json-number-out-of-range-error")
DEFSYM (Qjson_escape_sequence_error, "json-escape-sequence-error")
DEFSYM (Qjson_unavailable, "json-unavailable");
define_error (Qjson_error, "generic JSON error", Qerror);
define_error (Qjson_out_of_memory,
"not enough memory for creating JSON object", Qjson_error);
define_error (Qjson_parse_error, "could not parse JSON stream",
Qjson_error);
define_error (Qjson_end_of_file, "end of JSON stream", Qjson_parse_error);
define_error (Qjson_trailing_content, "trailing content after JSON stream",
Qjson_parse_error);
define_error (Qjson_object_too_deep,
"object cyclic or Lisp evaluation too deep", Qjson_error);
define_error (Qjson_utf8_decode_error,
"invalid utf-8 encoding", Qjson_error);
define_error (Qjson_invalid_surrogate_error,
"invalid surrogate pair", Qjson_error);
define_error (Qjson_number_out_of_range,
"number out of range", Qjson_error);
define_error (Qjson_escape_sequence_error,
"invalid escape sequence", Qjson_parse_error);
DEFSYM (Qpure, "pure");
DEFSYM (Qside_effect_free, "side-effect-free");
DEFSYM (Qjson_serialize, "json-serialize");
DEFSYM (Qjson_parse_string, "json-parse-string");
Fput (Qjson_serialize, Qpure, Qt);
Fput (Qjson_serialize, Qside_effect_free, Qt);
Fput (Qjson_parse_string, Qpure, Qt);
Fput (Qjson_parse_string, Qside_effect_free, Qt);
DEFSYM (QCobject_type, ":object-type");
DEFSYM (QCarray_type, ":array-type");
DEFSYM (QCnull_object, ":null-object");
DEFSYM (QCfalse_object, ":false-object");
DEFSYM (Qalist, "alist");
DEFSYM (Qplist, "plist");
DEFSYM (Qarray, "array");
defsubr (&Sjson__available_p);
defsubr (&Sjson_serialize);
defsubr (&Sjson_insert);
defsubr (&Sjson_parse_string);
defsubr (&Sjson_parse_buffer);
}
|