Struct JsonPropertyMap

Source
pub struct JsonPropertyMap(/* private fields */);

Implementations§

Source§

impl JsonPropertyMap

Source

pub fn new() -> Self

Create a new property map with no mappings.

Source

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");
Source

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");
Source

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 - The JsonNode to be associated with the property_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);
Source

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);
Source

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"));
Source

pub fn property_names(&self) -> Vec<&String>

Gets all property names in the object.

Source

pub fn property_names_mut(&mut self) -> Vec<&mut String>

Source

pub fn nodes(&self) -> Vec<&JsonNode>

Gets all child nodes in the object.

Source

pub fn nodes_mut(&mut self) -> Vec<&mut JsonNode>

Gets all child nodes in the object as mutable references.

Source

pub fn clear(&mut self)

Clears the map of all mappings.

Source

pub fn iter(&self) -> Iter<'_, (String, JsonNode)>

Returns an iterator over the mappings represented as tuples.

Source

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.

Source

pub fn len(&self) -> usize

Returns the number of mappings in the object.

Source

pub fn is_empty(&self) -> bool

Returns true if the object has zero mappings.

Source

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

Source§

fn clone(&self) -> JsonPropertyMap

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 JsonPropertyMap

Source§

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

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

impl<const COUNT: usize> From<[(String, JsonNode); COUNT]> for JsonPropertyMap

Source§

fn from(value: [(String, JsonNode); COUNT]) -> Self

Converts to this type from the input type.
Source§

impl From<Vec<(String, JsonNode)>> for JsonPropertyMap

Source§

fn from(value: Vec<(String, JsonNode)>) -> Self

Converts to this type from the input type.
Source§

impl FromIterator<(String, JsonNode)> for JsonPropertyMap

Source§

fn from_iter<T: IntoIterator<Item = (String, JsonNode)>>(iter: T) -> Self

Creates a value from an iterator. Read more
Source§

impl Index<usize> for JsonPropertyMap

Source§

type Output = (String, JsonNode)

The returned type after indexing.
Source§

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

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

impl IndexMut<usize> for JsonPropertyMap

Source§

fn index_mut(&mut self, index: usize) -> &mut Self::Output

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

impl PartialEq for JsonPropertyMap

Source§

fn eq(&self, other: &JsonPropertyMap) -> 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 StructuralPartialEq for JsonPropertyMap

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, dest: *mut u8)

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