package it.mice.voila.runtime.hibernatespatial.serializers;

import it.mice.voila.runtime.hibernatespatial.utils.GisConvertUtils;

import java.io.IOException;

import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.DeserializationContext;
import org.codehaus.jackson.map.JsonDeserializer;

import com.vividsolutions.jts.geom.Point;

public class GisPointDeserializer extends JsonDeserializer<Point> {
	@Override
	public Point deserialize(JsonParser jp, DeserializationContext ctxt)
			throws IOException, JsonProcessingException {
		
		String tmp = jp.getText(); // {
        jp.nextToken(); // skip over { to the rel
        jp.nextToken(); // skip over 'latitude' text
        String latitude = jp.getText();
        jp.nextToken(); // skip over  {
        jp.nextToken(); // skip over 'longitude' text
        String longitude = jp.getText();
        jp.nextToken(); // skip over  {

        Point result = (Point)GisConvertUtils.toGeometry(latitude, longitude);
		return result;
	}
}
