c# - 如何使用 newtonsoft JSON 反序列化器反序列化 Geopoint?
问题描述
我正在使用以下类存储客户位置数据:
public class CustomerData
{
public string CustomerName { get; set; }
public int CustomerNumber { get; set; }
public string CustomerAddress { get; set; }
public string CustomerCity { get; set; }
public string CustomerZip { get; set; }
public string CustomerState { get; set; }
public Geopoint CustomerGeopoint { get; set; }
}
在 JSON 文件中......并使用如下服务检索数据:
public static async Task<ObservableCollection<CustomerData>> GetCustomerData()
{
var folder = ApplicationData.Current.LocalFolder;
var dataFile = await folder.TryGetItemAsync("CustomerData.json") as IStorageFile;
var stringResult = await FileIO.ReadTextAsync(dataFile);
ObservableCollection<CustomerData> CustomersRetrievedData = JsonConvert.DeserializeObject<ObservableCollection<CustomerData>>(stringResult, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.All
});
Customers = CustomersRetrievedData;
return await Task.FromResult(CustomersRetrievedData);
}
以及像这样保存数据:
public static async void SaveCustomerData()
{
var folder = ApplicationData.Current.LocalFolder;
StorageFile newFile = await folder.CreateFileAsync("CustomerData.json", CreationCollisionOption.ReplaceExisting);
var stringData = JsonConvert.SerializeObject(Customers);
await FileIO.WriteTextAsync(newFile, stringData);
}
我的问题是,在所有地理点数据都在那里之后,当我尝试通过在 GetCustomerData() 方法中反序列化数据来读取数据时,出现以下错误:
找不到用于类型的构造函数
Windows.Devices.Geolocation.Geopoint
。一个类应该有一个默认构造函数、一个带参数的构造函数或一个标有 JsonConstructor 属性的构造函数
我不明白如何解决这个问题,我在 newtonsoft 文档上找不到任何东西,有人知道这是怎么做的吗?
解决方案
查看文档,Geopoint
我们可以看到它有 3 个 .ctor,其中没有一个是无参数的。JSON.NET 需要一个无参数的 .ctor 来进行反序列化,正如您从收到的错误消息中看到的那样。
因此,您可以做的一件事是更改您的类以包含一个具有(您创建的)类型的属性,该类型反映了Geopoint
结构(包括其所有属性),但还包括一个无参数的 .ctor。您甚至可以实现IGeoshape
,以便它可以在任何IGeoshape
需要的地方使用。
类似于以下内容:
public class GeopointProxy : IGeoshape {
public GeopointProxy() { }
private AltitudeReferenceSystem _altitudeReferenceSystem;
// This is part of the IGeoshape interface, you can't change it's signature, and it's readonly. Fun.
[JsonIgnore]
public AltitudeReferenceSystem AltitudeReferenceSystem { get { return _altitudeReferenceSystem; } }
public AltitudeReferenceSystem SettableAltitudeReferenceSystem {
get {
return AltitudeReferenceSystem;
}
set {
_altitudeReferenceSystem = value;
}
}
// rinse and repeat as appropriate for the other interface members and class properties
// Then include a conversion function, or get fancy and add type conversion operators:
public Geopoint ToGeopoint() {
return new Geopoint(Position, AltitudeReferenceSystem, SpatialReferenceId);
}
}
推荐阅读
- html - Angular中复选框的标签
- if-statement - Prolog if-then-else 结构:-> vs *-> vs. if_/3
- angular - RxJS 所有单独的 Observables 已完成
- c++ - 我可以拥有一个 lambda 指针数组来保存具有不同类型参数/返回值的函数吗?
- json - JSON Builder 访问 JSON 数组中的变量
- php - Wordpress:致命错误:未捕获的ArgumentCountError:函数posts_custom_columns()的参数太少,传入1
- python - 查询导致 SQL Server 2016 死锁?
- javascript - 如何在另一个应用程序的 iframe 中包含 angular 4 应用程序?
- c# - 面向对象编程中的装饰器模式
- java - 提供 Spring Rest 文档