CosmosDB每个项目的ttl设置引发异常:无法反序列化当前JSON对象 [英] CosmosDB per item ttl setting throwing exception: Cannot deserialize the current JSON object

查看:179
本文介绍了CosmosDB每个项目的ttl设置引发异常:无法反序列化当前JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注

I am following this documentation to set per item ttl to a CosmosDB table entries. But when I add a field name ttl in the entity class I am facing the below error while making Insert/Replace calls:

无法将当前JSON对象(例如{"name":"value"})反序列化为类型'System.Nullable'1 [System.Int32]',因为该类型需要JSON基本值(例如字符串,数字,boolean,null)正确反序列化. 要解决此错误,可以将JSON更改为JSON基本值(例如,字符串,数字,布尔值,null),或者更改反序列化类型,使其成为普通的.NET类型(例如,不像整数这样的原始类型,而不是集合类型) (例如数组或列表),可以从JSON对象反序列化.还可以将JsonObjectAttribute添加到类型中,以强制其从JSON对象反序列化. 路径"ttl.$ t",第1行,位置109.

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Nullable`1[System.Int32]' because the type requires a JSON primitive value (e.g. string, number, boolean, null) to deserialize correctly. To fix this error either change the JSON to a JSON primitive value (e.g. string, number, boolean, null) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path 'ttl.$t', line 1, position 109.

public class MyEntity : TableEntity
{
    public string Prop { get; set; }
    
    [JsonProperty(PropertyName = "ttl", NullValueHandling = NullValueHandling.Ignore)]
    public int? ttl { get; set; }

    public MyEntity(
       string pk,
       string rk,
       string prop)
    {
        this.PartitionKey = pk;
        this.RowKey = rk;
        this.Prop =prop;
        this.ttl = -1;
    }
}

如何解决?

推荐答案

更新

关注官方文档并在Azure Cosmos DB Emulator中更新我的商品.将根据TTL的期望值删除项目.

Follow the offical document and update my Items in Azure Cosmos DB Emulator. Items will be deleted according to the expected value of TTL.

while (feedIterator.HasMoreResults)
{
    foreach (var item in await feedIterator.ReadNextAsync())
    {
       item.ttl = 10;
       await container.UpsertItemAsync<MyEntity>(item, new PartitionKey(item.address));
    }
}

希望我的回答可以为您提供帮助.

using System;
using System.Threading.Tasks;
using System.Configuration;
using System.Collections.Generic;
using System.Net;
using System.Linq;
using Newtonsoft.Json;
using Microsoft.Azure.Cosmos;
using Microsoft.Azure.Cosmos.Table;

namespace CosmosGettingStartedTutorial
{
    class Program
    {
        // <Main>
        public static async Task Main(string[] args)
        {
            try
            {
                Console.WriteLine("Beginning operations...\n");
                CosmosClient client = new CosmosClient("https://localhost:8081/", "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==");
                Database database = await client.CreateDatabaseIfNotExistsAsync("ToDoList");
                Container container = database.GetContainer("jason");
                // Add for an item
                QueryDefinition queryDefinition = new QueryDefinition("select * from c ");
                FeedIterator<MyEntity> feedIterator = container.GetItemQueryIterator<MyEntity>(queryDefinition, null, new QueryRequestOptions() { PartitionKey = new PartitionKey("address0") });
                int count = 0;
                while (feedIterator.HasMoreResults)
                {
                    foreach (var item in await feedIterator.ReadNextAsync())
                    {
                        if (item.id == "0")
                        {
                            Console.WriteLine("id equal 0  is exist: id = " + item.id);
                            Console.WriteLine("We will change id='test" + item.id + "'");
                            Console.WriteLine("pls wait ,will update ");
                            item.id = "test" + item.id;
                            await container.UpsertItemAsync<MyEntity>(item, new PartitionKey(item.address));
                        }
                        count++;
                    }
                }

                int num = count + 5;
                for (int i = count; i < num; i++)
                {
                    MyEntity entity = new MyEntity(i.ToString(), i.ToString(), i.ToString());
                    entity.id = i.ToString();
                    entity.address = "address0";
                    await container.CreateItemAsync<MyEntity>(entity, new PartitionKey(entity.address));
                }
            }
            catch (CosmosException de)
            {
                Exception baseException = de.GetBaseException();
                Console.WriteLine("{0} error occurred: {1}", de.StatusCode, de);
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: {0}", e);
            }
            finally
            {
                Console.WriteLine("End of demo, press any key to exit.");
                Console.ReadKey();
            }
        }
        public class MyEntity : TableEntity
        {
            public string Prop { get; set; }

            [JsonProperty(PropertyName = "ttl", NullValueHandling = NullValueHandling.Ignore)]
            public int? ttl { get; set; }

            public MyEntity(string pk, string rk, string prop)
            {
                this.PartitionKey = pk;
                this.RowKey = rk;
                this.Prop = prop;
                this.ttl = -1;
            }
            public string address { get; set; }
            public string id { get; set; }
        }
    }
}

这篇关于CosmosDB每个项目的ttl设置引发异常:无法反序列化当前JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆