如何使用JSON方法序列化javascript对象? [英] How do you serialize javascript objects with methods using JSON?

查看:133
本文介绍了如何使用JSON方法序列化javascript对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找对JSON的增强,该增强还将对方法进行序列化.我有一个对象,它充当对象的集合,并且还希望序列化集合对象的方法.到目前为止,我已经找到了 ClassyJSON .有什么想法吗?

I am looking for an enhancement to JSON that will also serialize methods. I have an object that acts as a collection of objects, and would like to serialize the methods of the collection object as well. So far I've located ClassyJSON. Any thoughts?

推荐答案

我认为序列化方法从来都不是一个好主意.如果打算在服务器端运行代码,则容易受到攻击.如果要在客户端运行它,最好不要使用本地方法,最好引用要在序列化对象中使用的方法的名称.

I don't think serializing methods is ever a good idea. If you intend to run the code serverside, you open yourself to attacks. If you want to run it client side, you are better off just the local methods, possibly referencing the name of the method you are going to use in the serialized objects.

我确实相信,尽管"f = "+function() {}会为您提供一个可以评估的字符串版本:

I do believe though that "f = "+function() {} will yield you a to string version that you can eval:

var test = "f = " + function() { alert("Hello"); };
eval(test)

为获得良好的json处理,我建议使用原型,该原型具有将对象序列化为json的出色方法.

And for good json handling, I would recommend prototype, which has great methods for serializing objects to json.

这篇关于如何使用JSON方法序列化javascript对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆