From c18ca0f85262b3218ce6455c4071a880adb055bc Mon Sep 17 00:00:00 2001 From: William Carroll Date: Sun, 26 Jun 2022 18:01:09 -0700 Subject: feat(wpcarro/rust): Show 2/3 json examples Documented in the module Change-Id: I550c233c8116904a1f9cd6841ee778eb0abb540a Reviewed-on: https://cl.tvl.fyi/c/depot/+/5897 Reviewed-by: wpcarro Autosubmit: wpcarro Tested-by: BuildkiteCI --- users/wpcarro/scratch/rust/json/src/main.rs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'users/wpcarro/scratch/rust/json/src') 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() } -- cgit 1.4.1