【发布时间】:2021-01-18 12:27:00
【问题描述】:
对于来自 Dynamo 的分页响应,我试图保留 ExclusiveStartKey 值。在代码示例中,如果我直接使用response.LastEvaluatedKey 值,后续请求工作正常。
但是,如果我序列化 response.LastEvaluatedKey,然后将其序列化回用作 ExclusiveStartKey 值,则后续请求将失败并显示以下错误消息:
提供的起始键无效:一个或多个参数值无效:Null 属性值类型的值必须为 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);
【问题讨论】:
标签: c# dictionary serialization amazon-dynamodb jsonconvert