alamb commented on code in PR #5622:
URL: https://github.com/apache/arrow-rs/pull/5622#discussion_r1559751612
##########
arrow-json/src/writer.rs:
##########
@@ -2137,4 +2139,80 @@ mod tests {
Ok(())
}
+
+ #[test]
+ fn test_writer_fixed_size_binary() {
+ // set up schema:
+ let size = 11;
+ let schema = SchemaRef::new(Schema::new(vec![Field::new(
+ "bytes",
+ DataType::FixedSizeBinary(size),
+ true,
+ )]));
+
+ // build record batch:
+ let mut builder = FixedSizeBinaryBuilder::new(size);
+ let values = [Some(b"hello world"), None, Some(b"summer rain")];
+ for value in values {
+ match value {
+ Some(v) => builder.append_value(v).unwrap(),
+ None => builder.append_null(),
+ }
+ }
+ let array = Arc::new(builder.finish()) as ArrayRef;
+ let batch = RecordBatch::try_new(schema, vec![array]).unwrap();
+
+ // encode and check JSON with explicit nulls:
+ {
+ let mut buf = Vec::new();
+ let json_value: Value = {
+ let mut writer = WriterBuilder::new()
+ .with_explicit_nulls(true)
+ .build::<_, JsonArray>(&mut buf);
+ writer.write(&batch).unwrap();
+ writer.close().unwrap();
+ serde_json::from_slice(&buf).unwrap()
+ };
+
+ assert_eq!(
+ json!([
+ {
+ "bytes": "68656c6c6f20776f726c64"
+ },
+ {
+ "bytes": null // the explicit null
+ },
+ {
+ "bytes": "73756d6d6572207261696e"
+ }
+ ]),
+ json_value,
+ );
+ }
+ // encode and check JSON with no explicit nulls:
+ {
+ let mut buf = Vec::new();
+ let json_value: Value = {
+ // explicit nulls are off by default, so we don't need
+ // to set that when creating the writer:
+ let mut writer = ArrayWriter::new(&mut buf);
+ writer.write(&batch).unwrap();
+ writer.close().unwrap();
+ serde_json::from_slice(&buf).unwrap()
+ };
+
+ assert_eq!(
+ json!([
+ {
+ "bytes": "68656c6c6f20776f726c64"
+ },
+ {}, // empty because nulls are omitted
Review Comment:
👍
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]