Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extern crate serde;
- #[macro_use]
- extern crate serde_derive;
- extern crate serde_json;
- use serde::Deserialize;
- use serde::de::Deserializer;
- static INPUT: &str = r#"{
- "post" : {
- "id" : "123",
- "comments" : [{
- "body" : "test body",
- "comments": [{
- "body": "test body 2",
- "comments": ""
- }]
- }]
- }
- }"#;
- #[derive(Serialize, Deserialize, Debug)]
- struct InputObject {
- post: Post
- }
- #[derive(Serialize, Deserialize, Debug)]
- struct Post {
- id: String,
- comments: Vec<Comments>
- }
- #[derive(Serialize, Deserialize, Debug)]
- struct Comments {
- body: String,
- #[serde(deserialize_with="parse_listing")]
- comments: Option<Vec<Comments>>
- }
- fn parse_listing<'de, D>(d: D) -> Result<Option<Vec<Comments>>, D::Error>
- where D: Deserializer<'de>
- {
- match Deserialize::deserialize(d) {
- Ok(comments) => Ok(Some(comments)),
- Err(e) => {
- println!("{:?}", e);
- Ok(None)
- }
- }
- }
- fn main() {
- let input: InputObject = serde_json::from_str(INPUT).unwrap();
- println!("{:?}", input);
- }
Add Comment
Please, Sign In to add comment