【问题标题】:Trying to create a custom Json Deserializer for Microsoft.Spatial.GeographyPoint尝试为 Microsoft.Spatial.GeographyPoint 创建自定义 Json Deserializer
【发布时间】:2020-07-24 16:11:20
【问题描述】:

我正在使用 Azure 定位服务来检索某些位置。我返回的 Json 是一个像这样填充的对象数组(出于隐私考虑,省略了数据):

{
    "Id": "",
    "Name": "",
    "Address": "",
    "PostCode": "",
    "Location": {
      "Latitude": 123
      "Longitude": -123,
      "IsEmpty": false,
      "Z": null,
      "M": null,
      "CoordinateSystem": {
        "EpsgId": 123,
        "Id": "123",
        "Name": "ABC"
      }
    }
  },

我正在尝试反序列化为一个 LookUpModel 对象,该对象的位置具有 Microsoft.Spatial.Geography 点: public GeographyPoint Location { get; set; }

当我使用默认的 newtonsoft json 反序列化器时,我收到以下错误:“无法创建 Microsoft.Spatial.GeographyPoint 类型的实例。类型是接口或抽象类,无法实例化。路径'[0]。 Location.Latitude',第 11 行,第 17 位。”

因此,我正在尝试创建一个自定义的 Json Deserializer,它将创建一个 GeographyPoint(每当点击位置节点时使用 GeographyPoint.Create(Longitude = x, Latitude = y)。到目前为止,我有:

public class GeographyPointJsonConverter : JsonConverter
    {
        private readonly Type[] _types;
        public GeographyPointJsonConverter(params Type[] types)
        {
            _types = types;
        }
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            JToken t = JToken.FromObject(value);
            if (t.Type != JTokenType.Object)
            {
                t.WriteTo(writer);
            }
            else
            {
                JObject o = (JObject)t;
                IList<string> propertyNames = o.Properties().Select(p => p.Name).ToList();
                o.AddFirst(new JProperty("Keys", new JArray(propertyNames)));
                o.WriteTo(writer);
            }
        }
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            throw new NotImplementedException("Unnecessary because CanRead is false. The type will skip the converter.");
        }
        public override bool CanRead
        {
            get { return false; }
        }
        public override bool CanConvert(Type objectType)
        {
            return _types.Any(t => t == typeof(GeographyPoint));
        }
    }

被调用: var jsonData = JsonConvert.DeserializeObject&lt;LookUpModel[]&gt;(json, new GeographyPointJsonConverter(typeof(LookUpModel[])));

但是我不知道如何成功地将位置信息反序列化为GeographyPoint?请问有人能指出我正确的方向吗?

【问题讨论】:

    标签: c# .net json


    【解决方案1】:

    你的班级会是这样的

    public partial class MyClass
    {
        [JsonProperty("Id")]
        public string Id { get; set; }
    
        [JsonProperty("Name")]
        public string Name { get; set; }
    
        [JsonProperty("Address")]
        public string Address { get; set; }
    
        [JsonProperty("PostCode")]
        public string PostCode { get; set; }
    
        [JsonProperty("Location")]
        public Location Location { get; set; }
    }
    
    public partial class Location
    {
        [JsonProperty("Latitude")]
        public long Latitude { get; set; }
    
        [JsonProperty("Longitude")]
        public long Longitude { get; set; }
    
        [JsonProperty("IsEmpty")]
        public bool IsEmpty { get; set; }
    
        [JsonProperty("Z")]
        public object Z { get; set; }
    
        [JsonProperty("M")]
        public object M { get; set; }
    
        [JsonProperty("CoordinateSystem")]
        public CoordinateSystem CoordinateSystem { get; set; }
    }
    
    public partial class CoordinateSystem
    {
        [JsonProperty("EpsgId")]
        public long EpsgId { get; set; }
    
        [JsonProperty("Id")]
        [JsonConverter(typeof(ParseStringConverter))]
        public long Id { get; set; }
    
        [JsonProperty("Name")]
        public string Name { get; set; }
    }
    

    在您的 Json 中,逗号 (,) 后面缺少

    "Latitude": 123
    

    将你的 json 反序列化为对象

    MyClass obj = JsonConvert.DeserializeObject<MyClass>(json);
    

    【讨论】:

    • 谢谢@BhaveshPatel,虽然我不想那样反序列化它,但我想在通过自定义转换器反序列化时从 json 创建一个 Microsoft.Spatial.Geography 类型
    • 访问stackoverflow.com/a/40439958/10945191希望对您有所帮助。
    猜你喜欢
    • 2020-05-20
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    • 2021-11-26
    • 2018-04-11
    • 2014-11-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多