diff --git a/packages/google_cloud_firestore/CHANGELOG.md b/packages/google_cloud_firestore/CHANGELOG.md index 840867e9..8e5de693 100644 --- a/packages/google_cloud_firestore/CHANGELOG.md +++ b/packages/google_cloud_firestore/CHANGELOG.md @@ -1,6 +1,7 @@ ## Unreleased - Updated `Transaction.delete` and `Transaction.update` type constraints to accept `DocumentReference`. (thanks to @Levin-Me) +- Made `Timestamp` encodable by adding `toJson` method. (thanks to @OutdatedGuy) ## 0.5.2 diff --git a/packages/google_cloud_firestore/lib/src/timestamp.dart b/packages/google_cloud_firestore/lib/src/timestamp.dart index c8642b95..b0d5ee1d 100644 --- a/packages/google_cloud_firestore/lib/src/timestamp.dart +++ b/packages/google_cloud_firestore/lib/src/timestamp.dart @@ -197,4 +197,18 @@ final class Timestamp implements _Serializable { String toString() { return 'Timestamp(seconds=$seconds, nanoseconds=$nanoseconds)'; } + + /// Converts this Timestamp to a JSON representation. + /// + /// Returns a Map\ containing the seconds and nanoseconds. + /// + /// Example: + /// ```dart + /// final timestamp = Timestamp(seconds: 1234567890, nanoseconds: 123456789); + /// final json = timestamp.toJson(); + /// print(json); // {'_seconds': 1234567890, '_nanoseconds': 123456789} + /// ``` + Map toJson() { + return {'_seconds': seconds, '_nanoseconds': nanoseconds}; + } } diff --git a/packages/google_cloud_firestore/test/timestamp_test.dart b/packages/google_cloud_firestore/test/timestamp_test.dart index 7f36a86a..39c32b44 100644 --- a/packages/google_cloud_firestore/test/timestamp_test.dart +++ b/packages/google_cloud_firestore/test/timestamp_test.dart @@ -106,5 +106,11 @@ void main() { final timestamp = Timestamp.fromMillis(millis); expect(timestamp.toMillis(), millis); }); + + test('toJson() returns correct JSON representation', () { + final timestamp = Timestamp(seconds: 1234567890, nanoseconds: 123456789); + final json = timestamp.toJson(); + expect(json, {'_seconds': 1234567890, '_nanoseconds': 123456789}); + }); }); }