Reputation: 610
I want to store list of custom Objects in cookie. Example scenario: Lets say i have a product class. I want to store the list of products selected by customer in cookie, until he logs in.
(eg) class Product{ int id;String name;float price;}
Since cookie allows only string, Possible solutions: 1) I can store it in a comma(,) seperated value in cookie. (ie)id:price,id:price and manupulate everytime. 2) I can make the class implements serializable, create another wrapper which holds this list and add it in cookie (ie)class wrapper implements Serializable{List product;}
Option 1 does preliminary computation on string while option 2 gives me a class level control, so that i can do further manipulation directly when needed. Please suggest me, which is the best option to go for. Please let me know, pros and cons on both approach,.. Suggest me, if any best solution than this, if exists. Thanks in advance..
Upvotes: 1
Views: 1105
Reputation: 8991
I would recommend using JSON to encode your objects and store that as a string in the cookie. This makes it easier to debug problems having to do with cookies since you can tell immediately what values the cookie is holding since JSON is human readable.
I just don't think this is a good use of Java serialization.
Upvotes: 2