Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
Fixed json writing of dates and datetimes (#867)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgecarleitao authored Feb 26, 2022
1 parent a26f95b commit fca3b8d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/io/json/write/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ where
move |x, buf| {
if let Some(x) = x {
let nd = convert(*x);
write!(buf, "{}", nd).unwrap();
write!(buf, "\"{}\"", nd).unwrap();
} else {
buf.extend_from_slice(b"null")
}
Expand All @@ -178,7 +178,7 @@ where
move |x, buf| {
if let Some(x) = x {
let ndt = convert(*x);
write!(buf, "{}", ndt).unwrap();
write!(buf, "\"{}\"", ndt).unwrap();
} else {
buf.extend_from_slice(b"null")
}
Expand Down
17 changes: 12 additions & 5 deletions tests/it/io/json/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn write_simple_rows() -> Result<()> {
let a = Int32Array::from([Some(1), Some(2), Some(3), None, Some(5)]);
let b = Utf8Array::<i32>::from(&vec![Some("a"), Some("b"), Some("c"), Some("d"), None]);

let batch = Chunk::try_new(vec![&a as &dyn Array, &b]).unwrap();
let batch = Chunk::try_new(vec![&a as &dyn Array, &b])?;

let buf = write_batch(
batch,
Expand Down Expand Up @@ -338,11 +338,15 @@ fn write_date32() -> Result<()> {
)?;

assert_eq!(
String::from_utf8(buf).unwrap().as_bytes(),
b"{\"c1\":1972-09-27}\n{\"c1\":1991-11-27}\n{\"c1\":1997-05-19}\n"
std::str::from_utf8(&buf).unwrap(),
r#"{"c1":"1972-09-27"}
{"c1":"1991-11-27"}
{"c1":"1997-05-19"}
"#
);
Ok(())
}

#[test]
fn write_timestamp() -> Result<()> {
let a = PrimitiveArray::from_data(
Expand All @@ -360,8 +364,11 @@ fn write_timestamp() -> Result<()> {
)?;

assert_eq!(
String::from_utf8(buf).unwrap().as_bytes(),
b"{\"c1\":1970-01-01 00:00:10}\n{\"c1\":2106-02-07 06:28:16}\n{\"c1\":2242-03-16 12:56:32}\n"
std::str::from_utf8(&buf).unwrap(),
r#"{"c1":"1970-01-01 00:00:10"}
{"c1":"2106-02-07 06:28:16"}
{"c1":"2242-03-16 12:56:32"}
"#
);
Ok(())
}

0 comments on commit fca3b8d

Please sign in to comment.