用Newtonsoft.Json反序列化DbGeometry [英] Deserializing DbGeometry with Newtonsoft.Json

查看:119
本文介绍了用Newtonsoft.Json反序列化DbGeometry的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在按照John Papa在其最新的PluralSight课程中概述的方法,使用Angular,Breeze和Web API 2构建SPA。



一切正常,并且我可以将信息,更新,插入,删除回服务器。但是,我使用的是空间类型,当我尝试使用空间类型更新实体时,出现以下错误


类型'Newtonsoft.Json.JsonSerializationException'发生在
Newtonsoft.Json.dll中,但未在用户代码中处理



其他信息:从'WellKnownValue获取值时出错'在
'System.Data.Entity.Spatial.DbGeometry'上。


内部异常似乎指向事实WellKnownValue为null,但不是,因为我检查了发送到服务器的JSON,然后将其发送到Breeze ContextProvider并使用SaveChanges方法保存。

  {
entities:[
{
TableKey:2,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:{}
}

所以我的问题是,可以反序列化DbGeometry类型,如果没有,则有什么建议可以解决。

解决方案

System.Data.Spatial.DbGeometry Newtonsoft.Json

不能很好地配合使用您需要创建一个 JsonConverter 来转换 DbGeometry

 公共类DbGeometryConverter:JsonConverter 
{
公共重写bool CanConvert(Type objectType)
{
return objectType.IsAssignableFrom(typeof(string) );
}

公共重写对象ReadJson(JsonReader reader,Type objectType,object existingValue,JsonSerializer serializer)
{
JObject location = JObject.Load(reader);
JToken token = location [ Geometry] [ WellKnownText];
字符串值= token.ToString();

DbGeometry已转换= DbGeometry.PolygonFromText(value,2193);
的回报已转换;
}

公共重写void WriteJson(JsonWriter writer,object value,JsonSerializer serializer)
{
//基本序列化是可以的
serializer.Serialize(作家,价值);
}
}

然后在模型的属性中添加属性

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

现在,当您点击BreezeController时,反序列化将由我们新的DbGeometryConverter处理。



希望有帮助。


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.

解决方案

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.

这篇关于用Newtonsoft.Json反序列化DbGeometry的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆