Values 'set' as number are changed to string after 'get'
When I made a round trip (set and then get) my numeric values came back as strings.
localStorageService.set("page_number", n); // n === 1
n = localStorageService.get("page_number"); // n === "1"
np, I fixed it with:
n = parseInt(localStorageService.get("page_number"), 10);
but I thinkit would be better to preserve the basic JSON types.
I'll check into this. Thanks.
@nmorse This problem is due to the Storage interface of the DOM.
When storing a value in the local storage, it has to be a String (see https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Storage#Storage).
Trying to determine automagically if the value stored should be retrieved as a number or not is not feasible (I could decide that page number is "01", and then parsing to a number would lose the 0).
So it's on the developer to choose whether the value should be retrieved as a number or not. Parsing yourself is therefore a good solution. But it can be painful if you store lots of numbers.
What I suggest is a new method named getNumber (or getAsNumber) that would parse the value for you.
In the same way, arrays and objects are automatically parsed which forbids one to store a string starting with [or {. We could add a method named getJson (or getAsJson) to help solve the problem too.
What are your thoughts on this?
@BluePyth thanks for really delving into this, What I have done in my little home-made localStorage.js is to JSON.stringify everything going into "one big" named localstorage string and JSON.parse on the way out... This does make JSON storage really handy and all the JSON types stay put. The down side (of this JSON storage idea) is that it could use a lot of CPU if large objects are stored When all you wanted to do was to change a number (in that large object).
@nmorse I see what you propose, and I agree that it could be handy. Although, I think that the purpose of ALS is to provide an angular way of using the local storage (with a fallback to cookies if local storage is not supported).
Thus IMHO, I think that our interest here is to stay close to the storage API. I will try to implement what I wrote in my previous comment.
@grevory Do you agree with the solution I proposed?
@BluePyth, that makes sense to stick with the core localStorage of the DOM (strings only). And so after reading the code and seeing the "sometimes" JSON handling... I think that 'get' and 'set' should only manage strings, and not try to interpret ('null', '{' and '[' ) as JSON. Then (I would suggest) adding in two new interfaces: 'getJSON' and 'setJSON' -- they would always process with 'angular.fromJson()' and 'angular.toJson()'.
@nmorse Thanks for your feedback. This is what I'll do :)
Yea I think we should consider storing and retreiving types based on the method called
get returns a string getJSON returns JSON
I'm not in a rush to make this change because it would be a breaking change for many users.
Good point about "not wanting to change what get does," and I agree, to leave get as it is.
Adding to (not altering) the API might be acceptable...
I may submit a pull request to add getJSON to the API, this would always return a JSON parsed value (i.e. object/array/string/number/true/false/null).
Possibly an other addition might be getRaw to return the DOM stored string (without any interpretation).
Wouldn't getRaw be the same as get? Seems like the only thing needed is getJSON, which is a good idea.
I'm going to close this.
get currently does some interpretation on the storage string, e.g. if
the string starts with a { like this '{"foo":"bar"}' it will JSONParse it
into an object, same with [, but otherwise it returns a string. I
started this thread because it was inconsistent (not always JSON and not
always String. Since get is established, as sometimes parsing and
sometimes not, I was proposing. getRaw as a way to always get a string.
On Wed, Feb 19, 2014 at 12:37 AM, Gregory Pike [email protected]:
Wouldn't getRaw be the same as get? Seems like the only thing needed is getJSON, which is a good idea.
I'm going to close this.
Reply to this email directly or view it on GitHubhttps://github.com/grevory/angular-local-storage/issues/46#issuecomment-35467992 .
Nate Morse Programmer AcademicCatalog.com http://www.academiccatalog.com/ 217 Commercial Street, Suite 406 Portland, ME 04101 207 774-4769
I see