Automatic byte to json conversion
I'm serializing an object to a byte and then setting a key in redis with the byte value. For some reason its not storing the byte value, it converts the byte value to json. Is this something that StringSet and SetAdd automatically does?
var test = JsonSerializer.Serialize(myListCollectionObject);
db0.StringSet("byte test stringset", test);
db0.SetAdd("byte test setadd", test);
The variable 'test' is a byte object, but after sending to Redis, it' converts it automatically back to json.
Is this expected behavior? I would think it would store it as a byte object.
I tested just creating a byte object and setting that and that then stayed as a byte in Redis.
Thoughts?
I suspect this just comes down to definitions. Redis (server) and SE.Redis (library/client) simply store opaque payloads without ant care what they represent (usually - I'm not including "incr" ops here).
What exactly do you mean by "a byte object"? And what do you mean by "converts the byte value to JSON"? How are you looking to observe this? The reason I ask is that : a stored string, and a stored blob whose contents are an encoded string - are essentially the exact same thing at the binary level. There's no marked in redis to say "and by the way I hold text".
So : what query / operation / etc are you doing that suggests something odd is happening?
On Wed, 19 Sep 2018, 20:57 aspnerd, [email protected] wrote:
I'm serializing an object to a byte and then setting a key in redis with the byte value. For some reason its not storing the byte value, it converts the byte value to json. Is this something that StringSet and SetAdd automatically does?
var test = JsonSerializer.Serialize(myListCollectionObject); db0.StringSet("byte test stringset", test); db0.SetAdd("byte test setadd", test);
The variable 'test' is a byte object, but after sending to Redis, it' converts it automatically back to json.
Is this expected behavior? I would think it would store it as a byte object.
I tested just creating a byte object and setting that and that then stayed as a byte in Redis.
Thoughts?
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/StackExchange/StackExchange.Redis/issues/957, or mute the thread https://github.com/notifications/unsubscribe-auth/AABDsBO4U6L-k685Xf7ALxyLKbX9UOdsks5ucqGOgaJpZM4Ww3WL .
I compress a list collection and to a byte array. After I send it to Redis it converts and stores the byte array to a json serialized string.
The 'get' back from Redis doesn't return the byte array. My visibility into the Redis server through other tools also suggest it's stored as a json serialized object instead of a byte array.
It is unclear what "I compress a list collection and to a byte array." means exactly. I'm not trying to be pedantic - the details here really matter. But: I suspect what you are doing is using an encoding such as UTF8 to generate a byte[] from a string, and storing that.
Now - here's the really important point: redis doesn't have separate string types and byte-array types. Everything is just an opaque blob. It isn't actually true to say "I stored a string in redis" - but rather "I stored a byte sequence that happens to be the same as what an encoder string looks like" - there literally isn't a "text" vs "binary" flag in redis.
So: when you say that some other tools are suggesting it is text - what that probably means is that those tools are running a speculative "does this byte sequence decode meaningfully with any obvious encodings, for example UTF8? If so, it is probably safe to say that it is a string".
So - I don't think anything is wrong here. Encoded string data is a blob. Redis only has blobs. It is impossible for SE.Redis to be incorrectly storing something as text, because redis doesn't actually have a text type - it just has blobs, which is some cases contain encoded text data. It sounds like in your case, the data is indeed: encoded text. So: it will look like encoded text, and many tools will guess that it is text.
If I have misunderstood, it would be really useful to include example code that actually shows what you're doing to generate the payload you're sending to redis and which is causing this confusion.
That was my thought that the tools were just guessing that it was text. But when I use var supposedToByByteArray = db.StringGet("mykey"); I would assume I should get my byte array back, not the 'guessed' object serialized json string.
Here is a rundown of what is happening...
db0 is IDatabase pointing to database 0, with multiplexer to redis.
List<MyObject> lmo = new List<MyObject>();
lmo = (populate from database)
var test = JsonSerializer.Serialize(lmo);
// test then is a byte array
db0.StringSet("myKey", test);
//should be stored a byte array in redis, but it doesn't appear to be in tools and once returned below
var myByteArrayOut = db0.StringGet("myKey");
// myBtyteArrayOut is a serialized json string not my byte array. <- This is confusing to me
var myTestOut = JsonSerializer.Deserialize<List<MyObject>>(myByteArrayOut);
// myTestOut should be List<MyObject>, which it is.
So my question/confusion is that when putting the byte array into redis I would expect to see that returned. What is being return is just a normal serialized json string, not a byte array?
I think the missing piece here is RedisValue is implicitly convertible to string and from string and since you are giving and getting a string, that's what's in play, though it's just bytes on the redis side. The RedisValue struct is part of StackExchange.Redis to make this seamless and intuitive. For example, JsonSerializer.Serialize(lmo); is being written to something, though that method doesn't exist today so I'm not sure if you're on an old version of JSON.NET of the pseudo-code is incomplete (e.g. JsonSerializer.Serialize is an instance method and requires a writer as a first argument).
Does that help a bit?
Hey Nick, I am working with Jeff on this one. Makes sense - we are using Utf8Json. I will investigate the serializer on our end.
Suffice to say, if we were putting bytes in, we should see bytes. So it must be the mechanism throwing us off.
Implicit conversions are fun. FYI, if you're using Utf8Json you may be interested to know that I'm currently looking at adding Utf8String (the new thing) support to RedisValue - so... that'll make it even more confusing. Yay for ambiguity :)
Haha. I am sure we will work it all out. I really think Newtonsoft is by far the best generalized solution, but we couldn't ignore the speed of Utf8Json.
I'm closing this down, because: I don't think we're doing anything here - we just provide RedisValue - what happens after that is at the caller