firebase / firebase-js-sdk

Firebase Javascript SDK
https://firebase.google.com/docs/web/setup
Other
4.85k stars 892 forks source link

Customize JSON stringification behavior of firestore.GeoPoint #3605

Closed cola119 closed 4 years ago

cola119 commented 4 years ago

[REQUIRED] Describe your environment

[REQUIRED] Describe the problem

When stringify GeoPoint instance, it becomes like a {_lat: 30, _long: 133}. The private fields have been exposed and so confusing. For example, in nuxtjs, the data fetched on the server-side is serialized and posted to the client-side. This variable results above and we have to access like geopoint._lat instead of geopoint.latitude.

So I propose implementing toJSON method like below:

class GeoPoint {
  // ...
  toJSON = () => {
    return {
      longitude: this._long,
      latitude: this._lat
    }
  }
}

toJSON behavior reference

What do you think about this proposal?

Steps to reproduce:

Relevant Code:

TS Playground

// TODO(you): code here to reproduce the problem
var-const commented 4 years ago

Thank you for the feature request. We can add a well-defined JSON representation for GeoPoint and Timestamp (though not for other classes in the API); this will be fixed in the next release after #3615 is merged.

Also, thanks a lot for a very detailed description!

cola119 commented 4 years ago

Thanks! It's a big help.