Deserializing DbGeometry with Newtonsoft.Json

后端 未结 3 598
挽巷
挽巷 2021-02-13 19:22

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

相关标签:
3条回答
  • 2021-02-13 19:47

    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.

    0 讨论(0)
  • 2021-02-13 20:04

    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["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.
                //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);
        }
    }
    
    0 讨论(0)
  • 2021-02-13 20:05

    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",
    
    0 讨论(0)
提交回复
热议问题