json
json copied to clipboard
Support for JSONL
Hi, Is there any support for jsonl format ? If yes, can you please document it with example code?
Does StreamDeserializer not work for this?
For me it worked. I used it like this
let loaded: Vec<YourType> =
serde_json::Deserializer::from_str(&std::fs::read_to_string(file)?)
.into_iter::<YourType>()
.collect::<std::result::Result<Vec<_>, _>>()?;
How about serializing Vec<T> into JSONL ?
This was my naive approach (not working with gigantic data amounts though)
std::fs::write(
file,
your_vec
.into_iter()
.map(|e| serde_json::to_string(&e))
.collect::<std::result::Result<Vec<_>, _>>()?
.join("\n"),
)?;
This was my naive approach (not working with gigantic data amounts though)
std::fs::write( file, your_vec .into_iter() .map(|e| serde_json::to_string(&e)) .collect::<std::result::Result<Vec<_>, _>>()? .join("\n"), )?;
let file = File::options()
.write(true)
.create(true)
.open("your_file.json")?;
your_vec.into_iter().try_fold(file, |mut file, item| {
serde_json::to_writer(&mut file, &item)?;
writeln!(file)?;
Ok(file)
})?
// or
for item in your_vec {
serde_json::to_writer(&mut file, &item)?;
writeln!(file)?;
}