Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support deserializing flattened fields in csv #1257

Closed
dtolnay opened this issue May 12, 2018 · 1 comment
Closed

Support deserializing flattened fields in csv #1257

dtolnay opened this issue May 12, 2018 · 1 comment
Labels

Comments

@dtolnay
Copy link
Member

dtolnay commented May 12, 2018

#[macro_use]
extern crate serde_derive;

extern crate serde;
extern crate csv;

#[derive(Debug, Deserialize)]
struct Record {
    city: String,
    region: String,
    country: String,
    #[serde(flatten)]
    dem: Demographics,
}

#[derive(Debug, Deserialize)]
struct Demographics {
    population: u64,
}

const INPUT: &[u8] = b"\
city,region,country,population
Southborough,MA,United States,9686
Northbridge,MA,United States,14061
Westborough,MA,United States,29313
Marlborough,MA,United States,38334
Springfield,MA,United States,152227
Springfield,MO,United States,150443
Springfield,NJ,United States,14976
Springfield,OH,United States,64325
Springfield,OR,United States,56032
Concord,NH,United States,42605";

fn main() {
    let mut input = INPUT;
    let mut rdr = csv::Reader::from_reader(&mut input);
    for record in rdr.deserialize::<Record>() {
        println!("{:?}", record.unwrap());
    }
}
CSV deserialize error: record 1 (line: 2, byte: 31): missing field `population`
@dtolnay dtolnay added the bug label May 12, 2018
@dtolnay
Copy link
Member Author

dtolnay commented May 12, 2018

Fixed in ea0012f.

Record { city: "Southborough", region: "MA", country: "United States", dem: Demographics { population: 9686 } }
Record { city: "Northbridge", region: "MA", country: "United States", dem: Demographics { population: 14061 } }
Record { city: "Westborough", region: "MA", country: "United States", dem: Demographics { population: 29313 } }
Record { city: "Marlborough", region: "MA", country: "United States", dem: Demographics { population: 38334 } }
Record { city: "Springfield", region: "MA", country: "United States", dem: Demographics { population: 152227 } }
Record { city: "Springfield", region: "MO", country: "United States", dem: Demographics { population: 150443 } }
Record { city: "Springfield", region: "NJ", country: "United States", dem: Demographics { population: 14976 } }
Record { city: "Springfield", region: "OH", country: "United States", dem: Demographics { population: 64325 } }
Record { city: "Springfield", region: "OR", country: "United States", dem: Demographics { population: 56032 } }
Record { city: "Concord", region: "NH", country: "United States", dem: Demographics { population: 42605 } }

@dtolnay dtolnay closed this as completed May 12, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

1 participant