This source file includes following definitions.
- json_delete
- init_json_functions
- json_malloc
- json_free
- init_json
- json_has_prefix
- json_has_suffix
- json_encode
- json_out_of_memory
- json_parse_error
- json_release_object
- check_string_without_embedded_nulls
- json_check
- json_check_utf8
- lisp_to_json_nonscalar_1
- lisp_to_json_nonscalar
- lisp_to_json
- json_parse_args
- json_available_p
- ensure_json_available
- DEFUN
- json_insert
- json_handle_nonlocal_exit
- json_insert_callback
- ARG_NONNULL
- json_read_buffer_callback
- syms_of_json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 #include <config.h>
21
22 #include <errno.h>
23 #include <stddef.h>
24 #include <stdint.h>
25 #include <stdlib.h>
26
27 #include <jansson.h>
28
29 #include "lisp.h"
30 #include "buffer.h"
31 #include "coding.h"
32
33 #define JSON_HAS_ERROR_CODE (JANSSON_VERSION_HEX >= 0x020B00)
34
35 #ifdef WINDOWSNT
36 # include <windows.h>
37 # include "w32common.h"
38 # include "w32.h"
39
40 DEF_DLL_FN (void, json_set_alloc_funcs,
41 (json_malloc_t malloc_fn, json_free_t free_fn));
42 DEF_DLL_FN (void, json_delete, (json_t *json));
43 DEF_DLL_FN (json_t *, json_array, (void));
44 DEF_DLL_FN (int, json_array_append_new, (json_t *array, json_t *value));
45 DEF_DLL_FN (size_t, json_array_size, (const json_t *array));
46 DEF_DLL_FN (json_t *, json_object, (void));
47 DEF_DLL_FN (int, json_object_set_new,
48 (json_t *object, const char *key, json_t *value));
49 DEF_DLL_FN (json_t *, json_null, (void));
50 DEF_DLL_FN (json_t *, json_true, (void));
51 DEF_DLL_FN (json_t *, json_false, (void));
52 DEF_DLL_FN (json_t *, json_integer, (json_int_t value));
53 DEF_DLL_FN (json_t *, json_real, (double value));
54 DEF_DLL_FN (json_t *, json_stringn, (const char *value, size_t len));
55 DEF_DLL_FN (char *, json_dumps, (const json_t *json, size_t flags));
56 DEF_DLL_FN (int, json_dump_callback,
57 (const json_t *json, json_dump_callback_t callback, void *data,
58 size_t flags));
59 DEF_DLL_FN (json_int_t, json_integer_value, (const json_t *integer));
60 DEF_DLL_FN (double, json_real_value, (const json_t *real));
61 DEF_DLL_FN (const char *, json_string_value, (const json_t *string));
62 DEF_DLL_FN (size_t, json_string_length, (const json_t *string));
63 DEF_DLL_FN (json_t *, json_array_get, (const json_t *array, size_t index));
64 DEF_DLL_FN (json_t *, json_object_get, (const json_t *object, const char *key));
65 DEF_DLL_FN (size_t, json_object_size, (const json_t *object));
66 DEF_DLL_FN (const char *, json_object_iter_key, (void *iter));
67 DEF_DLL_FN (void *, json_object_iter, (json_t *object));
68 DEF_DLL_FN (json_t *, json_object_iter_value, (void *iter));
69 DEF_DLL_FN (void *, json_object_key_to_iter, (const char *key));
70 DEF_DLL_FN (void *, json_object_iter_next, (json_t *object, void *iter));
71 DEF_DLL_FN (json_t *, json_loads,
72 (const char *input, size_t flags, json_error_t *error));
73 DEF_DLL_FN (json_t *, json_load_callback,
74 (json_load_callback_t callback, void *data, size_t flags,
75 json_error_t *error));
76
77
78 void json_delete(json_t *json)
79 {
80 fn_json_delete (json);
81 }
82
83 static bool json_initialized;
84
85 static bool
86 init_json_functions (void)
87 {
88 HMODULE library = w32_delayed_load (Qjson);
89
90 if (!library)
91 return false;
92
93 LOAD_DLL_FN (library, json_set_alloc_funcs);
94 LOAD_DLL_FN (library, json_delete);
95 LOAD_DLL_FN (library, json_array);
96 LOAD_DLL_FN (library, json_array_append_new);
97 LOAD_DLL_FN (library, json_array_size);
98 LOAD_DLL_FN (library, json_object);
99 LOAD_DLL_FN (library, json_object_set_new);
100 LOAD_DLL_FN (library, json_null);
101 LOAD_DLL_FN (library, json_true);
102 LOAD_DLL_FN (library, json_false);
103 LOAD_DLL_FN (library, json_integer);
104 LOAD_DLL_FN (library, json_real);
105 LOAD_DLL_FN (library, json_stringn);
106 LOAD_DLL_FN (library, json_dumps);
107 LOAD_DLL_FN (library, json_dump_callback);
108 LOAD_DLL_FN (library, json_integer_value);
109 LOAD_DLL_FN (library, json_real_value);
110 LOAD_DLL_FN (library, json_string_value);
111 LOAD_DLL_FN (library, json_string_length);
112 LOAD_DLL_FN (library, json_array_get);
113 LOAD_DLL_FN (library, json_object_get);
114 LOAD_DLL_FN (library, json_object_size);
115 LOAD_DLL_FN (library, json_object_iter_key);
116 LOAD_DLL_FN (library, json_object_iter);
117 LOAD_DLL_FN (library, json_object_iter_value);
118 LOAD_DLL_FN (library, json_object_key_to_iter);
119 LOAD_DLL_FN (library, json_object_iter_next);
120 LOAD_DLL_FN (library, json_loads);
121 LOAD_DLL_FN (library, json_load_callback);
122
123 init_json ();
124
125 return true;
126 }
127
128 #define json_set_alloc_funcs fn_json_set_alloc_funcs
129 #define json_array fn_json_array
130 #define json_array_append_new fn_json_array_append_new
131 #define json_array_size fn_json_array_size
132 #define json_object fn_json_object
133 #define json_object_set_new fn_json_object_set_new
134 #define json_null fn_json_null
135 #define json_true fn_json_true
136 #define json_false fn_json_false
137 #define json_integer fn_json_integer
138 #define json_real fn_json_real
139 #define json_stringn fn_json_stringn
140 #define json_dumps fn_json_dumps
141 #define json_dump_callback fn_json_dump_callback
142 #define json_integer_value fn_json_integer_value
143 #define json_real_value fn_json_real_value
144 #define json_string_value fn_json_string_value
145 #define json_string_length fn_json_string_length
146 #define json_array_get fn_json_array_get
147 #define json_object_get fn_json_object_get
148 #define json_object_size fn_json_object_size
149 #define json_object_iter_key fn_json_object_iter_key
150 #define json_object_iter fn_json_object_iter
151 #define json_object_iter_value fn_json_object_iter_value
152 #define json_object_key_to_iter fn_json_object_key_to_iter
153 #define json_object_iter_next fn_json_object_iter_next
154 #define json_loads fn_json_loads
155 #define json_load_callback fn_json_load_callback
156
157 #endif
158
159
160
161
162
163
164
165
166
167
168
169
170 static void *
171 json_malloc (size_t size)
172 {
173 if (size > PTRDIFF_MAX)
174 {
175 errno = ENOMEM;
176 return NULL;
177 }
178 return malloc (size);
179 }
180
181 static void
182 json_free (void *ptr)
183 {
184 free (ptr);
185 }
186
187 void
188 init_json (void)
189 {
190 json_set_alloc_funcs (json_malloc, json_free);
191 }
192
193 #if !JSON_HAS_ERROR_CODE
194
195
196
197 static bool
198 json_has_prefix (const char *string, const char *prefix)
199 {
200 return strncmp (string, prefix, strlen (prefix)) == 0;
201 }
202
203
204
205 static bool
206 json_has_suffix (const char *string, const char *suffix)
207 {
208 size_t string_len = strlen (string);
209 size_t suffix_len = strlen (suffix);
210 return string_len >= suffix_len
211 && memcmp (string + string_len - suffix_len, suffix, suffix_len) == 0;
212 }
213
214 #endif
215
216
217
218
219
220
221
222
223
224
225
226 static Lisp_Object
227 json_encode (Lisp_Object string)
228 {
229
230
231 return encode_string_utf_8 (string, Qnil, false, Qt, Qt);
232 }
233
234 static AVOID
235 json_out_of_memory (void)
236 {
237 xsignal0 (Qjson_out_of_memory);
238 }
239
240
241
242 static AVOID
243 json_parse_error (const json_error_t *error)
244 {
245 Lisp_Object symbol;
246 #if JSON_HAS_ERROR_CODE
247 switch (json_error_code (error))
248 {
249 case json_error_premature_end_of_input:
250 symbol = Qjson_end_of_file;
251 break;
252 case json_error_end_of_input_expected:
253 symbol = Qjson_trailing_content;
254 break;
255 default:
256 symbol = Qjson_parse_error;
257 break;
258 }
259 #else
260 if (json_has_suffix (error->text, "expected near end of file"))
261 symbol = Qjson_end_of_file;
262 else if (json_has_prefix (error->text, "end of file expected"))
263 symbol = Qjson_trailing_content;
264 else
265 symbol = Qjson_parse_error;
266 #endif
267 xsignal (symbol,
268 list5 (build_string_from_utf8 (error->text),
269 build_string_from_utf8 (error->source),
270 INT_TO_INTEGER (error->line),
271 INT_TO_INTEGER (error->column),
272 INT_TO_INTEGER (error->position)));
273 }
274
275 static void
276 json_release_object (void *object)
277 {
278 json_decref (object);
279 }
280
281
282
283
284 static void
285 check_string_without_embedded_nulls (Lisp_Object object)
286 {
287 CHECK_STRING (object);
288 CHECK_TYPE (memchr (SDATA (object), '\0', SBYTES (object)) == NULL,
289 Qstring_without_embedded_nulls_p, object);
290 }
291
292
293
294
295 static json_t *
296 json_check (json_t *object)
297 {
298 if (object == NULL)
299 json_out_of_memory ();
300 return object;
301 }
302
303
304
305
306 static void
307 json_check_utf8 (Lisp_Object string)
308 {
309 CHECK_TYPE (utf8_string_p (string), Qutf_8_string_p, string);
310 }
311
312 enum json_object_type {
313 json_object_hashtable,
314 json_object_alist,
315 json_object_plist
316 };
317
318 enum json_array_type {
319 json_array_array,
320 json_array_list
321 };
322
323 struct json_configuration {
324 enum json_object_type object_type;
325 enum json_array_type array_type;
326 Lisp_Object null_object;
327 Lisp_Object false_object;
328 };
329
330 static json_t *lisp_to_json (Lisp_Object,
331 const struct json_configuration *conf);
332
333
334
335 static json_t *
336 lisp_to_json_nonscalar_1 (Lisp_Object lisp,
337 const struct json_configuration *conf)
338 {
339 json_t *json;
340 specpdl_ref count;
341
342 if (VECTORP (lisp))
343 {
344 ptrdiff_t size = ASIZE (lisp);
345 json = json_check (json_array ());
346 count = SPECPDL_INDEX ();
347 record_unwind_protect_ptr (json_release_object, json);
348 for (ptrdiff_t i = 0; i < size; ++i)
349 {
350 int status
351 = json_array_append_new (json, lisp_to_json (AREF (lisp, i),
352 conf));
353 if (status == -1)
354 json_out_of_memory ();
355 }
356 eassert (json_array_size (json) == size);
357 }
358 else if (HASH_TABLE_P (lisp))
359 {
360 struct Lisp_Hash_Table *h = XHASH_TABLE (lisp);
361 json = json_check (json_object ());
362 count = SPECPDL_INDEX ();
363 record_unwind_protect_ptr (json_release_object, json);
364 for (ptrdiff_t i = 0; i < HASH_TABLE_SIZE (h); ++i)
365 {
366 Lisp_Object key = HASH_KEY (h, i);
367 if (!BASE_EQ (key, Qunbound))
368 {
369 CHECK_STRING (key);
370 Lisp_Object ekey = json_encode (key);
371
372
373 check_string_without_embedded_nulls (ekey);
374 const char *key_str = SSDATA (ekey);
375
376
377 if (json_object_get (json, key_str) != NULL)
378 wrong_type_argument (Qjson_value_p, lisp);
379 int status
380 = json_object_set_new (json, key_str,
381 lisp_to_json (HASH_VALUE (h, i), conf));
382 if (status == -1)
383 {
384
385
386 json_check_utf8 (ekey);
387 json_out_of_memory ();
388 }
389 }
390 }
391 }
392 else if (NILP (lisp))
393 return json_check (json_object ());
394 else if (CONSP (lisp))
395 {
396 Lisp_Object tail = lisp;
397 json = json_check (json_object ());
398 count = SPECPDL_INDEX ();
399 record_unwind_protect_ptr (json_release_object, json);
400 bool is_plist = !CONSP (XCAR (tail));
401 FOR_EACH_TAIL (tail)
402 {
403 const char *key_str;
404 Lisp_Object value;
405 Lisp_Object key_symbol;
406 if (is_plist)
407 {
408 key_symbol = XCAR (tail);
409 tail = XCDR (tail);
410 CHECK_CONS (tail);
411 value = XCAR (tail);
412 }
413 else
414 {
415 Lisp_Object pair = XCAR (tail);
416 CHECK_CONS (pair);
417 key_symbol = XCAR (pair);
418 value = XCDR (pair);
419 }
420 CHECK_SYMBOL (key_symbol);
421 Lisp_Object key = SYMBOL_NAME (key_symbol);
422
423
424 check_string_without_embedded_nulls (key);
425 key_str = SSDATA (key);
426
427
428 if (is_plist && ':' == key_str[0] && key_str[1])
429 {
430 key_str = &key_str[1];
431 }
432
433 if (json_object_get (json, key_str) == NULL)
434 {
435 int status
436 = json_object_set_new (json, key_str, lisp_to_json (value,
437 conf));
438 if (status == -1)
439 json_out_of_memory ();
440 }
441 }
442 CHECK_LIST_END (tail, lisp);
443 }
444 else
445 wrong_type_argument (Qjson_value_p, lisp);
446
447 clear_unwind_protect (count);
448 unbind_to (count, Qnil);
449 return json;
450 }
451
452
453
454
455
456 static json_t *
457 lisp_to_json_nonscalar (Lisp_Object lisp,
458 const struct json_configuration *conf)
459 {
460 if (++lisp_eval_depth > max_lisp_eval_depth)
461 xsignal0 (Qjson_object_too_deep);
462 json_t *json = lisp_to_json_nonscalar_1 (lisp, conf);
463 --lisp_eval_depth;
464 return json;
465 }
466
467
468
469
470
471 static json_t *
472 lisp_to_json (Lisp_Object lisp, const struct json_configuration *conf)
473 {
474 if (EQ (lisp, conf->null_object))
475 return json_check (json_null ());
476 else if (EQ (lisp, conf->false_object))
477 return json_check (json_false ());
478 else if (EQ (lisp, Qt))
479 return json_check (json_true ());
480 else if (INTEGERP (lisp))
481 {
482 intmax_t low = TYPE_MINIMUM (json_int_t);
483 intmax_t high = TYPE_MAXIMUM (json_int_t);
484 intmax_t value = check_integer_range (lisp, low, high);
485 return json_check (json_integer (value));
486 }
487 else if (FLOATP (lisp))
488 return json_check (json_real (XFLOAT_DATA (lisp)));
489 else if (STRINGP (lisp))
490 {
491 Lisp_Object encoded = json_encode (lisp);
492 json_t *json = json_stringn (SSDATA (encoded), SBYTES (encoded));
493 if (json == NULL)
494 {
495
496
497 json_check_utf8 (encoded);
498 json_out_of_memory ();
499 }
500 return json;
501 }
502
503
504 return lisp_to_json_nonscalar (lisp, conf);
505 }
506
507 static void
508 json_parse_args (ptrdiff_t nargs,
509 Lisp_Object *args,
510 struct json_configuration *conf,
511 bool parse_object_types)
512 {
513 if ((nargs % 2) != 0)
514 wrong_type_argument (Qplistp, Flist (nargs, args));
515
516
517
518 for (ptrdiff_t i = nargs; i > 0; i -= 2) {
519 Lisp_Object key = args[i - 2];
520 Lisp_Object value = args[i - 1];
521 if (parse_object_types && EQ (key, QCobject_type))
522 {
523 if (EQ (value, Qhash_table))
524 conf->object_type = json_object_hashtable;
525 else if (EQ (value, Qalist))
526 conf->object_type = json_object_alist;
527 else if (EQ (value, Qplist))
528 conf->object_type = json_object_plist;
529 else
530 wrong_choice (list3 (Qhash_table, Qalist, Qplist), value);
531 }
532 else if (parse_object_types && EQ (key, QCarray_type))
533 {
534 if (EQ (value, Qarray))
535 conf->array_type = json_array_array;
536 else if (EQ (value, Qlist))
537 conf->array_type = json_array_list;
538 else
539 wrong_choice (list2 (Qarray, Qlist), value);
540 }
541 else if (EQ (key, QCnull_object))
542 conf->null_object = value;
543 else if (EQ (key, QCfalse_object))
544 conf->false_object = value;
545 else if (parse_object_types)
546 wrong_choice (list4 (QCobject_type,
547 QCarray_type,
548 QCnull_object,
549 QCfalse_object),
550 value);
551 else
552 wrong_choice (list2 (QCnull_object,
553 QCfalse_object),
554 value);
555 }
556 }
557
558 static bool
559 json_available_p (void)
560 {
561 #ifdef WINDOWSNT
562 if (!json_initialized)
563 {
564 Lisp_Object status;
565 json_initialized = init_json_functions ();
566 status = json_initialized ? Qt : Qnil;
567 Vlibrary_cache = Fcons (Fcons (Qjson, status), Vlibrary_cache);
568 }
569 return json_initialized;
570 #else
571 return true;
572 #endif
573 }
574
575 #ifdef WINDOWSNT
576 static void
577 ensure_json_available (void)
578 {
579 if (!json_available_p ())
580 Fsignal (Qjson_unavailable,
581 list1 (build_unibyte_string ("jansson library not found")));
582 }
583 #endif
584
585 DEFUN ("json--available-p", Fjson__available_p, Sjson__available_p, 0, 0, NULL,
586 doc: )
587 (void)
588 {
589 return json_available_p () ? Qt : Qnil;
590 }
591
592 DEFUN ("json-serialize", Fjson_serialize, Sjson_serialize, 1, MANY,
593 NULL,
594 doc:
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 (ptrdiff_t nargs, Lisp_Object *args)
620 {
621 specpdl_ref count = SPECPDL_INDEX ();
622
623 #ifdef WINDOWSNT
624 ensure_json_available ();
625 #endif
626
627 struct json_configuration conf =
628 {json_object_hashtable, json_array_array, QCnull, QCfalse};
629 json_parse_args (nargs - 1, args + 1, &conf, false);
630
631 json_t *json = lisp_to_json (args[0], &conf);
632 record_unwind_protect_ptr (json_release_object, json);
633
634 char *string = json_dumps (json, JSON_COMPACT | JSON_ENCODE_ANY);
635 if (string == NULL)
636 json_out_of_memory ();
637 record_unwind_protect_ptr (json_free, string);
638
639 return unbind_to (count, build_string_from_utf8 (string));
640 }
641
642 struct json_buffer_and_size
643 {
644 const char *buffer;
645 ptrdiff_t size;
646
647
648 ptrdiff_t inserted_bytes;
649 };
650
651 static Lisp_Object
652 json_insert (void *data)
653 {
654 struct json_buffer_and_size *buffer_and_size = data;
655 ptrdiff_t len = buffer_and_size->size;
656 ptrdiff_t inserted_bytes = buffer_and_size->inserted_bytes;
657 ptrdiff_t gap_size = GAP_SIZE - inserted_bytes;
658
659
660 if (gap_size < len)
661 make_gap (len - gap_size);
662
663
664 memcpy ((char *) BEG_ADDR + PT_BYTE - BEG_BYTE + inserted_bytes,
665 buffer_and_size->buffer, len);
666 buffer_and_size->inserted_bytes += len;
667 return Qnil;
668 }
669
670 static Lisp_Object
671 json_handle_nonlocal_exit (enum nonlocal_exit type, Lisp_Object data)
672 {
673 switch (type)
674 {
675 case NONLOCAL_EXIT_SIGNAL:
676 return data;
677 case NONLOCAL_EXIT_THROW:
678 return Fcons (Qno_catch, data);
679 default:
680 eassume (false);
681 }
682 }
683
684 struct json_insert_data
685 {
686
687
688 ptrdiff_t inserted_bytes;
689
690
691 Lisp_Object error;
692 };
693
694
695
696
697
698
699
700 static int
701 json_insert_callback (const char *buffer, size_t size, void *data)
702 {
703 struct json_insert_data *d = data;
704 struct json_buffer_and_size buffer_and_size
705 = {.buffer = buffer, .size = size, .inserted_bytes = d->inserted_bytes};
706 d->error = internal_catch_all (json_insert, &buffer_and_size,
707 json_handle_nonlocal_exit);
708 d->inserted_bytes = buffer_and_size.inserted_bytes;
709 return NILP (d->error) ? 0 : -1;
710 }
711
712 DEFUN ("json-insert", Fjson_insert, Sjson_insert, 1, MANY,
713 NULL,
714 doc:
715
716
717
718 )
719 (ptrdiff_t nargs, Lisp_Object *args)
720 {
721 specpdl_ref count = SPECPDL_INDEX ();
722
723 #ifdef WINDOWSNT
724 ensure_json_available ();
725 #endif
726
727 struct json_configuration conf =
728 {json_object_hashtable, json_array_array, QCnull, QCfalse};
729 json_parse_args (nargs - 1, args + 1, &conf, false);
730
731 json_t *json = lisp_to_json (args[0], &conf);
732 record_unwind_protect_ptr (json_release_object, json);
733
734 prepare_to_modify_buffer (PT, PT, NULL);
735 move_gap_both (PT, PT_BYTE);
736 struct json_insert_data data;
737 data.inserted_bytes = 0;
738
739
740 int status = json_dump_callback (json, json_insert_callback, &data,
741 JSON_COMPACT | JSON_ENCODE_ANY);
742 if (status == -1)
743 {
744 if (CONSP (data.error))
745 xsignal (XCAR (data.error), XCDR (data.error));
746 else
747 json_out_of_memory ();
748 }
749
750 ptrdiff_t inserted = 0;
751 ptrdiff_t inserted_bytes = data.inserted_bytes;
752 if (inserted_bytes > 0)
753 {
754
755 struct coding_system coding;
756
757
758
759
760 setup_coding_system (Qutf_8_unix, &coding);
761 coding.dst_multibyte =
762 !NILP (BVAR (current_buffer, enable_multibyte_characters));
763 if (CODING_MAY_REQUIRE_DECODING (&coding))
764 {
765
766
767
768 memmove (GAP_END_ADDR - inserted_bytes, GPT_ADDR, inserted_bytes);
769 decode_coding_gap (&coding, inserted_bytes);
770 inserted = coding.produced_char;
771 }
772 else
773 {
774
775 eassert (NILP (BVAR (current_buffer, enable_multibyte_characters)));
776 insert_from_gap_1 (inserted_bytes, inserted_bytes, false);
777
778
779 invalidate_buffer_caches (current_buffer,
780 PT, PT + inserted_bytes);
781 adjust_after_insert (PT, PT_BYTE,
782 PT + inserted_bytes,
783 PT_BYTE + inserted_bytes,
784 inserted_bytes);
785 inserted = inserted_bytes;
786 }
787 }
788
789
790 signal_after_change (PT, 0, inserted);
791 if (inserted > 0)
792 {
793 update_compositions (PT, PT, CHECK_BORDER);
794
795 SET_PT_BOTH (PT + inserted, PT_BYTE + inserted_bytes);
796 }
797
798 return unbind_to (count, Qnil);
799 }
800
801
802
803 static Lisp_Object ARG_NONNULL ((1))
804 json_to_lisp (json_t *json, const struct json_configuration *conf)
805 {
806 switch (json_typeof (json))
807 {
808 case JSON_NULL:
809 return conf->null_object;
810 case JSON_FALSE:
811 return conf->false_object;
812 case JSON_TRUE:
813 return Qt;
814 case JSON_INTEGER:
815 {
816 json_int_t i = json_integer_value (json);
817 return INT_TO_INTEGER (i);
818 }
819 case JSON_REAL:
820 return make_float (json_real_value (json));
821 case JSON_STRING:
822 return make_string_from_utf8 (json_string_value (json),
823 json_string_length (json));
824 case JSON_ARRAY:
825 {
826 if (++lisp_eval_depth > max_lisp_eval_depth)
827 xsignal0 (Qjson_object_too_deep);
828 size_t size = json_array_size (json);
829 if (PTRDIFF_MAX < size)
830 overflow_error ();
831 Lisp_Object result;
832 switch (conf->array_type)
833 {
834 case json_array_array:
835 {
836 result = make_vector (size, Qunbound);
837 for (ptrdiff_t i = 0; i < size; ++i)
838 {
839 rarely_quit (i);
840 ASET (result, i,
841 json_to_lisp (json_array_get (json, i), conf));
842 }
843 break;
844 }
845 case json_array_list:
846 {
847 result = Qnil;
848 for (ptrdiff_t i = size - 1; i >= 0; --i)
849 {
850 rarely_quit (i);
851 result = Fcons (json_to_lisp (json_array_get (json, i), conf),
852 result);
853 }
854 break;
855 }
856 default:
857
858 emacs_abort ();
859 }
860 --lisp_eval_depth;
861 return result;
862 }
863 case JSON_OBJECT:
864 {
865 if (++lisp_eval_depth > max_lisp_eval_depth)
866 xsignal0 (Qjson_object_too_deep);
867 Lisp_Object result;
868 switch (conf->object_type)
869 {
870 case json_object_hashtable:
871 {
872 size_t size = json_object_size (json);
873 if (FIXNUM_OVERFLOW_P (size))
874 overflow_error ();
875 result = CALLN (Fmake_hash_table, QCtest, Qequal, QCsize,
876 make_fixed_natnum (size));
877 struct Lisp_Hash_Table *h = XHASH_TABLE (result);
878 const char *key_str;
879 json_t *value;
880 json_object_foreach (json, key_str, value)
881 {
882 Lisp_Object key = build_string_from_utf8 (key_str), hash;
883 ptrdiff_t i = hash_lookup (h, key, &hash);
884
885
886 eassert (i < 0);
887 hash_put (h, key, json_to_lisp (value, conf), hash);
888 }
889 break;
890 }
891 case json_object_alist:
892 {
893 result = Qnil;
894 const char *key_str;
895 json_t *value;
896 json_object_foreach (json, key_str, value)
897 {
898 Lisp_Object key
899 = Fintern (build_string_from_utf8 (key_str), Qnil);
900 result
901 = Fcons (Fcons (key, json_to_lisp (value, conf)),
902 result);
903 }
904 result = Fnreverse (result);
905 break;
906 }
907 case json_object_plist:
908 {
909 result = Qnil;
910 const char *key_str;
911 json_t *value;
912 json_object_foreach (json, key_str, value)
913 {
914 USE_SAFE_ALLOCA;
915 ptrdiff_t key_str_len = strlen (key_str);
916 char *keyword_key_str = SAFE_ALLOCA (1 + key_str_len + 1);
917 keyword_key_str[0] = ':';
918 strcpy (&keyword_key_str[1], key_str);
919 Lisp_Object key = intern_1 (keyword_key_str, key_str_len + 1);
920
921
922 result = Fcons (key, result);
923 result = Fcons (json_to_lisp (value, conf), result);
924 SAFE_FREE ();
925 }
926 result = Fnreverse (result);
927 break;
928 }
929 default:
930
931 emacs_abort ();
932 }
933 --lisp_eval_depth;
934 return result;
935 }
936 }
937
938 emacs_abort ();
939 }
940
941 DEFUN ("json-parse-string", Fjson_parse_string, Sjson_parse_string, 1, MANY,
942 NULL,
943 doc:
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966 )
967 (ptrdiff_t nargs, Lisp_Object *args)
968 {
969 specpdl_ref count = SPECPDL_INDEX ();
970
971 #ifdef WINDOWSNT
972 ensure_json_available ();
973 #endif
974
975 Lisp_Object string = args[0];
976 CHECK_STRING (string);
977 Lisp_Object encoded = json_encode (string);
978 check_string_without_embedded_nulls (encoded);
979 struct json_configuration conf =
980 {json_object_hashtable, json_array_array, QCnull, QCfalse};
981 json_parse_args (nargs - 1, args + 1, &conf, true);
982
983 json_error_t error;
984 json_t *object
985 = json_loads (SSDATA (encoded), JSON_DECODE_ANY | JSON_ALLOW_NUL, &error);
986 if (object == NULL)
987 json_parse_error (&error);
988
989
990 if (object != NULL)
991 record_unwind_protect_ptr (json_release_object, object);
992
993 return unbind_to (count, json_to_lisp (object, &conf));
994 }
995
996 struct json_read_buffer_data
997 {
998
999 ptrdiff_t point;
1000 };
1001
1002
1003
1004
1005
1006
1007
1008 static size_t
1009 json_read_buffer_callback (void *buffer, size_t buflen, void *data)
1010 {
1011 struct json_read_buffer_data *d = data;
1012
1013
1014
1015 ptrdiff_t point = d->point;
1016 ptrdiff_t end = BUFFER_CEILING_OF (point) + 1;
1017 ptrdiff_t count = end - point;
1018 if (buflen < count)
1019 count = buflen;
1020 memcpy (buffer, BYTE_POS_ADDR (point), count);
1021 d->point += count;
1022 return count;
1023 }
1024
1025 DEFUN ("json-parse-buffer", Fjson_parse_buffer, Sjson_parse_buffer,
1026 0, MANY, NULL,
1027 doc:
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 (ptrdiff_t nargs, Lisp_Object *args)
1056 {
1057 specpdl_ref count = SPECPDL_INDEX ();
1058
1059 #ifdef WINDOWSNT
1060 ensure_json_available ();
1061 #endif
1062
1063 struct json_configuration conf =
1064 {json_object_hashtable, json_array_array, QCnull, QCfalse};
1065 json_parse_args (nargs, args, &conf, true);
1066
1067 ptrdiff_t point = PT_BYTE;
1068 struct json_read_buffer_data data = {.point = point};
1069 json_error_t error;
1070 json_t *object
1071 = json_load_callback (json_read_buffer_callback, &data,
1072 JSON_DECODE_ANY
1073 | JSON_DISABLE_EOF_CHECK
1074 | JSON_ALLOW_NUL,
1075 &error);
1076
1077 if (object == NULL)
1078 json_parse_error (&error);
1079
1080
1081 record_unwind_protect_ptr (json_release_object, object);
1082
1083
1084 Lisp_Object lisp = json_to_lisp (object, &conf);
1085
1086
1087 point += error.position;
1088 SET_PT_BOTH (BYTE_TO_CHAR (point), point);
1089
1090 return unbind_to (count, lisp);
1091 }
1092
1093 void
1094 syms_of_json (void)
1095 {
1096 DEFSYM (QCnull, ":null");
1097 DEFSYM (QCfalse, ":false");
1098
1099 DEFSYM (Qstring_without_embedded_nulls_p, "string-without-embedded-nulls-p");
1100 DEFSYM (Qjson_value_p, "json-value-p");
1101
1102 DEFSYM (Qjson_error, "json-error");
1103 DEFSYM (Qjson_out_of_memory, "json-out-of-memory");
1104 DEFSYM (Qjson_parse_error, "json-parse-error");
1105 DEFSYM (Qjson_end_of_file, "json-end-of-file");
1106 DEFSYM (Qjson_trailing_content, "json-trailing-content");
1107 DEFSYM (Qjson_object_too_deep, "json-object-too-deep");
1108 DEFSYM (Qjson_unavailable, "json-unavailable");
1109 define_error (Qjson_error, "generic JSON error", Qerror);
1110 define_error (Qjson_out_of_memory,
1111 "not enough memory for creating JSON object", Qjson_error);
1112 define_error (Qjson_parse_error, "could not parse JSON stream",
1113 Qjson_error);
1114 define_error (Qjson_end_of_file, "end of JSON stream", Qjson_parse_error);
1115 define_error (Qjson_trailing_content, "trailing content after JSON stream",
1116 Qjson_parse_error);
1117 define_error (Qjson_object_too_deep,
1118 "object cyclic or Lisp evaluation too deep", Qjson_error);
1119
1120 DEFSYM (Qpure, "pure");
1121 DEFSYM (Qside_effect_free, "side-effect-free");
1122
1123 DEFSYM (Qjson_serialize, "json-serialize");
1124 DEFSYM (Qjson_parse_string, "json-parse-string");
1125 Fput (Qjson_serialize, Qpure, Qt);
1126 Fput (Qjson_serialize, Qside_effect_free, Qt);
1127 Fput (Qjson_parse_string, Qpure, Qt);
1128 Fput (Qjson_parse_string, Qside_effect_free, Qt);
1129
1130 DEFSYM (QCobject_type, ":object-type");
1131 DEFSYM (QCarray_type, ":array-type");
1132 DEFSYM (QCnull_object, ":null-object");
1133 DEFSYM (QCfalse_object, ":false-object");
1134 DEFSYM (Qalist, "alist");
1135 DEFSYM (Qplist, "plist");
1136 DEFSYM (Qarray, "array");
1137
1138 defsubr (&Sjson__available_p);
1139 defsubr (&Sjson_serialize);
1140 defsubr (&Sjson_insert);
1141 defsubr (&Sjson_parse_string);
1142 defsubr (&Sjson_parse_buffer);
1143 }