Skip to content

Commit 32c7a7a

Browse files
authored
Add toJSON methods to GeoPoint and Timestamp (#3615)
`GeoPoint` and `Timestamp` are the only classes in the public API that have a well-defined JSON representation. Fixes #3605
1 parent 4e09c70 commit 32c7a7a

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

packages/firestore/src/api/geo_point.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ export class GeoPoint {
7171
return this._lat === other._lat && this._long === other._long;
7272
}
7373

74+
toJSON(): { latitude: number; longitude: number } {
75+
return { latitude: this._lat, longitude: this._long };
76+
}
77+
7478
/**
7579
* Actually private to JS consumers of our API, so this function is prefixed
7680
* with an underscore.

packages/firestore/src/api/timestamp.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ export class Timestamp {
9595
);
9696
}
9797

98+
toJSON(): { seconds: number; nanoseconds: number } {
99+
return { seconds: this.seconds, nanoseconds: this.nanoseconds };
100+
}
101+
98102
valueOf(): string {
99103
// This method returns a string of the form <seconds>.<nanoseconds> where <seconds> is
100104
// translated to have a non-negative value and both <seconds> and <nanoseconds> are left-padded

packages/firestore/test/unit/api/geo_point.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,19 @@ describe('GeoPoint', () => {
100100
expectNotEqual(new GeoPoint(1, 2), new GeoPoint(1, 1));
101101
expectNotEqual(new GeoPoint(1, 2), new GeoPoint(2, 1));
102102
});
103+
104+
it('serializes to JSON', () => {
105+
expect(new GeoPoint(1, 2).toJSON()).to.deep.equal({
106+
latitude: 1,
107+
longitude: 2
108+
});
109+
expect(new GeoPoint(0, 0).toJSON()).to.deep.equal({
110+
latitude: 0,
111+
longitude: 0
112+
});
113+
expect(new GeoPoint(90, 180).toJSON()).to.deep.equal({
114+
latitude: 90,
115+
longitude: 180
116+
});
117+
});
103118
});

packages/firestore/test/unit/api/timestamp.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,19 @@ describe('Timestamp', () => {
132132
expect(t1 > t2).to.be.false;
133133
expect(t1 >= t2).to.be.false;
134134
});
135+
136+
it('serializes to JSON', () => {
137+
expect(new Timestamp(123, 456).toJSON()).to.deep.equal({
138+
seconds: 123,
139+
nanoseconds: 456
140+
});
141+
expect(new Timestamp(0, 0).toJSON()).to.deep.equal({
142+
seconds: 0,
143+
nanoseconds: 0
144+
});
145+
expect(new Timestamp(-123, 456).toJSON()).to.deep.equal({
146+
seconds: -123,
147+
nanoseconds: 456
148+
});
149+
});
135150
});

0 commit comments

Comments
 (0)