highsource/jsonix-schema-compiler

How to specify jsonix type in binding

Closed this issue · 2 comments

Hi there! I've been attempting to use jaxb to override the jsonix types in a particular set of XSDs. In particular, I'm trying to work around this issue by customizing particular xs:long types to marshal into [Jsonix.Schema.XSD.String types(https://github.com/highsource/jsonix/wiki/Types).

I've tried several different strategies, trying both baseType and javaType, but couldn't get either to take effect. Is it possible to do something like this?

XSD

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="testxsd" xmlns:xp="testxsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">

    <xs:simpleType name="longIdType">
        <xs:restriction base="xs:long">
            <xs:totalDigits value="19" />
        </xs:restriction>
    </xs:simpleType>

    <xs:element name="exampleElement" type="xp:longIdType"></xs:element>
</xs:schema>

Bindings

<jaxb:bindings version="2.1"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" 
    xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" 
    jaxb:extensionBindingPrefixes="xjc">
    
    <jaxb:bindings node="/xs:schema" schemaLocation="schema.xsd">
        <jaxb:bindings node="xs:simpleType[@name='longIdType']">
            <jaxb:baseType name="String" />
        </jaxb:bindings>
    </jaxb:bindings>

</jaxb:bindings>

I am trying to coerce this:

{
    name: 'testxsd',
    defaultElementNamespaceURI: 'testxsd',
    typeInfos: [],
    elementInfos: [{
        elementName: 'exampleElement',
        typeInfo: 'Long'
      }]
  }

to this:

{
    name: 'testxsd',
    defaultElementNamespaceURI: 'testxsd',
    typeInfos: [],
    elementInfos: [{
        elementName: 'exampleElement',
        typeInfo: 'String'
      }]
  }

Thanks in advance!

This is not possible at the moment. Schema compiler only uses XML Schema types to determine type in the mapping, not Java type (which you customized with jaxb:baseType). Java types are not good for types in mappings because you often have many XML Schema types mapped onto the same Java type.

The workaround at the moment would be to fix the mappings prior to creating the Jsonix context. Mappings are no more than a JavaScript object so you could set mapping.elementInfos[0].typeInfo= 'String'. Not a long-term solution but will get things work first.

The real solution will be probably like:

  • If the property is annotated with jaxb:javaType, use the specified Java type to choose the type of the property. Also consider adapter cases (you can provide an adapter or marshal/unmarshal methods for the type).
  • Allow customizing property type.

Thanks so much for the explanation here. I can definitely see why this isn't supported for a general purpose mapping. I'm going to close this issue since this definitely answers the question, but out of curiosity, are there any custom fields (annotations, etc) that persist in the mapping from the xsd/bindings?

Thanks again!