我使用类型化的DocumentQuery从Azure DocumentDb的集合中读取文档.
from f in client.CreateDocumentQuery<MyModel>(Collection.SelfLink) select f
因为我找不到如何设置neccesarry自定义json转换器的方法,所以它抛出了这个例子:
Could not create an instance of type AbstractObject. Type is an
interface or abstract class and cannot be instantiated.
通常你会做这样的事情来使它工作:
var settings = new JsonSerializerSettings(); settings.Converters.Add(new MyAbstractConverter()); client.SerializerSettings = settings;
DocumentClient没有任何SerializerSettings.所以问题是,如何在将json数据反序列化到我的模型时告诉DocumentDB客户端它必须使用自定义转换器?
解决方法
您可以将[JsonConverter(typeof(MyAbstractConverter))]添加到模型类中.
这是一个带有自定义Json设置的示例模型类:
namespace DocumentDB.Samples.Twitter { using System; using System.Collections.Generic; using System.Linq; using DocumentDB.Samples.Shared.Util; using Newtonsoft; using Newtonsoft.Json; /// <summary> /// Represents a user. /// </summary> public class User { [JsonProperty("id")] public long UserId { get; set; } [JsonProperty("name")] public string Name { get; set; } [JsonProperty("screen_name")] public string ScreenName { get; set; } [JsonProperty("created_at")] [JsonConverter(typeof(UnixDateTimeConverter))] public DateTime CreatedAt { get; set; } [JsonProperty("followers_count")] public int FollowersCount { get; set; } [JsonProperty("friends_count")] public int FriendsCount { get; set; } [JsonProperty("favourites_count")] public int FavouritesCount { get; set; } } }