Skip to content

Commit

Permalink
Add documentation examples for members and entries (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
syvb authored Mar 29, 2020
1 parent 752f942 commit 0775592
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,19 @@ impl JsonValue {

/// Works on `JsonValue::Array` - returns an iterator over members.
/// Will return an empty iterator if called on non-array types.
/// ## Example
/// ```
/// # use json::JsonValue;
/// # #[macro_use] use json::array;
/// let animals = array!["Cat", "Dog", "Snail"];
/// let mut animals_with_letter_a = Vec::new();
/// for animal in animals.members() {
/// if animal.as_str().unwrap().contains('a') {
/// animals_with_letter_a.push(animal);
/// }
/// }
/// assert_eq!(animals_with_letter_a, vec!["Cat", "Snail"]);
/// ```
pub fn members(&self) -> Members {
match *self {
JsonValue::Array(ref vec) => {
Expand All @@ -457,6 +470,27 @@ impl JsonValue {

/// Works on `JsonValue::Object` - returns an iterator over key value pairs.
/// Will return an empty iterator if called on non-object types.
/// ## Example
/// ```
/// # use json::JsonValue;
/// let heights = json::parse(r#"
/// {
/// "Alice": 1.42,
/// "Bob": 1.6,
/// "Carlos": 2.1
/// }
/// "#).unwrap();
/// let mut total_height = 0.0;
/// let mut names_with_o: Vec<&str> = Vec::new();
/// for (name, height) in heights.entries() {
/// total_height += height.as_f64().unwrap();
/// if name.contains('o') {
/// names_with_o.push(name);
/// }
/// }
/// assert_eq!(total_height, 5.12);
/// assert_eq!(names_with_o, vec!["Bob", "Carlos"]);
/// ```
pub fn entries(&self) -> Entries {
match *self {
JsonValue::Object(ref object) => {
Expand Down

0 comments on commit 0775592

Please sign in to comment.