Skip to content
Alexey Valikov edited this page Apr 10, 2015 · 5 revisions

xsi:type support

Jsonix supports the xsi:type attribute.

Unmarshalling

When unmarshalling, Jsonix checks, if an XML element has xsi:type element declared. If it does, then the value of this attribute will be parsed as a qualified name. Thenn Jsonix will try to find a type by this name in the current context an use this type for unmarshalling.

Marshalling

When marshalling, Jsonix checks, if the value has the same type as declared in the property or element declaration. If these types differ, Jsonix will add an xsi:type attribute specifying the actual type of the value.

Example

Mappings:

var Mappings = {
    n: 'Mappings',
    tis: [{
        ln: 'Expression'
    }, {
        ln: 'Literal',
        bti: '.Expression',
        ps: [{
            n: 'value',
            t: 'v'
        }]
    }, {
        ln: 'And',
        bti: '.Expression',
        ps: [{
            n: 'expressions',
            en: 'Expression',
            t: 'e',
            ti: '.Expression',
            col: true
        }]
    }],
    eis: [{
        en: 'Literal',
        ti: '.Literal'
    }, {
        en: 'Expression',
        ti: '.Expression'
    }, {
        en: 'And',
        ti: '.And'
    }]
};

XML:

<Expression xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:type="And">
	<Expression xsi:type="Literal">a</Expression>
	<Expression xsi:type="Literal">b</Expression>
</Expression>

JSON:

{
  "name": {
    "localPart": "Expression",
  },
  "value": {
    "TYPE_NAME": "Mappings.And",
    "expressions": [
      {
        "TYPE_NAME": "Mappings.Literal",
        "value": "a"
      },
      {
        "TYPE_NAME": "Mappings.Literal",
        "value": "b"
      }
    ]
  }
}

Fiddle.

Turning xsi:type support off

xsi:type support is turned on by default. You can turn xsi:type support off using the supportXsiType : false option:

var context = new Jsonix.Context(mappings, { supportXsiType : false });

Turning xsi:type may give some performance gain as Jsonix does not have to check the xsi:type attribute on every XML element it processes.

Clone this wiki locally