pub struct JsonPropertyMap(/* private fields */);
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, JsonPropertyMap};
// Create node with mappings.
let object_node = JsonNode::Object(JsonPropertyMap::from([
("name".to_owned(), JsonNode::String("John Doe".to_owned())),
("age".to_owned(), JsonNode::Integer(42)),
]));
let map = object_node.as_object().unwrap(); // &JsonPropertyMap.
let property = map.get("name").unwrap(); // &JsonNode.
let name = property.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, JsonPropertyMap};
// Create node with mappings.
let mut object_node = JsonNode::Object(JsonPropertyMap::from([
("name".to_owned(), JsonNode::String("John Doe".to_owned())),
("age".to_owned(), JsonNode::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_name = mut_property.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 name = property.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, JsonPropertyMap};
let mut map = JsonPropertyMap::new();
map.add("number", JsonNode::Integer(42));
let expected = JsonPropertyMap::from([
("number".to_owned(), JsonNode::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, JsonPropertyMap};
let mut map = JsonPropertyMap::from([
("number".to_owned(), JsonNode::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, JsonPropertyMap};
let mut map = JsonPropertyMap::from([
("number".to_owned(), JsonNode::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 for JsonPropertyMap
impl PartialEq for JsonPropertyMap
impl StructuralPartialEq for JsonPropertyMap
Auto Trait Implementations§
impl Freeze for JsonPropertyMap
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