@@ -79,11 +79,6 @@ export class GeoPoint {
7979 return this . _lat === other . _lat && this . _long === other . _long ;
8080 }
8181
82- /** Returns a JSON-serializable representation of this GeoPoint. */
83- toJSON ( ) : { latitude : number ; longitude : number } {
84- return { latitude : this . _lat , longitude : this . _long } ;
85- }
86-
8782 /**
8883 * Actually private to JS consumers of our API, so this function is prefixed
8984 * with an underscore.
@@ -94,4 +89,53 @@ export class GeoPoint {
9489 primitiveComparator ( this . _long , other . _long )
9590 ) ;
9691 }
92+
93+ /** Returns a JSON-serializable representation of this GeoPoint. */
94+ toJSON ( ) : { latitude : number ; longitude : number ; type : string } {
95+ return {
96+ latitude : this . _lat ,
97+ longitude : this . _long ,
98+ type : 'firestore/geopoint/1.0'
99+ } ;
100+ }
101+
102+ /** Builds a `Timestamp` instance from a JSON serialized version of `Bytes`. */
103+ static fromJSON ( json : object ) : GeoPoint {
104+ const requiredFields = [ 'type' , 'latitude' , 'longitude' ] ;
105+ let error : string | undefined = undefined ;
106+ let lat : number = 0 ;
107+ let long : number = 0 ;
108+ for ( const key of requiredFields ) {
109+ if ( ! ( key in json ) ) {
110+ error = `json missing required field: ${ key } ` ;
111+ }
112+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
113+ const value = ( json as any ) [ key ] ;
114+ if ( key === 'type' ) {
115+ if ( typeof value !== 'string' ) {
116+ error = `json field 'type' must be a string.` ;
117+ break ;
118+ } else if ( value !== 'firestore/geopoint/1.0' ) {
119+ error = "Expected 'type' field to equal 'firestore/geopoint/1.0'" ;
120+ break ;
121+ }
122+ } else if ( key === 'latitude' ) {
123+ if ( typeof value !== 'number' ) {
124+ error = `json field 'latitude' must be a number.` ;
125+ break ;
126+ }
127+ lat = value ;
128+ } else {
129+ if ( typeof value !== 'number' ) {
130+ error = `json field 'longitude' must be a string.` ;
131+ break ;
132+ }
133+ long = value ;
134+ }
135+ }
136+ if ( error ) {
137+ throw new FirestoreError ( Code . INVALID_ARGUMENT , error ) ;
138+ }
139+ return new GeoPoint ( lat , long ) ;
140+ }
97141}
0 commit comments