使用服务堆栈反序列化的问题

Issues deserializing with service stack

我在使用 servicestack 从 redis 中获取对象列表时遇到问题。错误是'Type definitions should start with a '{'数组....'

using (var redis = _redisService.GetClient())
{
     redis.Set(key, myListOfThings.SerializeToString());
}

在缓存中似乎是有效的格式化表 JSON:

[{"id":34,"someid":1012,"stuff":"blah"},{"id":33,"someid":1012,"stuff":"dfsfd"}]

但是我在检索时遇到错误:

using (var redis = _redisService.GetClient())
{
     return redis.Get<List<MyThing>>(key);
}

"Additional information: Type definitions should start with a '{', expecting serialized type 'MyThing', got string starting with: [my json string from cache]"

我什至包装了它,使列表成为主对象的子对象,这使得 JSON 以“{”开头,但我仍然遇到同样的错误...

我也尝试过反序列化为数组,以及各种反序列化方法,但都在 servicestack 库中,

有什么想法吗?

编辑其他人的信息

GetValue 方法应该与 SetValue 齐头并进,而不是 Set,因为它进行编码的方式。我仍然不知道为什么 Get with a type 不反序列化。

redis.Get<DataResponse>(key);

这个方法似乎可以解决问题:

redis.Get<string>(key).FromJson<DataResponse>()

您的 API 使用不平衡:

如果您自己序列化 POCO 并将 POCO 保存为字符串,您应该将值检索为字符串并且自己反序列化,例如:

redis.SetValue(key, myListOfThings.ToJson());
var dtos = redis.GetValue(key).FromJson<List<MyThing>>();

如果你想让 Redis 客户端序列化它,那么你应该使用等效类型的 API 来代替,例如:

redis.Set(key, myListOfThings);
var dtos = redis.Get<List<MyThing>>(key);