@@ -37,6 +37,16 @@ pub struct EncoderOptions {
37
37
struct_mode : StructMode ,
38
38
/// An optional hook for customizing encoding behavior.
39
39
encoder_factory : Option < Arc < dyn EncoderFactory > > ,
40
+ /// Optional date format for date arrays
41
+ date_format : Option < String > ,
42
+ /// Optional datetime format for datetime arrays
43
+ datetime_format : Option < String > ,
44
+ /// Optional timestamp format for timestamp arrays
45
+ timestamp_format : Option < String > ,
46
+ /// Optional timestamp format for timestamp with timezone arrays
47
+ timestamp_tz_format : Option < String > ,
48
+ /// Optional time format for time arrays
49
+ time_format : Option < String > ,
40
50
}
41
51
42
52
impl EncoderOptions {
@@ -72,6 +82,61 @@ impl EncoderOptions {
72
82
pub fn encoder_factory ( & self ) -> Option < & Arc < dyn EncoderFactory > > {
73
83
self . encoder_factory . as_ref ( )
74
84
}
85
+
86
+ /// Set the JSON file's date format
87
+ pub fn with_date_format ( mut self , format : String ) -> Self {
88
+ self . date_format = Some ( format) ;
89
+ self
90
+ }
91
+
92
+ /// Get the JSON file's date format if set, defaults to RFC3339
93
+ pub fn date_format ( & self ) -> Option < & str > {
94
+ self . date_format . as_deref ( )
95
+ }
96
+
97
+ /// Set the JSON file's datetime format
98
+ pub fn with_datetime_format ( mut self , format : String ) -> Self {
99
+ self . datetime_format = Some ( format) ;
100
+ self
101
+ }
102
+
103
+ /// Get the JSON file's datetime format if set, defaults to RFC3339
104
+ pub fn datetime_format ( & self ) -> Option < & str > {
105
+ self . datetime_format . as_deref ( )
106
+ }
107
+
108
+ /// Set the JSON file's time format
109
+ pub fn with_time_format ( mut self , format : String ) -> Self {
110
+ self . time_format = Some ( format) ;
111
+ self
112
+ }
113
+
114
+ /// Get the JSON file's datetime time if set, defaults to RFC3339
115
+ pub fn time_format ( & self ) -> Option < & str > {
116
+ self . time_format . as_deref ( )
117
+ }
118
+
119
+ /// Set the JSON file's timestamp format
120
+ pub fn with_timestamp_format ( mut self , format : String ) -> Self {
121
+ self . timestamp_format = Some ( format) ;
122
+ self
123
+ }
124
+
125
+ /// Get the JSON file's timestamp format if set, defaults to RFC3339
126
+ pub fn timestamp_format ( & self ) -> Option < & str > {
127
+ self . timestamp_format . as_deref ( )
128
+ }
129
+
130
+ /// Set the JSON file's timestamp tz format
131
+ pub fn with_timestamp_tz_format ( mut self , tz_format : String ) -> Self {
132
+ self . timestamp_tz_format = Some ( tz_format) ;
133
+ self
134
+ }
135
+
136
+ /// Get the JSON file's timestamp tz format if set, defaults to RFC3339
137
+ pub fn timestamp_tz_format ( & self ) -> Option < & str > {
138
+ self . timestamp_tz_format . as_deref ( )
139
+ }
75
140
}
76
141
77
142
/// A trait to create custom encoders for specific data types.
@@ -350,8 +415,14 @@ pub fn make_encoder<'a>(
350
415
// characters that would need to be escaped within a JSON string, e.g. `'"'`.
351
416
// If support for user-provided format specifications is added, this assumption
352
417
// may need to be revisited
353
- let options = FormatOptions :: new( ) . with_display_error( true ) ;
354
- let formatter = ArrayFormatter :: try_new( array, & options) ?;
418
+ let fops = FormatOptions :: new( ) . with_display_error( true )
419
+ . with_date_format( options. date_format. as_deref( ) )
420
+ . with_datetime_format( options. datetime_format. as_deref( ) )
421
+ . with_timestamp_format( options. timestamp_format. as_deref( ) )
422
+ . with_timestamp_tz_format( options. timestamp_tz_format. as_deref( ) )
423
+ . with_time_format( options. time_format. as_deref( ) ) ;
424
+
425
+ let formatter = ArrayFormatter :: try_new( array, & fops) ?;
355
426
let formatter = JsonArrayFormatter :: new( formatter) ;
356
427
NullableEncoder :: new( Box :: new( formatter) as Box <dyn Encoder + ' a>, nulls)
357
428
}
0 commit comments