Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting a custom json converter for DocumentDb

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

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

Because I do not find a way how I can set the neccesarry custom json converter, it throws this exeption:

Could not create an instance of type AbstractObject. Type is an interface or abstract class and cannot be instantiated.

Usually you do something like this to make it work:

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

DocumentClient doesn't have any SerializerSettings. So the question is, how can I tell the DocumentDB client that it must use a custom converter when deserializing the json data to my model?

like image 708
dixus Avatar asked Dec 11 '14 16:12

dixus


People also ask

How do I set the default JSON converter for a class?

Apply the [JsonConverter] attribute to a class or a struct that represents a custom value type. Here's an example that makes the DateTimeOffsetJsonConverter the default for properties of type DateTimeOffset: Suppose you serialize an instance of the following type: Here's an example of JSON output that shows the custom converter was used:

What is the JSON used by DocumentDB?

DocumentDB used Newtonsoft Json.NET for its serialization. However, with the growth of .NET Core and the introduction of shiny new System.Text.Json, the team wanted to reduce exposure to Newtonsoft. So the idea was in future, with CosmosDB SDK 4, System.Text.Json would replace Json.NET.

How to create a jsonconverter in Java?

The following steps explain how to create a converter by following the basic pattern: Create a class that derives from JsonConverter<T> where T is the type to be serialized and deserialized. Override the Read method to deserialize the incoming JSON and convert it to type T. Use the Utf8JsonReader that is passed to the method to read the JSON.

How are JSON converters chosen during serialization or deserialization?

During serialization or deserialization, a converter is chosen for each JSON element in the following order, listed from highest priority to lowest: [JsonConverter] applied to a property.


2 Answers

You can add [JsonConverter(typeof(MyAbstractConverter))] to your model class.

Here's an example model class with custom Json settings:

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; }
    }
}
like image 90
Andrew Liu Avatar answered Oct 14 '22 06:10

Andrew Liu


The latest CosmosDB SDK now includes support for JsonSerializerSettings so you don't have to use JsonConverter anymore, you can use your own ContractResolver. See related SO post.

like image 38
SliverNinja - MSFT Avatar answered Oct 14 '22 07:10

SliverNinja - MSFT