package me.itszooti.geojson.gson;

import java.lang.reflect.Type;

import me.itszooti.geojson.GeoGeometry;
import me.itszooti.geojson.GeoLineString;
import me.itszooti.geojson.GeoPoint;
import me.itszooti.geojson.GeoPolygon;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

public class GeoGeometrySerializer implements JsonSerializer<GeoGeometry> {

	@Override
	public JsonElement serialize(GeoGeometry geom, Type type, JsonSerializationContext context) {
		// always an object with type
		JsonObject obj = new JsonObject();
		obj.addProperty("type", geom.getClass().getSimpleName().substring(3));
		
		// check type
		if (geom instanceof GeoPoint) {
			GeoPoint point = (GeoPoint)geom;
			obj.add("coordinates", context.serialize(point.getPosition()));
		} else if (geom instanceof GeoLineString) {
			GeoLineString lineString = (GeoLineString)geom;
			obj.add("coordinates", context.serialize(lineString.getPositions()));
		} else if (geom instanceof GeoPolygon) {
			GeoPolygon polygon = (GeoPolygon)geom;
			JsonArray coords = new JsonArray();
			coords.add(context.serialize(polygon.getExterior()));
			for (int i = 0; i < polygon.getNumInteriors(); i++) {
				coords.add(context.serialize(polygon.getInterior(i)));
			}
			obj.add("coordinates", coords);
		}
		
		return obj;
	}

}

@@ -142,16 +142,58 @@
 String;%0A
+import me.itszooti.geojson.GeoMultiPoint;%0A
 import m
@@ -1443,16 +1443,322 @@
 oords);%0A
+%09%09%7D else if (geom instanceof GeoMultiPoint) %7B%0A%09%09%09GeoMultiPoint multiPoint = (GeoMultiPoint)geom;%0A%09%09%09JsonArray coords = new JsonArray();%0A%09%09%09for (int i = 0; i %3C multiPoint.getNumPoints(); i++) %7B%0A%09%09%09%09coords.add(context.serialize(multiPoint.getPoint(i).getPosition()));%0A%09%09%09%7D%0A%09%09%09obj.add(%22coordinates%22, coords);%0A
 %09%09%7D%0A%09%09%0A%09

Implement MultiPoint encoding