Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. use serde::{Deserialize, Serialize}; // 1.0.88
  2. use serde_json; // 1.0.38
  3.  
  4. #[derive(Debug, PartialEq, Serialize, Deserialize)]
  5. struct Application {
  6. application: Data,
  7. }
  8.  
  9. #[derive(Debug, PartialEq, Serialize, Deserialize)]
  10. struct Data {
  11. build: String,
  12. container_name: String,
  13. environment: Environment,
  14. }
  15.  
  16. use std::collections::BTreeMap;
  17.  
  18. #[derive(Debug, PartialEq, Serialize, Deserialize)]
  19. #[serde(untagged)]
  20. enum Environment {
  21. Vec(Vec<String>),
  22. Hash(BTreeMap<String, String>),
  23. }
  24.  
  25. fn main() {
  26. let content_vec = r#"
  27. {
  28. "application": {
  29. "build": "something",
  30. "container_name": "another_thing",
  31. "environment": [
  32. "ONE_ENV=fake",
  33. "SEC_ENV=somethingexample.yml"
  34. ]
  35. }
  36. }
  37. "#;
  38.  
  39. let content_hash = r#"
  40. {
  41. "application": {
  42. "build": "something",
  43. "container_name": "another_thing",
  44. "environment": {
  45. "ONE_ENV": "fake",
  46. "SEC_ENV": "somethingexample.yml"
  47. }
  48. }
  49. }
  50. "#;
  51.  
  52. let application_data: Application = serde_json::from_str(content_vec).unwrap();
  53. println!("{:?}", application_data.application.environment);
  54.  
  55. let application_data: Application = serde_json::from_str(content_hash).unwrap();
  56. println!("{:?}", application_data.application.environment);
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement