Skip to main content

Json

Struct Json 

Source
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

Source

pub fn from_str(json: &str) -> Result<Self, JsonError>

Create a JSON renderable from a JSON string.

§Arguments
  • json - A valid JSON string
§Returns

Returns a Json instance or a JsonError if parsing fails.

Source

pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self, JsonError>

Create a JSON renderable from a file path.

§Arguments
  • path - Path to a JSON file
§Returns

Returns a Json instance or a JsonError if reading/parsing fails.

§Example
use fast_rich::json::Json;
let json = Json::from_file("config.json").unwrap();
Source

pub fn from_data<T: Serialize>(data: &T) -> Result<Self, JsonError>

Create a JSON renderable from serializable data.

§Arguments
  • data - Any type implementing Serialize
§Returns

Returns a Json instance or a JsonError if serialization fails.

Source

pub fn from_str_with_options( json: &str, options: JsonOptions, ) -> Result<Self, JsonError>

Create a JSON renderable with custom options.

Source

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);
Source

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");
Source

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}
Source

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"
Source

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"
Source

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();
Source

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();
Source

pub fn plain_text(&self) -> String

Get the plain text content (useful for testing).

Trait Implementations§

Source§

impl Clone for Json

Source§

fn clone(&self) -> Json

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Json

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Renderable for Json

Source§

fn render(&self, context: &RenderContext) -> Vec<Segment>

Render this object to a sequence of segments. Read more
Source§

fn min_width(&self) -> usize

Get the minimum width required to render this object.
Source§

fn max_width(&self) -> usize

Get the maximum/natural width of this object.

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Measurable for T
where T: Renderable,

Source§

fn measure(&self, width: usize) -> Measurement

Measure this renderable at the given width.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.