about summary refs log tree commit diff
path: root/users
diff options
context:
space:
mode:
authorWilliam Carroll <wpcarro@gmail.com>2022-06-27T01·01-0700
committerclbot <clbot@tvl.fyi>2022-06-27T17·18+0000
commitc18ca0f85262b3218ce6455c4071a880adb055bc (patch)
tree5cf0d45c039067ce075545e334555be4b82c66e6 /users
parent8e9bfaba4743394d7620a6f61c23c2d2fd30b5e8 (diff)
feat(wpcarro/rust): Show 2/3 json examples r/4258
Documented in the module

Change-Id: I550c233c8116904a1f9cd6841ee778eb0abb540a
Reviewed-on: https://cl.tvl.fyi/c/depot/+/5897
Reviewed-by: wpcarro <wpcarro@gmail.com>
Autosubmit: wpcarro <wpcarro@gmail.com>
Tested-by: BuildkiteCI
Diffstat (limited to 'users')
-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()
 }