简体   繁体   English

地理位置反序列化错误(Newtonsoft.Json)-2146233088

[英]Geoposition deserialization error (Newtonsoft.Json) -2146233088

I'm trying to deserialize a Geoposition Object in my Windows Phone (WinRT) App but getting this Exception : 我正在尝试在Windows Phone(WinRT)应用程序中反序列化Geoposition对象,但出现此Exception

-2146233088 with Message: "Unable to find a constructor to use for type Windows.Devices.Geolocation.Geoposition . A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path 'CivicAddress'". -2146233088,消息:“无法找到用于Windows.Devices.Geolocation.Geoposition类型的构造函数。类应具有默认构造函数,一个带参数的构造函数或带有JsonConstructor属性标记的构造函数。路径'CivicAddress'” 。

Is Geoposition not compatible with Newtonsoft Json.NET? Geoposition与Newtonsoft Json.NET不兼容吗? If it is not, I could use some help in finding a workaround. 如果不是这样,我可以使用一些帮助找到解决方法。

The code snippet below shows where the exception is thrown. 下面的代码片段显示了引发异常的位置。

public async void Sample()
{    
    Geolocator geo = new Geolocator();
    geo.DesiredAccuracyInMeters = 50;
    var x = await geo.GetGeopositionAsync();
    Geoposition geo = JsonConvert.DeserializeObject<Geoposition>(CurrentContext.LocationAsString);
    await EventMap.TrySetViewAsync(geo.Coordinate.Point, 18D);
}

It seems like Geoposition only has read-only properties, and no public constructor that takes values for these properties as constructor arguments. 似乎Geoposition仅具有只读属性,并且没有将这些属性的值用作构造函数参数的公共构造函数。 So you can serialize it, but not construct it during deserialization. 因此,您可以序列化它,但不能在反序列化期间构造它。

What you could do as a work-around, is to construct your own version of this class specifically for serialization purposes, including only those properties you actually need in your app. 解决方法您可以做的是构造您自己的此类的版本,专门用于序列化目的,仅包括应用程序中实际需要的那些属性。

For example, if as in your question, you only need the Geoposition.Coordinate.Point field to be serialized, you could do the following: 例如,如果如您的问题中所示,仅需要序列化Geoposition.Coordinate.Point字段,则可以执行以下操作:

namespace YourAppNameSpaceHere
{
    using Windows.Devices.Geolocation;

    public class SerializableGeoPoint
    {
        public SerializableGeoPoint(Geoposition location) 
            : this(location.Coordinate.Point) {}

        public SerializableGeoPoint(Geopoint point)
        {
            this.AltitudeReferenceSystem = point.AltitudeReferenceSystem;
            this.GeoshapeType = point.GeoshapeType;
            this.Position = point.Position;
            this.SpatialReferenceId = point.SpatialReferenceId;
        }

        [JsonConverter(typeof(StringEnumConverter))]
        public AltitudeReferenceSystem AltitudeReferenceSystem { get; set; }

        [JsonConverter(typeof(StringEnumConverter))]
        public GeoshapeType GeoshapeType { get; set; }

        [JsonProperty]
        public BasicGeoposition Position { get; set; }

        [JsonProperty]
        public uint SpatialReferenceId { get; set; }

        public Geopoint ToWindowsGeopoint()
        {
            return new Geopoint(Position, AltitudeReferenceSystem, SpatialReferenceId);
        }
    }
}

When serializing you can then do: 序列化时,您可以执行以下操作:

Geoposition position = ....; // call to get position.
CurrentContext.LocationAsString = JsonConvert
    .SerializeObject(new SerializableGeoPoint(position));

And deserializing as: 反序列化为:

var locationPoint = JsonConvert
    .DeserializeObject<SerializableGeoPoint>(CurrentContext.LocationAsString)
    .ToWindowsGeopoint();

You may need to add this for your serialization settings somewhere during application startup, where you perform other one-time initializations for your app. 您可能需要在应用程序启动期间的某个地方为序列化设置添加此设置,在此您可以对应用程序执行其他一次性初始化。

JsonConvert.DefaultSettings = (() =>
{
    var settings = new JsonSerializerSettings();
    settings.Converters.Add(new StringEnumConverter());
    return settings;
});

It specifies the default settings for JSON serialization, in this case adding the converter that knows how to convert between enums and strings. 它指定JSON序列化的默认设置,在这种情况下,添加知道如何在枚举和字符串之间转换的转换器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM