phil-opp commented on code in PR #8737:
URL: https://github.com/apache/arrow-rs/pull/8737#discussion_r2494718551


##########
arrow-json/src/lib.rs:
##########
@@ -261,4 +283,135 @@ mod tests {
             assert_eq!(list_input, &list_output);
         }
     }
+
+    #[test]
+    fn test_json_roundtrip_binary() {
+        let values: [Option<&[u8]>; 3] = [
+            Some(b"Ned Flanders" as &[u8]),
+            None,
+            Some(b"Troy McClure" as &[u8]),
+        ];
+        // Binary:
+        {
+            let batch = build_array_binary::<i32>(&values);
+            assert_binary_json(&batch);
+        }
+        // LargeBinary:
+        {
+            let batch = build_array_binary::<i64>(&values);
+            assert_binary_json(&batch);
+        }
+        // FixedSizeBinary:
+        {
+            let batch = build_array_fixed_size_binary(12, &values);
+            assert_binary_json(&batch);
+        }
+        // BinaryView:
+        {
+            let batch = build_array_binary_view(&values);
+            assert_binary_json(&batch);
+        }
+    }
+
+    fn build_array_binary<O: OffsetSizeTrait>(values: &[Option<&[u8]>]) -> 
RecordBatch {
+        let schema = SchemaRef::new(Schema::new(vec![Field::new(
+            "bytes",
+            GenericBinaryType::<O>::DATA_TYPE,
+            true,
+        )]));
+        let mut builder = GenericByteBuilder::<GenericBinaryType<O>>::new();
+        for value in values {
+            match value {
+                Some(v) => builder.append_value(v),
+                None => builder.append_null(),
+            }
+        }
+        let array = Arc::new(builder.finish()) as ArrayRef;
+        RecordBatch::try_new(schema, vec![array]).unwrap()
+    }

Review Comment:
   Thanks for the suggestion, this is much nicer! I applied this change in 
85d16f218 and tried to further simplify things. I wasn't sure how to apply this 
to fixed size binary arrays, so I kept the builder pattern code for now. I'd be 
happy to simplify this too if you can show me how. Thanks!



-- 
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]

Reply via email to