I’m using Gson to convert a class containing two GeoPoints to a JSON string. The code below is inside my class implementing LocationListener…
lat = (float) loc.getLatitude();
lng = (float) loc.getLongitude();
alt = (float) loc.getAltitude();
GeoPoint nextGeoPoint = new GeoPoint((int)(lat * 1E6), (int)(lng * 1E6));
MapPlot mapleg = new MapPlot();
mapleg.fromPoint = LastGeoPoint;
mapleg.toPoint = nextGeoPoint;
Gson gson = new Gson();
String jsonString = gson.toJson(mapleg); //convert the mapleg class to a json string
I expected the JSON string two sets of latitude and longitude but the following JSON string is generated…
{"fromPoint":{"mMapPoint":{"latitudeE6":33736724,"longitudeE6":-118101837,"pixelCoordX":92309232,"pixelCoordY":214935878}},"toPoint":{"mMapPoint":{"latitudeE6":37422004,"longitudeE6":-122084091,"pixelCoordX":86370464,"pixelCoordY":208176089}}}
What is with the pixel coordinates? the docs on GeoPoint don’t show anything to do with pixels. I address this issue when I take this data later and plot it on a mapView but I haven’t gotten there yet. Strange.
GSON looks at the fields on an object rather than the getter methods, and the GeoPoints actually have those private fields as well as the ones you’re interested in. See here for a request to make those fields accessible on GeoPoint: http://code.google.com/p/android/issues/detail?id=21327.
So GSON basically tries to serialize the whole state of the object, but you only want the externally visible stuff. You can change the behaviour by creating your Gson object like so: