about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--users/wpcarro/scratch/rust/json/src/main.rs20
1 files changed, 18 insertions, 2 deletions
diff --git a/users/wpcarro/scratch/rust/json/src/main.rs b/users/wpcarro/scratch/rust/json/src/main.rs
index b810cfa77fe6..e5f1078b5d01 100644
--- a/users/wpcarro/scratch/rust/json/src/main.rs
+++ b/users/wpcarro/scratch/rust/json/src/main.rs
@@ -1,4 +1,4 @@
-use serde_json::json;
+use serde_json::{json, Value};
 
 // From the serde_json docs:
 //
@@ -32,6 +32,22 @@ fn one() {
     println!("result: {:?}", data);
 }
 
+// 2) Parse into a loosely typed representation; mutate it; serialize it back.
+//    TL;DR:
+//    - read:  serde_json::from_str(data)
+//    - write: x.to_string()
+fn two() {
+    let data = r#"{"fname":"William","lname":"Carroll","age":30}"#;
+
+    let mut parsed: Value = serde_json::from_str(data).unwrap();
+    parsed["fname"] = json!("Norm");
+    parsed["lname"] = json!("Macdonald");
+    parsed["age"] = json!(61);
+
+    let result = parsed.to_string();
+    println!("result: {:?}", result);
+}
+
 fn main() {
-    one()
+    two()
 }