Skip to content

JSON Encoding of Minified Property Names #13

Closed
rviscomi opened this Issue Mar 19, 2013 · 1 comment

1 participant

@rviscomi
Owner

The toJSON method of the RedDwarf object returns an object containing a geocodes property. The value of this property is an object of key-value pairs where keys are GitHub user-defined locations ("New York, NY") and values are their corresponding LatLng objects.

LatLng objects are defined by google.maps.LatLng. The properties of this object include values for latitude and longitude, as well as a few utility methods. The problem with this object is that the property names for the lat/long values change depending on the aggressive code minification. For example, sometimes latitude may be referenced by "a" and longitude by "b". When the code base changes and is recompressed, these property names may change. Internally to the Google Maps API, this is not a problem because the corresponding methods .lat() and .lng() return the appropriate values. Externally, however, this inconsistency poses a problem when JSON encoding the objects.

JSON encoding the LatLng objects would yield a stringified version of an object similar to {a: [latitude], b: [longitude]}. The code to read geocodes from cache needs to reference latitudes and longitudes by these minified property names, causing a tightly coupled and highly unstable dependency.

ALAS! There is a fantastically straightforward workaround. When stringifying the geocodes object, the user agent will attempt to call a .toJSON() method on the object's values. By adding this custom method to the prototype of the LatLng object, stringified geocodes can consistently reference unique property names for latitudes and longitudes.

@rviscomi rviscomi added a commit that referenced this issue Mar 19, 2013
@rviscomi #13 consistent JSON stringification of geocodes
add custom toJSON method on google.maps.LatLng prototype
59f70dd
@rviscomi rviscomi closed this Mar 19, 2013
@rviscomi
Owner

Instead of "a" and "b" in the JSON encoding, RedDwarf will now always produce a JSON string corresponding to objects like this:

{
  longitude: [longitude],
  latitude: [latitude]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.