gpt4 book ai didi

c# - DynamoDB ExclusiveStartKey 的字典序列化不起作用

转载 作者:行者123 更新时间:2023-12-05 02:49:04 25 4
gpt4 key购买 nike

对于来自 Dynamo 的分页响应,我试图保留 ExclusiveStartKey 值。在代码示例中,如果我直接使用 response.LastEvaluatedKey 值,则后续请求可以正常工作。

但是,如果我序列化 response.LastEvaluatedKey,然后将其序列化回用作 ExclusiveStartKey 值,后续请求将失败并显示以下错误消息:

The provided starting key is invalid: One or more parameter values were invalid: Null attribute value types must have the value of true

反序列化的字典似乎与原始字典具有相同的值...有什么要检查的,看看两者之间有什么不同吗?

QueryResponse response = null;

do
{

string gsiPartitionKey = "gsi-pk-value-1";

var queryRequest = new QueryRequest()
{
TableName = "my-table",
IndexName = "my-index",
KeyConditionExpression = "IndexPk = :s_gsiPartitionKey",
ExpressionAttributeValues = new Dictionary<string, AttributeValue>
{
{
":s_gsiPartitionKey", new AttributeValue { S = gsiPartitionKey}
}
},
Limit = 1
};

if (response != null)
{
//OPTION 1 - OK - Using LastEvaluatedKey directly works fine
//queryRequest.ExclusiveStartKey = response.LastEvaluatedKey;

//OPTION 2 - BAD - Serializing and deserializing fails
var serialized = JsonConvert.SerializeObject(response.LastEvaluatedKey);
var deserialized = JsonConvert.DeserializeObject<Dictionary<string, AttributeValue>>(serialized);
queryRequest.ExclusiveStartKey = deserialized;
}

response = await DynamoDbClient.QueryAsync(queryRequest);

} while (response.LastEvaluatedKey.Count != 0);

最佳答案

我今天遇到了这个问题,我想我会更新这个。里面AttributeValue class,有一个非public成员_null类型 bool?从 JSON 反序列化时初始化不正确。它被设置为 false什么时候应该设置为null .

使用反射,反序列化后,我将值设置为 null对于字典中的每个键,AWS 现在按预期返回数据。

为了访问私有(private)成员,我使用了这个函数:

public void SetPrivatePropertyValue<T>(object obj, string propName, T val)
{
Type t = obj.GetType();

// add a check here that the object obj and propertyName string are not null
foreach (FieldInfo fi in obj.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic))
{
if (fi.Name.ToLower().Contains(propName.ToLower()))
{
fi.SetValue(obj, val);
break;
}
}
}

方法调用是 SetPrivatePropertyValue<bool?>(attribute, "_null", null);

祝你好运!

关于c# - DynamoDB ExclusiveStartKey 的字典序列化不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64178120/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com