Jsonix Complex Types

by langsamu

HTML

<script src="https://rawgithub.com/highsource/jsonix/master/dist/Jsonix-all.js"></script>

JavaScript

rewriteJsonixClassInfo();
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",
        typeInfo: CustomClasses.ChildClass,
        propertyInfos: [
        new Jsonix.Model.ValuePropertyInfo({
            name: "stringProperty",
            typeInfo: Jsonix.Schema.XSD.String.INSTANCE
        })]
    });

    Context.RootType = new Jsonix.Model.ClassInfo({
        name: "NS.RootClass",
        typeInfo: 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
     ...