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
impl JsonValue
sourcepub fn is_f64(&self) -> bool
pub fn is_f64(&self) -> bool
Returns true
if the value is a floating-point number (JsonValue::Number::Float
).
sourcepub fn is_u128(&self) -> bool
pub fn is_u128(&self) -> bool
Returns true
if the value is an unsigned integer (JsonValue::Number::UInteger
).
sourcepub fn is_i128(&self) -> bool
pub fn is_i128(&self) -> bool
Returns true
if the value is a signed integer (JsonValue::Number::Integer
).
sourcepub fn as_str(&self) -> Option<&str>
pub fn as_str(&self) -> Option<&str>
If the value is a JsonValue::String
, returns the string as a &str
.
Otherwise, returns None
.
sourcepub fn as_string_mut(&mut self) -> Option<&mut String>
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
.
sourcepub fn as_number(&self) -> Option<Number>
pub fn as_number(&self) -> Option<Number>
If the value is a JsonValue::Number
, returns the number.
Otherwise, returns None
.
sourcepub fn as_bool(&self) -> Option<bool>
pub fn as_bool(&self) -> Option<bool>
If the value is a JsonValue::Bool
, returns the boolean.
Otherwise, returns None
.
sourcepub fn as_array(&self) -> Option<&[JsonValue]>
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
.
sourcepub fn as_array_mut(&mut self) -> Option<&mut Vec<JsonValue>>
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
.
sourcepub fn as_map(&self) -> Option<&OrderedMap<String, JsonValue>>
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
.
sourcepub fn as_map_mut(&mut self) -> Option<&mut OrderedMap<String, JsonValue>>
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
.
sourcepub fn take(&mut self) -> JsonValue
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.
sourcepub fn get<I: JsonIndex>(&self, index: I) -> Option<&JsonValue>
pub fn get<I: JsonIndex>(&self, index: I) -> Option<&JsonValue>
Returns a reference element to the given index.
sourcepub fn get_mut<I: JsonIndex>(&mut self, index: I) -> Option<&mut JsonValue>
pub fn get_mut<I: JsonIndex>(&mut self, index: I) -> Option<&mut JsonValue>
Returns a reference mutable element to the given index.
sourcepub fn insert<I: JsonIndexMut>(
&mut self,
index: I,
new_value: JsonValue,
) -> Option<JsonValue>
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
sourcepub fn try_remove<I: JsonIndexMut>(
&mut self,
index: I,
) -> Result<Option<JsonValue>, TryRemoveError>
pub fn try_remove<I: JsonIndexMut>( &mut self, index: I, ) -> Result<Option<JsonValue>, TryRemoveError>
Removes the element at the given index.
sourcepub fn try_insert<I: JsonIndexMut>(
&mut self,
index: I,
new_value: JsonValue,
) -> Result<Option<JsonValue>, TryInsertError>
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.
sourcepub fn select(&self, path: &str) -> Option<&JsonValue>
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)));
sourcepub fn select_mut(&mut self, path: &str) -> Option<&mut JsonValue>
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)")));
Trait Implementations§
source§impl Deserialize for JsonValue
impl Deserialize for JsonValue
fn deserialize<D: Deserializer>(deserializer: D) -> Result<Self, Error>
source§impl Deserializer for JsonValue
impl Deserializer for JsonValue
fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor,
fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor,
fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor,
fn deserialize_u16<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor,
fn deserialize_u32<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor,
fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor,
fn deserialize_u128<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor,
fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor,
fn deserialize_i16<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor,
fn deserialize_i32<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor,
fn deserialize_i64<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor,
fn deserialize_i128<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor,
fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor,
fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor,
fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor,
fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor,
fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor,
fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor,
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor,
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor,
fn deserialize_bytes_buf<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor,
fn deserialize_bytes_seq<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor,
Auto Trait Implementations§
impl Freeze for JsonValue
impl RefUnwindSafe for JsonValue
impl Send for JsonValue
impl Sync for JsonValue
impl Unpin for JsonValue
impl UnwindSafe for JsonValue
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
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)