JSONIX: Get restrictions and default value for properties

冷暖自知 提交于 2019-12-07 13:27:18

问题


I am using JSONIX for marshalling and unmarshalling XML files. So far it works pretty well. What I am missing is the possibility to get default values and restrictions like minOccours and maxOccours-Values. Is this somehow possible with JSONIX?

These properties:

    <xsd:sequence>
      <xsd:element name="inflowMin" type="framework:flowType" minOccurs="0" maxOccurs="1"/>
      <xsd:element name="inflowMax" type="framework:flowType" minOccurs="0" maxOccurs="1"/>
      <xsd:element name="unitOfFlowControl" type="framework:flowUnit" minOccurs="0" maxOccurs="1"/>
    </xsd:sequence>
    <xsd:attribute name="waterCosts" type="xsd:double" default="0.0"/>
    <xsd:attribute name="controllable" type="xsd:boolean" default="0"/>
    <xsd:attribute name="scalingOfControl" type="xsd:double" default="1.0" />

Get:

    propertyInfos: [{
        type: 'element',
        name: 'inflowMin',
        elementName: 'inflowMin',
        typeInfo: ...
    }, {
        type: 'element',
        name: 'inflowMax',
        elementName: 'inflowMax',
        typeInfo: ...
    }, {
        type: 'element',
        name: 'unitOfFlowControl',
        elementName: 'unitOfFlowControl',
        typeInfo: 'String'
    }, {
        name: 'waterCosts',
        typeInfo: 'Double',
        attributeName: 'waterCosts',
        type: 'attribute'
    }, {
        name: 'controllable',
        typeInfo: 'Boolean',
        attributeName: 'controllable',
        type: 'attribute'
    }, {
        name: 'scalingOfControl',
        typeInfo: 'Double',
        attributeName: 'scalingOfControl',
        type: 'attribute'
    }]
}

Thanks!


回答1:


The feature you requested is now implemented in Jsonix 2.3.2 and Jsonix Schema Compiler 2.3.7.

Jsonix Schema Compiler now produces required, minOccurs and maxOccurs in the generated mappings and the JSON schema.

This is how it looks in the mappings:

  {
    localName: 'Items',
    propertyInfos: [{
        name: 'item',
        minOccurs: 0,
        maxOccurs: 100,
        collection: true,
        elementName: {
          localPart: 'item'
        },
        typeInfo: '.Items.Item'
      }]
  }

And in the JSON Schema:

    "Items":{
        "type":"object",
        "title":"Items",
        "properties":{
            "item":{
                "title":"item",
                "allOf":[
                    {
                        "type":"array",
                        "items":{
                            "$ref":"#/definitions/Items.Item"
                        },
                        "maxItems":100,
                        "minItems":0
                    }
                ],
                "propertyType":"element",
                "elementName":{
                    "localPart":"item",
                    "namespaceURI":""
                }
            }
        },
        "typeType":"classInfo",
        "typeName":{
            "localPart":"Items",
            "namespaceURI":""
        },
        "propertiesOrder":[
            "item"
        ]
    }

You can access this metadata from the Jsonix context as follows:

    var context = new Jsonix.Context([ PO ]);
    var itemsClassInfo = context.getTypeInfoByName("PO.Items");
    var itemPropertyInfo = itemsClassInfo.getPropertyInfoByName("item");
    test.equal(false, itemPropertyInfo.required);
    test.equal(0, itemPropertyInfo.minOccurs);
    test.equal(100, itemPropertyInfo.maxOccurs);
    test.done();



回答2:


Disclaimer: I'm the author.

At the moment not, this information is not generated yet. There was this issue back then, but it was not implemented.

If you're interested in this functionality, please file two issues here (one for the default value and the other one for minOccurs/maxOccurs).

In principle, this information is available from the XML Schema, but in some cases it is not clearly mappable to the generated model. In a few weird cases like repeatable choice or sequence this won't work, but in most cases it will. So it is implementable, please file the issues.

Do you just need these things in the generated mappings? Or some kind of API to access it?



来源:https://stackoverflow.com/questions/29256036/jsonix-get-restrictions-and-default-value-for-properties

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!