Search code examples
c#azure-cosmosdbsystem.text.jsonazure-cosmosdb-changefeed

Change feed iterator content deserialization


Currently, I'm looking for ways to deserialize entities from Change Feed Iterator content with System.Text.Json.

I found this example from the documentation, but it uses Newtonsoft.Json and I don't like an approach with JObject and JsonTextReader. Is there are any way to make it more properly and clean?

I've tried to make something like this, but my approach doesn't work.

ResponseMessage response = await iterator.ReadNextAsync(); 

var entities = await JsonSerializer.DeserializeAsync<IEnumerable<CosmosEntity1>>(response.Content);

Solution

  • The response stream you get is not an IEnumerable of your documents but rather a class that contains the documents as one of its properties. Try creating the following class:

    public class ExampleResponse
    {
        public string _rid { get; set; }
        public List<CosmosEntity1> Documents { get; set; }
        public int _count { get; set; }
    }
    

    and deserialize your stream to that class.