Struct json_node::models::json_property_map::JsonPropertyMap
source · pub struct JsonPropertyMap(_);
Implementations§
source§impl JsonPropertyMap
impl JsonPropertyMap
sourcepub fn get(&self, property_name: &str) -> Option<&JsonNode>
pub fn get(&self, property_name: &str) -> Option<&JsonNode>
Get the JsonNode
associated with a name.
Arguments
property_name
- The name of the property you want.
Examples
use json_node::{JsonNode, JsonValue, JsonPropertyMap};
// Create node with mappings.
let object_node = JsonNode::Object(JsonPropertyMap::from([
("name".to_owned(), JsonNode::Value(JsonValue::String("John Doe".to_owned()))),
("age".to_owned(), JsonNode::Value(JsonValue::Integer(42))),
]));
let map = object_node.as_object().unwrap(); // &JsonPropertyMap.
let property = map.get("name").unwrap(); // &JsonNode.
let value = property.as_value().unwrap(); // &JsonValue.
let name = value.as_string().unwrap(); // &str.
assert_eq!(name, "John Doe");
sourcepub fn get_mut(&mut self, property_name: &str) -> Option<&mut JsonNode>
pub fn get_mut(&mut self, property_name: &str) -> Option<&mut JsonNode>
Get the JsonNode
associated with a name as a mutable value.
Arguments
property_name
- The name of the property you want.
Examples
use json_node::{JsonNode, JsonValue, JsonPropertyMap};
// Create node with mappings.
let mut object_node = JsonNode::Object(JsonPropertyMap::from([
("name".to_owned(), JsonNode::Value(JsonValue::String("John Doe".to_owned()))),
("age".to_owned(), JsonNode::Value(JsonValue::Integer(42))),
]));
let mut_map = object_node.as_object_mut().unwrap(); // &mut JsonPropertyMap.
let mut_property = mut_map.get_mut("name").unwrap(); // &mut JsonNode.
let mut_value = mut_property.as_value_mut().unwrap(); // &mut JsonValue.
let mut_name = mut_value.as_string_mut().unwrap(); // &mut str.
mut_name.make_ascii_uppercase(); // Mutates the string slice.
let map = object_node.as_object().unwrap(); // &JsonPropertyMap.
let property = map.get("name").unwrap(); // &JsonNode.
let value = property.as_value().unwrap(); // &JsonValue.
let name = value.as_string().unwrap(); // &String.
assert_eq!(name, "JOHN DOE");
sourcepub fn add(&mut self, property_name: &str, json_node: JsonNode)
pub fn add(&mut self, property_name: &str, json_node: JsonNode)
Adds a new mapping to the object.
Arguments
property_name
- Name of the new property.json_node
- TheJsonNode
to be associated with theproperty_name
.
Examples
use json_node::{JsonNode, JsonValue, JsonPropertyMap};
let mut map = JsonPropertyMap::new();
map.add("number", JsonNode::Value(JsonValue::Integer(42)));
let expected = JsonPropertyMap::from([
("number".to_owned(), JsonNode::Value(JsonValue::Integer(42)))
]);
assert_eq!(map, expected);
sourcepub fn remove(&mut self, property_name: &str) -> Result<JsonNode>
pub fn remove(&mut self, property_name: &str) -> Result<JsonNode>
Removes a mapping from the object if it exists.
Arguments
property_name
- Name of the property to be removed.
Examples
use json_node::{JsonNode, JsonValue, JsonPropertyMap};
let mut map = JsonPropertyMap::from([
("number".to_owned(), JsonNode::Value(JsonValue::Integer(42)))
]);
map.remove("number");
let expected = JsonPropertyMap::new();
assert_eq!(map, expected);
sourcepub fn contains_property(&self, property_name: &str) -> bool
pub fn contains_property(&self, property_name: &str) -> bool
Checks if a property with the name property_name
exists.
Arguments
property_name
- The name to check for.
Examples
use json_node::{JsonNode, JsonValue, JsonPropertyMap};
let mut map = JsonPropertyMap::from([
("number".to_owned(), JsonNode::Value(JsonValue::Integer(42)))
]);
assert!(map.contains_property("number"));
assert!(!map.contains_property("name"));
sourcepub fn property_names(&self) -> Vec<&String>
pub fn property_names(&self) -> Vec<&String>
Gets all property names in the object.
pub fn property_names_mut(&mut self) -> Vec<&mut String>
sourcepub fn nodes_mut(&mut self) -> Vec<&mut JsonNode>
pub fn nodes_mut(&mut self) -> Vec<&mut JsonNode>
Gets all child nodes in the object as mutable references.
sourcepub fn iter(&self) -> Iter<'_, (String, JsonNode)>
pub fn iter(&self) -> Iter<'_, (String, JsonNode)>
Returns an iterator over the mappings represented as tuples.
sourcepub fn iter_mut(&mut self) -> IterMut<'_, (String, JsonNode)>
pub fn iter_mut(&mut self) -> IterMut<'_, (String, JsonNode)>
Returns an iterator over the mappings represented as tuples that allows modifying each element and its name.
sourcepub fn to_json_string(&self) -> String
pub fn to_json_string(&self) -> String
Serialized the object as a JSON object string.
Remarks
This function does zero formatting meaning the JSON string will have no spaces or new-lines.
Trait Implementations§
source§impl Clone for JsonPropertyMap
impl Clone for JsonPropertyMap
source§fn clone(&self) -> JsonPropertyMap
fn clone(&self) -> JsonPropertyMap
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moresource§impl Debug for JsonPropertyMap
impl Debug for JsonPropertyMap
source§impl FromIterator<(String, JsonNode)> for JsonPropertyMap
impl FromIterator<(String, JsonNode)> for JsonPropertyMap
source§impl Index<usize> for JsonPropertyMap
impl Index<usize> for JsonPropertyMap
source§impl IndexMut<usize> for JsonPropertyMap
impl IndexMut<usize> for JsonPropertyMap
source§impl PartialEq<JsonPropertyMap> for JsonPropertyMap
impl PartialEq<JsonPropertyMap> for JsonPropertyMap
source§fn eq(&self, other: &JsonPropertyMap) -> bool
fn eq(&self, other: &JsonPropertyMap) -> bool
This method tests for
self
and other
values to be equal, and is used
by ==
.impl StructuralPartialEq for JsonPropertyMap
Auto Trait Implementations§
impl RefUnwindSafe for JsonPropertyMap
impl Send for JsonPropertyMap
impl Sync for JsonPropertyMap
impl Unpin for JsonPropertyMap
impl UnwindSafe for JsonPropertyMap
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