serde::json::value

Enum JsonValue

source
pub enum JsonValue {
    Number(Number),
    String(String),
    Bool(bool),
    Array(Vec<JsonValue>),
    Object(OrderedMap<String, JsonValue>),
    Null,
}
Expand description

A JSON value, which can represent various types of data such as numbers, strings, booleans, arrays, objects, or null values.

Variants§

§

Number(Number)

A numeric value (can be a float, integer, or unsigned integer).

§

String(String)

A string value.

§

Bool(bool)

A boolean value.

§

Array(Vec<JsonValue>)

An array of JSON values.

§

Object(OrderedMap<String, JsonValue>)

An object (a map from strings to JSON values).

§

Null

A null value.

Implementations§

source§

impl JsonValue

source

pub fn is_null(&self) -> bool

Returns true if the value is JsonValue::Null.

source

pub fn is_number(&self) -> bool

Returns true if the value is JsonValue::Number.

source

pub fn is_bool(&self) -> bool

Returns true if the value is JsonValue::Bool.

source

pub fn is_string(&self) -> bool

Returns true if the value is JsonValue::String.

source

pub fn is_array(&self) -> bool

Returns true if the value is JsonValue::Array.

source

pub fn is_object(&self) -> bool

Returns true if the value is JsonValue::Object.

source

pub fn is_f64(&self) -> bool

Returns true if the value is a floating-point number (JsonValue::Number::Float).

source

pub fn is_u128(&self) -> bool

Returns true if the value is an unsigned integer (JsonValue::Number::UInteger).

source

pub fn is_i128(&self) -> bool

Returns true if the value is a signed integer (JsonValue::Number::Integer).

source

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

If the value is a JsonValue::String, returns the string as a &str. Otherwise, returns None.

source

pub fn as_string_mut(&mut self) -> Option<&mut String>

If the value is a JsonValue::String, returns the string as a mutable reference. Otherwise, returns None.

source

pub fn as_number(&self) -> Option<Number>

If the value is a JsonValue::Number, returns the number. Otherwise, returns None.

source

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

If the value is a JsonValue::Bool, returns the boolean. Otherwise, returns None.

source

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

If the value is a JsonValue::Array, returns the array as a slice of JSON values. Otherwise, returns None.

source

pub fn as_array_mut(&mut self) -> Option<&mut Vec<JsonValue>>

If the value is a JsonValue::Array, returns the array as a mutable reference to a vector of JSON values. Otherwise, returns None.

source

pub fn as_map(&self) -> Option<&OrderedMap<String, JsonValue>>

If the value is a JsonValue::Object, returns the object as a reference to a map. Otherwise, returns None.

source

pub fn as_map_mut(&mut self) -> Option<&mut OrderedMap<String, JsonValue>>

If the value is a JsonValue::Object, returns the object as a mutable reference to a map. Otherwise, returns None.

source

pub fn take(&mut self) -> JsonValue

Takes the current value and replaces it with JsonValue::Null.

This can be used to extract the current value while leaving a null in its place.

source

pub fn get<I: JsonIndex>(&self, index: I) -> Option<&JsonValue>

Returns a reference element to the given index.

source

pub fn get_mut<I: JsonIndex>(&mut self, index: I) -> Option<&mut JsonValue>

Returns a reference mutable element to the given index.

source

pub fn remove<I: JsonIndexMut>(&mut self, index: I) -> Option<JsonValue>

Removes the element at the given index.

§Panics
  • This element is not a json
source

pub fn insert<I: JsonIndexMut>( &mut self, index: I, new_value: JsonValue, ) -> Option<JsonValue>

Insert or replace the element at the given index and returns the previous value if any.

§Panics
  • This element is not a json
  • This element is an array and the index is out of bounds
source

pub fn try_remove<I: JsonIndexMut>( &mut self, index: I, ) -> Result<Option<JsonValue>, TryRemoveError>

Removes the element at the given index.

source

pub fn try_insert<I: JsonIndexMut>( &mut self, index: I, new_value: JsonValue, ) -> Result<Option<JsonValue>, TryInsertError>

Insert or replace the element at the given index and returns the previous value if any.

source

pub fn select(&self, path: &str) -> Option<&JsonValue>

Selects a nested JsonValue based on a dot-separated path.

The path can specify nested fields or indices using dot-separated components. For example:

  • "students.0.name" will access the name of the first student in an array.
  • "skills.2" will access the third skill in the skills array.
§Parameters
  • path: A dot-separated string representing the nested path to traverse. If the path is empty or ".", the method returns the current value.
§Returns
  • Some(&JsonValue) if the path is valid and the corresponding value exists.
  • None if the path is invalid or the value does not exist.
§Examples
use serde::json;

let jjk = json!({
    name: "Satoru Gojo",
    age: 28,
    is_active: true,
    skills: [
        "Infinity",
        "Limitless",
        "Domain Expansion: Unlimited Void"
    ],
    students: [
        json!({
            name: "Yuji Itadori",
            age: 15
        }),
        json!({
            name: "Megumi Fushiguro",
            age: 16
        })
    ]
});

assert_eq!(jjk.select("students.0.name"), Some(&json!("Yuji Itadori")));
assert_eq!(jjk.select("skills.2"), Some(&json!("Domain Expansion: Unlimited Void")));
assert_eq!(jjk.select("age"), Some(&json!(28)));
assert_eq!(jjk.select("students.1.age"), Some(&json!(16)));
source

pub fn select_mut(&mut self, path: &str) -> Option<&mut JsonValue>

Selects a mutable reference to a nested JsonValue based on a dot-separated path.

The path can specify nested fields or indices using dot-separated components. For example:

  • "students.0.name" will access the name of the first student in an array.
  • "skills.2" will access the third skill in the skills array.
§Parameters
  • path: A dot-separated string representing the nested path to traverse. If the path is empty or ".", the method returns the current value.
§Returns
  • Some(&mut JsonValue) if the path is valid and the corresponding value exists.
  • None if the path is invalid or the value does not exist.
§Examples
use serde::json;
let mut jjk = json!({
    name: "Satoru Gojo",
    age: 28,
    is_active: true,
    skills: [
        "Infinity",
        "Limitless",
        "Domain Expansion: Unlimited Void"
    ],
    students: [
        json!({
            name: "Yuji Itadori",
            age: 15
        }),
        json!({
            name: "Megumi Fushiguro",
            age: 16
        })
    ]
});

if let Some(age) = jjk.select_mut("age") {
    *age = json!(29);
}

assert_eq!(jjk.select("age"), Some(&json!(29)));

if let Some(name) = jjk.select_mut("students.0.name") {
    *name = json!("Yuji Itadori (updated)");
}

assert_eq!(jjk.select("students.0.name"), Some(&json!("Yuji Itadori (updated)")));
source

pub fn variant(&self) -> &'static str

Returns the string representation of the type of the JsonValue (e.g., “string”, “array”).

Trait Implementations§

source§

impl Clone for JsonValue

source§

fn clone(&self) -> JsonValue

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl Debug for JsonValue

source§

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

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

impl Default for JsonValue

source§

fn default() -> JsonValue

Returns the “default value” for a type. Read more
source§

impl Deserialize for JsonValue

source§

fn deserialize<D: Deserializer>(deserializer: D) -> Result<Self, Error>

source§

impl Deserializer for JsonValue

source§

fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Error>
where V: Visitor,

source§

fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Error>
where V: Visitor,

source§

fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value, Error>
where V: Visitor,

source§

fn deserialize_u16<V>(self, visitor: V) -> Result<V::Value, Error>
where V: Visitor,

source§

fn deserialize_u32<V>(self, visitor: V) -> Result<V::Value, Error>
where V: Visitor,

source§

fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value, Error>
where V: Visitor,

source§

fn deserialize_u128<V>(self, visitor: V) -> Result<V::Value, Error>
where V: Visitor,

source§

fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value, Error>
where V: Visitor,

source§

fn deserialize_i16<V>(self, visitor: V) -> Result<V::Value, Error>
where V: Visitor,

source§

fn deserialize_i32<V>(self, visitor: V) -> Result<V::Value, Error>
where V: Visitor,

source§

fn deserialize_i64<V>(self, visitor: V) -> Result<V::Value, Error>
where V: Visitor,

source§

fn deserialize_i128<V>(self, visitor: V) -> Result<V::Value, Error>
where V: Visitor,

source§

fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value, Error>
where V: Visitor,

source§

fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value, Error>
where V: Visitor,

source§

fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, Error>
where V: Visitor,

source§

fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, Error>
where V: Visitor,

source§

fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Error>
where V: Visitor,

source§

fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Error>
where V: Visitor,

source§

fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Error>
where V: Visitor,

source§

fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Error>
where V: Visitor,

source§

fn deserialize_bytes_buf<V>(self, visitor: V) -> Result<V::Value, Error>
where V: Visitor,

source§

fn deserialize_bytes_seq<V>(self, visitor: V) -> Result<V::Value, Error>
where V: Visitor,

source§

impl Display for JsonValue

source§

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

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

impl<'a> From<&'a str> for JsonValue

source§

fn from(value: &'a str) -> Self

Converts to this type from the input type.
source§

impl<T: Into<JsonValue>, const N: usize> From<[T; N]> for JsonValue

source§

fn from(value: [T; N]) -> Self

Converts to this type from the input type.
source§

impl From<()> for JsonValue

source§

fn from(_value: ()) -> Self

Converts to this type from the input type.
source§

impl<V: Into<JsonValue>> From<BTreeMap<String, V>> for JsonValue

source§

fn from(value: BTreeMap<String, V>) -> Self

Converts to this type from the input type.
source§

impl<T: Into<JsonValue>> From<BTreeSet<T>> for JsonValue

source§

fn from(value: BTreeSet<T>) -> Self

Converts to this type from the input type.
source§

impl<V: Into<JsonValue>> From<HashMap<String, V>> for JsonValue

source§

fn from(value: HashMap<String, V>) -> Self

Converts to this type from the input type.
source§

impl<T: Into<JsonValue>> From<HashSet<T>> for JsonValue

source§

fn from(value: HashSet<T>) -> Self

Converts to this type from the input type.
source§

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

source§

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

Converts to this type from the input type.
source§

impl<V: Into<JsonValue>> From<OrderedMap<String, V>> for JsonValue

source§

fn from(value: OrderedMap<String, V>) -> Self

Converts to this type from the input type.
source§

impl From<String> for JsonValue

source§

fn from(value: String) -> Self

Converts to this type from the input type.
source§

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

source§

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

Converts to this type from the input type.
source§

impl From<bool> for JsonValue

source§

fn from(value: bool) -> Self

Converts to this type from the input type.
source§

impl From<f32> for JsonValue

source§

fn from(value: f32) -> Self

Converts to this type from the input type.
source§

impl From<f64> for JsonValue

source§

fn from(value: f64) -> Self

Converts to this type from the input type.
source§

impl From<i128> for JsonValue

source§

fn from(value: i128) -> Self

Converts to this type from the input type.
source§

impl From<i16> for JsonValue

source§

fn from(value: i16) -> Self

Converts to this type from the input type.
source§

impl From<i32> for JsonValue

source§

fn from(value: i32) -> Self

Converts to this type from the input type.
source§

impl From<i64> for JsonValue

source§

fn from(value: i64) -> Self

Converts to this type from the input type.
source§

impl From<i8> for JsonValue

source§

fn from(value: i8) -> Self

Converts to this type from the input type.
source§

impl From<isize> for JsonValue

source§

fn from(value: isize) -> Self

Converts to this type from the input type.
source§

impl From<u128> for JsonValue

source§

fn from(value: u128) -> Self

Converts to this type from the input type.
source§

impl From<u16> for JsonValue

source§

fn from(value: u16) -> Self

Converts to this type from the input type.
source§

impl From<u32> for JsonValue

source§

fn from(value: u32) -> Self

Converts to this type from the input type.
source§

impl From<u64> for JsonValue

source§

fn from(value: u64) -> Self

Converts to this type from the input type.
source§

impl From<u8> for JsonValue

source§

fn from(value: u8) -> Self

Converts to this type from the input type.
source§

impl From<usize> for JsonValue

source§

fn from(value: usize) -> Self

Converts to this type from the input type.
source§

impl<I: JsonIndex> Index<I> for JsonValue

source§

type Output = JsonValue

The returned type after indexing.
source§

fn index(&self, index: I) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
source§

impl PartialEq for JsonValue

source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Serialize for JsonValue

source§

fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Err>

Auto Trait Implementations§

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, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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§

default 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 = 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.