pub struct Json { /* private fields */ }Expand description
A renderable that pretty-prints JSON with syntax highlighting.
§Example
use fast_rich::json::Json;
// From a JSON string
let json = Json::from_str(r#"{"key": "value"}"#).unwrap();
// From serializable data
use serde::Serialize;
#[derive(Serialize)]
struct User { name: String, age: u32 }
let user = User { name: "Alice".into(), age: 30 };
let json = Json::from_data(&user).unwrap();
// With options
let json = Json::from_str(r#"{"z": 1, "a": 2}"#)
.unwrap()
.sort_keys()
.indent(4)
.ensure_ascii();
// Compact output
let json = Json::from_str(r#"{"a": 1}"#).unwrap().compact();
// Tab indentation
let json = Json::from_str(r#"{"a": 1}"#).unwrap().indent_with("\t");Implementations§
Source§impl Json
impl Json
Sourcepub fn from_str_with_options(
json: &str,
options: JsonOptions,
) -> Result<Self, JsonError>
pub fn from_str_with_options( json: &str, options: JsonOptions, ) -> Result<Self, JsonError>
Create a JSON renderable with custom options.
Sourcepub fn indent(self, spaces: usize) -> Self
pub fn indent(self, spaces: usize) -> Self
Set custom indentation (number of spaces).
Use 0 for compact output, or any positive number for that many spaces.
§Example
use fast_rich::json::Json;
let json = Json::from_str(r#"{"a": 1}"#).unwrap().indent(4);Sourcepub fn indent_with(self, indent_str: &str) -> Self
pub fn indent_with(self, indent_str: &str) -> Self
Set custom indentation using a string (e.g., tab).
§Example
use fast_rich::json::Json;
let json = Json::from_str(r#"{"a": 1}"#).unwrap().indent_with("\t");Sourcepub fn compact(self) -> Self
pub fn compact(self) -> Self
Output compact JSON (no indentation, single line).
§Example
use fast_rich::json::Json;
let json = Json::from_str(r#"{"a": 1, "b": 2}"#).unwrap().compact();
// Output: {"a":1,"b":2}Sourcepub fn sort_keys(self) -> Self
pub fn sort_keys(self) -> Self
Sort object keys alphabetically.
§Example
use fast_rich::json::Json;
let json = Json::from_str(r#"{"z": 1, "a": 2}"#).unwrap().sort_keys();
// Output will have keys in order: "a", "z"Sourcepub fn ensure_ascii(self) -> Self
pub fn ensure_ascii(self) -> Self
Escape all non-ASCII characters to \uXXXX format.
Useful when outputting to systems that don’t handle Unicode well.
§Example
use fast_rich::json::Json;
let json = Json::from_str(r#"{"emoji": "😀"}"#).unwrap().ensure_ascii();
// "😀" becomes "\ud83d\ude00"Sourcepub fn no_highlight(self) -> Self
pub fn no_highlight(self) -> Self
Disable syntax highlighting.
§Example
use fast_rich::json::Json;
let json = Json::from_str(r#"{"a": 1}"#).unwrap().no_highlight();Sourcepub fn wrap(self) -> Self
pub fn wrap(self) -> Self
Enable word wrapping (by default, wrapping is disabled).
§Example
use fast_rich::json::Json;
let json = Json::from_str(r#"{"a": 1}"#).unwrap().wrap();Sourcepub fn plain_text(&self) -> String
pub fn plain_text(&self) -> String
Get the plain text content (useful for testing).
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Json
impl RefUnwindSafe for Json
impl Send for Json
impl Sync for Json
impl Unpin for Json
impl UnsafeUnpin for Json
impl UnwindSafe for Json
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Measurable for Twhere
T: Renderable,
impl<T> Measurable for Twhere
T: Renderable,
Source§fn measure(&self, width: usize) -> Measurement
fn measure(&self, width: usize) -> Measurement
Measure this renderable at the given width.