Diggsey / ijson

More efficient alternative to `serde_json::Value` which saves memory by interning primitive values and using tagged pointers.
Apache License 2.0
127 stars 14 forks source link

CSV deserialize error #4

Closed iBinh closed 3 years ago

iBinh commented 3 years ago

I'm using ijson version 0.1.2 to deserialize csv records into IObject, example below:

use std::io::{Result, Error, ErrorKind};
use ijson::IObject;
type Object = IObject;

fn main () -> Result<()> {
    let data = "\
    city,country,popcount
    Boston,United States,4628910
";

    let records = records_from(data.as_bytes())?;
    println!("{:?}", records);
    Ok(())
}

fn records_from(data: &[u8]) -> Result<Vec<Object>> {
    let mut rdr = csv::Reader::from_reader(data);
    let mut rows: Vec<Object> = Vec::new();
    for record in rdr.deserialize() {
        match record {
            Ok(r) => rows.push(r),
            Err(e) => {
                return Err(Error::new(
                    ErrorKind::InvalidData,
                    e
                ))
            }
        }
    }
    Ok(rows)
}

It says CSV deserialize error: record 1 (line: 2, byte: 22): invalid type: byte array, expected JSON string at line 0 The same error with type Object = std::collections::HashMap<IString, IObject> However, if I change type to std::collections::HashMap<String, IObject> it works well

Diggsey commented 3 years ago

Interesting - does deserialization work if you use a serde_json::Map<String, serde_json::Value> instead?

iBinh commented 3 years ago

It works

Diggsey commented 3 years ago

OK - it sounds like I'm missing a deserialize implementation for byte slices - you can PR it or else I'll add it when I get a chance.

Diggsey commented 3 years ago

I just published 0.1.3 which should resolve this issue.