@@ -268,7 +268,7 @@ impl JsonValue {
268268 }
269269}
270270
271- /// Implements indexing by `usize` to easily access members of an array:
271+ /// Implements indexing by `usize` to easily access array members :
272272///
273273/// ```
274274/// # use json::JsonValue;
@@ -289,30 +289,20 @@ impl Index<usize> for JsonValue {
289289 }
290290}
291291
292- /// Implements indexing by `&str ` to easily access object members:
292+ /// Implements mutable indexing by `usie ` to easily modify array members:
293293///
294294/// ```
295- /// # use json::JsonValue;
296- /// let mut object = JsonValue::new_object();
295+ /// # #[macro_use]
296+ /// # extern crate json;
297+ /// #
298+ /// # fn main() {
299+ /// let mut array = array!["foo", 3.14];
297300///
298- /// object.put("foo", "bar");
301+ /// array[1] = "bar".into( );
299302///
300- /// assert!(object["foo"].is("bar"));
303+ /// assert!(array[1].is("bar"));
304+ /// # }
301305/// ```
302- impl < ' a > Index < & ' a str > for JsonValue {
303- type Output = JsonValue ;
304-
305- fn index ( & self , index : & str ) -> & JsonValue {
306- match * self {
307- JsonValue :: Object ( ref btree) => match btree. get ( index) {
308- Some ( value) => value,
309- _ => & NULL
310- } ,
311- _ => & NULL
312- }
313- }
314- }
315-
316306impl IndexMut < usize > for JsonValue {
317307 fn index_mut ( & mut self , index : usize ) -> & mut JsonValue {
318308 match * self {
@@ -335,6 +325,48 @@ impl IndexMut<usize> for JsonValue {
335325 }
336326}
337327
328+ /// Implements indexing by `&str` to easily access object members:
329+ ///
330+ /// ```
331+ /// # #[macro_use]
332+ /// # extern crate json;
333+ /// #
334+ /// # fn main() {
335+ /// let object = object!{
336+ /// "foo" => "bar"
337+ /// };
338+ ///
339+ /// assert!(object["foo"].is("bar"));
340+ /// # }
341+ /// ```
342+ impl < ' a > Index < & ' a str > for JsonValue {
343+ type Output = JsonValue ;
344+
345+ fn index ( & self , index : & str ) -> & JsonValue {
346+ match * self {
347+ JsonValue :: Object ( ref btree) => match btree. get ( index) {
348+ Some ( value) => value,
349+ _ => & NULL
350+ } ,
351+ _ => & NULL
352+ }
353+ }
354+ }
355+
356+ /// Implements mutable indexing by `&str` to easily modify object members:
357+ ///
358+ /// ```
359+ /// # #[macro_use]
360+ /// # extern crate json;
361+ /// #
362+ /// # fn main() {
363+ /// let mut object = object!{};
364+ ///
365+ /// object["foo"] = 42.into();
366+ ///
367+ /// assert!(object["foo"].is(42));
368+ /// # }
369+ /// ```
338370impl < ' a > IndexMut < & ' a str > for JsonValue {
339371 fn index_mut ( & mut self , index : & str ) -> & mut JsonValue {
340372 match * self {
0 commit comments