pub enum JSONValue {
Object(OrderedMap<JSONValue>),
Array(Vec<JSONValue>),
String(String),
Number(f64),
Boolean(bool),
Null,
}Variants§
Implementations§
Source§impl JSONValue
impl JSONValue
pub fn get<A: JSONAccess>(&self, accessor: A) -> Option<&JSONValue>
Sourcepub fn as_str(&self) -> Option<&str>
pub fn as_str(&self) -> Option<&str>
Returns the value as a string if it is a string. Returns None otherwise.
§Example
let value = JSONValue::String("Hello, world!".to_string());
assert_eq!(value.as_str(), Some("Hello, world!"));Sourcepub fn as_f64(&self) -> Option<f64>
pub fn as_f64(&self) -> Option<f64>
Returns the value as a number if it is a number. Returns None otherwise.
§Example
let value = JSONValue::Number(42.0);
assert_eq!(value.as_f64(), Some(42.0));Sourcepub fn as_bool(&self) -> Option<bool>
pub fn as_bool(&self) -> Option<bool>
Returns the value as a boolean if it is a boolean. Returns None otherwise.
§Example
let value = JSONValue::Boolean(true);
assert_eq!(value.as_bool(), Some(true));Sourcepub fn as_array(&self) -> Option<&Vec<JSONValue>>
pub fn as_array(&self) -> Option<&Vec<JSONValue>>
Returns the value as an array if it is an array. Returns None otherwise.
§Example
let value = JSONValue::Array(vec![JSONValue::Number(1.0), JSONValue::Number(2.0)]);
assert_eq!(value.as_array(), Some(&vec![JSONValue::Number(1.0), JSONValue::Number(2.0)]));Sourcepub fn as_object(&self) -> Option<&OrderedMap<JSONValue>>
pub fn as_object(&self) -> Option<&OrderedMap<JSONValue>>
Returns the value as an object if it is an object. Returns None otherwise.
§Example
let mut object = OrderedMap::new();
object.insert("name", JSONValue::String("John Doe".to_string()));
object.insert("age", JSONValue::Number(30.0));
let value = JSONValue::Object(object);
assert_eq!(value.as_object(), Some(&object));Sourcepub fn as_object_mut(&mut self) -> Option<&mut OrderedMap<JSONValue>>
pub fn as_object_mut(&mut self) -> Option<&mut OrderedMap<JSONValue>>
Returns the value as an object if it is an object. Returns None otherwise. This method allows modifying the object.
§Example
let mut object = OrderedMap::new();
object.insert("name", JSONValue::String("John Doe".to_string()));
object.insert("age", JSONValue::Number(30.0));
let mut value = JSONValue::Object(object);
value.as_object_mut().unwrap().insert("isStudent", JSONValue::Boolean(false));Trait Implementations§
Source§impl Serialize for JSONValue
impl Serialize for JSONValue
Source§fn serialize(&self) -> String
fn serialize(&self) -> String
Serialize a JSON value to a string.
§Example
let value = JSONValue::Object(OrderedMap::new());
value.insert("name", JSONValue::String("John Doe".to_string()));
value.insert("age", JSONValue::Number(30.0));
assert_eq!(serialize(&value), r#"{"name":"John Doe","age":30}"#);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
Mutably borrows from an owned value. Read more