13

I'm building a SPA using Angular,Breeze and Web API 2 following the approach as outlined by John Papa in his latest PluralSight course.

Everything works well and I can pull information, update, insert, delete back to the server. However I'm using Spatial Types, and when I try to update an entity with a spatial type I get the following error

An exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll but was not handled in user code

Additional information: Error getting value from 'WellKnownValue' on 'System.Data.Entity.Spatial.DbGeometry'.

The inner exception seems to point to the fact that the WellKnownValue is null, its not though, as I have checked the JSON being sent to the server which is then sent to the Breeze ContextProvider and saved using the SaveChanges method.

{
"entities": [
 {
  "TableKey": 2,
  "CaseName": "Mikhail Lermontov",
  "StartDate": "2013-06-11T00:00:00Z",
  "EndDate": null,
  "IsCurrent": true,
  "SRID": 109,
  "Shape": {
    "$id": "2",
    "$type": "System.Data.Entity.Spatial.DbGeometry, EntityFramework",
    "Geometry": {
      "$id": "3",
      "$type": "System.Data.Entity.Spatial.DbGeometryWellKnownValue, EntityFramework",
      "CoordinateSystemId": 2193,
      "WellKnownText": "POLYGON ((1695943 5462665, 1713098 5462665, 1713098 5449659, 1695943 5449659, 1695943 5462665))"
    }
  },
  "SpillLocation": "Marlborough Sounds",
  "Image": "http://www.nzmaritime.co.nz/images/lm5.jpg\r\n",
  "DefaultBaseMapKey": 2,
  "__unmapped": {
    "isPartial": false
  },
  "entityAspect": {
    "entityTypeName": "DatSpillCase:#Osiris.Model",
    "defaultResourceName": "DatSpillCases",
    "entityState": "Modified",
    "originalValuesMap": {
      "CaseName": "Mikhail Lermontov"
    },
    "autoGeneratedKey": {
      "propertyName": "TableKey",
      "autoGeneratedKeyType": "Identity"
    }
  }
}
 ],
  "saveOptions": {}
}

So my question is, is possible to deserialize DbGeometry types within the NewtonSoft library, and if not, what suggestions are there to get around that.

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
Grunter
  • 187
  • 2
  • 12
  • What i use to serialize and de-serialize geography type variables is GeoJSON. There is already an uncomplete library for .Net at this address https://github.com/jbattermann/GeoJSON.Net though there are still some things missing you can always write your own serializers/deserializers from that project – Dominic Lacaille May 29 '14 at 18:58

3 Answers3

12

System.Data.Spatial.DbGeometry does not play nicely with Newtonsoft.Json

You need to create a JsonConverter to convert the DbGeometry

public class DbGeometryConverter : JsonConverter
    {
        public override bool CanConvert(Type objectType)
        {
            return objectType.IsAssignableFrom(typeof(string));
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            JObject location = JObject.Load(reader);
            JToken token = location["Geometry"]["WellKnownText"];
            string value = token.ToString();

            DbGeometry converted = DbGeometry.PolygonFromText(value, 2193);
            return converted;
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            // Base serialization is fine
            serializer.Serialize(writer, value);
        }
    }

Then on your property in your model add the attribute

[JsonConverter(typeof(DbGeometryConverter))]
public DbGeometry Shape { get; set; }

Now when you hit your BreezeController the deserialization will be handled by our new DbGeometryConverter.

Hope it helps.

qorsmond
  • 1,246
  • 1
  • 18
  • 29
  • 7
    This approach works well for `DbGeography` too, you only need to change `location["Geometry"]["WellKnownText"]` to `location["Geography"]["WellKnownText"]` and `DbGeometry converted = DbGeometry.PolygonFromText(value, 2193);` to `var converted = DbGeography.FromText(value);` – Adam Szabo Oct 30 '15 at 11:04
7

The answer above works great, but is hardcoded for SRID (CoordinateSystemId) 2193. The Coordinate System Id can however be present in the serialised data as shown in the question, or it can be present in the WellKnownText "SRID=2193;POINT (0 0)". Also this method will only read a polygon, but the WellKnownText can be a lot of things, like Geometry Collections, Point, Linestring, etc. To retreive this the ReadJson method can be updated to use the more generic FromText method as shown below. Here is the class above updated with a more generic Coordinate System, and also for any Geometry Type. I have also added the Geography version for reference.

public class DbGeometryConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return objectType.IsAssignableFrom(typeof(string));
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        JObject location = JObject.Load(reader);
        JToken token = location["Geometry"]["WellKnownText"];
        string value = token.ToString();
        JToken sridToken = location["Geometry"]["CoordinateSystemId"];
        int srid;
        if (sridToken == null || int.TryParse(sridToken.ToString(), out srid) == false || value.Contains("SRID"))
        {
            //Set default coordinate system here.
            srid = 0;
        }

        DbGeometry converted;
        if (srid > 0)
            converted = DbGeometry.FromText(value, srid);
        else
            converted = DbGeometry.FromText(value);
        return converted;
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        // Base serialization is fine
        serializer.Serialize(writer, value);
    }
}

public class DbGeographyConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return objectType.IsAssignableFrom(typeof(string));
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        JObject location = JObject.Load(reader);
        JToken token = location["Geography"]["WellKnownText"];
        string value = token.ToString();
        JToken sridToken = location["Geography"]["CoordinateSystemId"];
        int srid;
        if (sridToken == null || int.TryParse(sridToken.ToString(), out srid) == false || value.Contains("SRID"))
        {
            //Set default coordinate system here.
            //NOTE: Geography should always have an SRID, and it has to match the data in the database else all comparisons will return NULL!
            srid = 0;
        }
        DbGeography converted;
        if (srid > 0)
            converted = DbGeography.FromText(value, srid);
        else
            converted = DbGeography.FromText(value);
        return converted;
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        // Base serialization is fine
        serializer.Serialize(writer, value);
    }
}
Ali Karaca
  • 3,365
  • 1
  • 35
  • 41
Francois Grobler
  • 480
  • 1
  • 4
  • 11
  • 1
    I think for the DbGeographyConverter you need to use the "Geography" string instead of the "Geometry" to het token and sridToken. – Dennis Mar 05 '19 at 07:57
0

I don't see why not. On the line with (DbGeometryWellKnownValue):

"$type": "System.Data.Entity.Spatial.DbGeometryWellKnownValue, EntityFramework",

should this be (DbGeometry.WellKnownValue)?

  "$type": "System.Data.Entity.Spatial.DbGeometry.WellKnownValue, EntityFramework",
wilsotc
  • 800
  • 4
  • 10