2

I have an object that contains arrays which is made up of other arrays it also contains methods. The problem that i am facing is that i can't use JSon because of the multidimensional arrays and the methods.

The reason i want to convert it to a string is to store into the Iphone's localStorage.

you can only store text inside of there and i was hoping for a way to store a complex object about a user as a string and able to deserialize it again after. Is there even a way to do this. I don't know javascript well enough to write my own api. That was the option i started to try and look into at this point :(

4
  • JSON can encode multi-dimensional arrays, but not methods. Commented May 23, 2012 at 13:01
  • can't you just save the .js script and then attempt to eval() it at runtime ? But I don't know if you can actually read and execute from local storage. Commented May 23, 2012 at 13:04
  • Why do you want to persist your methods? Commented May 23, 2012 at 13:21
  • Im coming from c++ took a course on it so in my mind keeping the object as one made sense. i guess i could just move methods outside the object it's self. I do use Jquery. I wanted to persist them so that when i reconstruct the object. i can perform the various methods. some methods are to update the object Commented May 23, 2012 at 13:54

4 Answers 4

2

Serialize it to JSON. You can serialize / deserialize complex objects and arrays.

There is no way you can serialize methods (code).

For local storage persistence I would recommend Lawnchair.

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

3 Comments

@mplungjan yes, because alert() does an implicit .toString() on each argument. The JSON serialiser will refuse to do so.
So we can add it ourselves, no?
@mplungjan - but why do you want to persist methods in the first place?
0

no need to store in localStorage you can use global array any where in your application try this once

1 Comment

no i need the localStorage so they can exit and come back to it later. the object contains alot of things
0

It's normally possible to serialise a method using myfunc.toString()

So, you could use a replacer function in JSON.stringify() and JSON.parse() to recognise methods, and call .toString() on them, and them somehow recognise them on parsing and turn them back into first class functions.

However - note that a reconstructed function will not have access to the original object's scope, i.e. private variables, or methods, etc.

e.g.

function A() {
    var foo = 'bar';
    this.test = function test() { console.log(foo) };
};

var b = new A();
var f = b.test.toString();  // the text of the method
b.test2 = new Function(f);

b.test2();
> undefined

Comments

0

well, there is a way to convert object with functions to string and vise verse. take a look at this plugin:

http://www.eslinstructor.net/jsonfn/

good luck,

-Vadim

Comments

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.