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

Problem with deserialising numeric field after character field #45

Open
platy opened this issue Dec 6, 2022 · 2 comments
Open

Problem with deserialising numeric field after character field #45

platy opened this issue Dec 6, 2022 · 2 comments

Comments

@platy
Copy link

platy commented Dec 6, 2022

I found that when I try to use the serde feature to deserialize a record which contains both character and numeric fields the numeric field (f64) deserialization fails with the error below. I am able to read to a dbase::Record no problem and it comes back with a character and a numeric field.

Error { record_num: 0, field: Some(FieldInfo { name: "FID_rail_d", field_type: Numeric, displacement_field: [0, 0, 0, 0], field_length: 9, num_decimal_places: 0, flags: FieldFlags(0), autoincrement_next_val: [0, 0, 0, 0, 0], autoincrement_step: 0 }), kind: BadConversion(FieldTypeNotAsExpected { expected: Character, actual: Numeric }) }'
    #[derive(Debug, Serialize, Deserialize)]
    struct R {
        #[serde(rename = "ISO")]
        iso: String,
        #[serde(rename = "FID_countr")]
        fid_countr: f64,
    }

I tried creating a minimal reproducable example by serializing this and then deserializing, but this problem didn't occur, so I wonder if it might be something perculiar about the layout of the dbf file I'm reading.

I'm using through shapefile version 0.3, so that's dbase 0.2, but I've also reproduced with dbase 0.3.

To reproduce, obtain the dbf from the shapefile at : https://biogeo.ucdavis.edu/data/diva/rrd/ALB_rrd.zip

    use std::io::Cursor;

    use serde::*;
    use dbase::*;

    #[derive(Debug, Serialize, Deserialize)]
    struct R {
        #[serde(rename = "ISO")]
        iso: String,
        #[serde(rename = "FID_countr")]
        fid_countr: f64,
    }

    #[test]
    fn test() {
        let mut reader = dbase::Reader::from_path("ALB_rrd/ALB_rails.dbf").unwrap();
        let stations = reader.read_as::<R>().unwrap();
    }
@tmontaigu
Copy link
Owner

tmontaigu commented Dec 19, 2022

So the thing is that the deserialization for now does not use the field name to read the data.

It reads the data in order in which the fields of the struct are declared.

So to be able to read your file you need to re-order your fieds to match the order that they appear in the file

        #[serde(rename = "FID_countr")]
        fid_countr: f64,
        #[serde(rename = "ISO")]
        iso: String,

@tmontaigu
Copy link
Owner

And after taking at look it does not seem like it can be improved/changed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants