Skip to content

Commit

Permalink
Issue #70.
Browse files Browse the repository at this point in the history
  • Loading branch information
highsource committed Apr 11, 2015
1 parent 0c0cc23 commit 0c0c5d8
Show file tree
Hide file tree
Showing 16 changed files with 266 additions and 245 deletions.
248 changes: 128 additions & 120 deletions nodejs/scripts/jsonix.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions scripts/src/main/javascript/org/hisrc/jsonix/Jsonix.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@
'Jsonix/Mapping/Style.js',
'Jsonix/Mapping/Styled.js',
'Jsonix/Binding.js',
'Jsonix/Binding/ElementMarshaller.js',
'Jsonix/Binding/ElementUnmarshaller.js',
'Jsonix/Binding/Marshalls.js',
'Jsonix/Binding/Unmarshalls.js',
'Jsonix/Context/Marshaller.js',
'Jsonix/Context/Unmarshaller.js',
'Jsonix/Model/TypeInfo.js',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Jsonix.Binding.Marshaller = Jsonix.Class(Jsonix.Binding.ElementMarshaller, {
Jsonix.Binding.Marshaller = Jsonix.Class(Jsonix.Binding.Marshalls.Element, Jsonix.Binding.Marshalls.Element.AsElementRef, {
context : null,
initialize : function(context) {
Jsonix.Util.Ensure.ensureObject(context);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
Jsonix.Binding.ElementMarshaller = Jsonix.Class({
Jsonix.Binding.Marshalls = {
};

Jsonix.Binding.Marshalls.Element = Jsonix.Class({
marshalElement : function(value, context, output, scope) {
var elementValue = this.getOutputElementValue(value, context, output, scope);
var elementValue = this.convertToTypedNamedValue(value, context, output, scope);
var declaredTypeInfo = elementValue.typeInfo;
var typeInfo = declaredTypeInfo;
if (Jsonix.Util.Type.exists(declaredTypeInfo))
{
if (Jsonix.Util.Type.exists(declaredTypeInfo)) {
output.writeStartElement(elementValue.name);
if (Jsonix.Util.Type.exists(elementValue.value)) {
if (context.supportXsiType) {
var actualTypeInfo = context.getTypeInfoByValue(elementValue.value);
if (actualTypeInfo && actualTypeInfo.typeName && declaredTypeInfo !== actualTypeInfo)
{
if (actualTypeInfo && actualTypeInfo.typeName && declaredTypeInfo !== actualTypeInfo) {
typeInfo = actualTypeInfo;
var xsiTypeName = actualTypeInfo.typeName;
var xsiType = Jsonix.Schema.XSD.QName.INSTANCE.print(xsiTypeName, context, output, scope);
Expand All @@ -20,22 +21,30 @@ Jsonix.Binding.ElementMarshaller = Jsonix.Class({
typeInfo.marshal(elementValue.value, context, output, scope);
}
output.writeEndElement();
}
else
{
} else {
throw new Error("Element [" + elementValue.name.key + "] is not known in this context.");
}
},
getOutputElementValue : function (value, context, output, scope) {
getTypeInfoByElementName : function(name, context, scope) {
var elementInfo = context.getElementInfo(name, scope);
if (Jsonix.Util.Type.exists(elementInfo)) {
return elementInfo.typeInfo;
} else {
return undefined;
}
}
});
Jsonix.Binding.Marshalls.Element.AsElementRef = Jsonix.Class({
convertToTypedNamedValue : function(value, context, output, scope) {
Jsonix.Util.Ensure.ensureObject(value);
var elementValue = this.convertFromElementValue(value, context, output, scope);
var elementValue = this.convertToNamedValue(value, context, output, scope);
return {
name : elementValue.name,
value : elementValue.value,
typeInfo : this.getTypeInfoByElementName(elementValue.name, context, scope)
};
},
convertFromElementValue : function(elementValue, context, output, scope) {
convertToNamedValue : function(elementValue, context, output, scope) {
var name;
var value;
if (Jsonix.Util.Type.exists(elementValue.name) && !Jsonix.Util.Type.isUndefined(elementValue.value)) {
Expand All @@ -58,13 +67,5 @@ Jsonix.Binding.ElementMarshaller = Jsonix.Class({
}
}
throw new Error("Invalid element value [" + elementValue + "]. Element values must either have {name:'myElementName', value: elementValue} or {myElementName:elementValue} structure.");
},
getTypeInfoByElementName : function(name, context, scope) {
var elementInfo = context.getElementInfo(name, scope);
if (Jsonix.Util.Type.exists(elementInfo)) {
return elementInfo.typeInfo;
} else {
return undefined;
}
}
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Jsonix.Binding.Unmarshaller = Jsonix.Class(Jsonix.Binding.ElementUnmarshaller, {
Jsonix.Binding.Unmarshaller = Jsonix.Class(Jsonix.Binding.Unmarshalls.Element, Jsonix.Binding.Unmarshalls.Element.AsElementRef, {
context : null,
allowTypedObject : true,
allowDom : false,
Expand Down Expand Up @@ -54,11 +54,8 @@ Jsonix.Binding.Unmarshaller = Jsonix.Class(Jsonix.Binding.ElementUnmarshaller, {
return result;

},
convertToElementValue : function(elementValue, context, input, scope) {
return elementValue;
},
CLASS_NAME : 'Jsonix.Binding.Unmarshaller'
});
Jsonix.Binding.Unmarshaller.Simplified = Jsonix.Class(Jsonix.Binding.Unmarshaller, Jsonix.Binding.ElementUnmarshaller.Simplified, {
Jsonix.Binding.Unmarshaller.Simplified = Jsonix.Class(Jsonix.Binding.Unmarshaller, Jsonix.Binding.Unmarshalls.Element.AsSimplifiedElementRef, {
CLASS_NAME : 'Jsonix.Binding.Unmarshaller.Simplified'
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Jsonix.Binding.ElementUnmarshaller = Jsonix.Class({
allowTypedObject : true,
allowDom : false,
Jsonix.Binding.Unmarshalls = {
};

Jsonix.Binding.Unmarshalls.WrapperElement = Jsonix.Class({
mixed : false,
unmarshalWrapperElement : function(context, input, scope, callback) {
var et = input.next();
Expand All @@ -18,7 +19,12 @@ Jsonix.Binding.ElementUnmarshaller = Jsonix.Class({
}
et = input.next();
}
},
}
});

Jsonix.Binding.Unmarshalls.Element = Jsonix.Class({
allowTypedObject : true,
allowDom : false,
unmarshalElement : function(context, input, scope, callback) {
if (input.eventType != 1) {
throw new Error("Parser must be on START_ELEMENT to read next element.");
Expand All @@ -28,10 +34,12 @@ Jsonix.Binding.ElementUnmarshaller = Jsonix.Class({
var elementValue;
if (this.allowTypedObject && Jsonix.Util.Type.exists(typeInfo)) {
var value = typeInfo.unmarshal(context, input, scope);
elementValue = this.convertToElementValue({
var typedNamedValue = {
name : name,
value : value
}, context, input, scope);
value : value,
typeInfo : typeInfo
};
elementValue = this.convertFromTypedNamedValue(typedNamedValue, context, input, scope);
} else if (this.allowDom) {
elementValue = input.getElement();
} else {
Expand Down Expand Up @@ -63,11 +71,20 @@ Jsonix.Binding.ElementUnmarshaller = Jsonix.Class({
}
});

Jsonix.Binding.ElementUnmarshaller.Simplified = Jsonix.Class({
convertToElementValue : function(elementValue, context, input, scope) {
var propertyName = elementValue.name.toCanonicalString(context);
Jsonix.Binding.Unmarshalls.Element.AsElementRef = Jsonix.Class({
convertFromTypedNamedValue : function(typedNamedValue, context, input, scope) {
return {
name : typedNamedValue.name,
value : typedNamedValue.value
};
}
});

Jsonix.Binding.Unmarshalls.Element.AsSimplifiedElementRef = Jsonix.Class({
convertFromTypedNamedValue : function(typedNamedValue, context, input, scope) {
var propertyName = typedNamedValue.name.toCanonicalString(context);
var value = {};
value[propertyName] = elementValue.value;
value[propertyName] = typedNamedValue.value;
return value;
}
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Jsonix.Model.AbstractElementRefsPropertyInfo = Jsonix.Class(Jsonix.Binding.ElementMarshaller, Jsonix.Binding.ElementUnmarshaller, Jsonix.Model.PropertyInfo, {
Jsonix.Model.AbstractElementRefsPropertyInfo = Jsonix.Class(Jsonix.Binding.Marshalls.Element, Jsonix.Binding.Marshalls.Element.AsElementRef, Jsonix.Binding.Unmarshalls.Element, Jsonix.Binding.Unmarshalls.WrapperElement, Jsonix.Binding.Unmarshalls.Element.AsElementRef, Jsonix.Model.PropertyInfo, {
wrapperElementName : null,
allowDom : true,
allowTypedObject : true,
Expand Down Expand Up @@ -104,9 +104,6 @@ Jsonix.Model.AbstractElementRefsPropertyInfo = Jsonix.Class(Jsonix.Binding.Eleme
}

},
convertToElementValue : function(elementValue, context, input, scope) {
return elementValue;
},
getTypeInfoByElementName : function(elementName, context, scope) {
var propertyElementTypeInfo = this.getPropertyElementTypeInfo(elementName, context);
if (Jsonix.Util.Type.exists(propertyElementTypeInfo)) {
Expand Down Expand Up @@ -148,7 +145,7 @@ Jsonix.Model.AbstractElementRefsPropertyInfo = Jsonix.Class(Jsonix.Binding.Eleme
// {
// structure.elements[key] = this;
// }

if ((this.allowDom || this.allowTypedObject)) {
structure.any = this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Jsonix.Model.AbstractElementsPropertyInfo = Jsonix.Class(Jsonix.Binding.ElementUnmarshaller, Jsonix.Model.PropertyInfo, {
Jsonix.Model.AbstractElementsPropertyInfo = Jsonix.Class(Jsonix.Binding.Unmarshalls.Element, Jsonix.Binding.Unmarshalls.WrapperElement, Jsonix.Model.PropertyInfo, {
wrapperElementName : null,
allowDom : false,
allowTypedObject : true,
Expand Down Expand Up @@ -69,7 +69,7 @@ Jsonix.Model.AbstractElementsPropertyInfo = Jsonix.Class(Jsonix.Binding.ElementU
output.writeEndElement();
}
},
convertToElementValue : function(elementValue, context, input, scope) {
convertFromTypedNamedValue : function(elementValue, context, input, scope) {
return elementValue.value;
},
buildStructure : function(context, structure) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Jsonix.Model.AnyElementPropertyInfo = Jsonix.Class(Jsonix.Binding.ElementMarshaller, Jsonix.Binding.ElementUnmarshaller, Jsonix.Model.PropertyInfo, {
Jsonix.Model.AnyElementPropertyInfo = Jsonix.Class(Jsonix.Binding.Marshalls.Element, Jsonix.Binding.Marshalls.Element.AsElementRef, Jsonix.Binding.Unmarshalls.Element, Jsonix.Binding.Unmarshalls.Element.AsElementRef, Jsonix.Model.PropertyInfo, {
allowDom : true,
allowTypedObject : true,
mixed : true,
Expand Down Expand Up @@ -31,7 +31,7 @@ Jsonix.Model.AnyElementPropertyInfo = Jsonix.Class(Jsonix.Binding.ElementMarshal
}
}
};

var et = input.eventType;
if (et === Jsonix.XML.Input.START_ELEMENT) {
this.unmarshalElement(context, input, scope, callback);
Expand All @@ -41,12 +41,12 @@ Jsonix.Model.AnyElementPropertyInfo = Jsonix.Class(Jsonix.Binding.ElementMarshal
// Whitespace
// return null;
} else if (et === Jsonix.XML.Input.COMMENT || et === Jsonix.XML.Input.PROCESSING_INSTRUCTION) {
//return null;
// return null;
} else {
// TODO better exception
throw new Error("Illegal state: unexpected event type [" + et + "].");
}

return result;
},
marshal : function(value, context, output, scope) {
Expand All @@ -57,7 +57,7 @@ Jsonix.Model.AnyElementPropertyInfo = Jsonix.Class(Jsonix.Binding.ElementMarshal
this.marshalItem(value, context, output, scope);
} else {
Jsonix.Util.Ensure.ensureArray(value);
for ( var index = 0; index < value.length; index++) {
for (var index = 0; index < value.length; index++) {
this.marshalItem(value[index], context, output, scope);
}
}
Expand All @@ -71,16 +71,12 @@ Jsonix.Model.AnyElementPropertyInfo = Jsonix.Class(Jsonix.Binding.ElementMarshal
output.writeNode(value);

} else {
if (this.allowTypedObject)
{
if (this.allowTypedObject) {
this.marshalElement(value, context, output, scope);
}
}
},
convertToElementValue : function(elementValue, context, input, scope) {
return elementValue;
},
doBuild : function(context, module) {
doBuild : function(context, module) {
// Nothing to do
},
buildStructure : function(context, structure) {
Expand Down Expand Up @@ -115,6 +111,6 @@ Jsonix.Model.AnyElementPropertyInfo = Jsonix.Class(Jsonix.Binding.ElementMarshal
},
CLASS_NAME : 'Jsonix.Model.AnyElementPropertyInfo'
});
Jsonix.Model.AnyElementPropertyInfo.Simplified = Jsonix.Class(Jsonix.Model.AnyElementPropertyInfo, Jsonix.Binding.ElementUnmarshaller.Simplified, {
Jsonix.Model.AnyElementPropertyInfo.Simplified = Jsonix.Class(Jsonix.Model.AnyElementPropertyInfo, Jsonix.Binding.Unmarshalls.Element.AsSimplifiedElementRef, {
CLASS_NAME : 'Jsonix.Model.AnyElementPropertyInfo.Simplified'
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ Jsonix.Model.ElementMapPropertyInfo = Jsonix.Class(Jsonix.Model.AbstractElements
Jsonix.Util.Ensure.ensureObject(mapping);
Jsonix.Model.AbstractElementsPropertyInfo.prototype.initialize.apply(this, [ mapping ]);
// TODO Ensure correct argument
var k = mapping.key||mapping.k||undefined;
var k = mapping.key || mapping.k || undefined;
Jsonix.Util.Ensure.ensureObject(k);
var v = mapping.value||mapping.v||undefined;
var v = mapping.value || mapping.v || undefined;
Jsonix.Util.Ensure.ensureObject(v);
// TODO Ensure correct argument
var en = mapping.elementName||mapping.en||undefined;
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)) {
Expand All @@ -21,7 +21,7 @@ Jsonix.Model.ElementMapPropertyInfo = Jsonix.Class(Jsonix.Model.AbstractElements
this.elementName = new Jsonix.XML.QName(this.defaultElementNamespaceURI, this.name);
}
this.entryTypeInfo = new Jsonix.Model.ClassInfo({
name: 'Map<' + k.name + ',' + v.name + '>',
name : 'Map<' + k.name + ',' + v.name + '>',
propertyInfos : [ k, v ]
});

Expand Down Expand Up @@ -64,10 +64,10 @@ Jsonix.Model.ElementMapPropertyInfo = Jsonix.Class(Jsonix.Model.AbstractElements
}
return result;
},
getTypeInfoByInputElement : function (context, input, scope) {
getTypeInfoByInputElement : function(context, input, scope) {
return this.entryTypeInfo;
},
convertToElementValue : function(elementValue, context, input, scope) {
convertFromTypedNamedValue : function(elementValue, context, input, scope) {
var entry = elementValue.value;
var result = {};
if (Jsonix.Util.Type.isString(entry[this.key.name])) {
Expand Down Expand Up @@ -106,7 +106,7 @@ Jsonix.Model.ElementMapPropertyInfo = Jsonix.Class(Jsonix.Model.AbstractElements
output.writeEndElement();

} else {
for ( var index = 0; index < attributeValue.length; index++) {
for (var index = 0; index < attributeValue.length; index++) {
var collectionEntry = {};
collectionEntry[this.key.name] = attributeName;
collectionEntry[this.value.name] = attributeValue[index];
Expand All @@ -119,10 +119,10 @@ Jsonix.Model.ElementMapPropertyInfo = Jsonix.Class(Jsonix.Model.AbstractElements
}
}
},
doBuild: function(context, module) {
doBuild : function(context, module) {
this.entryTypeInfo.build(context, module);
// TODO get property by name
this.key = this.entryTypeInfo.properties[0];
this.key = this.entryTypeInfo.properties[0];
this.value = this.entryTypeInfo.properties[1];
},
buildStructureElements : function(context, structure) {
Expand All @@ -143,7 +143,7 @@ Jsonix.Model.ElementMapPropertyInfo = Jsonix.Class(Jsonix.Model.AbstractElements
map[attributeName] = [];
}

for ( var index = 0; index < attributeValue.length; index++) {
for (var index = 0; index < attributeValue.length; index++) {
map[attributeName].push(attributeValue[index]);
}
} else {
Expand Down
Loading

0 comments on commit 0c0c5d8

Please sign in to comment.