Jsonix Element Declaration Scope

by Alexey Valikov

HTML

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

JavaScript

var MyModule = {
    name: 'MyModule',
    typeInfos: [{
        type: 'classInfo',
        localName: 'ElementRefType',
        propertyInfos: [{
            type: 'element',
            name: 'a',
            typeInfo: 'String'
        }, {
            type: 'elementRef',
            name: 'b',
            typeInfo: 'String'
        }]
    }],
    elementInfos: [{
        elementName: 'data',
        typeInfo: 'MyModule.ElementRefType'
    }, {
        elementName: 'c',
        substitutionHead: 'b',
        typeInfo: 'String'
    }, {
        elementName: 'd',
        substitutionHead: 'b',
        scope: 'MyModule.ElementRefType',
        typeInfo: 'String'
    }]
};
var context = new Jsonix.Context([MyModule]);
var unmarshaller = context.createUnmarshaller();

var unmarshalledB = unmarshaller.unmarshalString('<data><a>1</a><b>2</b></data>');
console.log(unmarshalledB);

var unmarshalledC = unmarshaller.unmarshalString('<data><a>1</a><c>3</c></data>');
console.log(unmarshalledC);

var unmarshalledRootC = unmarshaller.unmarshalString('<c>4</c>');
console.log(unmarshalledRootC);

var unmarshalledD = unmarshaller.unmarshalString('<data><a>1</a><d>5</d></data>');
console.log(unmarshalledD);

try {
    var unmarshalledD = unmarshaller.unmarshalString('<d>6</d>');
    console.log(unmarshalledD);
} catch (error) {
    console.log('Could not unmarshal <d/> as a root element (as expected).');
    console.log(error);
}