Skip to content

Commit 2a795ec

Browse files
committed
Fixes issue #96.
1 parent 9afbb87 commit 2a795ec

File tree

5 files changed

+65
-54
lines changed

5 files changed

+65
-54
lines changed

dist/Jsonix-all.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1849,24 +1849,29 @@ Jsonix.Binding.Marshalls.Element = Jsonix.Class({
18491849
marshalElement : function(value, context, output, scope) {
18501850
var elementValue = this.convertToTypedNamedValue(value, context, output, scope);
18511851
var declaredTypeInfo = elementValue.typeInfo;
1852-
var typeInfo = declaredTypeInfo;
1853-
if (Jsonix.Util.Type.exists(declaredTypeInfo)) {
1852+
var actualTypeInfo = undefined;
1853+
if (context.supportXsiType && Jsonix.Util.Type.exists(elementValue.value))
1854+
{
1855+
var typeInfoByValue = context.getTypeInfoByValue(elementValue.value);
1856+
if (typeInfoByValue && typeInfoByValue.typeName)
1857+
{
1858+
actualTypeInfo = typeInfoByValue;
1859+
}
1860+
}
1861+
var typeInfo = actualTypeInfo || declaredTypeInfo;
1862+
if (typeInfo) {
18541863
output.writeStartElement(elementValue.name);
1864+
if (actualTypeInfo && declaredTypeInfo !== actualTypeInfo) {
1865+
var xsiTypeName = actualTypeInfo.typeName;
1866+
var xsiType = Jsonix.Schema.XSD.QName.INSTANCE.print(xsiTypeName, context, output, scope);
1867+
output.writeAttribute(Jsonix.Schema.XSI.TYPE_QNAME, xsiType);
1868+
}
18551869
if (Jsonix.Util.Type.exists(elementValue.value)) {
1856-
if (context.supportXsiType) {
1857-
var actualTypeInfo = context.getTypeInfoByValue(elementValue.value);
1858-
if (actualTypeInfo && actualTypeInfo.typeName && declaredTypeInfo !== actualTypeInfo) {
1859-
typeInfo = actualTypeInfo;
1860-
var xsiTypeName = actualTypeInfo.typeName;
1861-
var xsiType = Jsonix.Schema.XSD.QName.INSTANCE.print(xsiTypeName, context, output, scope);
1862-
output.writeAttribute(Jsonix.Schema.XSI.TYPE_QNAME, xsiType);
1863-
}
1864-
}
18651870
typeInfo.marshal(elementValue.value, context, output, scope);
18661871
}
18671872
output.writeEndElement();
18681873
} else {
1869-
throw new Error("Element [" + elementValue.name.key + "] is not known in this context.");
1874+
throw new Error("Element [" + elementValue.name.key + "] is not known in this context, could not determine its type.");
18701875
}
18711876
},
18721877
getTypeInfoByElementName : function(name, context, scope) {

dist/Jsonix-min.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -739,18 +739,19 @@ if(!c){throw new Error("Mapping style ["+d.mappingStyle+"] is not known.")
739739
}},CLASS_NAME:"Jsonix.Mapping.Styled"});
740740
Jsonix.Binding={};
741741
Jsonix.Binding.Marshalls={};
742-
Jsonix.Binding.Marshalls.Element=Jsonix.Class({marshalElement:function(n,s,r,k){var p=this.convertToTypedNamedValue(n,s,r,k);
743-
var o=p.typeInfo;
744-
var l=o;
745-
if(Jsonix.Util.Type.exists(o)){r.writeStartElement(p.name);
746-
if(Jsonix.Util.Type.exists(p.value)){if(s.supportXsiType){var t=s.getTypeInfoByValue(p.value);
747-
if(t&&t.typeName&&o!==t){l=t;
748-
var m=t.typeName;
749-
var q=Jsonix.Schema.XSD.QName.INSTANCE.print(m,s,r,k);
750-
r.writeAttribute(Jsonix.Schema.XSI.TYPE_QNAME,q)
751-
}}l.marshal(p.value,s,r,k)
752-
}r.writeEndElement()
753-
}else{throw new Error("Element ["+p.name.key+"] is not known in this context.")
742+
Jsonix.Binding.Marshalls.Element=Jsonix.Class({marshalElement:function(o,u,t,l){var q=this.convertToTypedNamedValue(o,u,t,l);
743+
var p=q.typeInfo;
744+
var v=undefined;
745+
if(u.supportXsiType&&Jsonix.Util.Type.exists(q.value)){var r=u.getTypeInfoByValue(q.value);
746+
if(r&&r.typeName){v=r
747+
}}var m=v||p;
748+
if(m){t.writeStartElement(q.name);
749+
if(v&&p!==v){var n=v.typeName;
750+
var s=Jsonix.Schema.XSD.QName.INSTANCE.print(n,u,t,l);
751+
t.writeAttribute(Jsonix.Schema.XSI.TYPE_QNAME,s)
752+
}if(Jsonix.Util.Type.exists(q.value)){m.marshal(q.value,u,t,l)
753+
}t.writeEndElement()
754+
}else{throw new Error("Element ["+q.name.key+"] is not known in this context, could not determine its type.")
754755
}},getTypeInfoByElementName:function(f,e,g){var h=e.getElementInfo(f,g);
755756
if(Jsonix.Util.Type.exists(h)){return h.typeInfo
756757
}else{return undefined

nodejs/scripts/jsonix.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1849,24 +1849,29 @@ Jsonix.Binding.Marshalls.Element = Jsonix.Class({
18491849
marshalElement : function(value, context, output, scope) {
18501850
var elementValue = this.convertToTypedNamedValue(value, context, output, scope);
18511851
var declaredTypeInfo = elementValue.typeInfo;
1852-
var typeInfo = declaredTypeInfo;
1853-
if (Jsonix.Util.Type.exists(declaredTypeInfo)) {
1852+
var actualTypeInfo = undefined;
1853+
if (context.supportXsiType && Jsonix.Util.Type.exists(elementValue.value))
1854+
{
1855+
var typeInfoByValue = context.getTypeInfoByValue(elementValue.value);
1856+
if (typeInfoByValue && typeInfoByValue.typeName)
1857+
{
1858+
actualTypeInfo = typeInfoByValue;
1859+
}
1860+
}
1861+
var typeInfo = actualTypeInfo || declaredTypeInfo;
1862+
if (typeInfo) {
18541863
output.writeStartElement(elementValue.name);
1864+
if (actualTypeInfo && declaredTypeInfo !== actualTypeInfo) {
1865+
var xsiTypeName = actualTypeInfo.typeName;
1866+
var xsiType = Jsonix.Schema.XSD.QName.INSTANCE.print(xsiTypeName, context, output, scope);
1867+
output.writeAttribute(Jsonix.Schema.XSI.TYPE_QNAME, xsiType);
1868+
}
18551869
if (Jsonix.Util.Type.exists(elementValue.value)) {
1856-
if (context.supportXsiType) {
1857-
var actualTypeInfo = context.getTypeInfoByValue(elementValue.value);
1858-
if (actualTypeInfo && actualTypeInfo.typeName && declaredTypeInfo !== actualTypeInfo) {
1859-
typeInfo = actualTypeInfo;
1860-
var xsiTypeName = actualTypeInfo.typeName;
1861-
var xsiType = Jsonix.Schema.XSD.QName.INSTANCE.print(xsiTypeName, context, output, scope);
1862-
output.writeAttribute(Jsonix.Schema.XSI.TYPE_QNAME, xsiType);
1863-
}
1864-
}
18651870
typeInfo.marshal(elementValue.value, context, output, scope);
18661871
}
18671872
output.writeEndElement();
18681873
} else {
1869-
throw new Error("Element [" + elementValue.name.key + "] is not known in this context.");
1874+
throw new Error("Element [" + elementValue.name.key + "] is not known in this context, could not determine its type.");
18701875
}
18711876
},
18721877
getTypeInfoByElementName : function(name, context, scope) {
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1 @@
1-
<?xml version="1.0" encoding="UTF-8"?>
2-
<testany:TESTANY xmlns:testany="test:any" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="test:any test_any.xsd">
3-
<en>
4-
<INCLUDED xsi:type="testany:INCLUDED" majorVersion="1"/>
5-
</en>
6-
</testany:TESTANY>
1+
<testany:TESTANY xmlns:testany="test:any"><en><INCLUDED xsi:type="testany:INCLUDED" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" majorVersion="1"/></en></testany:TESTANY>

scripts/src/main/javascript/org/hisrc/jsonix/Jsonix/Binding/Marshalls.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,29 @@ Jsonix.Binding.Marshalls.Element = Jsonix.Class({
55
marshalElement : function(value, context, output, scope) {
66
var elementValue = this.convertToTypedNamedValue(value, context, output, scope);
77
var declaredTypeInfo = elementValue.typeInfo;
8-
var typeInfo = declaredTypeInfo;
9-
if (Jsonix.Util.Type.exists(declaredTypeInfo)) {
8+
var actualTypeInfo = undefined;
9+
if (context.supportXsiType && Jsonix.Util.Type.exists(elementValue.value))
10+
{
11+
var typeInfoByValue = context.getTypeInfoByValue(elementValue.value);
12+
if (typeInfoByValue && typeInfoByValue.typeName)
13+
{
14+
actualTypeInfo = typeInfoByValue;
15+
}
16+
}
17+
var typeInfo = actualTypeInfo || declaredTypeInfo;
18+
if (typeInfo) {
1019
output.writeStartElement(elementValue.name);
20+
if (actualTypeInfo && declaredTypeInfo !== actualTypeInfo) {
21+
var xsiTypeName = actualTypeInfo.typeName;
22+
var xsiType = Jsonix.Schema.XSD.QName.INSTANCE.print(xsiTypeName, context, output, scope);
23+
output.writeAttribute(Jsonix.Schema.XSI.TYPE_QNAME, xsiType);
24+
}
1125
if (Jsonix.Util.Type.exists(elementValue.value)) {
12-
if (context.supportXsiType) {
13-
var actualTypeInfo = context.getTypeInfoByValue(elementValue.value);
14-
if (actualTypeInfo && actualTypeInfo.typeName && declaredTypeInfo !== actualTypeInfo) {
15-
typeInfo = actualTypeInfo;
16-
var xsiTypeName = actualTypeInfo.typeName;
17-
var xsiType = Jsonix.Schema.XSD.QName.INSTANCE.print(xsiTypeName, context, output, scope);
18-
output.writeAttribute(Jsonix.Schema.XSI.TYPE_QNAME, xsiType);
19-
}
20-
}
2126
typeInfo.marshal(elementValue.value, context, output, scope);
2227
}
2328
output.writeEndElement();
2429
} else {
25-
throw new Error("Element [" + elementValue.name.key + "] is not known in this context.");
30+
throw new Error("Element [" + elementValue.name.key + "] is not known in this context, could not determine its type.");
2631
}
2732
},
2833
getTypeInfoByElementName : function(name, context, scope) {

0 commit comments

Comments
 (0)