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

Commit

Permalink
fix unescaped '"' in json writing
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Feb 5, 2022
1 parent 9a8edd0 commit 525d521
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/io/json/write/serialize.rs
Expand Up @@ -139,15 +139,15 @@ fn list_serializer<'a, O: Offset>(

#[inline]
fn utf8_serialize(value: &str, buf: &mut Vec<u8>) {
if value.as_bytes().is_ascii() {
if value.as_bytes().is_ascii() && !value.contains('"') {
buf.reserve(value.len() + 2);
buf.push(b'"');
buf.extend_from_slice(value.as_bytes());
buf.push(b'"');
} else {
// it may contain reserved keywords: perform roundtrip for
// todo: avoid this roundtrip over serde_json
serde_json::to_writer(buf, &Value::String(value.to_string())).unwrap();
serde_json::to_writer(buf, value).unwrap();
}
}

Expand Down
19 changes: 19 additions & 0 deletions tests/it/io/json/write.rs
Expand Up @@ -305,3 +305,22 @@ fn write_escaped_utf8() -> Result<()> {
);
Ok(())
}

#[test]
fn write_quotation_marks_in_utf8() -> Result<()> {
let a = Utf8Array::<i32>::from(&vec![Some("a\"a"), None]);

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

let buf = write_batch(
batch,
vec!["c1".to_string()],
json_write::LineDelimited::default(),
)?;

assert_eq!(
String::from_utf8(buf).unwrap().as_bytes(),
b"{\"c1\":\"a\\\"a\"}\n{\"c1\":null}\n"
);
Ok(())
}

0 comments on commit 525d521

Please sign in to comment.