2

I am having issues getting a list of an object out of redis using servicestack. The error is 'Type definitions should start with a '{' array....'

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

Appears to be valid formattable JSON in the cache:

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

But I am getting an error thrown in retrieval:

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]"

I even wrapped it so that the list is a child of a main object, which made the JSON start with a '{' but I still got the same error...

I also tried deserializing to an array, and various methods to deserialize but all within the servicestack library,

Any ideas?

EDIT for anyone else's info

GetValue method should go hand in hand with SetValue and not Set because of the way it does encoding. I still don't know why Get with a type does not deserialize.

redis.Get<DataResponse>(key);

This method seems to do the trick:

redis.Get<string>(key).FromJson<DataResponse>()
Grace
  • 2,548
  • 5
  • 26
  • 23
  • I haven't used redis at all, however, judging by your message: `expecting serialized type 'MyThing'`, it appears *somewhere* you've told it to expect a `MyThing`. Perhaps changing that definition to `List` will fix it ? – Rob Dec 01 '15 at 12:09
  • @Rob The error seems to be suggesting a single object is expected but at no point have I suggested that would be the case. So i wrapped it in a single object and I still get the same error that it expects a serialized type of that object – Grace Dec 01 '15 at 12:12

1 Answers1

0

Your API usage is unbalanced:

If you're serializing the POCO's yourself and saving the POCO as a string you should be retrieving the value as a string and deserializing it yourself, e.g:

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

If you want to instead let the Redis Client serialize it, then you should be using the equivalent typed API's for doing that instead, e.g:

redis.Set(key, myListOfThings);
var dtos = redis.Get<List<MyThing>>(key);
mythz
  • 141,670
  • 29
  • 246
  • 390
  • Yeah I added a later comment realizing my mistake of combining the two - but my issue still stands that redis.Get>(key) will not work for me. I have to 'get' with a type of string and deserialize manually. (However GetValue with SetValue does work) – Grace Dec 01 '15 at 17:21
  • @Grace Are you trying to `Get` something you saved with `Set`? Because that [does work as expected](https://github.com/ServiceStack/ServiceStack.Redis/blob/7cef98a6e4bcd5efd159394f7d0e6c74385d663b/tests/ServiceStack.Redis.Tests/Generic/RedisClientTests.cs#L130). – mythz Dec 01 '15 at 17:37