Configuring custom json converter for DocumentDb

I am using a typed DocumentQuery to read documents from the Azure DocumentDb collection.

from f in client.CreateDocumentQuery<MyModel>(Collection.SelfLink) select f

Since I cannot find a way in which I can configure the neccesarry custom json converter, it throws this exception:

Failed to create an instance of type AbstractObject. A type is an interface or abstract class and cannot be created.

Usually you do something like this to make it work:

var settings = new JsonSerializerSettings();
settings.Converters.Add(new MyAbstractConverter());
client.SerializerSettings = settings;

DocumentClient has no SerializerSettings settings. So the question is, how can I tell the DocumentDB client that it should use its own converter when deserializing json data for my model?

+4
2

[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; }
    }
}
+4

CosmosDB SDK JsonSerializerSettings, JsonConverter, ContractResolver. . SO.

+1

All Articles