SHARE
TWEET

Untitled

a guest Oct 21st, 2019 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. use std::collections::HashMap;
  2. use serde::{Deserialize, Serialize};
  3. use serde_json::Value;
  4.  
  5. #[derive(Debug, Deserialize, Serialize)]
  6. pub struct Test {
  7.     a: String,
  8.     b: u64,
  9.     c: Base,
  10. }
  11.  
  12. #[derive(Debug, Deserialize, Serialize)]
  13. pub struct Base {
  14.     d: u64,
  15. }
  16.  
  17. fn main() {
  18.     let x = Test { a: "a".to_string(), b: 7, c: Base { d: 1 } };
  19.     println!("before {:?}", x);
  20.     let y = serde_json::to_string(&x).unwrap();
  21.  
  22.     let mut z: HashMap<String, Value> = serde_json::from_str(&y).unwrap();
  23.  
  24.     let new_string = "{\"a\": \"b\"}";
  25.     let new: HashMap<String, Value> = serde_json::from_str(&new_string).unwrap();
  26.     z.extend(new);
  27.  
  28.     let y = serde_json::to_string(&z).unwrap();
  29.  
  30.     let x: Test = serde_json::from_str(&y).unwrap();
  31.     println!("after  {:?}", x);
  32. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top