@@ -2147,38 +2147,72 @@ mod tests {
21472147 let schema = SchemaRef :: new ( Schema :: new ( vec ! [ Field :: new(
21482148 "bytes" ,
21492149 DataType :: FixedSizeBinary ( size) ,
2150- false ,
2150+ true ,
21512151 ) ] ) ) ;
21522152
21532153 // build record batch:
21542154 let mut builder = FixedSizeBinaryBuilder :: new ( size) ;
2155- let values = [ b"hello world" , b"summer rain" ] ;
2156- for v in values {
2157- builder. append_value ( v) . unwrap ( ) ;
2155+ let values = [ Some ( b"hello world" ) , None , Some ( b"summer rain" ) ] ;
2156+ for value in values {
2157+ match value {
2158+ Some ( v) => builder. append_value ( v) . unwrap ( ) ,
2159+ None => builder. append_null ( ) ,
2160+ }
21582161 }
21592162 let array = Arc :: new ( builder. finish ( ) ) as ArrayRef ;
21602163 let batch = RecordBatch :: try_new ( schema, vec ! [ array] ) . unwrap ( ) ;
21612164
2162- // encode JSON:
2163- let mut buf = Vec :: new ( ) ;
2164- let json_value: Value = {
2165- let mut writer = ArrayWriter :: new ( & mut buf) ;
2166- writer. write ( & batch) . unwrap ( ) ;
2167- writer. close ( ) . unwrap ( ) ;
2168- serde_json:: from_slice ( & buf) . unwrap ( )
2169- } ;
2170-
2171- // check the encoded JSON:
2172- assert_eq ! (
2173- json!( [
2174- {
2175- "bytes" : "68656c6c6f20776f726c64"
2176- } ,
2177- {
2178- "bytes" : "73756d6d6572207261696e"
2179- }
2180- ] ) ,
2181- json_value,
2182- ) ;
2165+ // encode and check JSON with explicit nulls:
2166+ {
2167+ let mut buf = Vec :: new ( ) ;
2168+ let json_value: Value = {
2169+ let mut writer = WriterBuilder :: new ( )
2170+ . with_explicit_nulls ( true )
2171+ . build :: < _ , JsonArray > ( & mut buf) ;
2172+ writer. write ( & batch) . unwrap ( ) ;
2173+ writer. close ( ) . unwrap ( ) ;
2174+ serde_json:: from_slice ( & buf) . unwrap ( )
2175+ } ;
2176+
2177+ assert_eq ! (
2178+ json!( [
2179+ {
2180+ "bytes" : "68656c6c6f20776f726c64"
2181+ } ,
2182+ {
2183+ "bytes" : null // the explicit null
2184+ } ,
2185+ {
2186+ "bytes" : "73756d6d6572207261696e"
2187+ }
2188+ ] ) ,
2189+ json_value,
2190+ ) ;
2191+ }
2192+ // encode and check JSON with no explicit nulls:
2193+ {
2194+ let mut buf = Vec :: new ( ) ;
2195+ let json_value: Value = {
2196+ // explicit nulls are off by default, so we don't need
2197+ // to set that when creating the writer:
2198+ let mut writer = ArrayWriter :: new ( & mut buf) ;
2199+ writer. write ( & batch) . unwrap ( ) ;
2200+ writer. close ( ) . unwrap ( ) ;
2201+ serde_json:: from_slice ( & buf) . unwrap ( )
2202+ } ;
2203+
2204+ assert_eq ! (
2205+ json!( [
2206+ {
2207+ "bytes" : "68656c6c6f20776f726c64"
2208+ } ,
2209+ { } , // empty because nulls are omitted
2210+ {
2211+ "bytes" : "73756d6d6572207261696e"
2212+ }
2213+ ] ) ,
2214+ json_value,
2215+ ) ;
2216+ }
21832217 }
21842218}
0 commit comments