Skip to content

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

json-tree-editor

json-tree-editor is a reusable Rust library for interactively editing serde_json::Value documents as Ratatui trees.

It provides:

  • Recursive object, array, and scalar editing.
  • Visible opening and closing tree rows with collapse state.
  • Cursor movement and selection over visible rows.
  • Inline key and value editing through backend-neutral semantic operations.
  • Adding, deleting, renaming, and replacing JSON members.
  • Duplicate object-key validation and reversible provisional insertions.
  • A configurable Ratatui stateful widget.

The crate deliberately leaves terminal events, key bindings, persistence, clipboard integration, process management, notifications, borders, and titles to the consuming application.

Usage

use json_tree_editor::{Editor, JsonTree, Theme};
use ratatui::widgets::Block;
use serde_json::json;

let mut editor = Editor::new(json!({
    "enabled": false,
    "items": [1, 2]
}));

editor.move_by(1);
editor.begin_value()?;
editor.set_text("true")?;
editor.confirm_value()?;

let widget = JsonTree::new()
    .theme(Theme::default())
    .block(Block::bordered().title("JSON"));

// Inside a Ratatui draw closure:
// frame.render_stateful_widget(widget, area, &mut editor);

let value = editor.to_value();
# Ok::<(), json_tree_editor::EditError>(())

Editor::value() and Editor::selected_value() return borrowed views. Use to_value() when an owned clone is needed, or into_value() to consume the editor without cloning.

Text input

Applications map their preferred event backend into operations such as:

# use json_tree_editor::Editor;
# use serde_json::json;
# let mut editor = Editor::new(json!("old"));
editor.begin_value()?;
editor.set_text("\"new\"")?;
editor.insert('!')?;
editor.backspace()?;
editor.newline()?;
editor.move_text_by(-1)?;
editor.delete_text()?;
editor.confirm_value()?;
# Ok::<(), json_tree_editor::EditError>(())

Navigation and collapse state do not mark the document dirty. JSON mutations do. Cancelling a provisional insertion restores both the prior document and the prior dirty state.

License

MIT

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages