annotate lisp/json.el @ 112364:42e22c4f06b7

Move all DEFVAR'd globals into a structure -- threading infrastructure * globals.h: New file. * xterm.h (Vx_pixel_size_width_font_regexp): Remove declaration. * window.h (Vinitial_window_system, Vminibuf_scroll_window) (Vwindow_system_version): Remove declaration. * w32term.h (Vw32_enable_palette) (Vx_pixel_size_width_font_regexp): Remove declaration. * w32menu.c (Voverriding_local_map) (Voverriding_local_map_menu_flag): Remove declaration. * w32inevt.c (Vw32_alt_is_meta, Vw32_apps_modifier) (Vw32_capslock_is_shiftlock, Vw32_enable_caps_lock) (Vw32_enable_num_lock, Vw32_lwindow_modifier) (Vw32_pass_lwindow_to_system, Vw32_pass_rwindow_to_system) (Vw32_phantom_key_code, Vw32_recognize_altgr) (Vw32_rwindow_modifier, Vw32_scroll_lock_modifier) (w32_use_full_screen_buffer): Remove declaration. * w32.c (Vsystem_configuration, Vw32_downcase_file_names) (Vw32_generate_fake_inodes, Vw32_get_true_file_attributes) (w32_num_mouse_buttons, w32_pipe_read_delay): Remove declaration. * termopts.h (Vtruncate_partial_width_windows, inverse_video) (no_redraw_on_reenter, visible_bell): Remove declaration. * sysdep.c (Vsystem_name): Remove declaration. * syntax.h (parse_sexp_lookup_properties): Remove declaration. * menu.h (Vmenu_updating_frame): Remove declaration. * macros.h (Vexecuting_kbd_macro, executing_kbd_macro_index): Remove declaration. * lisp.h (Vafter_init_time, Vafter_load_alist) (Vauto_save_list_file_name, Vbefore_init_time, Vcommand_history) (Vcompletion_regexp_list, Vcurrent_load_list) (Vcurrent_prefix_arg, Vdata_directory, Vdebug_on_error) (Vdoc_directory, Vdoc_file_name, Vdynamic_library_alist) (Vexec_directory, Vexec_path, Vexec_suffixes) (Vface_font_rescale_alist, Vface_ignored_fonts, Vfeatures) (Vhelp_form, Vhistory_length, Vinhibit_field_text_motion) (Vinhibit_quit, Vinhibit_read_only, Vinhibit_redisplay) (Vinstallation_directory, Vinvocation_directory) (Vinvocation_name, Vload_file_rep_suffixes, Vload_history) (Vload_suffixes, Vmark_even_if_inactive, Vmemory_full) (Vmessage_log_max, Vobarray, Vprint_length, Vprint_level) (Vpurify_flag, Vquit_flag, Vsaved_region_selection) (Vscalable_fonts_allowed, Vselect_active_regions) (Vshell_file_name, Vstandard_input, Vstandard_output) (Vsystem_name, Vtemporary_file_directory, Vthrow_on_input) (Vtop_level, Vtty_erase_char, Vundo_outer_limit) (Vuser_login_name, Vwindow_scroll_functions) (Vwindow_system_version, Vx_no_window_manager) (Vx_resource_class, Vx_resource_name, baud_rate) (completion_ignore_case, debug_on_next_call, gc_cons_threshold) (history_delete_duplicates, inhibit_x_resources) (last_nonmenu_event, load_in_progress, max_specpdl_size) (minibuffer_auto_raise, print_escape_newlines, scroll_margin) (use_dialog_box, use_file_dialog): Remove declaration. Include globals.h. * keymap.h (Voverriding_local_map) (Voverriding_local_map_menu_flag, meta_prefix_char): Remove declaration. * keyboard.h (Vdouble_click_time, Vfunction_key_map) (Vinput_method_function, Vkey_translation_map) (Vlucid_menu_bar_dirty_flag, Vthis_original_command) (do_mouse_tracking, extra_keyboard_modifiers) (num_nonmacro_input_events): Remove declaration. * intervals.h (Vchar_property_alias_alist) (Vdefault_text_properties, Vinhibit_point_motion_hooks) (Vtext_property_default_nonsticky): Remove declaration. * gtkutil.h (x_gtk_file_dialog_help_text) (x_gtk_show_hidden_files, x_gtk_use_old_file_dialog) (x_gtk_whole_detached_tool_bar): Remove declaration. * frame.h (Vdefault_frame_alist, Vframe_alpha_lower_limit) (Vmenu_bar_mode, Vmouse_highlight, Vterminal_frame) (Vtool_bar_mode, Vx_resource_class, Vx_resource_name) (focus_follows_mouse): Remove declaration. * fontset.h (Valternate_fontname_alist, Vfontset_alias_alist) (Vignore_relative_composition, Votf_script_alist) (Vuse_default_ascent, Vvertical_centering_font_regexp): Remove declaration. * font.h (Vfont_log): Remove declaration. * dosfns.h (Vdos_display_scancodes, Vdos_version) (Vdos_windows_version, dos_codepage, dos_country_code) (dos_decimal_point, dos_hyper_key, dos_keyboard_layout) (dos_keypad_mode, dos_super_key, dos_timezone_offset): Remove declaration. * disptab.h (Vglyph_table, Vstandard_display_table): Remove declaration. * dispextern.h (Vface_remapping_alist, Vglyphless_char_display) (Vmouse_autoselect_window, Voverflow_newline_into_fringe) (Vshow_trailing_whitespace, Vtool_bar_button_margin) (Vtool_bar_style, cursor_in_echo_area, display_hourglass_p) (inverse_video, mode_line_in_non_selected_windows) (tool_bar_button_relief, tool_bar_max_label_size) (underline_minimum_offset) (unibyte_display_via_language_environment, x_stretch_cursor_p): Remove declaration. * composite.h (Vauto_composition_function) (Vcomposition_function_table): Remove declaration. * commands.h (Vexecuting_kbd_macro) (Vminibuffer_local_completion_map) (Vminibuffer_local_filename_completion_map) (Vminibuffer_local_filename_must_match_map) (Vminibuffer_local_map, Vminibuffer_local_must_match_map) (Vminibuffer_local_ns_map, Vthis_command) (Vunread_command_events, cursor_in_echo_area) (last_command_event, last_nonmenu_event, unread_command_char): Remove declaration. * coding.h (Vcoding_system_for_read, Vcoding_system_for_write) (Vdefault_file_name_coding_system) (Vdefault_process_coding_system, Vfile_name_coding_system) (Vlast_coding_system_used, Vlocale_coding_system) (Vselect_safe_coding_system_function) (Vtranslation_table_for_input, coding_system_require_warning) (eol_mnemonic_dos, eol_mnemonic_mac, eol_mnemonic_undecided) (eol_mnemonic_unix, inherit_process_coding_system): Remove declaration. * charset.h (Vcharset_list, Vcurrent_iso639_language): Remove declaration. * character.h (Vauto_fill_chars, Vchar_direction_table) (Vchar_script_table, Vchar_width_table, Vprintable_chars) (Vscript_representative_chars, Vtranslation_table_vector) (Vunicode_category_table): Remove declaration. * ccl.h (Vfont_ccl_encoder_alist): Remove declaration. * buffer.h (Vafter_change_functions, Vbefore_change_functions) (Vdeactivate_mark, Vfirst_change_hook, Vtransient_mark_mode) (inhibit_modification_hooks): Remove declaration. * xterm.c (syms_of_xterm): Update. (Vx_alt_keysym, Vx_hyper_keysym, Vx_keysym_table) (Vx_meta_keysym, Vx_super_keysym, Vx_toolkit_scroll_bars) (x_mouse_click_focus_ignore_position) (x_underline_at_descent_line) (x_use_underline_position_properties): Remove. * xsmfns.c (syms_of_xsmfns): Update. (Vx_session_id, Vx_session_previous_id): Remove. * xsettings.c (syms_of_xsettings): Update. (Vxft_settings, use_system_font): Remove. * xselect.c (syms_of_xselect): Update. (Vselection_converter_alist, Vx_lost_selection_functions) (Vx_sent_selection_functions, x_selection_timeout): Remove. * xfns.c (syms_of_xfns): Update. (Vgtk_version_string, Vmotif_version_string) (Vx_cursor_fore_pixel, Vx_hourglass_pointer_shape) (Vx_max_tooltip_size, Vx_mode_pointer_shape) (Vx_no_window_manager, Vx_nontext_pointer_shape) (Vx_pixel_size_width_font_regexp, Vx_pointer_shape) (Vx_sensitive_text_pointer_shape) (Vx_window_horizontal_drag_shape, x_gtk_file_dialog_help_text) (x_gtk_show_hidden_files, x_gtk_use_old_file_dialog) (x_gtk_use_system_tooltips, x_gtk_whole_detached_tool_bar): Remove. * xfaces.c (syms_of_xfaces): Update. (Vface_default_stipple, Vface_font_rescale_alist) (Vface_ignored_fonts, Vface_new_frame_defaults) (Vface_remapping_alist, Vfont_list_limit) (Vscalable_fonts_allowed, Vtty_defined_color_alist): Remove. * xdisp.c (syms_of_xdisp): Update. (Vauto_resize_tool_bars, Vblink_cursor_alist) (Vdisplay_pixels_per_inch, Vfontification_functions) (Vframe_title_format, Vglobal_mode_string) (Vglyphless_char_display, Vhourglass_delay, Vhscroll_step) (Vicon_title_format, Vinhibit_redisplay) (Vline_number_display_limit, Vline_prefix) (Vmax_mini_window_height, Vmenu_bar_update_hook) (Vmenu_updating_frame, Vmessage_log_max) (Vmouse_autoselect_window, Vnobreak_char_display) (Voverlay_arrow_position, Voverlay_arrow_string) (Voverlay_arrow_variable_list, Vredisplay_end_trigger_functions) (Vresize_mini_windows, Vshow_trailing_whitespace) (Vtool_bar_border, Vtool_bar_button_margin, Vtool_bar_style) (Vtruncate_partial_width_windows, Vvoid_text_area_pointer) (Vwindow_scroll_functions, Vwindow_size_change_functions) (Vwindow_text_change_functions, Vwrap_prefix) (auto_raise_tool_bar_buttons_p, automatic_hscrolling_p) (debug_end_pos, display_hourglass_p, emacs_scroll_step) (highlight_nonselected_windows, hscroll_margin) (inhibit_eval_during_redisplay, inhibit_free_realized_faces) (inhibit_menubar_update, inhibit_try_cursor_movement) (inhibit_try_window_id, inhibit_try_window_reusing) (line_number_display_limit_width) (make_cursor_line_fully_visible_p, message_truncate_lines) (mode_line_inverse_video, multiple_frames, overline_margin) (scroll_conservatively, scroll_margin, tool_bar_button_relief) (tool_bar_max_label_size, underline_minimum_offset) (unibyte_display_via_language_environment, x_stretch_cursor_p): Remove. * window.c (syms_of_window): Update. (Vminibuf_scroll_window, Vother_window_scroll_buffer) (Vrecenter_redisplay, Vscroll_preserve_screen_position) (Vtemp_buffer_show_function, Vwindow_configuration_change_hook) (Vwindow_point_insertion_type, auto_window_vscroll_p) (mode_line_in_non_selected_windows, next_screen_context_lines) (window_min_height, window_min_width): Remove. (scroll_margin): Remove declaration. * w32term.c (syms_of_w32term): Update. (Vw32_capslock_is_shiftlock, Vw32_grab_focus_on_raise) (Vw32_recognize_altgr, Vw32_swap_mouse_buttons) (Vx_toolkit_scroll_bars, w32_num_mouse_buttons) (w32_use_visible_system_caret, x_underline_at_descent_line) (x_use_underline_position_properties): Remove. (Vcommand_line_args, Vsystem_name, extra_keyboard_modifiers): Remove declaration. * w32select.c (syms_of_w32select): Update. (Vnext_selection_coding_system, Vselection_coding_system): Remove. * w32proc.c (syms_of_ntproc): Update. (Vw32_downcase_file_names, Vw32_generate_fake_inodes) (Vw32_get_true_file_attributes, Vw32_quote_process_args) (Vw32_start_process_inherit_error_mode) (Vw32_start_process_share_console) (Vw32_start_process_show_window, w32_pipe_read_delay): Remove. (Vsystem_name): Remove declaration. * w32font.c (syms_of_w32font): Update. (Vw32_charset_info_alist): Remove. * w32fns.c (globals_of_w32fns, syms_of_w32fns): Update. (Vw32_alt_is_meta, Vw32_apps_modifier, Vw32_bdf_filename_alist) (Vw32_color_map, Vw32_enable_caps_lock, Vw32_enable_num_lock) (Vw32_enable_palette, Vw32_lwindow_modifier) (Vw32_pass_alt_to_system, Vw32_pass_lwindow_to_system) (Vw32_pass_rwindow_to_system, Vw32_phantom_key_code) (Vw32_rwindow_modifier, Vw32_scroll_lock_modifier) (Vx_cursor_fore_pixel, Vx_hourglass_pointer_shape) (Vx_max_tooltip_size, Vx_mode_pointer_shape) (Vx_no_window_manager, Vx_nontext_pointer_shape) (Vx_pixel_size_width_font_regexp, Vx_pointer_shape) (Vx_sensitive_text_pointer_shape) (Vx_window_horizontal_drag_shape, w32_ansi_code_page) (w32_enable_synthesized_fonts, w32_mouse_button_tolerance) (w32_mouse_move_interval) (w32_pass_extra_mouse_buttons_to_system) (w32_pass_multimedia_buttons_to_system, w32_quit_key) (w32_strict_fontnames, w32_strict_painting): Remove. (Vhourglass_delay, Vmenu_bar_mode, Vtool_bar_mode) (Vw32_recognize_altgr, Vwindow_system_version) (w32_num_mouse_buttons, w32_use_visible_system_caret): Remove declaration. * w32console.c (syms_of_ntterm): Update. (w32_use_full_screen_buffer): Remove. (Vtty_defined_color_alist): Remove declaration. * w16select.c (syms_of_win16select): Update. (Vnext_selection_coding_system, Vselection_coding_system): Remove. * undo.c (syms_of_undo): Update. (Vundo_outer_limit, Vundo_outer_limit_function) (undo_inhibit_record_point, undo_limit, undo_strong_limit): Remove. * textprop.c (syms_of_textprop): Update. (Vchar_property_alias_alist, Vdefault_text_properties) (Vinhibit_point_motion_hooks, Vtext_property_default_nonsticky): Remove. * terminal.c (syms_of_terminal): Update. (Vdelete_terminal_functions, Vring_bell_function): Remove. * term.c (syms_of_term): Update. (Vresume_tty_functions, Vsuspend_tty_functions) (no_redraw_on_reenter, system_uses_terminfo, visible_cursor): Remove. * syntax.c (syms_of_syntax): Update. (Vfind_word_boundary_function_table, multibyte_syntax_as_symbol) (open_paren_in_column_0_is_defun_start) (parse_sexp_ignore_comments, parse_sexp_lookup_properties) (words_include_escapes): Remove. * search.c (syms_of_search): Update. (Vinhibit_changing_match_data, Vsearch_spaces_regexp): Remove. * process.c (syms_of_process): Update. (Vprocess_adaptive_read_buffering, Vprocess_connection_type) (delete_exited_processes): Remove. * print.c (syms_of_print): Update. (Vfloat_output_format, Vprint_charset_text_property) (Vprint_circle, Vprint_continuous_numbering, Vprint_gensym) (Vprint_length, Vprint_level, Vprint_number_table) (Vstandard_output, print_escape_multibyte) (print_escape_newlines, print_escape_nonascii, print_quoted): Remove. * msdos.c (syms_of_msdos): Update. (Vdos_unsupported_char_glyph): Remove. (unibyte_display_via_language_environment): Remove declaration. * minibuf.c (syms_of_minibuf): Update. (Vcompletion_regexp_list, Vhistory_add_new_input) (Vhistory_length, Vminibuffer_completing_file_name) (Vminibuffer_completion_confirm) (Vminibuffer_completion_predicate, Vminibuffer_completion_table) (Vminibuffer_exit_hook, Vminibuffer_help_form) (Vminibuffer_history_position, Vminibuffer_history_variable) (Vminibuffer_prompt_properties, Vminibuffer_setup_hook) (Vread_buffer_function, Vread_expression_map) (completion_ignore_case, enable_recursive_minibuffers) (history_delete_duplicates, minibuffer_allow_text_properties) (minibuffer_auto_raise, read_buffer_completion_ignore_case): Remove. * marker.c (syms_of_marker): Update. (byte_debug_flag): Remove. * macros.c (syms_of_macros): Update. (Vexecuting_kbd_macro, executing_kbd_macro_index): Remove. * lread.c (syms_of_lread): Update. (Vafter_load_alist, Vbyte_boolean_vars) (Vbytecomp_version_regexp, Vcurrent_load_list) (Veval_buffer_list, Vload_file_name, Vload_file_rep_suffixes) (Vload_history, Vload_path, Vload_read_function) (Vload_source_file_function, Vload_suffixes, Vobarray) (Vold_style_backquotes, Vpreloaded_file_list, Vread_circle) (Vread_symbol_positions_list, Vread_with_symbol_positions) (Vsource_directory, Vstandard_input, Vuser_init_file, Vvalues) (force_load_messages, load_convert_to_unibyte) (load_dangerous_libraries, load_force_doc_strings) (load_in_progress): Remove. * keymap.c (syms_of_keymap): Update. (Vdefine_key_rebound_commands, Vemulation_mode_map_alists) (Vminibuffer_local_completion_map) (Vminibuffer_local_filename_completion_map) (Vminibuffer_local_filename_must_match_map) (Vminibuffer_local_map, Vminibuffer_local_must_match_map) (Vminibuffer_local_ns_map, Vminor_mode_map_alist) (Vminor_mode_overriding_map_alist, Vwhere_is_preferred_modifier): Remove. * keyboard.c (syms_of_keyboard): Update. (Vauto_save_timeout, Vcommand_error_function) (Vcommand_hook_internal, Vdeactivate_mark) (Vdeferred_action_function, Vdeferred_action_list) (Vdisable_point_adjustment, Vdouble_click_time) (Vecho_keystrokes, Venable_disabled_menus_and_buttons) (Vfunction_key_map, Vglobal_disable_point_adjustment) (Vhelp_char, Vhelp_event_list, Vhelp_form) (Vinput_method_function, Vinput_method_previous_message) (Vkey_translation_map, Vlast_event_frame) (Vlucid_menu_bar_dirty_flag, Vmenu_bar_final_items) (Vminibuffer_message_timeout, Voverriding_local_map) (Voverriding_local_map_menu_flag, Vpost_command_hook) (Vpre_command_hook, Vprefix_help_command) (Vsaved_region_selection, Vselect_active_regions) (Vshow_help_function, Vspecial_event_map, Vsuggest_key_bindings) (Vthis_command, Vthis_command_keys_shift_translated) (Vthis_original_command, Vthrow_on_input, Vtimer_idle_list) (Vtimer_list, Vtool_bar_separator_image_expression, Vtop_level) (Vtty_erase_char, Vunread_command_events) (Vunread_input_method_events, Vunread_post_input_method_events) (auto_save_interval, cannot_suspend, do_mouse_tracking) (double_click_fuzz, extra_keyboard_modifiers) (inhibit_local_menu_bar_menus, last_command_event) (last_input_event, last_nonmenu_event, menu_prompt_more_char) (menu_prompting, meta_prefix_char, num_input_keys) (num_nonmacro_input_events, polling_period, unread_command_char): Remove. * insdel.c (syms_of_insdel): Update. (Vcombine_after_change_calls, check_markers_debug_flag): Remove. * indent.c (syms_of_indent): Update. (indent_tabs_mode): Remove. * image.c (syms_of_image): Update. (Vimage_cache_eviction_delay, Vimage_types) (Vimagemagick_render_type, Vmax_image_size, Vx_bitmap_file_path) (cross_disabled_images): Remove. * fringe.c (syms_of_fringe): Update. (Vfringe_bitmaps, Voverflow_newline_into_fringe): Remove. * frame.c (syms_of_frame): Update. (Vdefault_frame_alist, Vdefault_frame_scroll_bars) (Vdelete_frame_functions, Vframe_alpha_lower_limit) (Vmake_pointer_invisible, Vmenu_bar_mode, Vmouse_highlight) (Vmouse_position_function, Vterminal_frame, Vtool_bar_mode) (Vx_resource_class, Vx_resource_name, focus_follows_mouse): Remove. * fontset.c (syms_of_fontset): Update. (Valternate_fontname_alist, Vfont_encoding_charset_alist) (Vfontset_alias_alist, Vignore_relative_composition) (Votf_script_alist, Vuse_default_ascent) (Vvertical_centering_font_regexp): Remove. * font.c (syms_of_font): Update. (Vfont_encoding_alist, Vfont_log, Vfont_slant_table) (Vfont_weight_table, Vfont_width_table): Remove. * fns.c (syms_of_fns): Update. (Vfeatures, use_dialog_box, use_file_dialog): Remove. * filelock.c (syms_of_filelock): Update. (Vtemporary_file_directory): Remove. * fileio.c (syms_of_fileio): Update. (Vafter_insert_file_functions, Vauto_save_include_big_deletions) (Vauto_save_list_file_name, Vauto_save_visited_file_name) (Vdefault_file_name_coding_system, Vfile_name_coding_system) (Vfile_name_handler_alist, Vinhibit_file_name_handlers) (Vinhibit_file_name_operation, Vset_auto_coding_function) (Vwrite_region_annotate_functions) (Vwrite_region_annotations_so_far) (Vwrite_region_post_annotation_function) (delete_by_moving_to_trash, write_region_inhibit_fsync): Remove. (Vw32_get_true_file_attributes): Remove declaration. * eval.c (syms_of_eval): Update. (Vdebug_ignored_errors, Vdebug_on_error, Vdebug_on_signal) (Vdebugger, Vinhibit_quit, Vmacro_declaration_function) (Vquit_flag, Vsignal_hook_function, Vstack_trace_on_error) (debug_on_next_call, debug_on_quit, debugger_may_continue) (max_lisp_eval_depth, max_specpdl_size): Remove. * emacs.c (syms_of_emacs): Update. (Vafter_init_time, Vbefore_init_time, Vcommand_line_args) (Vdynamic_library_alist, Vemacs_copyright, Vemacs_version) (Vinstallation_directory, Vinvocation_directory) (Vinvocation_name, Vkill_emacs_hook, Vpath_separator) (Vprevious_system_messages_locale, Vprevious_system_time_locale) (Vsystem_configuration, Vsystem_configuration_options) (Vsystem_messages_locale, Vsystem_time_locale, Vsystem_type) (inhibit_x_resources, noninteractive1): Remove. * editfns.c (syms_of_editfns): Update. (Vbuffer_access_fontified_property) (Vbuffer_access_fontify_functions, Vinhibit_field_text_motion) (Voperating_system_release, Vsystem_name, Vuser_full_name) (Vuser_login_name, Vuser_real_login_name): Remove. * dosfns.c (syms_of_dosfns): Update. (Vdos_display_scancodes, Vdos_version, Vdos_windows_version) (dos_codepage, dos_country_code, dos_decimal_point) (dos_hyper_key, dos_keyboard_layout, dos_keypad_mode) (dos_super_key, dos_timezone_offset): Remove. * doc.c (syms_of_doc): Update. (Vbuild_files, Vdoc_file_name): Remove. * dispnew.c (syms_of_display): Update. (Vglyph_table, Vinitial_window_system) (Vredisplay_preemption_period, Vstandard_display_table) (Vwindow_system_version, baud_rate, cursor_in_echo_area) (inverse_video, redisplay_dont_pause, visible_bell): Remove. * dired.c (syms_of_dired): Update. (Vcompletion_ignored_extensions): Remove. (Vw32_get_true_file_attributes): Remove declaration. * dbusbind.c (syms_of_dbusbind): Update. (Vdbus_debug, Vdbus_registered_buses) (Vdbus_registered_objects_table): Remove. * data.c (syms_of_data): Update. (Vmost_negative_fixnum, Vmost_positive_fixnum): Remove. * composite.c (syms_of_composite): Update. (Vauto_composition_function, Vauto_composition_mode) (Vcompose_chars_after_function, Vcomposition_function_table): Remove. * coding.c (syms_of_coding): Update. (Vcharset_revision_table, Vcoding_category_list) (Vcoding_system_alist, Vcoding_system_for_read) (Vcoding_system_for_write, Vcoding_system_list) (Vdefault_process_coding_system, Venable_character_translation) (Vfile_coding_system_alist, Vlast_code_conversion_error) (Vlast_coding_system_used, Vlatin_extra_code_table) (Vlocale_coding_system, Vnetwork_coding_system_alist) (Vprocess_coding_system_alist) (Vselect_safe_coding_system_function) (Vstandard_translation_table_for_decode) (Vstandard_translation_table_for_encode) (Vtranslation_table_for_input, coding_system_require_warning) (eol_mnemonic_dos, eol_mnemonic_mac, eol_mnemonic_undecided) (eol_mnemonic_unix, inherit_process_coding_system) (inhibit_eol_conversion, inhibit_iso_escape_detection) (inhibit_null_byte_detection): Remove. * cmds.c (syms_of_cmds): Update. (Vpost_self_insert_hook): Remove. * charset.c (syms_of_charset): Update. (Vcharset_list, Vcharset_map_path, Vcurrent_iso639_language) (inhibit_load_charset_map): Remove. * character.c (syms_of_character): Update. (Vauto_fill_chars, Vchar_direction_table, Vchar_script_table) (Vchar_width_table, Vprintable_chars) (Vscript_representative_chars, Vtranslation_table_vector) (Vunicode_category_table): Remove. * ccl.c (syms_of_ccl): Update. (Vcode_conversion_map_vector, Vfont_ccl_encoder_alist) (Vtranslation_hash_table_vector): Remove. * category.c (syms_of_category): Update. (Vword_combining_categories, Vword_separating_categories): Remove. * callproc.c (syms_of_callproc): Update. (Vconfigure_info_directory, Vdata_directory, Vdoc_directory) (Vexec_directory, Vexec_path, Vexec_suffixes) (Vinitial_environment, Vprocess_environment) (Vshared_game_score_directory, Vshell_file_name): Remove. * callint.c (syms_of_callint): Update. (Vcommand_debug_status, Vcommand_history, Vcurrent_prefix_arg) (Vmark_even_if_inactive, Vmouse_leave_buffer_hook): Remove. * bytecode.c (syms_of_bytecode): Update. (Vbyte_code_meter, byte_metering_on): Remove. * buffer.c (syms_of_buffer): Update. (Vafter_change_functions, Vbefore_change_functions) (Vchange_major_mode_hook, Vfirst_change_hook) (Vinhibit_read_only, Vkill_buffer_query_functions) (Vtransient_mark_mode, inhibit_modification_hooks): Remove. * alloc.c (syms_of_alloc): Update. (Vgc_cons_percentage, Vgc_elapsed, Vmemory_full) (Vmemory_signal_data, Vpost_gc_hook, Vpurify_flag) (cons_cells_consed, floats_consed, garbage_collection_messages) (gc_cons_threshold, gcs_done, intervals_consed) (misc_objects_consed, pure_bytes_used, string_chars_consed) (strings_consed, symbols_consed, vector_cells_consed): Remove. * lisp.h (DEFVAR_LISP, DEFVAR_LISP_NOPRO, DEFVAR_BOOL) (DEFVAR_INT): Assume global is in `globals'. * alloc.c (globals): Define.
author Tom Tromey <tromey@redhat.com>
date Tue, 18 Jan 2011 13:45:37 -0700
parents ef719132ddfa
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
92061
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
1 ;;; json.el --- JavaScript Object Notation parser / generator
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
2
112218
376148b31b5e Add 2011 to FSF/AIST copyright years.
Glenn Morris <rgm@gnu.org>
parents: 107517
diff changeset
3 ;; Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
92061
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
4
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
5 ;; Author: Edward O'Connor <ted@oconnor.cx>
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
6 ;; Version: 1.2
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
7 ;; Keywords: convenience
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
8
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
9 ;; This file is part of GNU Emacs.
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
10
94678
ee5932bf781d Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents: 92123
diff changeset
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
92061
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
12 ;; it under the terms of the GNU General Public License as published by
94678
ee5932bf781d Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents: 92123
diff changeset
13 ;; the Free Software Foundation, either version 3 of the License, or
ee5932bf781d Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents: 92123
diff changeset
14 ;; (at your option) any later version.
92061
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
15
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
16 ;; GNU Emacs is distributed in the hope that it will be useful,
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
19 ;; GNU General Public License for more details.
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
20
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
21 ;; You should have received a copy of the GNU General Public License
94678
ee5932bf781d Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents: 92123
diff changeset
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
92061
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
23
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
24 ;;; Commentary:
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
25
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
26 ;; This is a library for parsing and generating JSON (JavaScript Object
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
27 ;; Notation).
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
28
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
29 ;; Learn all about JSON here: <URL:http://json.org/>.
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
30
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
31 ;; The user-serviceable entry points for the parser are the functions
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
32 ;; `json-read' and `json-read-from-string'. The encoder has a single
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
33 ;; entry point, `json-encode'.
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
34
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
35 ;; Since there are several natural representations of key-value pair
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
36 ;; mappings in elisp (alist, plist, hash-table), `json-read' allows you
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
37 ;; to specify which you'd prefer (see `json-object-type' and
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
38 ;; `json-array-type').
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
39
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
40 ;; Similarly, since `false' and `null' are distinct in JSON, you can
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
41 ;; distinguish them by binding `json-false' and `json-null' as desired.
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
42
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
43 ;;; History:
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
44
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
45 ;; 2006-03-11 - Initial version.
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
46 ;; 2006-03-13 - Added JSON generation in addition to parsing. Various
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
47 ;; other cleanups, bugfixes, and improvements.
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
48 ;; 2006-12-29 - XEmacs support, from Aidan Kehoe <kehoea@parhasard.net>.
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
49 ;; 2008-02-21 - Installed in GNU Emacs.
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
50
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
51 ;;; Code:
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
52
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
53 (eval-when-compile (require 'cl))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
54
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
55 ;; Compatibility code
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
56
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
57 (defalias 'json-encode-char0 'encode-char)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
58 (defalias 'json-decode-char0 'decode-char)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
59
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
60
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
61 ;; Parameters
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
62
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
63 (defvar json-object-type 'alist
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
64 "Type to convert JSON objects to.
107517
ed16fdd2685a Fix typos in docstrings.
Juanma Barranquero <lekktu@gmail.com>
parents: 106815
diff changeset
65 Must be one of `alist', `plist', or `hash-table'. Consider let-binding
92061
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
66 this around your call to `json-read' instead of `setq'ing it.")
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
67
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
68 (defvar json-array-type 'vector
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
69 "Type to convert JSON arrays to.
107517
ed16fdd2685a Fix typos in docstrings.
Juanma Barranquero <lekktu@gmail.com>
parents: 106815
diff changeset
70 Must be one of `vector' or `list'. Consider let-binding this around
92061
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
71 your call to `json-read' instead of `setq'ing it.")
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
72
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
73 (defvar json-key-type nil
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
74 "Type to convert JSON keys to.
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
75 Must be one of `string', `symbol', `keyword', or nil.
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
76
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
77 If nil, `json-read' will guess the type based on the value of
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
78 `json-object-type':
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
79
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
80 If `json-object-type' is: nil will be interpreted as:
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
81 `hash-table' `string'
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
82 `alist' `symbol'
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
83 `plist' `keyword'
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
84
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
85 Note that values other than `string' might behave strangely for
107517
ed16fdd2685a Fix typos in docstrings.
Juanma Barranquero <lekktu@gmail.com>
parents: 106815
diff changeset
86 Sufficiently Weird keys. Consider let-binding this around your call to
92061
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
87 `json-read' instead of `setq'ing it.")
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
88
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
89 (defvar json-false :json-false
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
90 "Value to use when reading JSON `false'.
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
91 If this has the same value as `json-null', you might not be able to tell
107517
ed16fdd2685a Fix typos in docstrings.
Juanma Barranquero <lekktu@gmail.com>
parents: 106815
diff changeset
92 the difference between `false' and `null'. Consider let-binding this
92061
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
93 around your call to `json-read' instead of `setq'ing it.")
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
94
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
95 (defvar json-null nil
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
96 "Value to use when reading JSON `null'.
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
97 If this has the same value as `json-false', you might not be able to
107517
ed16fdd2685a Fix typos in docstrings.
Juanma Barranquero <lekktu@gmail.com>
parents: 106815
diff changeset
98 tell the difference between `false' and `null'. Consider let-binding
92061
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
99 this around your call to `json-read' instead of `setq'ing it.")
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
100
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
101
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
102
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
103 ;;; Utilities
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
104
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
105 (defun json-join (strings separator)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
106 "Join STRINGS with SEPARATOR."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
107 (mapconcat 'identity strings separator))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
108
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
109 (defun json-alist-p (list)
94735
2b39e9f51e54 (json-alist-p, json-plist-p): Don't use `iff' in docstrings.
Juanma Barranquero <lekktu@gmail.com>
parents: 94678
diff changeset
110 "Non-null if and only if LIST is an alist."
92061
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
111 (or (null list)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
112 (and (consp (car list))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
113 (json-alist-p (cdr list)))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
114
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
115 (defun json-plist-p (list)
94735
2b39e9f51e54 (json-alist-p, json-plist-p): Don't use `iff' in docstrings.
Juanma Barranquero <lekktu@gmail.com>
parents: 94678
diff changeset
116 "Non-null if and only if LIST is a plist."
92061
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
117 (or (null list)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
118 (and (keywordp (car list))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
119 (consp (cdr list))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
120 (json-plist-p (cddr list)))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
121
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
122 ;; Reader utilities
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
123
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
124 (defsubst json-advance (&optional n)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
125 "Skip past the following N characters."
98553
4b085b844163 (json-advance): Use forward-char.
Chong Yidong <cyd@stupidchicken.com>
parents: 97796
diff changeset
126 (forward-char n))
92061
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
127
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
128 (defsubst json-peek ()
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
129 "Return the character at point."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
130 (let ((char (char-after (point))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
131 (or char :json-eof)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
132
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
133 (defsubst json-pop ()
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
134 "Advance past the character at point, returning it."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
135 (let ((char (json-peek)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
136 (if (eq char :json-eof)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
137 (signal 'end-of-file nil)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
138 (json-advance)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
139 char)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
140
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
141 (defun json-skip-whitespace ()
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
142 "Skip past the whitespace at point."
98572
e26dedf05927 (json-skip-whitespace): Fix last change.
Chong Yidong <cyd@stupidchicken.com>
parents: 98553
diff changeset
143 (skip-chars-forward "\t\r\n\f\b "))
92061
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
144
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
145
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
146
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
147 ;; Error conditions
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
148
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
149 (put 'json-error 'error-message "Unknown JSON error")
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
150 (put 'json-error 'error-conditions '(json-error error))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
151
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
152 (put 'json-readtable-error 'error-message "JSON readtable error")
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
153 (put 'json-readtable-error 'error-conditions
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
154 '(json-readtable-error json-error error))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
155
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
156 (put 'json-unknown-keyword 'error-message "Unrecognized keyword")
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
157 (put 'json-unknown-keyword 'error-conditions
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
158 '(json-unknown-keyword json-error error))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
159
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
160 (put 'json-number-format 'error-message "Invalid number format")
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
161 (put 'json-number-format 'error-conditions
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
162 '(json-number-format json-error error))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
163
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
164 (put 'json-string-escape 'error-message "Bad unicode escape")
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
165 (put 'json-string-escape 'error-conditions
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
166 '(json-string-escape json-error error))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
167
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
168 (put 'json-string-format 'error-message "Bad string format")
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
169 (put 'json-string-format 'error-conditions
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
170 '(json-string-format json-error error))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
171
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
172 (put 'json-object-format 'error-message "Bad JSON object")
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
173 (put 'json-object-format 'error-conditions
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
174 '(json-object-format json-error error))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
175
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
176
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
177
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
178 ;;; Keywords
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
179
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
180 (defvar json-keywords '("true" "false" "null")
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
181 "List of JSON keywords.")
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
182
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
183 ;; Keyword parsing
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
184
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
185 (defun json-read-keyword (keyword)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
186 "Read a JSON keyword at point.
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
187 KEYWORD is the keyword expected."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
188 (unless (member keyword json-keywords)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
189 (signal 'json-unknown-keyword (list keyword)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
190 (mapc (lambda (char)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
191 (unless (char-equal char (json-peek))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
192 (signal 'json-unknown-keyword
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
193 (list (save-excursion
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
194 (backward-word 1)
92086
23ee465a6963 (top-level): No need to require thingatpt.
Glenn Morris <rgm@gnu.org>
parents: 92061
diff changeset
195 (thing-at-point 'word)))))
92061
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
196 (json-advance))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
197 keyword)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
198 (unless (looking-at "\\(\\s-\\|[],}]\\|$\\)")
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
199 (signal 'json-unknown-keyword
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
200 (list (save-excursion
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
201 (backward-word 1)
92086
23ee465a6963 (top-level): No need to require thingatpt.
Glenn Morris <rgm@gnu.org>
parents: 92061
diff changeset
202 (thing-at-point 'word)))))
92061
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
203 (cond ((string-equal keyword "true") t)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
204 ((string-equal keyword "false") json-false)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
205 ((string-equal keyword "null") json-null)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
206
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
207 ;; Keyword encoding
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
208
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
209 (defun json-encode-keyword (keyword)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
210 "Encode KEYWORD as a JSON value."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
211 (cond ((eq keyword t) "true")
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
212 ((eq keyword json-false) "false")
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
213 ((eq keyword json-null) "null")))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
214
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
215 ;;; Numbers
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
216
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
217 ;; Number parsing
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
218
97796
5d474cdb19f8 (json-read-number): New arg. Handle explicitly signed numbers.
Chong Yidong <cyd@stupidchicken.com>
parents: 94735
diff changeset
219 (defun json-read-number (&optional sign)
5d474cdb19f8 (json-read-number): New arg. Handle explicitly signed numbers.
Chong Yidong <cyd@stupidchicken.com>
parents: 94735
diff changeset
220 "Read the JSON number following point.
107517
ed16fdd2685a Fix typos in docstrings.
Juanma Barranquero <lekktu@gmail.com>
parents: 106815
diff changeset
221 The optional SIGN argument is for internal use.
97796
5d474cdb19f8 (json-read-number): New arg. Handle explicitly signed numbers.
Chong Yidong <cyd@stupidchicken.com>
parents: 94735
diff changeset
222
92061
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
223 N.B.: Only numbers which can fit in Emacs Lisp's native number
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
224 representation will be parsed correctly."
97796
5d474cdb19f8 (json-read-number): New arg. Handle explicitly signed numbers.
Chong Yidong <cyd@stupidchicken.com>
parents: 94735
diff changeset
225 ;; If SIGN is non-nil, the number is explicitly signed.
5d474cdb19f8 (json-read-number): New arg. Handle explicitly signed numbers.
Chong Yidong <cyd@stupidchicken.com>
parents: 94735
diff changeset
226 (let ((number-regexp
5d474cdb19f8 (json-read-number): New arg. Handle explicitly signed numbers.
Chong Yidong <cyd@stupidchicken.com>
parents: 94735
diff changeset
227 "\\([0-9]+\\)?\\(\\.[0-9]+\\)?\\([Ee][+-]?[0-9]+\\)?"))
5d474cdb19f8 (json-read-number): New arg. Handle explicitly signed numbers.
Chong Yidong <cyd@stupidchicken.com>
parents: 94735
diff changeset
228 (cond ((and (null sign) (char-equal (json-peek) ?-))
5d474cdb19f8 (json-read-number): New arg. Handle explicitly signed numbers.
Chong Yidong <cyd@stupidchicken.com>
parents: 94735
diff changeset
229 (json-advance)
5d474cdb19f8 (json-read-number): New arg. Handle explicitly signed numbers.
Chong Yidong <cyd@stupidchicken.com>
parents: 94735
diff changeset
230 (- (json-read-number t)))
5d474cdb19f8 (json-read-number): New arg. Handle explicitly signed numbers.
Chong Yidong <cyd@stupidchicken.com>
parents: 94735
diff changeset
231 ((and (null sign) (char-equal (json-peek) ?+))
5d474cdb19f8 (json-read-number): New arg. Handle explicitly signed numbers.
Chong Yidong <cyd@stupidchicken.com>
parents: 94735
diff changeset
232 (json-advance)
5d474cdb19f8 (json-read-number): New arg. Handle explicitly signed numbers.
Chong Yidong <cyd@stupidchicken.com>
parents: 94735
diff changeset
233 (json-read-number t))
5d474cdb19f8 (json-read-number): New arg. Handle explicitly signed numbers.
Chong Yidong <cyd@stupidchicken.com>
parents: 94735
diff changeset
234 ((and (looking-at number-regexp)
5d474cdb19f8 (json-read-number): New arg. Handle explicitly signed numbers.
Chong Yidong <cyd@stupidchicken.com>
parents: 94735
diff changeset
235 (or (match-beginning 1)
5d474cdb19f8 (json-read-number): New arg. Handle explicitly signed numbers.
Chong Yidong <cyd@stupidchicken.com>
parents: 94735
diff changeset
236 (match-beginning 2)))
92061
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
237 (goto-char (match-end 0))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
238 (string-to-number (match-string 0)))
97796
5d474cdb19f8 (json-read-number): New arg. Handle explicitly signed numbers.
Chong Yidong <cyd@stupidchicken.com>
parents: 94735
diff changeset
239 (t (signal 'json-number-format (list (point)))))))
92061
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
240
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
241 ;; Number encoding
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
242
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
243 (defun json-encode-number (number)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
244 "Return a JSON representation of NUMBER."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
245 (format "%s" number))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
246
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
247 ;;; Strings
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
248
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
249 (defvar json-special-chars
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
250 '((?\" . ?\")
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
251 (?\\ . ?\\)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
252 (?/ . ?/)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
253 (?b . ?\b)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
254 (?f . ?\f)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
255 (?n . ?\n)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
256 (?r . ?\r)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
257 (?t . ?\t))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
258 "Characters which are escaped in JSON, with their elisp counterparts.")
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
259
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
260 ;; String parsing
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
261
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
262 (defun json-read-escaped-char ()
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
263 "Read the JSON string escaped character at point."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
264 ;; Skip over the '\'
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
265 (json-advance)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
266 (let* ((char (json-pop))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
267 (special (assq char json-special-chars)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
268 (cond
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
269 (special (cdr special))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
270 ((not (eq char ?u)) char)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
271 ((looking-at "[0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f]")
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
272 (let ((hex (match-string 0)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
273 (json-advance 4)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
274 (json-decode-char0 'ucs (string-to-number hex 16))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
275 (t
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
276 (signal 'json-string-escape (list (point)))))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
277
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
278 (defun json-read-string ()
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
279 "Read the JSON string at point."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
280 (unless (char-equal (json-peek) ?\")
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
281 (signal 'json-string-format (list "doesn't start with '\"'!")))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
282 ;; Skip over the '"'
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
283 (json-advance)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
284 (let ((characters '())
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
285 (char (json-peek)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
286 (while (not (char-equal char ?\"))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
287 (push (if (char-equal char ?\\)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
288 (json-read-escaped-char)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
289 (json-pop))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
290 characters)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
291 (setq char (json-peek)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
292 ;; Skip over the '"'
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
293 (json-advance)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
294 (if characters
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
295 (apply 'string (nreverse characters))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
296 "")))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
297
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
298 ;; String encoding
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
299
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
300 (defun json-encode-char (char)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
301 "Encode CHAR as a JSON string."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
302 (setq char (json-encode-char0 char 'ucs))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
303 (let ((control-char (car (rassoc char json-special-chars))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
304 (cond
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
305 ;; Special JSON character (\n, \r, etc.)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
306 (control-char
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
307 (format "\\%c" control-char))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
308 ;; ASCIIish printable character
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
309 ((and (> char 31) (< char 161))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
310 (format "%c" char))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
311 ;; Fallback: UCS code point in \uNNNN form
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
312 (t
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
313 (format "\\u%04x" char)))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
314
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
315 (defun json-encode-string (string)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
316 "Return a JSON representation of STRING."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
317 (format "\"%s\"" (mapconcat 'json-encode-char string "")))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
318
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
319 ;;; JSON Objects
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
320
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
321 (defun json-new-object ()
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
322 "Create a new Elisp object corresponding to a JSON object.
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
323 Please see the documentation of `json-object-type'."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
324 (cond ((eq json-object-type 'hash-table)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
325 (make-hash-table :test 'equal))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
326 (t
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
327 (list))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
328
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
329 (defun json-add-to-object (object key value)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
330 "Add a new KEY -> VALUE association to OBJECT.
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
331 Returns the updated object, which you should save, e.g.:
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
332 (setq obj (json-add-to-object obj \"foo\" \"bar\"))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
333 Please see the documentation of `json-object-type' and `json-key-type'."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
334 (let ((json-key-type
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
335 (if (eq json-key-type nil)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
336 (cdr (assq json-object-type '((hash-table . string)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
337 (alist . symbol)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
338 (plist . keyword))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
339 json-key-type)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
340 (setq key
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
341 (cond ((eq json-key-type 'string)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
342 key)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
343 ((eq json-key-type 'symbol)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
344 (intern key))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
345 ((eq json-key-type 'keyword)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
346 (intern (concat ":" key)))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
347 (cond ((eq json-object-type 'hash-table)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
348 (puthash key value object)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
349 object)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
350 ((eq json-object-type 'alist)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
351 (cons (cons key value) object))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
352 ((eq json-object-type 'plist)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
353 (cons key (cons value object))))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
354
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
355 ;; JSON object parsing
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
356
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
357 (defun json-read-object ()
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
358 "Read the JSON object at point."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
359 ;; Skip over the "{"
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
360 (json-advance)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
361 (json-skip-whitespace)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
362 ;; read key/value pairs until "}"
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
363 (let ((elements (json-new-object))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
364 key value)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
365 (while (not (char-equal (json-peek) ?}))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
366 (json-skip-whitespace)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
367 (setq key (json-read-string))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
368 (json-skip-whitespace)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
369 (if (char-equal (json-peek) ?:)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
370 (json-advance)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
371 (signal 'json-object-format (list ":" (json-peek))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
372 (setq value (json-read))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
373 (setq elements (json-add-to-object elements key value))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
374 (json-skip-whitespace)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
375 (unless (char-equal (json-peek) ?})
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
376 (if (char-equal (json-peek) ?,)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
377 (json-advance)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
378 (signal 'json-object-format (list "," (json-peek))))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
379 ;; Skip over the "}"
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
380 (json-advance)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
381 elements))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
382
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
383 ;; Hash table encoding
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
384
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
385 (defun json-encode-hash-table (hash-table)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
386 "Return a JSON representation of HASH-TABLE."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
387 (format "{%s}"
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
388 (json-join
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
389 (let (r)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
390 (maphash
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
391 (lambda (k v)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
392 (push (format "%s:%s"
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
393 (json-encode k)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
394 (json-encode v))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
395 r))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
396 hash-table)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
397 r)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
398 ", ")))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
399
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
400 ;; List encoding (including alists and plists)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
401
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
402 (defun json-encode-alist (alist)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
403 "Return a JSON representation of ALIST."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
404 (format "{%s}"
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
405 (json-join (mapcar (lambda (cons)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
406 (format "%s:%s"
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
407 (json-encode (car cons))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
408 (json-encode (cdr cons))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
409 alist)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
410 ", ")))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
411
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
412 (defun json-encode-plist (plist)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
413 "Return a JSON representation of PLIST."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
414 (let (result)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
415 (while plist
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
416 (push (concat (json-encode (car plist))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
417 ":"
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
418 (json-encode (cadr plist)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
419 result)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
420 (setq plist (cddr plist)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
421 (concat "{" (json-join (nreverse result) ", ") "}")))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
422
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
423 (defun json-encode-list (list)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
424 "Return a JSON representation of LIST.
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
425 Tries to DWIM: simple lists become JSON arrays, while alists and plists
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
426 become JSON objects."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
427 (cond ((null list) "null")
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
428 ((json-alist-p list) (json-encode-alist list))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
429 ((json-plist-p list) (json-encode-plist list))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
430 ((listp list) (json-encode-array list))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
431 (t
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
432 (signal 'json-error (list list)))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
433
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
434 ;;; Arrays
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
435
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
436 ;; Array parsing
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
437
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
438 (defun json-read-array ()
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
439 "Read the JSON array at point."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
440 ;; Skip over the "["
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
441 (json-advance)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
442 (json-skip-whitespace)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
443 ;; read values until "]"
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
444 (let (elements)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
445 (while (not (char-equal (json-peek) ?\]))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
446 (push (json-read) elements)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
447 (json-skip-whitespace)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
448 (unless (char-equal (json-peek) ?\])
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
449 (if (char-equal (json-peek) ?,)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
450 (json-advance)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
451 (signal 'json-error (list 'bleah)))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
452 ;; Skip over the "]"
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
453 (json-advance)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
454 (apply json-array-type (nreverse elements))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
455
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
456 ;; Array encoding
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
457
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
458 (defun json-encode-array (array)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
459 "Return a JSON representation of ARRAY."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
460 (concat "[" (mapconcat 'json-encode array ", ") "]"))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
461
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
462
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
463
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
464 ;;; JSON reader.
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
465
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
466 (defvar json-readtable
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
467 (let ((table
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
468 '((?t json-read-keyword "true")
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
469 (?f json-read-keyword "false")
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
470 (?n json-read-keyword "null")
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
471 (?{ json-read-object)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
472 (?\[ json-read-array)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
473 (?\" json-read-string))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
474 (mapc (lambda (char)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
475 (push (list char 'json-read-number) table))
97796
5d474cdb19f8 (json-read-number): New arg. Handle explicitly signed numbers.
Chong Yidong <cyd@stupidchicken.com>
parents: 94735
diff changeset
476 '(?- ?+ ?. ?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9))
92061
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
477 table)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
478 "Readtable for JSON reader.")
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
479
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
480 (defun json-read ()
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
481 "Parse and return the JSON object following point.
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
482 Advances point just past JSON object."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
483 (json-skip-whitespace)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
484 (let ((char (json-peek)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
485 (if (not (eq char :json-eof))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
486 (let ((record (cdr (assq char json-readtable))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
487 (if (functionp (car record))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
488 (apply (car record) (cdr record))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
489 (signal 'json-readtable-error record)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
490 (signal 'end-of-file nil))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
491
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
492 ;; Syntactic sugar for the reader
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
493
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
494 (defun json-read-from-string (string)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
495 "Read the JSON object contained in STRING and return it."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
496 (with-temp-buffer
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
497 (insert string)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
498 (goto-char (point-min))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
499 (json-read)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
500
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
501 (defun json-read-file (file)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
502 "Read the first JSON object contained in FILE and return it."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
503 (with-temp-buffer
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
504 (insert-file-contents file)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
505 (goto-char (point-min))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
506 (json-read)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
507
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
508
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
509
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
510 ;;; JSON encoder
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
511
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
512 (defun json-encode (object)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
513 "Return a JSON representation of OBJECT as a string."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
514 (cond ((memq object (list t json-null json-false))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
515 (json-encode-keyword object))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
516 ((stringp object) (json-encode-string object))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
517 ((keywordp object) (json-encode-string
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
518 (substring (symbol-name object) 1)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
519 ((symbolp object) (json-encode-string
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
520 (symbol-name object)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
521 ((numberp object) (json-encode-number object))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
522 ((arrayp object) (json-encode-array object))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
523 ((hash-table-p object) (json-encode-hash-table object))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
524 ((listp object) (json-encode-list object))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
525 (t (signal 'json-error (list object)))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
526
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
527 (provide 'json)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
528
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
529 ;;; json.el ends here