Skip to content

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

Merged
merged 4 commits into from
Aug 13, 2020

Conversation

var-const
Copy link
Contributor

GeoPoint and Timestamp are the only classes in the public API that have a well-defined JSON representation.

Fixes #3605

@changeset-bot
Copy link

changeset-bot bot commented Aug 12, 2020

💥 No Changeset

Latest 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 changesets

When 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 {
);
}

Copy link
Contributor Author

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?

Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation!

Comment on lines 105 to 116
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}'
);
Copy link
Contributor

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}
);

Copy link
Contributor Author

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 {
);
}

Copy link
Contributor

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.

@var-const var-const merged commit 32c7a7a into firebase:master Aug 13, 2020
@firebase firebase locked and limited conversation to collaborators Sep 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Customize JSON stringification behavior of firestore.GeoPoint
2 participants