@@ -38,11 +38,13 @@ const TOP_LEVEL_KEYS: [&str; 10] = [
3838 "collection" ,
3939] ;
4040
41+ use crate :: Error ;
4142use arrow_array:: RecordBatchReader ;
4243use arrow_array:: { cast:: * , types:: * , * } ;
4344use arrow_cast:: display:: { ArrayFormatter , FormatOptions } ;
4445use arrow_json:: JsonSerializable ;
4546use arrow_schema:: * ;
47+ use chrono:: DateTime ;
4648use geo_traits:: to_geo:: {
4749 ToGeoGeometry , ToGeoGeometryCollection , ToGeoLineString , ToGeoMultiLineString , ToGeoMultiPoint ,
4850 ToGeoMultiPolygon , ToGeoPoint , ToGeoPolygon , ToGeoRect ,
@@ -53,6 +55,8 @@ use geoarrow_array::cast::AsGeoArrowArray;
5355use serde_json:: { Value , json, map:: Map as JsonMap } ;
5456use std:: iter;
5557
58+ use super :: DATETIME_COLUMNS ;
59+
5660fn primitive_array_to_json < T > ( array : & dyn Array ) -> Result < Vec < Value > , ArrowError >
5761where
5862 T : ArrowPrimitiveType ,
@@ -427,7 +431,7 @@ fn set_column_for_json_rows(
427431/// Creates JSON values from a record batch reader.
428432pub fn from_record_batch_reader < R : RecordBatchReader > (
429433 reader : R ,
430- ) -> Result < Vec < serde_json:: Map < String , Value > > , crate :: Error > {
434+ ) -> Result < Vec < serde_json:: Map < String , Value > > , Error > {
431435 use geoarrow_array:: GeoArrowType ;
432436
433437 let schema = reader. schema ( ) ;
@@ -489,16 +493,18 @@ pub fn from_record_batch_reader<R: RecordBatchReader>(
489493 "geometry" . into ( ) ,
490494 serde_json:: to_value ( geojson:: Geometry :: new ( value) ) ?,
491495 ) ;
492- items. push ( unflatten ( row) ) ;
496+ items. push ( unflatten ( row) ? ) ;
493497 }
494498 }
495499 } else {
496- items = json_rows. map ( unflatten) . collect ( ) ;
500+ items = json_rows. map ( unflatten) . collect :: < Result < _ , Error > > ( ) ? ;
497501 }
498502 Ok ( items)
499503}
500504
501- fn unflatten ( mut item : serde_json:: Map < String , Value > ) -> serde_json:: Map < String , Value > {
505+ fn unflatten (
506+ mut item : serde_json:: Map < String , Value > ,
507+ ) -> Result < serde_json:: Map < String , Value > , Error > {
502508 let mut properties = serde_json:: Map :: new ( ) ;
503509 let keys: Vec < _ > = item
504510 . keys ( )
@@ -512,13 +518,25 @@ fn unflatten(mut item: serde_json::Map<String, Value>) -> serde_json::Map<String
512518 . collect ( ) ;
513519 for key in keys {
514520 if let Some ( value) = item. remove ( & key) {
515- let _ = properties. insert ( key, value) ;
521+ if DATETIME_COLUMNS . contains ( & key. as_str ( ) ) {
522+ if let Some ( value) = value. as_str ( ) {
523+ let _ = properties. insert (
524+ key,
525+ DateTime :: parse_from_rfc3339 ( value) ?
526+ . to_utc ( )
527+ . to_rfc3339 ( )
528+ . into ( ) ,
529+ ) ;
530+ }
531+ } else {
532+ let _ = properties. insert ( key, value) ;
533+ }
516534 }
517535 }
518536 if !properties. is_empty ( ) {
519537 let _ = item. insert ( "properties" . to_string ( ) , Value :: Object ( properties) ) ;
520538 }
521- item
539+ Ok ( item)
522540}
523541
524542fn record_batches_to_json_rows (
0 commit comments