Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# Json.net DbGeography deserialize error

I am trying to get Spartial Data from a Database and then Serialize it (working great). When I try to deserialize that Data a JsonSerializationException is thrown.

DbGeography geoPoint = CreatePoint(40.7056308, -73.9780035);
string serializedPoint = JsonConvert.SerializeObject(geoPoint);
DbGeography resjson = JsonConvert.DeserializeObject<DbGeography>(serializedPoint);

Here is the CreatePoint Method:

public static DbGeography CreatePoint(double latitude, double longitude)
{
string text = string.Format(CultureInfo.InvariantCulture.NumberFormat,
"POINT({0} {1})", longitude, latitude);
return DbGeography.PointFromText(text, 4326);
}

I produced this Error in a Console Application.

Nuget Packages installed: 
EntityFramework 6.1.0
Newtonsoft.Json 6.0.3

Does anybody know what I am doing wrong?

like image 399
Alex Doe Avatar asked May 21 '14 17:05

Alex Doe


1 Answers

System.Data.Spatial.DbGeography (which I assume you are using) is not suitable for deserialization by Newtonsoft.Json. You do not mention the thrown exception, but the one I observe is:

Unable to find a constructor to use for type System.Data.Spatial.DbGeography. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute.

You can resolve this by providing the serialization/deserialization logic yourself by means of a JsonConverter. I've used the one from JSON.Net JsonConverter for DbGeography and slightly modified it to fix a bug for my local culture (which uses ',' as the decimal separator):

public class DbGeographyConverter : JsonConverter
{
    private const string LATITUDE_KEY = "latitude";
    private const string LONGITUDE_KEY = "longitude";

    public override bool CanConvert(Type objectType)
    {
        return objectType.Equals(typeof(DbGeography));
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        if (reader.TokenType == JsonToken.Null)
            return default(DbGeography);

        var jObject = JObject.Load(reader);

        if (!jObject.HasValues || (jObject.Property(LATITUDE_KEY) == null || jObject.Property(LONGITUDE_KEY) == null))
            return default(DbGeography);

        string wkt = string.Format(CultureInfo.InvariantCulture, "POINT({1} {0})", jObject[LATITUDE_KEY], jObject[LONGITUDE_KEY]);
        return DbGeography.FromText(wkt, DbGeography.DefaultCoordinateSystemId);
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        var dbGeography = value as DbGeography;

        serializer.Serialize(writer, dbGeography == null || dbGeography.IsEmpty ? null : new { latitude = dbGeography.Latitude.Value, longitude = dbGeography.Longitude.Value });
    }
}

You can then use it like this:

DbGeography geoPoint = CreatePoint(40.7056308, -73.9780035);
string serializedPoint = JsonConvert.SerializeObject(geoPoint, new DbGeographyConverter());
DbGeography resjson = JsonConvert.DeserializeObject<DbGeography>(serializedPoint, new DbGeographyConverter());
like image 191
TC. Avatar answered Oct 06 '22 16:10

TC.