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

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

import java.io.IOException;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.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;
	}
}
