开发者

How can I serialize functions using JSON or some other serialization library?

开发者 https://www.devze.com 2022-12-29 10:08 出处:网络
I am trying to create a program that uses javascript to write a simple textad开发者_运维知识库venture that I can then post on my blog and run on my iphone. I have run into a problem though. I was tryi

I am trying to create a program that uses javascript to write a simple textad开发者_运维知识库venture that I can then post on my blog and run on my iphone. I have run into a problem though. I was trying to make it so that my program would save it's state into cookies using JSON to convert it into strings and then post it into a cookie but then I realised that I couldn't serialize the functions that are on my item object.

I was trying to make it so that my item would have an associative array that would contain the name of the use as the key, and the function as the value. This worked well until I tried to serialize it. I learned that I could create a JSON-like serialization for functions by storing the body into a string and using escape characters for the double quotes, but for some reason I was unable to make my cookie with the function as the string stored.

When I posted the cookie and then tried to get it back the string wasn't there. My code and the over all project are on my site if you want to look at that, though my full code including the item actions are not posted yet.


Cookies are generally limited to around 4K in size each - you may be hitting that limit. You're right in that JSON is only going to serialize the properties of your object - not any functions. You could have an array like the one you mention (key/function name), then use eval() to run the function.


If I correct understood your problems the following can be helpful: You can post the state information to the web server and provide on the same web server per HTTP GET the same information from the server. Such HTTP GET requests will be cached on the client. So you will be save information which you needs on both sides on client (in local cache) and on the server. There are no restriction about the size of the local cache. To be exactly you can set this restriction in your web browser. What do you think about?

0

精彩评论

暂无评论...
验证码 换一张
取 消