3

我正在使用 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'
    }]
}

谢谢!

4

2 回答 2

1

免责声明:我是作者。

目前没有,尚未生成此信息。当时有这个问题,但没有实施。

如果您对此功能感兴趣,请在此处提交两个问题(一个用于默认值,另一个用于minOccurs/ maxOccurs)。

原则上,此信息可从 XML 模式获得,但在某些情况下,它并不能清楚地映射到生成的模型。在一些奇怪的情况下,比如可重复的选择或顺序,这不起作用,但在大多数情况下它会起作用。所以它是可以实施的,请提出问题。

您只需要在生成的映射中使用这些东西吗?或者某种API来访问它?

于 2015-03-25T21:34:58.333 回答
1

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

Jsonix Schema Compiler 现在在生成的映射和 JSON 模式中生成required,minOccurs和。maxOccurs

这是它在映射中的外观:

  {
    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();
于 2015-09-12T09:15:21.023 回答