Jsonix custom unmarshalling rewrite
HTML
<script src="http://rawgithub.com/highsource/jsonix/master/dist/Jsonix-all.js"></script>
<script src="http://rawgit.com/langsamu/jsonix/rewrite/scripts/src/main/javascript/org/hisrc/jsonix/Jsonix/Model/ClassInfo.js"></script>
JavaScript
defineCustomClasses();
var mappingContext = getMappingContext();
unmarshal(mappingContext);
function unmarshal(mappingContext) {
var context = new Jsonix.Context([mappingContext]);
var unmarshaller = context.createUnmarshaller();
var result = unmarshaller.unmarshalString("<root textAttribute='text1' numberAttribute='1' attributeToIgnore1='ignored1' attributeToIgnore2='ignored2'><children><child>text2</child><child>text3</child></children></root>");
console.log(result.value);
}
function defineCustomClasses() {
window.CustomClasses = {
RootClass: function() {
this.stringProperty = "";
this.integerProperty = 0;
this.collectionProperty = [];
},
ChildClass: function() {
this.stringProperty = "";
}
};
}
function getMappingContext() {
var Context = {};
Context.ChildType = new Jsonix.Model.ClassInfo({
name: "ChildType",
instanceFactory: CustomClasses.ChildClass,
propertyInfos: [
new Jsonix.Model.ValuePropertyInfo({
name: "stringProperty",
typeInfo: Jsonix.Schema.XSD.String.INSTANCE
})
]
});
Context.RootType = new Jsonix.Model.ClassInfo({
name: "NS.RootClass",
instanceFactory: CustomClasses.RootClass,
propertyInfos: [
new Jsonix.Model.AttributePropertyInfo({
name: "stringProperty",
attributeName: "textAttribute",
typeInfo: Jsonix.Schema.XSD.String.INSTANCE
}),
new Jsonix.Model.AttributePropertyInfo({
name: "integerProperty",
attributeName: "numberAttribute",
typeInfo: Jsonix.Schema.XSD.Number.INSTANCE
}),
new Jsonix.Model.ElementPropertyInfo({
name: "collectionProperty",
wrapperElementName: "children",
elementName: "child",
collection: true,
typeInfo: Context.ChildType
})
]
});
Context.elementInfos = [{
elementName: "root",
typeInfo: Context.RootType,
}];
return Context;
}