Skip to main content

Json

Struct Json 

Source
pub struct Json { /* private fields */ }
Expand description

A parsed JSON document, rendered with syntax highlighting. Mirrors rich.json.JSON.

Like upstream, the document is re-serialized (json.dumps) with the JsonOptions and highlighted with JSONHighlighter when it is built; text is that highlighted Text.

Implementations§

Source§

impl Json

Source

pub fn new(text: &str) -> Result<Self>

Parse text as JSON with upstream’s default options. Returns an error if it is not valid JSON.

Source

pub fn with_options(text: &str, options: &JsonOptions) -> Result<Self>

Parse text as JSON and format it with options. Port of rich.json.JSON(json, indent=…, highlight=…, …).

Source

pub fn text(&self) -> Text

The formatted, highlighted document. Port of the JSON.text attribute (upstream’s __rich__ returns it).

Source

pub fn no_wrap(self, no_wrap: bool) -> Self

Keep rich.json.JSON’s self.text.no_wrap = True, which crops each line at the available width instead of wrapping it.

Defaults to false, because that is what a top-level Console.print(JSON(...)) produces and that is how this renderable is normally reached. Upstream’s flag really is set, but Console.print never renders the Text it is set on: _collect_renderables sees a Text, buffers it, and check_text hands back Text(sep, justify=…, end=…).join(buffered) — and Text.join starts from self.blank_copy(), i.e. from the separator’s metadata. The separator has no_wrap=None, so the copy that actually renders wraps with the default fold overflow — or with the no_wrap and overflow that print was given, which reach it as ConsoleOptions and which this renderable honours while the flag is off.

Turn it on whenever the document is nested inside another renderable — a Panel, Padding, Styled, Constrain, or rich-cli’s ForceWidth. Those are ConsoleRenderables, so _collect_renderables appends them untouched and the inner Text reaches Text.__rich_console__ with the flag intact; Text.wrap then skips divide_line and only truncates to the width.

The two are not interchangeable. Wrapping keeps every character; cropping discards what does not fit, which for JSON means the printed document no longer parses — so the choice has to follow upstream’s, not taste.

Trait Implementations§

Source§

impl Renderable for Json

Source§

fn measure(&self, _console: &Console, _options: &ConsoleOptions) -> Measurement

Upstream JSON.__rich__ returns its highlighted Text, so measuring a JSON is Text.__rich_measure__ over the formatted document.

Source§

fn rich_render( &self, console: &Console, options: &ConsoleOptions, ) -> Vec<Segment>

Text.__rich_console__ of the document: each line wraps (or, with no_wrap, is truncated) to the width with the options’ justify and overflow — the document’s own overflow is None.

Source§

fn fit_to_measurement(&self) -> bool

Whether a top-level Console::print shrinks this renderable to its measured width. Upstream renders every top-level renderable at the full console width, so the default is false; an extension may opt in.
Source§

fn vertical(&self) -> Option<VerticalAlign>

The vertical alignment a Table cell holding this renderable uses in place of its column’s. Upstream reads getattr(renderable, "vertical", None); Align sets it.

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.