|
1 | 1 | use std::{ ptr, mem, str, slice, fmt };
|
| 2 | +use std::ops::{ Index, IndexMut, Deref }; |
2 | 3 |
|
3 | 4 | use value::JsonValue;
|
4 | 5 |
|
5 | 6 | const KEY_BUF_LEN: usize = 32;
|
| 7 | +static NULL: JsonValue = JsonValue::Null; |
6 | 8 |
|
7 | 9 | struct Node {
|
8 | 10 | // Internal buffer to store keys that fit within `KEY_BUF_LEN`,
|
@@ -575,3 +577,90 @@ impl<'a> DoubleEndedIterator for IterMut<'a> {
|
575 | 577 | self.inner.next_back().map(|node| (node.key_str(), &mut node.value))
|
576 | 578 | }
|
577 | 579 | }
|
| 580 | + |
| 581 | +/// Implements indexing by `&str` to easily access object members: |
| 582 | +/// |
| 583 | +/// ## Example |
| 584 | +/// |
| 585 | +/// ``` |
| 586 | +/// # #[macro_use] |
| 587 | +/// # extern crate json; |
| 588 | +/// # use json::JsonValue; |
| 589 | +/// # |
| 590 | +/// # fn main() { |
| 591 | +/// let value = object!{ |
| 592 | +/// "foo" => "bar" |
| 593 | +/// }; |
| 594 | +/// |
| 595 | +/// if let JsonValue::Object(object) = value { |
| 596 | +/// assert!(object["foo"] == "bar"); |
| 597 | +/// } |
| 598 | +/// # } |
| 599 | +/// ``` |
| 600 | +// TODO: doc |
| 601 | +impl<'a> Index<&'a str> for Object { |
| 602 | + type Output = JsonValue; |
| 603 | + |
| 604 | + fn index(&self, index: &str) -> &JsonValue { |
| 605 | + match self.get(index) { |
| 606 | + Some(value) => value, |
| 607 | + _ => &NULL |
| 608 | + } |
| 609 | + } |
| 610 | +} |
| 611 | + |
| 612 | +impl Index<String> for Object { |
| 613 | + type Output = JsonValue; |
| 614 | + |
| 615 | + fn index(&self, index: String) -> &JsonValue { |
| 616 | + self.index(index.deref()) |
| 617 | + } |
| 618 | +} |
| 619 | + |
| 620 | +impl<'a> Index<&'a String> for Object { |
| 621 | + type Output = JsonValue; |
| 622 | + |
| 623 | + fn index(&self, index: &String) -> &JsonValue { |
| 624 | + self.index(index.deref()) |
| 625 | + } |
| 626 | +} |
| 627 | + |
| 628 | +/// Implements mutable indexing by `&str` to easily modify object members: |
| 629 | +/// |
| 630 | +/// ## Example |
| 631 | +/// |
| 632 | +/// ``` |
| 633 | +/// # #[macro_use] |
| 634 | +/// # extern crate json; |
| 635 | +/// # use json::JsonValue; |
| 636 | +/// # |
| 637 | +/// # fn main() { |
| 638 | +/// let value = object!{}; |
| 639 | +/// |
| 640 | +/// if let JsonValue::Object(mut object) = value { |
| 641 | +/// object["foo"] = 42.into(); |
| 642 | +/// |
| 643 | +/// assert!(object["foo"] == 42); |
| 644 | +/// } |
| 645 | +/// # } |
| 646 | +/// ``` |
| 647 | +impl<'a> IndexMut<&'a str> for Object { |
| 648 | + fn index_mut(&mut self, index: &str) -> &mut JsonValue { |
| 649 | + if self.get(index).is_none() { |
| 650 | + self.insert(index, JsonValue::Null); |
| 651 | + } |
| 652 | + self.get_mut(index).unwrap() |
| 653 | + } |
| 654 | +} |
| 655 | + |
| 656 | +impl IndexMut<String> for Object { |
| 657 | + fn index_mut(&mut self, index: String) -> &mut JsonValue { |
| 658 | + self.index_mut(index.deref()) |
| 659 | + } |
| 660 | +} |
| 661 | + |
| 662 | +impl<'a> IndexMut<&'a String> for Object { |
| 663 | + fn index_mut(&mut self, index: &String) -> &mut JsonValue { |
| 664 | + self.index_mut(index.deref()) |
| 665 | + } |
| 666 | +} |
0 commit comments