dojo把一个对象序列化成json时,如果对象中包含正则表达式,正则表达式会被转换为{},而不能转换为toString()形式,可以给dojo打个补丁,参看如下加粗代码:
/**
*dojo.toJson方法,增加正则表达式支持
*/
dojo.toJson=function(/*Object*/it,/*Boolean?*/prettyPrint,/*String?*/_indentStr){
if(it===undefined){
return"undefined";
}
varobjtype=typeofit;
if(objtype=="number"||objtype=="boolean"){
returnit+"";
}
if(it===null){
return"null";
}
if(itinstanceofRegExp){
returnit.toString();
}
if(dojo.isString(it)){
returndojo._escapeString(it);
}
varrecurse=arguments.callee;
varnewObj;
_indentStr=_indentStr||"";
varnextIndent=prettyPrint?_indentStr+dojo.toJsonIndentStr:"";
vartf=it.__json__||it.json;
if(dojo.isFunction(tf)){
newObj=tf.call(it);
if(it!==newObj){
returnrecurse(newObj,prettyPrint,nextIndent);
}
}
if(it.nodeType&&it.cloneNode){
thrownewError("Can'tserializeDOMnodes");
}
varsep=prettyPrint?"":"";
varnewLine=prettyPrint?"\n":"";
if(dojo.isArray(it)){
varres=dojo.map(it,function(obj){
varval=recurse(obj,prettyPrint,nextIndent);
if(typeofval!="string"){
val="undefined";
}
returnnewLine+nextIndent+val;
});
return"["+res.join(","+sep)+newLine+_indentStr+"]";
}
if(objtype=="function"){
returnnull;
}
varoutput=[],key;
for(keyinit){
varkeyStr,val;
if(typeofkey=="number"){
keyStr='"'+key+'"';
}
else
if(typeofkey=="string"){
keyStr=dojo._escapeString(key);
}
else{
continue;
}
val=recurse(it[key],prettyPrint,nextIndent);
if(typeofval!="string"){
continue;
}
output.push(newLine+nextIndent+keyStr+":"+sep+val);
}
return"{"+output.join(","+sep)+newLine+_indentStr+"}";
}
比如vara={reg:/a/}
dojo.toJson(a)会转换为"{reg:/a/}",而没改之前dojo会转换为"{reg:{}}"
欢迎光临我的独立博客http://bwong.me