Skip to content

Commit d99750e

Browse files
committed
0.6.0 docs
1 parent 719f240 commit d99750e

File tree

3 files changed

+98
-24
lines changed

3 files changed

+98
-24
lines changed

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ assert!(data["payload"]["features"].contains("easyAPI"));
4444
assert!(data["this"]["does"]["not"]["exist"].is_null());
4545
```
4646

47-
## Easily create JSON data without defining structs
47+
## Create JSON data without defining structs
4848

4949
```rust
5050
#[macro_use]
@@ -60,6 +60,27 @@ fn main() {
6060
}
6161
```
6262

63+
## Mutate simply by assigning new values
64+
65+
```rust
66+
let mut data = json::parse(r#"
67+
68+
{
69+
"name": "Bob",
70+
"isAwesome": false
71+
}
72+
73+
"#).unwrap();
74+
75+
data["isAwesome"] = true.into();
76+
data["likes"] = "Rust".into();
77+
78+
assert_eq!(data.dump(), r#"{"isAwesome":true,"likes":"Rust","name":"Bob"}"#);
79+
80+
// Pretty print the output
81+
println!("{:#}", data);
82+
```
83+
6384
## Installation
6485

6586
Just add it to your `Cargo.toml` file:

src/lib.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
//! assert!(data["this"]["does"]["not"]["exist"].is_null());
4545
//! ```
4646
//!
47-
//! ## Easily create JSON data without defining structs
47+
//! ## Create JSON data without defining structs
4848
//!
4949
//! ```
5050
//! #[macro_use]
@@ -60,6 +60,27 @@
6060
//! }
6161
//! ```
6262
//!
63+
//! ## Mutate simply by assigning new values
64+
//!
65+
//! ```
66+
//! let mut data = json::parse(r#"
67+
//!
68+
//! {
69+
//! "name": "Bob",
70+
//! "isAwesome": false
71+
//! }
72+
//!
73+
//! "#).unwrap();
74+
//!
75+
//! data["isAwesome"] = true.into();
76+
//! data["likes"] = "Rust".into();
77+
//!
78+
//! assert_eq!(data.dump(), r#"{"isAwesome":true,"likes":"Rust","name":"Bob"}"#);
79+
//!
80+
//! // Pretty print the output
81+
//! println!("{:#}", data);
82+
//! ```
83+
//!
6384
//! ## Serialize with `json::stringify(value)`
6485
//!
6586
//! Primitives:
@@ -126,8 +147,8 @@
126147
//! ```
127148
//! let mut data = json::JsonValue::new_object();
128149
//!
129-
//! data.put("answer", 42);
130-
//! data.put("foo", "bar");
150+
//! data["answer"] = 42.into();
151+
//! data["foo"] = "bar".into();
131152
//!
132153
//! assert_eq!(json::stringify(data), "{\"answer\":42,\"foo\":\"bar\"}");
133154
//! ```

src/value.rs

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
316306
impl 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+
/// ```
338370
impl<'a> IndexMut<&'a str> for JsonValue {
339371
fn index_mut(&mut self, index: &str) -> &mut JsonValue {
340372
match *self {

0 commit comments

Comments
 (0)