Skip to content

Commit 20b6831

Browse files
committed
Initial commit
1 parent 5b87b59 commit 20b6831

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-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(JSON.stringify(new GeoPoint(1, 2))).to.equal(
106+
'{"latitude":1,"longitude":2}'
107+
);
108+
expect(JSON.stringify(new GeoPoint(0, 0))).to.equal(
109+
'{"latitude":0,"longitude":0}'
110+
);
111+
expect(JSON.stringify(new GeoPoint(-0, -0))).to.equal(
112+
'{"latitude":0,"longitude":0}'
113+
);
114+
expect(JSON.stringify(new GeoPoint(90, 180))).to.equal(
115+
'{"latitude":90,"longitude":180}'
116+
);
117+
});
103118
});

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,16 @@ 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(JSON.stringify(new Timestamp(123, 456))).to.equal(
138+
'{"seconds":123,"nanoseconds":456}'
139+
);
140+
expect(JSON.stringify(new Timestamp(0, 0))).to.equal(
141+
'{"seconds":0,"nanoseconds":0}'
142+
);
143+
expect(JSON.stringify(new Timestamp(-123, 456))).to.equal(
144+
'{"seconds":-123,"nanoseconds":456}'
145+
);
146+
});
135147
});

0 commit comments

Comments
 (0)