3

I'm trying to create an activity feed list in redis using the redis-node library and had a json serialization question.

If I lpush a value into a key, the values get stored as escaped json, and when I retrieve with lrange, I get a list of escaped json back.

Should I simply iterate over the list items and JSON.parse(item) each, or is there a better more efficient method? Specifically, is there a configuration I can use to avoid the overhead of escaping and unescaping the json values?

1 Answer 1

2

Use JSON.strinfigy(object) when storing your value and use JSON.parse(value) when retrieving it.

Sign up to request clarification or add additional context in comments.

6 Comments

is there a way to store it without the escaped quotes, or as a native json object rather than stringily and parse?
redis stores JSON as a string. Why does it matter if quotes in values are escaped? They have to be in order for anyone to know when the value ends.
was hoping to avoid iterating through the list item collection and calling json parse on each item.
If it did return a Javascript object instead of a JSON string, it would have to call JSON.parse on it at some point. You can extend the redis module by adding a middleware function that does just that.
@MonkeyBonkey Yes, I modified Douglas Crawfords JSON script. It removes the unecessary quotes around the key names (but still leaves the quotes around the property names (obviously or JSON wouldn't work :)). Here is the script, and use JSONLITERAL.stringify. Also, this method saves extra bytes from the removing the quotes, I use to send data across the websocket pipe. Might not seem like a lot of bytes saved from shredding off the quotations, but if there is a lot of stuff being parsed, it will help.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.