org.hisrc.jsonix.Jsonix.Model.ElementMapPropertyInfo.js Maven / Gradle / Ivy
Jsonix.Model.ElementMapPropertyInfo = Jsonix.Class(Jsonix.Model.AbstractElementsPropertyInfo, {
elementName : null,
key : null,
value : null,
entryTypeInfo : null,
initialize : function(mapping) {
Jsonix.Util.Ensure.ensureObject(mapping);
Jsonix.Model.AbstractElementsPropertyInfo.prototype.initialize.apply(this, [ mapping ]);
// TODO Ensure correct argument
var k = mapping.key||mapping.k||undefined;
Jsonix.Util.Ensure.ensureObject(k);
var v = mapping.value||mapping.v||undefined;
Jsonix.Util.Ensure.ensureObject(v);
// TODO Ensure correct argument
var en = mapping.elementName||mapping.en||undefined;
if (Jsonix.Util.Type.isObject(en)) {
this.elementName = Jsonix.XML.QName.fromObject(en);
} else if (Jsonix.Util.Type.isString(en)) {
this.elementName = new Jsonix.XML.QName(this.defaultElementNamespaceURI, en);
} else {
this.elementName = new Jsonix.XML.QName(this.defaultElementNamespaceURI, this.name);
}
this.entryTypeInfo = new Jsonix.Model.ClassInfo({
name: 'Map<' + k.name + ',' + v.name + '>',
propertyInfos : [ k, v ]
});
},
unmarshalWrapperElement : function(context, input, scope) {
var result = Jsonix.Model.AbstractElementsPropertyInfo.prototype.unmarshalWrapperElement.apply(this, arguments);
},
unmarshal : function(context, input, scope) {
var result = null;
var that = this;
var callback = function(value) {
if (Jsonix.Util.Type.exists(value)) {
Jsonix.Util.Ensure.ensureObject(value, 'Map property requires an object.');
if (!Jsonix.Util.Type.exists(result)) {
result = {};
}
for ( var attributeName in value) {
if (value.hasOwnProperty(attributeName)) {
var attributeValue = value[attributeName];
if (that.collection) {
if (!Jsonix.Util.Type.exists(result[attributeName])) {
result[attributeName] = [];
}
result[attributeName].push(attributeValue);
} else {
if (!Jsonix.Util.Type.exists(result[attributeName])) {
result[attributeName] = attributeValue;
} else {
// TODO Report validation error
throw new Error("Value was already set.");
}
}
}
}
}
};
if (Jsonix.Util.Type.exists(this.wrapperElementName)) {
this.unmarshalWrapperElement(context, input, scope, callback);
} else {
this.unmarshalElement(context, input, scope, callback);
}
return result;
},
unmarshalElement : function(context, input, scope, callback) {
var entry = this.entryTypeInfo.unmarshal(context, input, scope);
var result = {};
if (!!entry[this.key.name]) {
result[entry[this.key.name]] = entry[this.value.name];
}
return callback(result);
},
marshal : function(value, context, output, scope) {
if (!Jsonix.Util.Type.exists(value)) {
// Do nothing
return;
}
if (Jsonix.Util.Type.exists(this.wrapperElementName)) {
output.writeStartElement(this.wrapperElementName);
}
this.marshalElementNode(value, context, output, scope);
if (Jsonix.Util.Type.exists(this.wrapperElementName)) {
output.writeEndElement();
}
},
marshalElementNode : function(value, context, output, scope) {
if (!!value) {
for ( var attributeName in value) {
if (value.hasOwnProperty(attributeName)) {
var attributeValue = value[attributeName];
if (!this.collection) {
var singleEntry = {};
singleEntry[this.key.name] = attributeName;
singleEntry[this.value.name] = attributeValue;
output.writeStartElement(this.elementName);
this.entryTypeInfo.marshal(singleEntry, context, output, scope);
output.writeEndElement();
} else {
for ( var index = 0; index < attributeValue.length; index++) {
var collectionEntry = {};
collectionEntry[this.key.name] = attributeName;
collectionEntry[this.value.name] = attributeValue[index];
output.writeStartElement(this.elementName);
this.entryTypeInfo.marshal(collectionEntry, context, output, scope);
output.writeEndElement();
}
}
}
}
}
},
doBuild: function(context, module) {
this.entryTypeInfo.build(context, module);
// TODO get property by name
this.key = this.entryTypeInfo.properties[0];
this.value = this.entryTypeInfo.properties[1];
},
buildStructureElements : function(context, structure) {
structure.elements[this.elementName.key] = this;
},
setProperty : function(object, value) {
if (Jsonix.Util.Type.exists(value)) {
Jsonix.Util.Ensure.ensureObject(value, 'Map property requires an object.');
if (!Jsonix.Util.Type.exists(object[this.name])) {
object[this.name] = {};
}
var map = object[this.name];
for ( var attributeName in value) {
if (value.hasOwnProperty(attributeName)) {
var attributeValue = value[attributeName];
if (this.collection) {
if (!Jsonix.Util.Type.exists(map[attributeName])) {
map[attributeName] = [];
}
for ( var index = 0; index < attributeValue.length; index++) {
map[attributeName].push(attributeValue[index]);
}
} else {
map[attributeName] = attributeValue;
}
}
}
}
},
CLASS_NAME : 'Jsonix.Model.ElementMapPropertyInfo'
});
© 2015 - 2024 Weber Informatics LLC | Privacy Policy