-
Notifications
You must be signed in to change notification settings - Fork 946
Add toJSON
methods to GeoPoint
and Timestamp
#3615
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add toJSON
methods to GeoPoint
and Timestamp
#3615
Conversation
💥 No ChangesetLatest commit: 757e62e Merging this PR will not cause any packages to be released. If these changes should not cause updates to packages in this repo, this is fine 🙂 If these changes should be published to npm, you need to add a changeset. This PR includes no changesetsWhen changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types Click here to learn what changesets are, and how to add one. Click here if you're a maintainer who wants to add a changeset to this PR |
@@ -95,6 +95,10 @@ export class Timestamp { | |||
); | |||
} | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Strictly speaking, this isn't necessary -- Timestamp
would have the same JSON representation without this function. I mostly added it to make it explicit that Timestamp
has a well-defined JSON representation -- let me know if you prefer to remove it.
By the way, why is it that GeoPoint
uses different names for internal variables and getters while Timestamp
doesn't?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The implementation in GeoPoint is a little safer: the readonly
attribute is Typescript-only, so nothing saves a JavaScript user from directly modifying Timestamp.seconds
or Timestamp.nanoseconds
. To do the same with a GeoPoint, they would have to use an underscore prefixed variable (which is likely mangled), which would obviously be something we don't support.
Why Timestamp doesn't use this pattern is a question I can't answer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the explanation!
expect(JSON.stringify(new GeoPoint(1, 2))).to.equal( | ||
'{"latitude":1,"longitude":2}' | ||
); | ||
expect(JSON.stringify(new GeoPoint(0, 0))).to.equal( | ||
'{"latitude":0,"longitude":0}' | ||
); | ||
expect(JSON.stringify(new GeoPoint(-0, -0))).to.equal( | ||
'{"latitude":0,"longitude":0}' | ||
); | ||
expect(JSON.stringify(new GeoPoint(90, 180))).to.equal( | ||
'{"latitude":90,"longitude":180}' | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not 100% convinced that this format is guaranteed to always be stable across all implementations. Do you mind doing something like
expect(new GeoPoint(90, 180).toJSON()).to.deep.equal(
{latitude:90, longitude:180}
);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed.
@@ -95,6 +95,10 @@ export class Timestamp { | |||
); | |||
} | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The implementation in GeoPoint is a little safer: the readonly
attribute is Typescript-only, so nothing saves a JavaScript user from directly modifying Timestamp.seconds
or Timestamp.nanoseconds
. To do the same with a GeoPoint, they would have to use an underscore prefixed variable (which is likely mangled), which would obviously be something we don't support.
Why Timestamp doesn't use this pattern is a question I can't answer.
GeoPoint
andTimestamp
are the only classes in the public API that have a well-defined JSON representation.Fixes #3605