我正在使用JSONIX来编码和解码XML文件。到目前为止,它运行良好。我缺少的是获得默认值和限制(如minOccours和maxOccours-Values)的可能性。 JSONIX可以通过某种方式实现吗?

这些属性:

    <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" />

得到:
    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'
    }]
}

谢谢!

最佳答案

您要求的功能现在已在Jsonix 2.3.2Jsonix Schema Compiler 2.3.7中实现。

现在,Jsonix Schema Compiler会在生成的映射和JSON模式中生成requiredminOccursmaxOccurs

这是在映射中的外观:

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

并在JSON模式中:
    "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"
        ]
    }

您可以从Jsonix上下文访问此元数据,如下所示:
    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();

07-24 13:11