c# – 为DocumentDb设置自定义json转换器
发布时间:2020-12-15 08:46:03 所属栏目:百科 来源:网络整理
导读:我使用类型化的DocumentQuery从Azure DocumentDb的集合中读取文档. from f in client.CreateDocumentQueryMyModel(Collection.SelfLink) select f 因为我找不到如何设置neccesarry自定义json转换器的方法,所以它抛出了这个例子: Could not create an instan
我使用类型化的DocumentQuery从Azure DocumentDb的集合中读取文档.
from f in client.CreateDocumentQuery<MyModel>(Collection.SelfLink) select f 因为我找不到如何设置neccesarry自定义json转换器的方法,所以它抛出了这个例子:
通常你会做这样的事情来使它工作: 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; } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |