egui_json_tree/
lib.rs

1//! An interactive JSON tree visualiser for [`egui`](https://github.com/emilk/egui), with search and highlight functionality.
2//!
3//! See the demo [source code](https://github.com/dmackdev/egui_json_tree/blob/main/demo) and [webpage](https://dmackdev.github.io/egui_json_tree) for detailed use cases, including:
4//! - Automatic expansion of arrays/objects and highlighting, based on search term matches.
5//! - Copying JSON paths and values to the clipboard.
6//! - A JSON editor UI.
7//!
8//! # Usage
9//! ```rust
10//! # use egui::{Color32};
11//! # use egui_json_tree::{
12//! #   render::{
13//! #       DefaultRender, RenderBaseValueContext, RenderContext, RenderExpandableDelimiterContext,
14//! #       RenderPropertyContext,
15//! #   },
16//! #   DefaultExpand, JsonTree, JsonTreeStyle, JsonTreeVisuals, ToggleButtonsState
17//! # };
18//! # egui::__run_test_ui(|ui| {
19//! let value = serde_json::json!({ "foo": "bar", "fizz": [1, 2, 3]});
20//!
21//! // Simple:
22//! JsonTree::new("simple-tree", &value).show(ui);
23//!
24//! // Customised:
25//! let response = JsonTree::new("customised-tree", &value)
26//!     .style(
27//!       JsonTreeStyle::new()
28//!           .abbreviate_root(true) // Show {...} when the root object is collapsed.
29//!           .toggle_buttons_state(ToggleButtonsState::VisibleDisabled)
30//!           .visuals(JsonTreeVisuals {
31//!               bool_color: Color32::YELLOW,
32//!               ..Default::default()
33//!           }),
34//!     )
35//!     .default_expand(DefaultExpand::All)  // Expand all arrays/object by default.
36//!     .on_render(|ui, ctx| {
37//!         // Customise rendering of the JsonTree, and/or handle interactions.
38//!         match ctx {
39//!             RenderContext::Property(ctx) => {
40//!                 ctx.render_default(ui).context_menu(|ui| {
41//!                     // Show a context menu when right clicking
42//!                     // an array index or object key.
43//!                 });
44//!             }
45//!             RenderContext::BaseValue(ctx) => {
46//!                 // Show a button after non-recursive JSON values.
47//!                 ctx.render_default(ui);
48//!                 if ui.small_button("+").clicked() {
49//!                     // ...
50//!                 }
51//!             }
52//!             RenderContext::ExpandableDelimiter(ctx) => {
53//!                 // Render array brackets and object braces as normal.
54//!                 ctx.render_default(ui);
55//!             }
56//!         };
57//!     })
58//!     .show(ui);
59//!
60//! // By default, the tree will expand/collapse all arrays/objects
61//! // to respect the `default_expand` setting when it changes.
62//! // If required, you can manually trigger this reset, e.g. after a button press:
63//! if ui.button("Reset").clicked() {
64//!   response.reset_expanded(ui);
65//! }
66//! # });
67//! ```
68//!
69//! # Supported JSON Types
70//!
71//! [`JsonTree`] can visualise any type that implements [`ToJsonTreeValue`](trait@value::ToJsonTreeValue).
72//! See the table of crate features below for provided implementations.
73//!
74//! | Feature/Dependency | JSON Type                 | Default |
75//! | ------------------ | ------------------------- | ------- |
76//! | `serde_json`       | `serde_json::Value`       | Yes     |
77//! | `simd_json`        | `simd_json::owned::Value` | No      |
78//!
79//! If you wish to use a different JSON type, see the [`value`](mod@value) module,
80//! and disable default features in your `Cargo.toml` if you do not need the `serde_json` dependency.
81mod default_expand;
82mod node;
83mod response;
84mod search;
85mod style;
86mod toggle_buttons_state;
87mod tree;
88
89pub mod delimiters;
90pub mod pointer;
91pub mod render;
92pub mod value;
93
94pub use default_expand::DefaultExpand;
95pub use response::JsonTreeResponse;
96pub use style::{
97    JsonTreeMaxWidth, JsonTreeStyle, JsonTreeVisuals, JsonTreeWrapping, JsonTreeWrappingConfig,
98};
99pub use toggle_buttons_state::ToggleButtonsState;
100pub use tree::JsonTree;