Function: json-insert

json-insert is a function defined in json.c.

Signature

(json-insert OBJECT &rest ARGS)

Documentation

Insert the JSON representation of OBJECT before point.

This is the same as (insert (json-serialize OBJECT ...)), but potentially faster, and with the difference that Unicode characters are inserted as themselves into multibyte buffers, and as UTF-8 byte sequences into unibyte buffers. See the function json-serialize for allowed values of OBJECT and ARGS.

View in manual

Probably introduced at or before Emacs version 27.1.

Source Code

// Defined in /usr/src/emacs/src/json.c
{
  specpdl_ref count = SPECPDL_INDEX ();
  json_out_t jo;
  json_serialize (&jo, args[0], nargs - 1, args + 1);

  prepare_to_modify_buffer (PT, PT, NULL);
  move_gap_both (PT, PT_BYTE);
  if (GAP_SIZE < jo.size)
    make_gap (jo.size - GAP_SIZE);
  memcpy (GPT_ADDR, jo.buf, jo.size);

  /* No need to keep allocation beyond this point.  */
  unbind_to (count, Qnil);

  bool ub_buffer = NILP (BVAR (current_buffer, enable_multibyte_characters));
  ptrdiff_t inserted_bytes = jo.size;
  ptrdiff_t inserted = ub_buffer ? jo.size : jo.size - jo.chars_delta;
  eassert (inserted > 0);

  insert_from_gap_1 (inserted, inserted_bytes, false);
  invalidate_buffer_caches (current_buffer, PT, PT + inserted);
  adjust_after_insert (PT, PT_BYTE, PT + inserted, PT_BYTE + inserted_bytes,
		       inserted);

  /* Call after-change hooks.  */
  signal_after_change (PT, 0, inserted);

  update_compositions (PT, PT, CHECK_BORDER);
  /* Move point to after the inserted text.  */
  SET_PT_BOTH (PT + inserted, PT_BYTE + inserted_bytes);

  return Qnil;
}