root / trunk / web / dojo / dojox / json / ref.js @ 12
History | View | Annotate | Download (5.05 KB)
1 |
/*
|
---|---|
2 |
Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
|
3 |
Available via Academic Free License >= 2.1 OR the modified BSD license.
|
4 |
see: http://dojotoolkit.org/license for details
|
5 |
*/
|
6 |
|
7 |
|
8 |
if(!dojo._hasResource["dojox.json.ref"]){ |
9 |
dojo._hasResource["dojox.json.ref"]=true; |
10 |
dojo.provide("dojox.json.ref");
|
11 |
dojo.require("dojo.date.stamp");
|
12 |
dojox.json.ref={resolveJson:function(_1,_2){ |
13 |
_2=_2||{}; |
14 |
var _3=_2.idAttribute||"id"; |
15 |
var _4=this.refAttribute; |
16 |
var _5=_2.idAsRef;
|
17 |
var _6=_2.idPrefix||""; |
18 |
var _7=_2.assignAbsoluteIds;
|
19 |
var _8=_2.index||{};
|
20 |
var _9=_2.timeStamps;
|
21 |
var _a,_b=[];
|
22 |
var _c=/^(.*\/)?(\w+:\/\/)|[^\/\.]+\/\.\.\/|^.*\/(\/)/; |
23 |
var _d=this._addProp; |
24 |
var F=function(){ |
25 |
}; |
26 |
function _e(it,_f,_10,_11,_12,_13){ |
27 |
var i,_14,val,id=_3 in it?it[_3]:_10; |
28 |
if(_3 in it||((id!==undefined)&&_11)){ |
29 |
id=(_6+id).replace(_c,"$2$3");
|
30 |
} |
31 |
var _15=_13||it;
|
32 |
if(id!==undefined){ |
33 |
if(_7){
|
34 |
it.__id=id; |
35 |
} |
36 |
if(_2.schemas&&(!(it instanceof Array))&&(val=id.match(/^(.+\/)[^\.\[]*$/))){ |
37 |
_12=_2.schemas[val[1]];
|
38 |
} |
39 |
if(_8[id]&&((it instanceof Array)==(_8[id] instanceof Array))){ |
40 |
_15=_8[id]; |
41 |
delete _15.$ref; |
42 |
delete _15._loadObject;
|
43 |
_14=true;
|
44 |
}else{
|
45 |
var _16=_12&&_12.prototype;
|
46 |
if(_16){
|
47 |
F.prototype=_16; |
48 |
_15=new F();
|
49 |
} |
50 |
} |
51 |
_8[id]=_15; |
52 |
if(_9){
|
53 |
_9[id]=_2.time; |
54 |
} |
55 |
} |
56 |
while(_12){
|
57 |
var _17=_12.properties;
|
58 |
if(_17){
|
59 |
for(i in it){ |
60 |
var _18=_17[i];
|
61 |
if(_18&&_18.format=="date-time"&&typeof it[i]=="string"){ |
62 |
it[i]=dojo.date.stamp.fromISOString(it[i]); |
63 |
} |
64 |
} |
65 |
} |
66 |
_12=_12["extends"];
|
67 |
} |
68 |
var _19=it.length;
|
69 |
for(i in it){ |
70 |
if(i==_19){
|
71 |
break;
|
72 |
} |
73 |
if(it.hasOwnProperty(i)){
|
74 |
val=it[i]; |
75 |
if((typeof val=="object")&&val&&!(val instanceof Date)&&i!="__parent"){ |
76 |
_a=val[_4]||(_5&&val[_3]); |
77 |
if(!_a||!val.__parent){
|
78 |
if(it!=_b){
|
79 |
val.__parent=_15; |
80 |
} |
81 |
} |
82 |
if(_a){
|
83 |
delete it[i];
|
84 |
var _1a=_a.toString().replace(/(#)([^\.\[])/,"$1.$2").match(/(^([^\[]*\/)?[^#\.\[]*)#?([\.\[].*)?/); |
85 |
if((_a=(_1a[1]=="$"||_1a[1]=="this"||_1a[1]=="")?_1:_8[(_6+_1a[1]).replace(_c,"$2$3")])){ |
86 |
if(_1a[3]){ |
87 |
_1a[3].replace(/(\[([^\]]+)\])|(\.?([^\.\[]+))/g,function(t,a,b,c,d){ |
88 |
_a=_a&&_a[b?b.replace(/[\"\'\\]/,""):d]; |
89 |
}); |
90 |
} |
91 |
} |
92 |
if(_a){
|
93 |
val=_a; |
94 |
}else{
|
95 |
if(!_f){
|
96 |
var _1b;
|
97 |
if(!_1b){
|
98 |
_b.push(_15); |
99 |
} |
100 |
_1b=true;
|
101 |
val=_e(val,false,val[_4],true,_18); |
102 |
val._loadObject=_2.loader; |
103 |
} |
104 |
} |
105 |
}else{
|
106 |
if(!_f){
|
107 |
val=_e(val,_b==it,id===undefined?undefined:_d(id,i),false,_18,_15!=it&&typeof _15[i]=="object"&&_15[i]); |
108 |
} |
109 |
} |
110 |
} |
111 |
it[i]=val; |
112 |
if(_15!=it&&!_15.__isDirty){
|
113 |
var old=_15[i];
|
114 |
_15[i]=val; |
115 |
if(_14&&val!==old&&!_15._loadObject&&!(i.charAt(0)=="_"&&i.charAt(1)=="_")&&i!="$ref"&&!(val instanceof Date&&old instanceof Date&&val.getTime()==old.getTime())&&!(typeof val=="function"&&typeof old=="function"&&val.toString()==old.toString())&&_8.onUpdate){ |
116 |
_8.onUpdate(_15,i,old,val); |
117 |
} |
118 |
} |
119 |
} |
120 |
} |
121 |
if(_14&&(_3 in it||_15 instanceof Array)){ |
122 |
for(i in _15){ |
123 |
if(!_15.__isDirty&&_15.hasOwnProperty(i)&&!it.hasOwnProperty(i)&&!(i.charAt(0)=="_"&&i.charAt(1)=="_")&&!(_15 instanceof Array&&isNaN(i))){ |
124 |
if(_8.onUpdate&&i!="_loadObject"&&i!="_idAttr"){ |
125 |
_8.onUpdate(_15,i,_15[i],undefined);
|
126 |
} |
127 |
delete _15[i];
|
128 |
while(_15 instanceof Array&&_15.length&&_15[_15.length-1]===undefined){ |
129 |
_15.length--; |
130 |
} |
131 |
} |
132 |
} |
133 |
}else{
|
134 |
if(_8.onLoad){
|
135 |
_8.onLoad(_15); |
136 |
} |
137 |
} |
138 |
return _15;
|
139 |
}; |
140 |
if(_1&&typeof _1=="object"){ |
141 |
_1=_e(_1,false,_2.defaultId,true); |
142 |
_e(_b,false);
|
143 |
} |
144 |
return _1;
|
145 |
},fromJson:function(str,_1c){ |
146 |
function ref(_1d){ |
147 |
var _1e={};
|
148 |
_1e[this.refAttribute]=_1d;
|
149 |
return _1e;
|
150 |
}; |
151 |
try{
|
152 |
var _1f=eval("("+str+")"); |
153 |
} |
154 |
catch(e){
|
155 |
throw new SyntaxError("Invalid JSON string: "+e.message+" parsing: "+str); |
156 |
} |
157 |
if(_1f){
|
158 |
return this.resolveJson(_1f,_1c); |
159 |
} |
160 |
return _1f;
|
161 |
},toJson:function(it,_20,_21,_22){ |
162 |
var _23=this._useRefs; |
163 |
var _24=this._addProp; |
164 |
var _25=this.refAttribute; |
165 |
_21=_21||"";
|
166 |
var _26={};
|
167 |
var _27={};
|
168 |
function _28(it,_29,_2a){ |
169 |
if(typeof it=="object"&&it){ |
170 |
var _2b;
|
171 |
if(it instanceof Date){ |
172 |
return "\""+dojo.date.stamp.toISOString(it,{zulu:true})+"\""; |
173 |
} |
174 |
var id=it.__id;
|
175 |
if(id){
|
176 |
if(_29!="#"&&((_23&&!id.match(/#/))||_26[id])){ |
177 |
var ref=id;
|
178 |
if(id.charAt(0)!="#"){ |
179 |
if(it.__clientId==id){
|
180 |
ref="cid:"+id;
|
181 |
}else{
|
182 |
if(id.substring(0,_21.length)==_21){ |
183 |
ref=id.substring(_21.length); |
184 |
}else{
|
185 |
ref=id; |
186 |
} |
187 |
} |
188 |
} |
189 |
var _2c={};
|
190 |
_2c[_25]=ref; |
191 |
return _28(_2c,"#"); |
192 |
} |
193 |
_29=id; |
194 |
}else{
|
195 |
it.__id=_29; |
196 |
_27[_29]=it; |
197 |
} |
198 |
_26[_29]=it; |
199 |
_2a=_2a||"";
|
200 |
var _2d=_20?_2a+dojo.toJsonIndentStr:""; |
201 |
var _2e=_20?"\n":""; |
202 |
var sep=_20?" ":""; |
203 |
if(it instanceof Array){ |
204 |
var res=dojo.map(it,function(obj,i){ |
205 |
var val=_28(obj,_24(_29,i),_2d);
|
206 |
if(typeof val!="string"){ |
207 |
val="undefined";
|
208 |
} |
209 |
return _2e+_2d+val;
|
210 |
}); |
211 |
return "["+res.join(","+sep)+_2e+_2a+"]"; |
212 |
} |
213 |
var _2f=[];
|
214 |
for(var i in it){ |
215 |
if(it.hasOwnProperty(i)){
|
216 |
var _30;
|
217 |
if(typeof i=="number"){ |
218 |
_30="\""+i+"\""; |
219 |
}else{
|
220 |
if(typeof i=="string"&&(i.charAt(0)!="_"||i.charAt(1)!="_")){ |
221 |
_30=dojo._escapeString(i); |
222 |
}else{
|
223 |
continue;
|
224 |
} |
225 |
} |
226 |
var val=_28(it[i],_24(_29,i),_2d);
|
227 |
if(typeof val!="string"){ |
228 |
continue;
|
229 |
} |
230 |
_2f.push(_2e+_2d+_30+":"+sep+val);
|
231 |
} |
232 |
} |
233 |
return "{"+_2f.join(","+sep)+_2e+_2a+"}"; |
234 |
}else{
|
235 |
if(typeof it=="function"&&dojox.json.ref.serializeFunctions){ |
236 |
return it.toString();
|
237 |
} |
238 |
} |
239 |
return dojo.toJson(it);
|
240 |
}; |
241 |
var _31=_28(it,"#",""); |
242 |
if(!_22){
|
243 |
for(var i in _27){ |
244 |
delete _27[i].__id;
|
245 |
} |
246 |
} |
247 |
return _31;
|
248 |
},_addProp:function(id,_32){ |
249 |
return id+(id.match(/#/)?id.length==1?"":".":"#")+_32; |
250 |
},refAttribute:"$ref",_useRefs:false,serializeFunctions:false}; |
251 |
} |