File tree Expand file tree Collapse file tree 2 files changed +48
-5
lines changed Expand file tree Collapse file tree 2 files changed +48
-5
lines changed Original file line number Diff line number Diff line change @@ -207,11 +207,24 @@ export class JsonProtoSerializer {
207
207
* to actually return a Timestamp proto.
208
208
*/
209
209
private toTimestamp ( timestamp : Timestamp ) : string {
210
- return {
211
- seconds : '' + timestamp . seconds ,
212
- nanos : timestamp . nanoseconds
213
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
214
- } as any ;
210
+ if ( this . options . useProto3Json ) {
211
+ // Serialize to ISO-8601 date format, but with full nano resolution.
212
+ // Since JS Date has only millis, let's only use it for the seconds and
213
+ // then manually add the fractions to the end.
214
+ const jsDateStr = new Date ( timestamp . seconds * 1000 ) . toISOString ( ) ;
215
+ // Remove .xxx frac part and Z in the end.
216
+ const strUntilSeconds = jsDateStr . replace ( / \. \d * / , '' ) . replace ( 'Z' , '' ) ;
217
+ // Pad the fraction out to 9 digits (nanos).
218
+ const nanoStr = ( '000000000' + timestamp . nanoseconds ) . slice ( - 9 ) ;
219
+
220
+ return `${ strUntilSeconds } .${ nanoStr } Z` ;
221
+ } else {
222
+ return {
223
+ seconds : '' + timestamp . seconds ,
224
+ nanos : timestamp . nanoseconds
225
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
226
+ } as any ;
227
+ }
215
228
}
216
229
217
230
private fromTimestamp ( date : string | TimestampProto ) : Timestamp {
Original file line number Diff line number Diff line change @@ -300,6 +300,36 @@ describe('Serializer', () => {
300
300
) ;
301
301
} ) ;
302
302
303
+ it ( 'converts TimestampValue to string (useProto3Json=true)' , ( ) => {
304
+ expect (
305
+ proto3JsonSerializer . toValue (
306
+ new fieldValue . TimestampValue ( new Timestamp ( 1488872578 , 916123456 ) ) )
307
+ ) . to . deep . equal (
308
+ { timestampValue : '2017-03-07T07:42:58.916123456Z' }
309
+ ) ;
310
+
311
+ expect (
312
+ proto3JsonSerializer . toValue (
313
+ new fieldValue . TimestampValue ( new Timestamp ( 1488872578 , 916123000 ) ) )
314
+ ) . to . deep . equal (
315
+ { timestampValue : '2017-03-07T07:42:58.916123000Z' }
316
+ ) ;
317
+
318
+ expect (
319
+ proto3JsonSerializer . toValue (
320
+ new fieldValue . TimestampValue ( new Timestamp ( 1488872578 , 916000000 ) ) )
321
+ ) . to . deep . equal (
322
+ { timestampValue : '2017-03-07T07:42:58.916000000Z' }
323
+ ) ;
324
+
325
+ expect (
326
+ proto3JsonSerializer . toValue (
327
+ new fieldValue . TimestampValue ( new Timestamp ( 1488872578 , 0 ) ) )
328
+ ) . to . deep . equal (
329
+ { timestampValue : '2017-03-07T07:42:58.000000000Z' }
330
+ ) ;
331
+ } ) ;
332
+
303
333
it ( 'converts GeoPointValue' , ( ) => {
304
334
const example = new GeoPoint ( 1.23 , 4.56 ) ;
305
335
const expected = {
You can’t perform that action at this time.
0 commit comments