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

Commit

Permalink
Fixed error in serializing json
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgecarleitao committed Feb 5, 2022
1 parent 362ebf9 commit 9942445
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/io/json/write/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! APIs to write to JSON
mod format;
mod serialize;
mod utf8;
pub use fallible_streaming_iterator::*;
pub use format::*;
pub use serialize::serialize;
Expand Down
16 changes: 1 addition & 15 deletions src/io/json/write/serialize.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use lexical_core::ToLexical;
use serde_json::Value;
use streaming_iterator::StreamingIterator;

use crate::bitmap::utils::zip_validity;
Expand All @@ -8,6 +7,7 @@ use crate::io::iterator::BufStreamingIterator;
use crate::util::lexical_to_bytes_mut;
use crate::{array::*, datatypes::DataType, types::NativeType};

use super::utf8::utf8_serialize;
use super::{JsonArray, JsonFormat};

fn boolean_serializer<'a>(
Expand Down Expand Up @@ -137,20 +137,6 @@ fn list_serializer<'a, O: Offset>(
))
}

#[inline]
fn utf8_serialize(value: &str, buf: &mut Vec<u8>) {
if value.as_bytes().is_ascii() {
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();
}
}

fn new_serializer<'a>(
array: &'a dyn Array,
) -> Box<dyn StreamingIterator<Item = [u8]> + 'a + Send + Sync> {
Expand Down
21 changes: 20 additions & 1 deletion tests/it/io/json/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,26 @@ fn write_escaped_utf8() -> Result<()> {

assert_eq!(
String::from_utf8(buf).unwrap().as_bytes(),
b"{\"c1\":\"a\na\"}\n{\"c1\":null}\n"
b"{\"c1\":\"a\\na\"}\n{\"c1\":null}\n"
);
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 9942445

Please sign in to comment.