package me.itszooti.geojson.gson;

import java.lang.reflect.Type;

import me.itszooti.geojson.GeoGeometry;
import me.itszooti.geojson.GeoLineString;
import me.itszooti.geojson.GeoMultiPoint;
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);
		} else if (geom instanceof GeoMultiPoint) {
			GeoMultiPoint multiPoint = (GeoMultiPoint)geom;
			JsonArray coords = new JsonArray();
			for (int i = 0; i < multiPoint.getNumPoints(); i++) {
				coords.add(context.serialize(multiPoint.getPoint(i).getPosition()));
			}
			obj.add("coordinates", coords);
		}
		
		return obj;
	}

}

@@ -142,16 +142,63 @@
 String;%0A
+import me.itszooti.geojson.GeoMultiLineString;%0A
 import m
@@ -1796,16 +1796,363 @@
 oords);%0A
+%09%09%7D else if (geom instanceof GeoMultiLineString) %7B%0A%09%09%09GeoMultiLineString multiLineString = (GeoMultiLineString)geom;%0A%09%09%09JsonArray coords = new JsonArray();%0A%09%09%09for (int i = 0; i %3C multiLineString.getNumLineStrings(); i++) %7B%0A%09%09%09%09coords.add(context.serialize(multiLineString.getLineString(i).getPositions()));%0A%09%09%09%7D%0A%09%09%09obj.add(%22coordinates%22, coords);%0A
 %09%09%7D%0A%09%09%0A%09

Implement MultiLineString encoding