Jian Lin
2010-May-28 07:07 UTC
cookie has key/value pairs and lost order after JSON decode
If a cookie has several items, and is encoded as JSON text as the value of the cookie, the order is actually apparent in the cookie''s text But if JSON.decode is used: ActiveSupport::JSON.decode(cookies[''item_list'']) and the result is actually in a hash, then the ordering is lost... Is it true that if the original JSON object has an array of hashes (1 key and 1 value), then the order can be preserved? But what if the original JSON object was a hash of key / value pairs, and is already in many users'' cookies, then is there a way to somehow keep the order? -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Andy Jeffries
2010-May-28 08:08 UTC
Re: cookie has key/value pairs and lost order after JSON decode
> ActiveSupport::JSON.decode(cookies[''item_list'']) > > and the result is actually in a hash, then the ordering is lost... > > Is it true that if the original JSON object has an array of hashes (1 > key and 1 value), then the order can be preserved? > > But what if the original JSON object was a hash of key / value pairs, > and is already in many users'' cookies, then is there a way to somehow > keep the order?No, Ruby Hash objects are specifically unordered. You could create an ActiveSupport::OrderedHash instead of a Hash and store that in the cookie instead if you need to maintain ordering? Or Array support ordering so an array would work. Cheers, Andy -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.