Skip to content

Commit 0c0c5d8

Browse files
committed
Issue #70.
1 parent 0c0cc23 commit 0c0c5d8

16 files changed

+266
-245
lines changed

nodejs/scripts/jsonix.js

+128-120
Large diffs are not rendered by default.

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@
4747
'Jsonix/Mapping/Style.js',
4848
'Jsonix/Mapping/Styled.js',
4949
'Jsonix/Binding.js',
50-
'Jsonix/Binding/ElementMarshaller.js',
51-
'Jsonix/Binding/ElementUnmarshaller.js',
50+
'Jsonix/Binding/Marshalls.js',
51+
'Jsonix/Binding/Unmarshalls.js',
5252
'Jsonix/Context/Marshaller.js',
5353
'Jsonix/Context/Unmarshaller.js',
5454
'Jsonix/Model/TypeInfo.js',

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Jsonix.Binding.Marshaller = Jsonix.Class(Jsonix.Binding.ElementMarshaller, {
1+
Jsonix.Binding.Marshaller = Jsonix.Class(Jsonix.Binding.Marshalls.Element, Jsonix.Binding.Marshalls.Element.AsElementRef, {
22
context : null,
33
initialize : function(context) {
44
Jsonix.Util.Ensure.ensureObject(context);
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
Jsonix.Binding.ElementMarshaller = Jsonix.Class({
1+
Jsonix.Binding.Marshalls = {
2+
};
3+
4+
Jsonix.Binding.Marshalls.Element = Jsonix.Class({
25
marshalElement : function(value, context, output, scope) {
3-
var elementValue = this.getOutputElementValue(value, context, output, scope);
6+
var elementValue = this.convertToTypedNamedValue(value, context, output, scope);
47
var declaredTypeInfo = elementValue.typeInfo;
58
var typeInfo = declaredTypeInfo;
6-
if (Jsonix.Util.Type.exists(declaredTypeInfo))
7-
{
9+
if (Jsonix.Util.Type.exists(declaredTypeInfo)) {
810
output.writeStartElement(elementValue.name);
911
if (Jsonix.Util.Type.exists(elementValue.value)) {
1012
if (context.supportXsiType) {
1113
var actualTypeInfo = context.getTypeInfoByValue(elementValue.value);
12-
if (actualTypeInfo && actualTypeInfo.typeName && declaredTypeInfo !== actualTypeInfo)
13-
{
14+
if (actualTypeInfo && actualTypeInfo.typeName && declaredTypeInfo !== actualTypeInfo) {
1415
typeInfo = actualTypeInfo;
1516
var xsiTypeName = actualTypeInfo.typeName;
1617
var xsiType = Jsonix.Schema.XSD.QName.INSTANCE.print(xsiTypeName, context, output, scope);
@@ -20,22 +21,30 @@ Jsonix.Binding.ElementMarshaller = Jsonix.Class({
2021
typeInfo.marshal(elementValue.value, context, output, scope);
2122
}
2223
output.writeEndElement();
23-
}
24-
else
25-
{
24+
} else {
2625
throw new Error("Element [" + elementValue.name.key + "] is not known in this context.");
2726
}
2827
},
29-
getOutputElementValue : function (value, context, output, scope) {
28+
getTypeInfoByElementName : function(name, context, scope) {
29+
var elementInfo = context.getElementInfo(name, scope);
30+
if (Jsonix.Util.Type.exists(elementInfo)) {
31+
return elementInfo.typeInfo;
32+
} else {
33+
return undefined;
34+
}
35+
}
36+
});
37+
Jsonix.Binding.Marshalls.Element.AsElementRef = Jsonix.Class({
38+
convertToTypedNamedValue : function(value, context, output, scope) {
3039
Jsonix.Util.Ensure.ensureObject(value);
31-
var elementValue = this.convertFromElementValue(value, context, output, scope);
40+
var elementValue = this.convertToNamedValue(value, context, output, scope);
3241
return {
3342
name : elementValue.name,
3443
value : elementValue.value,
3544
typeInfo : this.getTypeInfoByElementName(elementValue.name, context, scope)
3645
};
3746
},
38-
convertFromElementValue : function(elementValue, context, output, scope) {
47+
convertToNamedValue : function(elementValue, context, output, scope) {
3948
var name;
4049
var value;
4150
if (Jsonix.Util.Type.exists(elementValue.name) && !Jsonix.Util.Type.isUndefined(elementValue.value)) {
@@ -58,13 +67,5 @@ Jsonix.Binding.ElementMarshaller = Jsonix.Class({
5867
}
5968
}
6069
throw new Error("Invalid element value [" + elementValue + "]. Element values must either have {name:'myElementName', value: elementValue} or {myElementName:elementValue} structure.");
61-
},
62-
getTypeInfoByElementName : function(name, context, scope) {
63-
var elementInfo = context.getElementInfo(name, scope);
64-
if (Jsonix.Util.Type.exists(elementInfo)) {
65-
return elementInfo.typeInfo;
66-
} else {
67-
return undefined;
68-
}
6970
}
70-
});
71+
});

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

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Jsonix.Binding.Unmarshaller = Jsonix.Class(Jsonix.Binding.ElementUnmarshaller, {
1+
Jsonix.Binding.Unmarshaller = Jsonix.Class(Jsonix.Binding.Unmarshalls.Element, Jsonix.Binding.Unmarshalls.Element.AsElementRef, {
22
context : null,
33
allowTypedObject : true,
44
allowDom : false,
@@ -54,11 +54,8 @@ Jsonix.Binding.Unmarshaller = Jsonix.Class(Jsonix.Binding.ElementUnmarshaller, {
5454
return result;
5555

5656
},
57-
convertToElementValue : function(elementValue, context, input, scope) {
58-
return elementValue;
59-
},
6057
CLASS_NAME : 'Jsonix.Binding.Unmarshaller'
6158
});
62-
Jsonix.Binding.Unmarshaller.Simplified = Jsonix.Class(Jsonix.Binding.Unmarshaller, Jsonix.Binding.ElementUnmarshaller.Simplified, {
59+
Jsonix.Binding.Unmarshaller.Simplified = Jsonix.Class(Jsonix.Binding.Unmarshaller, Jsonix.Binding.Unmarshalls.Element.AsSimplifiedElementRef, {
6360
CLASS_NAME : 'Jsonix.Binding.Unmarshaller.Simplified'
6461
});

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

+28-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
Jsonix.Binding.ElementUnmarshaller = Jsonix.Class({
2-
allowTypedObject : true,
3-
allowDom : false,
1+
Jsonix.Binding.Unmarshalls = {
2+
};
3+
4+
Jsonix.Binding.Unmarshalls.WrapperElement = Jsonix.Class({
45
mixed : false,
56
unmarshalWrapperElement : function(context, input, scope, callback) {
67
var et = input.next();
@@ -18,7 +19,12 @@ Jsonix.Binding.ElementUnmarshaller = Jsonix.Class({
1819
}
1920
et = input.next();
2021
}
21-
},
22+
}
23+
});
24+
25+
Jsonix.Binding.Unmarshalls.Element = Jsonix.Class({
26+
allowTypedObject : true,
27+
allowDom : false,
2228
unmarshalElement : function(context, input, scope, callback) {
2329
if (input.eventType != 1) {
2430
throw new Error("Parser must be on START_ELEMENT to read next element.");
@@ -28,10 +34,12 @@ Jsonix.Binding.ElementUnmarshaller = Jsonix.Class({
2834
var elementValue;
2935
if (this.allowTypedObject && Jsonix.Util.Type.exists(typeInfo)) {
3036
var value = typeInfo.unmarshal(context, input, scope);
31-
elementValue = this.convertToElementValue({
37+
var typedNamedValue = {
3238
name : name,
33-
value : value
34-
}, context, input, scope);
39+
value : value,
40+
typeInfo : typeInfo
41+
};
42+
elementValue = this.convertFromTypedNamedValue(typedNamedValue, context, input, scope);
3543
} else if (this.allowDom) {
3644
elementValue = input.getElement();
3745
} else {
@@ -63,11 +71,20 @@ Jsonix.Binding.ElementUnmarshaller = Jsonix.Class({
6371
}
6472
});
6573

66-
Jsonix.Binding.ElementUnmarshaller.Simplified = Jsonix.Class({
67-
convertToElementValue : function(elementValue, context, input, scope) {
68-
var propertyName = elementValue.name.toCanonicalString(context);
74+
Jsonix.Binding.Unmarshalls.Element.AsElementRef = Jsonix.Class({
75+
convertFromTypedNamedValue : function(typedNamedValue, context, input, scope) {
76+
return {
77+
name : typedNamedValue.name,
78+
value : typedNamedValue.value
79+
};
80+
}
81+
});
82+
83+
Jsonix.Binding.Unmarshalls.Element.AsSimplifiedElementRef = Jsonix.Class({
84+
convertFromTypedNamedValue : function(typedNamedValue, context, input, scope) {
85+
var propertyName = typedNamedValue.name.toCanonicalString(context);
6986
var value = {};
70-
value[propertyName] = elementValue.value;
87+
value[propertyName] = typedNamedValue.value;
7188
return value;
7289
}
7390
});

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

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Jsonix.Model.AbstractElementRefsPropertyInfo = Jsonix.Class(Jsonix.Binding.ElementMarshaller, Jsonix.Binding.ElementUnmarshaller, Jsonix.Model.PropertyInfo, {
1+
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, {
22
wrapperElementName : null,
33
allowDom : true,
44
allowTypedObject : true,
@@ -104,9 +104,6 @@ Jsonix.Model.AbstractElementRefsPropertyInfo = Jsonix.Class(Jsonix.Binding.Eleme
104104
}
105105

106106
},
107-
convertToElementValue : function(elementValue, context, input, scope) {
108-
return elementValue;
109-
},
110107
getTypeInfoByElementName : function(elementName, context, scope) {
111108
var propertyElementTypeInfo = this.getPropertyElementTypeInfo(elementName, context);
112109
if (Jsonix.Util.Type.exists(propertyElementTypeInfo)) {
@@ -148,7 +145,7 @@ Jsonix.Model.AbstractElementRefsPropertyInfo = Jsonix.Class(Jsonix.Binding.Eleme
148145
// {
149146
// structure.elements[key] = this;
150147
// }
151-
148+
152149
if ((this.allowDom || this.allowTypedObject)) {
153150
structure.any = this;
154151
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Jsonix.Model.AbstractElementsPropertyInfo = Jsonix.Class(Jsonix.Binding.ElementUnmarshaller, Jsonix.Model.PropertyInfo, {
1+
Jsonix.Model.AbstractElementsPropertyInfo = Jsonix.Class(Jsonix.Binding.Unmarshalls.Element, Jsonix.Binding.Unmarshalls.WrapperElement, Jsonix.Model.PropertyInfo, {
22
wrapperElementName : null,
33
allowDom : false,
44
allowTypedObject : true,
@@ -69,7 +69,7 @@ Jsonix.Model.AbstractElementsPropertyInfo = Jsonix.Class(Jsonix.Binding.ElementU
6969
output.writeEndElement();
7070
}
7171
},
72-
convertToElementValue : function(elementValue, context, input, scope) {
72+
convertFromTypedNamedValue : function(elementValue, context, input, scope) {
7373
return elementValue.value;
7474
},
7575
buildStructure : function(context, structure) {

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

+8-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Jsonix.Model.AnyElementPropertyInfo = Jsonix.Class(Jsonix.Binding.ElementMarshaller, Jsonix.Binding.ElementUnmarshaller, Jsonix.Model.PropertyInfo, {
1+
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, {
22
allowDom : true,
33
allowTypedObject : true,
44
mixed : true,
@@ -31,7 +31,7 @@ Jsonix.Model.AnyElementPropertyInfo = Jsonix.Class(Jsonix.Binding.ElementMarshal
3131
}
3232
}
3333
};
34-
34+
3535
var et = input.eventType;
3636
if (et === Jsonix.XML.Input.START_ELEMENT) {
3737
this.unmarshalElement(context, input, scope, callback);
@@ -41,12 +41,12 @@ Jsonix.Model.AnyElementPropertyInfo = Jsonix.Class(Jsonix.Binding.ElementMarshal
4141
// Whitespace
4242
// return null;
4343
} else if (et === Jsonix.XML.Input.COMMENT || et === Jsonix.XML.Input.PROCESSING_INSTRUCTION) {
44-
//return null;
44+
// return null;
4545
} else {
4646
// TODO better exception
4747
throw new Error("Illegal state: unexpected event type [" + et + "].");
4848
}
49-
49+
5050
return result;
5151
},
5252
marshal : function(value, context, output, scope) {
@@ -57,7 +57,7 @@ Jsonix.Model.AnyElementPropertyInfo = Jsonix.Class(Jsonix.Binding.ElementMarshal
5757
this.marshalItem(value, context, output, scope);
5858
} else {
5959
Jsonix.Util.Ensure.ensureArray(value);
60-
for ( var index = 0; index < value.length; index++) {
60+
for (var index = 0; index < value.length; index++) {
6161
this.marshalItem(value[index], context, output, scope);
6262
}
6363
}
@@ -71,16 +71,12 @@ Jsonix.Model.AnyElementPropertyInfo = Jsonix.Class(Jsonix.Binding.ElementMarshal
7171
output.writeNode(value);
7272

7373
} else {
74-
if (this.allowTypedObject)
75-
{
74+
if (this.allowTypedObject) {
7675
this.marshalElement(value, context, output, scope);
7776
}
7877
}
7978
},
80-
convertToElementValue : function(elementValue, context, input, scope) {
81-
return elementValue;
82-
},
83-
doBuild : function(context, module) {
79+
doBuild : function(context, module) {
8480
// Nothing to do
8581
},
8682
buildStructure : function(context, structure) {
@@ -115,6 +111,6 @@ Jsonix.Model.AnyElementPropertyInfo = Jsonix.Class(Jsonix.Binding.ElementMarshal
115111
},
116112
CLASS_NAME : 'Jsonix.Model.AnyElementPropertyInfo'
117113
});
118-
Jsonix.Model.AnyElementPropertyInfo.Simplified = Jsonix.Class(Jsonix.Model.AnyElementPropertyInfo, Jsonix.Binding.ElementUnmarshaller.Simplified, {
114+
Jsonix.Model.AnyElementPropertyInfo.Simplified = Jsonix.Class(Jsonix.Model.AnyElementPropertyInfo, Jsonix.Binding.Unmarshalls.Element.AsSimplifiedElementRef, {
119115
CLASS_NAME : 'Jsonix.Model.AnyElementPropertyInfo.Simplified'
120116
});

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

+10-10
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ Jsonix.Model.ElementMapPropertyInfo = Jsonix.Class(Jsonix.Model.AbstractElements
77
Jsonix.Util.Ensure.ensureObject(mapping);
88
Jsonix.Model.AbstractElementsPropertyInfo.prototype.initialize.apply(this, [ mapping ]);
99
// TODO Ensure correct argument
10-
var k = mapping.key||mapping.k||undefined;
10+
var k = mapping.key || mapping.k || undefined;
1111
Jsonix.Util.Ensure.ensureObject(k);
12-
var v = mapping.value||mapping.v||undefined;
12+
var v = mapping.value || mapping.v || undefined;
1313
Jsonix.Util.Ensure.ensureObject(v);
1414
// TODO Ensure correct argument
15-
var en = mapping.elementName||mapping.en||undefined;
15+
var en = mapping.elementName || mapping.en || undefined;
1616
if (Jsonix.Util.Type.isObject(en)) {
1717
this.elementName = Jsonix.XML.QName.fromObject(en);
1818
} else if (Jsonix.Util.Type.isString(en)) {
@@ -21,7 +21,7 @@ Jsonix.Model.ElementMapPropertyInfo = Jsonix.Class(Jsonix.Model.AbstractElements
2121
this.elementName = new Jsonix.XML.QName(this.defaultElementNamespaceURI, this.name);
2222
}
2323
this.entryTypeInfo = new Jsonix.Model.ClassInfo({
24-
name: 'Map<' + k.name + ',' + v.name + '>',
24+
name : 'Map<' + k.name + ',' + v.name + '>',
2525
propertyInfos : [ k, v ]
2626
});
2727

@@ -64,10 +64,10 @@ Jsonix.Model.ElementMapPropertyInfo = Jsonix.Class(Jsonix.Model.AbstractElements
6464
}
6565
return result;
6666
},
67-
getTypeInfoByInputElement : function (context, input, scope) {
67+
getTypeInfoByInputElement : function(context, input, scope) {
6868
return this.entryTypeInfo;
6969
},
70-
convertToElementValue : function(elementValue, context, input, scope) {
70+
convertFromTypedNamedValue : function(elementValue, context, input, scope) {
7171
var entry = elementValue.value;
7272
var result = {};
7373
if (Jsonix.Util.Type.isString(entry[this.key.name])) {
@@ -106,7 +106,7 @@ Jsonix.Model.ElementMapPropertyInfo = Jsonix.Class(Jsonix.Model.AbstractElements
106106
output.writeEndElement();
107107

108108
} else {
109-
for ( var index = 0; index < attributeValue.length; index++) {
109+
for (var index = 0; index < attributeValue.length; index++) {
110110
var collectionEntry = {};
111111
collectionEntry[this.key.name] = attributeName;
112112
collectionEntry[this.value.name] = attributeValue[index];
@@ -119,10 +119,10 @@ Jsonix.Model.ElementMapPropertyInfo = Jsonix.Class(Jsonix.Model.AbstractElements
119119
}
120120
}
121121
},
122-
doBuild: function(context, module) {
122+
doBuild : function(context, module) {
123123
this.entryTypeInfo.build(context, module);
124124
// TODO get property by name
125-
this.key = this.entryTypeInfo.properties[0];
125+
this.key = this.entryTypeInfo.properties[0];
126126
this.value = this.entryTypeInfo.properties[1];
127127
},
128128
buildStructureElements : function(context, structure) {
@@ -143,7 +143,7 @@ Jsonix.Model.ElementMapPropertyInfo = Jsonix.Class(Jsonix.Model.AbstractElements
143143
map[attributeName] = [];
144144
}
145145

146-
for ( var index = 0; index < attributeValue.length; index++) {
146+
for (var index = 0; index < attributeValue.length; index++) {
147147
map[attributeName].push(attributeValue[index]);
148148
}
149149
} else {

0 commit comments

Comments
 (0)