Skip to main content

Json

Enum Json 

Source
pub enum Json {
    Null,
    Bool(bool),
    Number(f64),
    String(String),
    Array(Vec<Json>),
    Object(BTreeMap<String, Json>),
}
Expand description

A parsed JSON value.

Object keys are kept in a BTreeMap so serialization is deterministic, which keeps test assertions and HTTP caching stable.

Variants§

§

Null

§

Bool(bool)

§

Number(f64)

§

String(String)

§

Array(Vec<Json>)

§

Object(BTreeMap<String, Json>)

Implementations§

Source§

impl Json

Source

pub fn object<K: Into<String>, I: IntoIterator<Item = (K, Json)>>( pairs: I, ) -> Self

Build an object from pairs: Json::object([("id", 1.into())]).

Examples found in repository?
examples/jsonsplit.rs (lines 10-16)
6fn build() -> Json {
7    Json::Array(
8        (1..=100i64)
9            .map(|id| {
10                Json::object([
11                    ("id", Json::from(id as f64)),
12                    ("name", Json::from(format!("User {id}"))),
13                    ("email", Json::from(format!("user{id}@example.test"))),
14                    ("active", Json::Bool(id % 2 == 0)),
15                    ("score", Json::from(id as f64 * 1.5)),
16                ])
17            })
18            .collect(),
19    )
20}
Source

pub fn get(&self, path: &str) -> Option<&Json>

Look up a nested value with a dotted path: value.get("user.name").

Numeric segments index into arrays, so items.0.id works too.

Source

pub fn as_str(&self) -> Option<&str>

Source

pub fn as_f64(&self) -> Option<f64>

Source

pub fn as_i64(&self) -> Option<i64>

Source

pub fn as_bool(&self) -> Option<bool>

Source

pub fn as_array(&self) -> Option<&[Json]>

Source

pub fn as_object(&self) -> Option<&BTreeMap<String, Json>>

Source

pub fn is_null(&self) -> bool

Source

pub fn to_string_pretty(&self) -> String

Serialize with two-space indentation, for config files and debug output.

Source

pub fn parse(input: &str) -> Result<Json>

Parse JSON text.

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 Display for Json

Compact JSON text. to_string() comes from here.

Source§

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

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

impl From<&str> for Json

Source§

fn from(v: &str) -> Self

Converts to this type from the input type.
Source§

impl<T: Into<Json>> From<Option<T>> for Json

Source§

fn from(v: Option<T>) -> Self

Converts to this type from the input type.
Source§

impl From<String> for Json

Source§

fn from(v: String) -> Self

Converts to this type from the input type.
Source§

impl<T: Into<Json>> From<Vec<T>> for Json

Source§

fn from(v: Vec<T>) -> Self

Converts to this type from the input type.
Source§

impl From<bool> for Json

Source§

fn from(v: bool) -> Self

Converts to this type from the input type.
Source§

impl From<f32> for Json

Source§

fn from(v: f32) -> Self

Converts to this type from the input type.
Source§

impl From<f64> for Json

Source§

fn from(v: f64) -> Self

Converts to this type from the input type.
Source§

impl From<i8> for Json

Source§

fn from(v: i8) -> Self

Converts to this type from the input type.
Source§

impl From<i16> for Json

Source§

fn from(v: i16) -> Self

Converts to this type from the input type.
Source§

impl From<i32> for Json

Source§

fn from(v: i32) -> Self

Converts to this type from the input type.
Source§

impl From<i64> for Json

Source§

fn from(v: i64) -> Self

Converts to this type from the input type.
Source§

impl From<isize> for Json

Source§

fn from(v: isize) -> Self

Converts to this type from the input type.
Source§

impl From<u8> for Json

Source§

fn from(v: u8) -> Self

Converts to this type from the input type.
Source§

impl From<u16> for Json

Source§

fn from(v: u16) -> Self

Converts to this type from the input type.
Source§

impl From<u32> for Json

Source§

fn from(v: u32) -> Self

Converts to this type from the input type.
Source§

impl From<u64> for Json

Source§

fn from(v: u64) -> Self

Converts to this type from the input type.
Source§

impl From<usize> for Json

Source§

fn from(v: usize) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for Json

Source§

fn eq(&self, other: &Json) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Json

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> 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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.