Skip to content

Commit 21baad4

Browse files
committed
Issue #75.
1 parent b14e526 commit 21baad4

File tree

7 files changed

+162
-36
lines changed

7 files changed

+162
-36
lines changed

dist/Jsonix-all.js

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,29 @@ Jsonix.Util.Type = {
503503
}
504504
}
505505
return target;
506+
},
507+
defaultValue : function()
508+
{
509+
var args = arguments;
510+
if (args.length === 0)
511+
{
512+
return undefined;
513+
}
514+
else
515+
{
516+
var defaultValue = args[args.length - 1];
517+
var typeOfDefaultValue = typeof defaultValue;
518+
for (var index = 0; index < args.length - 1; index++)
519+
{
520+
var candidateValue = args[index];
521+
if (typeof candidateValue === typeOfDefaultValue)
522+
{
523+
return candidateValue;
524+
}
525+
}
526+
return defaultValue;
527+
528+
}
506529
}
507530
};
508531
Jsonix.Util.NumberUtils = {
@@ -1904,8 +1927,18 @@ Jsonix.Binding.Unmarshaller = Jsonix.Class(Jsonix.Binding.ElementUnmarshaller, {
19041927
throw new Error("Parser must be on START_ELEMENT to read next text.");
19051928
}
19061929
var result = null;
1930+
// Issue #70 work in progress here
1931+
var xsiTypeInfo = null;
1932+
if (context.supportXsiType) {
1933+
var xsiType = input.getAttributeValueNS(Jsonix.Schema.XSI.NAMESPACE_URI, Jsonix.Schema.XSI.TYPE);
1934+
if (Jsonix.Util.StringUtils.isNotBlank(xsiType))
1935+
{
1936+
var xsiTypeName = Jsonix.Schema.XSD.QName.INSTANCE.parse(xsiType, context, input, scope);
1937+
xsiTypeInfo = context.getTypeInfoByTypeNameKey(xsiTypeName.key);
1938+
}
1939+
}
19071940
var name = input.getName();
1908-
var typeInfo = this.getElementTypeInfo(name, context, scope);
1941+
var typeInfo = xsiTypeInfo ? xsiTypeInfo : this.getElementTypeInfo(name, context, scope);
19091942
if (Jsonix.Util.Type.exists(typeInfo))
19101943
{
19111944
var value = typeInfo.unmarshal(context, input, scope);
@@ -3145,9 +3178,9 @@ Jsonix.Model.AbstractElementRefsPropertyInfo = Jsonix.Class(Jsonix.Binding.Eleme
31453178
} else {
31463179
this.wrapperElementName = null;
31473180
}
3148-
var dom = mapping.allowDom || mapping.dom || true;
3149-
var typed = mapping.allowTypedObject || mapping.typed || true;
3150-
var mx = mapping.mixed || mapping.mx || true;
3181+
var dom = Jsonix.Util.Type.defaultValue(mapping.allowDom, mapping.dom, true);
3182+
var typed = Jsonix.Util.Type.defaultValue(mapping.allowTypedObject, mapping.typed, true);
3183+
var mx = Jsonix.Util.Type.defaultValue(mapping.mixed, mapping.mx, true);
31513184
this.allowDom = dom;
31523185
this.allowTypedObject = typed;
31533186
this.mixed = mx;
@@ -3464,9 +3497,9 @@ Jsonix.Model.AnyElementPropertyInfo = Jsonix.Class(Jsonix.Binding.ElementMarshal
34643497
initialize : function(mapping) {
34653498
Jsonix.Util.Ensure.ensureObject(mapping);
34663499
Jsonix.Model.PropertyInfo.prototype.initialize.apply(this, [ mapping ]);
3467-
var dom = mapping.allowDom || mapping.dom || true;
3468-
var typed = mapping.allowTypedObject || mapping.typed || true;
3469-
var mx = mapping.mixed || mapping.mx || true;
3500+
var dom = Jsonix.Util.Type.defaultValue(mapping.allowDom, mapping.dom, true);
3501+
var typed = Jsonix.Util.Type.defaultValue(mapping.allowTypedObject, mapping.typed, true);
3502+
var mx = Jsonix.Util.Type.defaultValue(mapping.mixed, mapping.mx, true);
34703503
this.allowDom = dom;
34713504
this.allowTypedObject = typed;
34723505
this.mixed = mx;
@@ -5457,6 +5490,17 @@ Jsonix.Schema.XSD.IDREFS = Jsonix.Class(Jsonix.Schema.XSD.List, {
54575490
CLASS_NAME : 'Jsonix.Schema.XSD.IDREFS'
54585491
});
54595492
Jsonix.Schema.XSD.IDREFS.INSTANCE = new Jsonix.Schema.XSD.IDREFS();
5493+
Jsonix.Schema.XSI = {};
5494+
Jsonix.Schema.XSI.NAMESPACE_URI = 'http://www.w3.org/2001/XMLSchema-instance';
5495+
Jsonix.Schema.XSI.PREFIX = 'xsi';
5496+
Jsonix.Schema.XSI.TYPE = 'type';
5497+
Jsonix.Schema.XSI.NIL = 'nil';
5498+
Jsonix.Schema.XSI.qname = function(localPart) {
5499+
Jsonix.Util.Ensure.ensureString(localPart);
5500+
return new Jsonix.XML.QName(Jsonix.Schema.XSI.NAMESPACE_URI, localPart,
5501+
Jsonix.Schema.XSI.PREFIX);
5502+
};
5503+
54605504
Jsonix.Context = Jsonix
54615505
.Class(Jsonix.Mapping.Styled, {
54625506
modules : [],
@@ -5466,6 +5510,7 @@ Jsonix.Context = Jsonix
54665510
options : null,
54675511
substitutionMembersMap : null,
54685512
scopedElementInfosMap : null,
5513+
supportXsiType : true,
54695514
initialize : function(mappings, options) {
54705515
Jsonix.Mapping.Styled.prototype.initialize.apply(this, [options]);
54715516
this.modules = [];
@@ -5478,7 +5523,6 @@ Jsonix.Context = Jsonix
54785523
this.substitutionMembersMap = {};
54795524
this.scopedElementInfosMap = {};
54805525

5481-
54825526
// Initialize options
54835527
if (Jsonix.Util.Type.exists(options)) {
54845528
Jsonix.Util.Ensure.ensureObject(options);
@@ -5487,6 +5531,10 @@ Jsonix.Context = Jsonix
54875531
this.namespacePrefixes =
54885532
Jsonix.Util.Type.cloneObject(options.namespacePrefixes, {});
54895533
}
5534+
if (Jsonix.Util.Type
5535+
.isBoolean(options.supportXsiType)) {
5536+
this.supportXsiType = options.supportXsiType;
5537+
}
54905538
}
54915539

54925540
// Initialize prefix/namespace mapping

dist/Jsonix-min.js

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,16 @@ w("Right: ["+t[q]+"].")
191191
},cloneObject:function(e,f){f=f||{};
192192
for(var d in e){if(e.hasOwnProperty(d)){f[d]=e[d]
193193
}}return f
194-
}};
194+
},defaultValue:function(){var i=arguments;
195+
if(i.length===0){return undefined
196+
}else{var g=i[i.length-1];
197+
var f=typeof g;
198+
for(var j=0;
199+
j<i.length-1;
200+
j++){var h=i[j];
201+
if(typeof h===f){return h
202+
}}return g
203+
}}};
195204
Jsonix.Util.NumberUtils={isInteger:function(b){return Jsonix.Util.Type.isNumber(b)&&((b%1)===0)
196205
}};
197206
Jsonix.Util.StringUtils={trim:(!!String.prototype.trim)?function(b){Jsonix.Util.Ensure.ensureString(b);
@@ -750,14 +759,18 @@ h(that.unmarshalDocument(b))
750759
var e=null;
751760
d.nextTag();
752761
return this.unmarshalElement(this.context,d)
753-
},unmarshalElement:function(o,i,n){if(i.eventType!=1){throw new Error("Parser must be on START_ELEMENT to read next text.")
754-
}var j=null;
755-
var p=i.getName();
756-
var l=this.getElementTypeInfo(p,o,n);
757-
if(Jsonix.Util.Type.exists(l)){var m=l.unmarshal(o,i,n);
758-
var k=this.convertToElementValue({name:p,value:m},o,i,n);
759-
return k
760-
}else{throw new Error("Element ["+p.key+"] is not known in this context.")
762+
},unmarshalElement:function(u,r,n){if(r.eventType!=1){throw new Error("Parser must be on START_ELEMENT to read next text.")
763+
}var l=null;
764+
var m=null;
765+
if(u.supportXsiType){var t=r.getAttributeValueNS(Jsonix.Schema.XSI.NAMESPACE_URI,Jsonix.Schema.XSI.TYPE);
766+
if(Jsonix.Util.StringUtils.isNotBlank(t)){var p=Jsonix.Schema.XSD.QName.INSTANCE.parse(t,u,r,n);
767+
m=u.getTypeInfoByTypeNameKey(p.key)
768+
}}var v=r.getName();
769+
var o=m?m:this.getElementTypeInfo(v,u,n);
770+
if(Jsonix.Util.Type.exists(o)){var q=o.unmarshal(u,r,n);
771+
var s=this.convertToElementValue({name:v,value:q},u,r,n);
772+
return s
773+
}else{throw new Error("Element ["+v.key+"] is not known in this context.")
761774
}},getElementTypeInfo:function(f,e,g){var h=e.getElementInfo(f,g);
762775
if(Jsonix.Util.Type.exists(h)){return h.typeInfo
763776
}else{return undefined
@@ -1209,9 +1222,9 @@ var g=f.wrapperElementName||f.wen||undefined;
12091222
if(Jsonix.Util.Type.isObject(g)){this.wrapperElementName=Jsonix.XML.QName.fromObject(g)
12101223
}else{if(Jsonix.Util.Type.isString(g)){this.wrapperElementName=new Jsonix.XML.QName(this.defaultElementNamespaceURI,g)
12111224
}else{this.wrapperElementName=null
1212-
}}var h=f.allowDom||f.dom||true;
1213-
var j=f.allowTypedObject||f.typed||true;
1214-
var i=f.mixed||f.mx||true;
1225+
}}var h=Jsonix.Util.Type.defaultValue(f.allowDom,f.dom,true);
1226+
var j=Jsonix.Util.Type.defaultValue(f.allowTypedObject,f.typed,true);
1227+
var i=Jsonix.Util.Type.defaultValue(f.mixed,f.mx,true);
12151228
this.allowDom=h;
12161229
this.allowTypedObject=j;
12171230
this.mixed=i
@@ -1328,9 +1341,9 @@ this.buildStructureElementTypeInfos(g,f,e)
13281341
Jsonix.Model.ElementRefsPropertyInfo.Simplified=Jsonix.Class(Jsonix.Model.ElementRefsPropertyInfo,Jsonix.Binding.ElementUnmarshaller.Simplified,{CLASS_NAME:"Jsonix.Model.ElementRefsPropertyInfo.Simplified"});
13291342
Jsonix.Model.AnyElementPropertyInfo=Jsonix.Class(Jsonix.Binding.ElementMarshaller,Jsonix.Binding.ElementUnmarshaller,Jsonix.Model.PropertyInfo,{allowDom:true,allowTypedObject:true,mixed:true,initialize:function(f){Jsonix.Util.Ensure.ensureObject(f);
13301343
Jsonix.Model.PropertyInfo.prototype.initialize.apply(this,[f]);
1331-
var g=f.allowDom||f.dom||true;
1332-
var e=f.allowTypedObject||f.typed||true;
1333-
var h=f.mixed||f.mx||true;
1344+
var g=Jsonix.Util.Type.defaultValue(f.allowDom,f.dom,true);
1345+
var e=Jsonix.Util.Type.defaultValue(f.allowTypedObject,f.typed,true);
1346+
var h=Jsonix.Util.Type.defaultValue(f.mixed,f.mx,true);
13341347
this.allowDom=g;
13351348
this.allowTypedObject=e;
13361349
this.mixed=h
@@ -2147,7 +2160,15 @@ Jsonix.Schema.XSD.IDREF.INSTANCE.LIST=new Jsonix.Schema.XSD.List(Jsonix.Schema.X
21472160
Jsonix.Schema.XSD.IDREFS=Jsonix.Class(Jsonix.Schema.XSD.List,{name:"IDREFS",initialize:function(){Jsonix.Schema.XSD.List.prototype.initialize.apply(this,[Jsonix.Schema.XSD.IDREF.INSTANCE,Jsonix.Schema.XSD.qname("IDREFS")," "])
21482161
},CLASS_NAME:"Jsonix.Schema.XSD.IDREFS"});
21492162
Jsonix.Schema.XSD.IDREFS.INSTANCE=new Jsonix.Schema.XSD.IDREFS();
2150-
Jsonix.Context=Jsonix.Class(Jsonix.Mapping.Styled,{modules:[],typeInfos:null,typeNameKeyToTypeInfo:null,elementInfos:null,options:null,substitutionMembersMap:null,scopedElementInfosMap:null,initialize:function(i,l){Jsonix.Mapping.Styled.prototype.initialize.apply(this,[l]);
2163+
Jsonix.Schema.XSI={};
2164+
Jsonix.Schema.XSI.NAMESPACE_URI="http://www.w3.org/2001/XMLSchema-instance";
2165+
Jsonix.Schema.XSI.PREFIX="xsi";
2166+
Jsonix.Schema.XSI.TYPE="type";
2167+
Jsonix.Schema.XSI.NIL="nil";
2168+
Jsonix.Schema.XSI.qname=function(b){Jsonix.Util.Ensure.ensureString(b);
2169+
return new Jsonix.XML.QName(Jsonix.Schema.XSI.NAMESPACE_URI,b,Jsonix.Schema.XSI.PREFIX)
2170+
};
2171+
Jsonix.Context=Jsonix.Class(Jsonix.Mapping.Styled,{modules:[],typeInfos:null,typeNameKeyToTypeInfo:null,elementInfos:null,options:null,substitutionMembersMap:null,scopedElementInfosMap:null,supportXsiType:true,initialize:function(i,l){Jsonix.Mapping.Styled.prototype.initialize.apply(this,[l]);
21512172
this.modules=[];
21522173
this.elementInfos=[];
21532174
this.typeInfos={};
@@ -2159,6 +2180,7 @@ this.substitutionMembersMap={};
21592180
this.scopedElementInfosMap={};
21602181
if(Jsonix.Util.Type.exists(l)){Jsonix.Util.Ensure.ensureObject(l);
21612182
if(Jsonix.Util.Type.isObject(l.namespacePrefixes)){this.namespacePrefixes=Jsonix.Util.Type.cloneObject(l.namespacePrefixes,{})
2183+
}if(Jsonix.Util.Type.isBoolean(l.supportXsiType)){this.supportXsiType=l.supportXsiType
21622184
}}for(var j in this.namespacePrefixes){if(this.namespacePrefixes.hasOwnProperty(j)){p=this.namespacePrefixes[j];
21632185
this.prefixNamespaces[p]=j
21642186
}}if(Jsonix.Util.Type.exists(i)){Jsonix.Util.Ensure.ensureArray(i);

nodejs/scripts/jsonix.js

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,29 @@ Jsonix.Util.Type = {
503503
}
504504
}
505505
return target;
506+
},
507+
defaultValue : function()
508+
{
509+
var args = arguments;
510+
if (args.length === 0)
511+
{
512+
return undefined;
513+
}
514+
else
515+
{
516+
var defaultValue = args[args.length - 1];
517+
var typeOfDefaultValue = typeof defaultValue;
518+
for (var index = 0; index < args.length - 1; index++)
519+
{
520+
var candidateValue = args[index];
521+
if (typeof candidateValue === typeOfDefaultValue)
522+
{
523+
return candidateValue;
524+
}
525+
}
526+
return defaultValue;
527+
528+
}
506529
}
507530
};
508531
Jsonix.Util.NumberUtils = {
@@ -1904,6 +1927,7 @@ Jsonix.Binding.Unmarshaller = Jsonix.Class(Jsonix.Binding.ElementUnmarshaller, {
19041927
throw new Error("Parser must be on START_ELEMENT to read next text.");
19051928
}
19061929
var result = null;
1930+
// Issue #70 work in progress here
19071931
var xsiTypeInfo = null;
19081932
if (context.supportXsiType) {
19091933
var xsiType = input.getAttributeValueNS(Jsonix.Schema.XSI.NAMESPACE_URI, Jsonix.Schema.XSI.TYPE);
@@ -3154,9 +3178,9 @@ Jsonix.Model.AbstractElementRefsPropertyInfo = Jsonix.Class(Jsonix.Binding.Eleme
31543178
} else {
31553179
this.wrapperElementName = null;
31563180
}
3157-
var dom = mapping.allowDom || mapping.dom || true;
3158-
var typed = mapping.allowTypedObject || mapping.typed || true;
3159-
var mx = mapping.mixed || mapping.mx || true;
3181+
var dom = Jsonix.Util.Type.defaultValue(mapping.allowDom, mapping.dom, true);
3182+
var typed = Jsonix.Util.Type.defaultValue(mapping.allowTypedObject, mapping.typed, true);
3183+
var mx = Jsonix.Util.Type.defaultValue(mapping.mixed, mapping.mx, true);
31603184
this.allowDom = dom;
31613185
this.allowTypedObject = typed;
31623186
this.mixed = mx;
@@ -3473,9 +3497,9 @@ Jsonix.Model.AnyElementPropertyInfo = Jsonix.Class(Jsonix.Binding.ElementMarshal
34733497
initialize : function(mapping) {
34743498
Jsonix.Util.Ensure.ensureObject(mapping);
34753499
Jsonix.Model.PropertyInfo.prototype.initialize.apply(this, [ mapping ]);
3476-
var dom = mapping.allowDom || mapping.dom || true;
3477-
var typed = mapping.allowTypedObject || mapping.typed || true;
3478-
var mx = mapping.mixed || mapping.mx || true;
3500+
var dom = Jsonix.Util.Type.defaultValue(mapping.allowDom, mapping.dom, true);
3501+
var typed = Jsonix.Util.Type.defaultValue(mapping.allowTypedObject, mapping.typed, true);
3502+
var mx = Jsonix.Util.Type.defaultValue(mapping.mixed, mapping.mx, true);
34793503
this.allowDom = dom;
34803504
this.allowTypedObject = typed;
34813505
this.mixed = mx;

nodejs/scripts/tests/util.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ module.exports = {
2222
test.equal(true, Jsonix.Util.Type.isNumberOrNaN(Number.NaN));
2323
test.equal(true, Jsonix.Util.Type.isNaN(Number.NaN));
2424
test.equal(false, Jsonix.Util.Type.isNumber(Number.NaN));
25+
26+
test.equal("undefined", typeof Jsonix.Util.Type.defaultValue());
27+
test.equal(1, Jsonix.Util.Type.defaultValue(1));
28+
test.equal(1, Jsonix.Util.Type.defaultValue(1, undefined, 2));
29+
test.equal(2, Jsonix.Util.Type.defaultValue(undefined, 2, 3));
30+
test.equal(3, Jsonix.Util.Type.defaultValue("1", {t:2}, 3));
31+
test.equal(false, Jsonix.Util.Type.defaultValue(false, undefined, true));
32+
test.equal(false, Jsonix.Util.Type.defaultValue("true", false, true));
33+
test.equal(false, Jsonix.Util.Type.defaultValue("true", null, false));
2534
test.done();
2635
},
2736
"StringUtils" : function(test) {

scripts/src/main/javascript/org/hisrc/jsonix/Jsonix/Model/AbstractElementRefsPropertyInfo.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ Jsonix.Model.AbstractElementRefsPropertyInfo = Jsonix.Class(Jsonix.Binding.Eleme
1414
} else {
1515
this.wrapperElementName = null;
1616
}
17-
var dom = mapping.allowDom || mapping.dom || true;
18-
var typed = mapping.allowTypedObject || mapping.typed || true;
19-
var mx = mapping.mixed || mapping.mx || true;
17+
var dom = Jsonix.Util.Type.defaultValue(mapping.allowDom, mapping.dom, true);
18+
var typed = Jsonix.Util.Type.defaultValue(mapping.allowTypedObject, mapping.typed, true);
19+
var mx = Jsonix.Util.Type.defaultValue(mapping.mixed, mapping.mx, true);
2020
this.allowDom = dom;
2121
this.allowTypedObject = typed;
2222
this.mixed = mx;

scripts/src/main/javascript/org/hisrc/jsonix/Jsonix/Model/AnyElementPropertyInfo.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ Jsonix.Model.AnyElementPropertyInfo = Jsonix.Class(Jsonix.Binding.ElementMarshal
55
initialize : function(mapping) {
66
Jsonix.Util.Ensure.ensureObject(mapping);
77
Jsonix.Model.PropertyInfo.prototype.initialize.apply(this, [ mapping ]);
8-
var dom = mapping.allowDom || mapping.dom || true;
9-
var typed = mapping.allowTypedObject || mapping.typed || true;
10-
var mx = mapping.mixed || mapping.mx || true;
8+
var dom = Jsonix.Util.Type.defaultValue(mapping.allowDom, mapping.dom, true);
9+
var typed = Jsonix.Util.Type.defaultValue(mapping.allowTypedObject, mapping.typed, true);
10+
var mx = Jsonix.Util.Type.defaultValue(mapping.mixed, mapping.mx, true);
1111
this.allowDom = dom;
1212
this.allowTypedObject = typed;
1313
this.mixed = mx;

scripts/src/main/javascript/org/hisrc/jsonix/Jsonix/Util/Type.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,5 +168,28 @@ Jsonix.Util.Type = {
168168
}
169169
}
170170
return target;
171+
},
172+
defaultValue : function()
173+
{
174+
var args = arguments;
175+
if (args.length === 0)
176+
{
177+
return undefined;
178+
}
179+
else
180+
{
181+
var defaultValue = args[args.length - 1];
182+
var typeOfDefaultValue = typeof defaultValue;
183+
for (var index = 0; index < args.length - 1; index++)
184+
{
185+
var candidateValue = args[index];
186+
if (typeof candidateValue === typeOfDefaultValue)
187+
{
188+
return candidateValue;
189+
}
190+
}
191+
return defaultValue;
192+
193+
}
171194
}
172195
};

0 commit comments

Comments
 (0)