| 1 |
define([ |
| 2 |
"dojo/_base/array", |
| 3 |
"dojo/_base/json", |
| 4 |
"dojo/_base/kernel", |
| 5 |
"dojo/_base/lang", |
| 6 |
"dojo/date/stamp", |
| 7 |
"dojox" |
| 8 |
], |
| 9 |
function(array, djson, dojo, lang, stamp, dojox){ |
| 10 |
|
| 11 |
lang.getObject("json", true, dojox); |
| 12 |
|
| 13 |
return dojox.json.ref = { |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
resolveJson: function( root, args){ |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
args = args || {}; |
| 55 |
var idAttribute = args.idAttribute || 'id'; |
| 56 |
var refAttribute = this.refAttribute; |
| 57 |
var idAsRef = args.idAsRef; |
| 58 |
var prefix = args.idPrefix || ''; |
| 59 |
var assignAbsoluteIds = args.assignAbsoluteIds; |
| 60 |
var index = args.index || {}; |
| 61 |
var timeStamps = args.timeStamps; |
| 62 |
var ref,reWalk=[]; |
| 63 |
var pathResolveRegex = /^(.*\/)?(\w+:\/\/)|[^\/\.]+\/\.\.\/|^.*\/(\/)/; |
| 64 |
var addProp = this._addProp; |
| 65 |
var F = function(){}; |
| 66 |
function walk(it, stop, defaultId, needsPrefix, schema, defaultObject, parent){ |
| 67 |
|
| 68 |
var i, update, val, id = idAttribute in it ? it[idAttribute] : defaultId; |
| 69 |
if(idAttribute in it || ((id !== undefined) && needsPrefix)){ |
| 70 |
id = (prefix + id).replace(pathResolveRegex,'$2$3'); |
| 71 |
} |
| 72 |
var target = defaultObject || it; |
| 73 |
if(id !== undefined){ |
| 74 |
if(assignAbsoluteIds){ |
| 75 |
it.__id = id; |
| 76 |
} |
| 77 |
if(args.schemas && (!(it instanceof Array)) && |
| 78 |
(val = id.match(/^(.+\/)[^\.\[]*$/))){ |
| 79 |
schema = args.schemas[val[1]]; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
if(index[id] && ((it instanceof Array) == (index[id] instanceof Array))){ |
| 84 |
target = index[id]; |
| 85 |
delete target.$ref; |
| 86 |
delete target._loadObject; |
| 87 |
update = true; |
| 88 |
}else{ |
| 89 |
var proto = schema && schema.prototype; |
| 90 |
if(proto){ |
| 91 |
|
| 92 |
F.prototype = proto; |
| 93 |
target = new F(); |
| 94 |
} |
| 95 |
} |
| 96 |
index[id] = target; |
| 97 |
if(timeStamps){ |
| 98 |
timeStamps[id] = args.time; |
| 99 |
} |
| 100 |
} |
| 101 |
while(schema){ |
| 102 |
var properties = schema.properties; |
| 103 |
if(properties){ |
| 104 |
for(i in it){ |
| 105 |
var propertyDefinition = properties[i]; |
| 106 |
if(propertyDefinition && propertyDefinition.format == 'date-time' && typeof it[i] == 'string'){ |
| 107 |
it[i] = stamp.fromISOString(it[i]); |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
schema = schema["extends"]; |
| 112 |
} |
| 113 |
var length = it.length; |
| 114 |
for(i in it){ |
| 115 |
if(i==length){ |
| 116 |
break; |
| 117 |
} |
| 118 |
if(it.hasOwnProperty(i)){ |
| 119 |
val=it[i]; |
| 120 |
if((typeof val =='object') && val && !(val instanceof Date) && i != '__parent'){ |
| 121 |
ref=val[refAttribute] || (idAsRef && val[idAttribute]); |
| 122 |
if(it != reWalk && (!ref || !val.__parent)){ |
| 123 |
val.__parent = parent ? parent : target; |
| 124 |
} |
| 125 |
if(ref){ |
| 126 |
|
| 127 |
delete it[i]; |
| 128 |
var path = ref.toString().replace(/(#)([^\.\[])/,'$1.$2').match(/(^([^\[]*\/)?[^#\.\[]*)#?([\.\[].*)?/); |
| 129 |
if(index[(prefix + ref).replace(pathResolveRegex,'$2$3')]){ |
| 130 |
ref = index[(prefix + ref).replace(pathResolveRegex,'$2$3')]; |
| 131 |
}else if((ref = (path[1]=='$' || path[1]=='this' || path[1]==='') ? root : index[(prefix + path[1]).replace(pathResolveRegex,'$2$3')])){ |
| 132 |
|
| 133 |
if(path[3]){ |
| 134 |
path[3].replace(/(\[([^\]]+)\])|(\.?([^\.\[]+))/g,function(t,a,b,c,d){ |
| 135 |
ref = ref && ref[b ? b.replace(/[\"\'\\]/,'') : d]; |
| 136 |
}); |
| 137 |
} |
| 138 |
} |
| 139 |
if(ref){ |
| 140 |
val = ref; |
| 141 |
}else{ |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
if(!stop){ |
| 146 |
var rewalking; |
| 147 |
if(!rewalking){ |
| 148 |
reWalk.push(target); |
| 149 |
} |
| 150 |
rewalking = true; |
| 151 |
|
| 152 |
val = walk(val, false, val[refAttribute], true, propertyDefinition); |
| 153 |
|
| 154 |
val._loadObject = args.loader; |
| 155 |
} |
| 156 |
} |
| 157 |
}else{ |
| 158 |
if(!stop){ |
| 159 |
|
| 160 |
val = walk( |
| 161 |
val, |
| 162 |
reWalk==it, |
| 163 |
id === undefined ? undefined : addProp(id, i), |
| 164 |
false, |
| 165 |
|
| 166 |
propertyDefinition, |
| 167 |
|
| 168 |
|
| 169 |
target != it && typeof target[i] == 'object' && target[i], |
| 170 |
it |
| 171 |
); |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
it[i] = val; |
| 176 |
if(target!=it && !target.__isDirty){ |
| 177 |
var old = target[i]; |
| 178 |
target[i] = val; |
| 179 |
if(update && val !== old && |
| 180 |
!target._loadObject && |
| 181 |
!(i.charAt(0) == '_' && i.charAt(1) == '_') && i != "$ref" && |
| 182 |
!(val instanceof Date && old instanceof Date && val.getTime() == old.getTime()) && |
| 183 |
!(typeof val == 'function' && typeof old == 'function' && val.toString() == old.toString()) && |
| 184 |
index.onUpdate){ |
| 185 |
index.onUpdate(target,i,old,val); |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
if(update && (idAttribute in it || target instanceof Array)){ |
| 192 |
|
| 193 |
for(i in target){ |
| 194 |
if(!target.__isDirty && target.hasOwnProperty(i) && !it.hasOwnProperty(i) && !(i.charAt(0) == '_' && i.charAt(1) == '_') && !(target instanceof Array && isNaN(i))){ |
| 195 |
if(index.onUpdate && i != "_loadObject" && i != "_idAttr"){ |
| 196 |
index.onUpdate(target,i,target[i],undefined); |
| 197 |
} |
| 198 |
delete target[i]; |
| 199 |
while(target instanceof Array && target.length && target[target.length-1] === undefined){ |
| 200 |
|
| 201 |
target.length--; |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
}else{ |
| 206 |
if(index.onLoad){ |
| 207 |
index.onLoad(target); |
| 208 |
} |
| 209 |
} |
| 210 |
return target; |
| 211 |
} |
| 212 |
if(root && typeof root == 'object'){ |
| 213 |
root = walk(root,false,args.defaultId, true); |
| 214 |
walk(reWalk,false); |
| 215 |
} |
| 216 |
return root; |
| 217 |
}, |
| 218 |
|
| 219 |
|
| 220 |
fromJson: function( str, args){ |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
function ref(target){ |
| 231 |
var refObject = {}; |
| 232 |
refObject[this.refAttribute] = target; |
| 233 |
return refObject; |
| 234 |
} |
| 235 |
var root; |
| 236 |
try{ |
| 237 |
root = eval('(' + str + ')'); |
| 238 |
}catch(e){ |
| 239 |
throw new SyntaxError("Invalid JSON string: " + e.message + " parsing: "+ str); |
| 240 |
} |
| 241 |
if(root){ |
| 242 |
return this.resolveJson(root, args); |
| 243 |
} |
| 244 |
return root; |
| 245 |
}, |
| 246 |
|
| 247 |
toJson: function( it, prettyPrint, idPrefix, indexSubObjects, idAttribute){ |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
var useRefs = this._useRefs; |
| 264 |
var addProp = this._addProp; |
| 265 |
var refAttribute = this.refAttribute; |
| 266 |
idPrefix = idPrefix || ''; |
| 267 |
var paths={}; |
| 268 |
var generated = {}; |
| 269 |
var idAttributeProvided = idAttribute ? true: false; |
| 270 |
var uniqueIdCount = 1; |
| 271 |
function getUniqueId() { |
| 272 |
var str = (uniqueIdCount++).toString(); |
| 273 |
if (generated.hasOwnProperty(str)) { |
| 274 |
return getUniqueId(); |
| 275 |
} |
| 276 |
return str; |
| 277 |
} |
| 278 |
function serialize(it,path,_indentStr){ |
| 279 |
if(typeof it == 'object' && it){ |
| 280 |
var value; |
| 281 |
if(it instanceof Date){ |
| 282 |
return '"' + stamp.toISOString(it,{zulu:true}) + '"'; |
| 283 |
} |
| 284 |
var id = it.__id; |
| 285 |
if(id){ |
| 286 |
if(path != '#' && ((useRefs && !id.match(/#/)) || paths[id])){ |
| 287 |
var ref = id; |
| 288 |
if(id.charAt(0)!='#'){ |
| 289 |
if(it.__clientId == id){ |
| 290 |
ref = "cid:" + id; |
| 291 |
}else if(id.substring(0, idPrefix.length) == idPrefix){ |
| 292 |
|
| 293 |
ref = id.substring(idPrefix.length); |
| 294 |
}else{ |
| 295 |
|
| 296 |
ref = id; |
| 297 |
} |
| 298 |
} |
| 299 |
var refObject = {}; |
| 300 |
refObject[refAttribute] = ref; |
| 301 |
return djson.toJson(refObject, prettyPrint); |
| 302 |
} |
| 303 |
path = id; |
| 304 |
}else{ |
| 305 |
if (idAttributeProvided) { |
| 306 |
if(!(it instanceof Array)){ |
| 307 |
path = getUniqueId(); |
| 308 |
it.__id = path; |
| 309 |
generated[path] = it; |
| 310 |
} |
| 311 |
} else { |
| 312 |
it.__id = path; |
| 313 |
generated[path] = it; |
| 314 |
} |
| 315 |
} |
| 316 |
paths[path] = it; |
| 317 |
_indentStr = _indentStr || ""; |
| 318 |
var nextIndent = prettyPrint ? _indentStr + djson.toJsonIndentStr : ""; |
| 319 |
var newLine = prettyPrint ? "\n" : ""; |
| 320 |
var sep = prettyPrint ? " " : ""; |
| 321 |
|
| 322 |
if(it instanceof Array){ |
| 323 |
var res = array.map(it, function(obj,i){ |
| 324 |
var val = serialize(obj, idAttributeProvided ? undefined : addProp(path, i), nextIndent); |
| 325 |
if(typeof val != "string"){ |
| 326 |
val = "undefined"; |
| 327 |
} |
| 328 |
return newLine + nextIndent + val; |
| 329 |
}); |
| 330 |
return "[" + res.join("," + sep) + newLine + _indentStr + "]"; |
| 331 |
} |
| 332 |
|
| 333 |
var output = []; |
| 334 |
if (idAttributeProvided && (typeof it[idAttribute] === 'undefined')) { |
| 335 |
output.push(newLine + nextIndent + djson._escapeString(idAttribute) + ":" + sep + djson.toJson(it.__id)); |
| 336 |
} |
| 337 |
for(var i in it){ |
| 338 |
if(it.hasOwnProperty(i)){ |
| 339 |
var keyStr; |
| 340 |
if(typeof i == "number"){ |
| 341 |
keyStr = '"' + i + '"'; |
| 342 |
}else if(typeof i == "string" && (i.charAt(0) != '_' || i.charAt(1) != '_')){ |
| 343 |
|
| 344 |
keyStr = djson._escapeString(i); |
| 345 |
}else{ |
| 346 |
|
| 347 |
continue; |
| 348 |
} |
| 349 |
var val = serialize(it[i],idAttributeProvided ? undefined : addProp(path, i),nextIndent); |
| 350 |
if(typeof val != "string"){ |
| 351 |
|
| 352 |
continue; |
| 353 |
} |
| 354 |
output.push(newLine + nextIndent + keyStr + ":" + sep + val); |
| 355 |
} |
| 356 |
} |
| 357 |
return "{" + output.join("," + sep) + newLine + _indentStr + "}"; |
| 358 |
}else if(typeof it == "function" && dojox.json.ref.serializeFunctions){ |
| 359 |
return it.toString(); |
| 360 |
} |
| 361 |
|
| 362 |
return djson.toJson(it); |
| 363 |
} |
| 364 |
var json = serialize(it,idAttributeProvided ? undefined : '#',''); |
| 365 |
if(!indexSubObjects){ |
| 366 |
for(var i in generated) { |
| 367 |
delete generated[i].__id; |
| 368 |
} |
| 369 |
} |
| 370 |
return json; |
| 371 |
}, |
| 372 |
_addProp: function(id, prop){ |
| 373 |
return id + (id.match(/#/) ? id.length == 1 ? '' : '.' : '#') + prop; |
| 374 |
}, |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
refAttribute: "$ref", |
| 380 |
_useRefs: false, |
| 381 |
serializeFunctions: false |
| 382 |
}; |
| 383 |
}); |