Jsonix - 是否可以创建一个集合并在 属性 声明中声明 2 个或更多元素?
Jsonix - Is it possible to create a collection and declare 2 or more elements in the property declarations?
预期的 XML 格式如下:
<c:condition>
<a:condition>
<fieldname>fieldName</fieldname>
<fieldtest>fieldTest</fieldtest>
<fieldvalues>
<fieldvalue>fieldValue</fieldvalue>
</fieldvalues>
</a:condition>
<operator>operator</operator>
<a:condition>
...<some>
</a:condition>
</c:condition>
<a:condition>
和 <operator>
都需要在 collection
中,因为两者都可以有 XML 请求所需的数量。
EDITED Type:elements
代码如下:
{{
var MyModule = {
name: 'MyModule',
typeInfos: [{
type: 'classInfo',
localName: 'ElementsType',
propertyInfos: [{
type: 'elements',
name: 'c:condition',
wrapperElementName: {
localPart: 'condition',
namespaceURI: 'http://us.xomplex',
prefix: 'c'
},
collection: true,
elementTypeInfos: [{
elementName: {
localPart: 'condition',
namespaceURI: 'http://us.xomplexio',
prefix: 'a'
},
typeInfo: 'MyModule.SubAtomic'
}, {
elementName: 'operator',
typeInfo: 'String'
}]
}]
},
{
type: 'classInfo',
localName: 'SubAtomic',
propertyInfos:[{
type: 'element',
name: 'fieldName',
elementName: 'fieldName',
typeInfo: 'String'
},
{
type: 'element',
name: 'fieldTest',
elementName: 'fieldTest',
typeInfo: 'String'
}]
}],
elementInfos: [{
elementName: 'root',
typeInfo: 'MyModule.ElementsType'
}]
};
console.log("creating unmarsaller");
var context = new Jsonix.Context([MyModule]);
var unmarshaller = context.createUnmarshaller();
var unmarshalled = unmarshaller.unmarshalString('<root><c:condition xmlns:c="http://us.xomplex"><a:condition xmlns:a="http://us.xomplexio">one</a:condition><operator>2</operator><a:condition xmlns:a="http://us.xomplexio"><fieldName>unmra</fieldName><fieldTest>Beneed</fieldTest></a:condition><a:condition xmlns:a="http://us.xomplexio">four</a:condition><operator>AND</operator><operator>4</operator></c:condition></root>');
console.log("unmarshalled");
console.log(unmarshalled);
var marshaller = context.createMarshaller();
var marshalled = marshaller.marshalString({
name: {
localPart: 'root'
},
//Marshalling - not working....
value: {
//'c:condition': ['one', 2, 'unmra', 'four', 10,4]
'c:condition': [
['Field','Test'],9
]
}
});
console.log(marshalled);
}}
EDITED 看起来 elements
可以满足我的要求。解组也有效。现在我只需要弄清楚如何从 Json 格式编组它。任何建议将不胜感激
不完全是你想要的。
您可以使用 elements
or elementReferences
属性 来声明一个集合 属性,它将包含不同的元素作为项目。类似于:
{
type: 'elementRefs',
name: 'acondition',
collection: true,
elementTypeInfos: [{
elementName: 'condition',
typeInfo: 'DOXML.Atomicon'
}, {
elementName: 'operator',
typeInfo: 'String'
}]
}
此 属性 将包含 condition
或 operator
任何数量或订单。但这并不能保证它们的数量相同,或者它总是 condition
后跟 operator
。如果这是您想要的,请考虑将 condition
和 operator
对包装到一个元素中,例如 conditionalOperator
.
这是我解决问题的方法:
var MyModule = {
name: 'MyModule',
typeInfos: [{
type: 'classInfo',
localName: 'ElementsType',
propertyInfos: [{
type: 'elements',
name: 'c:condition',
wrapperElementName: {
localPart: 'condition',
namespaceURI: 'http://us.xomplex',
prefix: 'c'
},
collection: true,
elementTypeInfos: [{
elementName: {
localPart: 'condition',
namespaceURI: 'http://us.xomplexio',
prefix: 'a'
},
typeInfo: 'MyModule.SubAtomic'
}, {
elementName: 'operator',
typeInfo: 'String'
}]
}]
},
{
type: 'classInfo',
localName: 'SubAtomic',
propertyInfos:[{
type: 'element',
name: 'fieldName',
elementName: 'fieldName',
typeInfo: 'String'
},
{
type: 'element',
name: 'fieldTest',
elementName: 'fieldTest',
typeInfo: 'String'
},
{
type: 'element',
name: 'fieldValues',
elementName: 'fieldValues',
typeInfo: 'MyModule.SubSubAtoms'
}]
},
{
type: 'classInfo',
localName: 'SubSubAtoms',
propertyInfos:[
{
type: 'element',
name: 'fieldValue',
collection: true,
elementName: 'fieldValue',
typeInfo: 'String'
}
]
}
],
elementInfos: [{
elementName: 'root',
typeInfo: 'MyModule.ElementsType',
},
{
elementName: 'atoms',
typeInfo:'MyModule.SubAtomic'
}
]
};
console.log("creating unmarsaller");
var context = new Jsonix.Context([MyModule]);
var unmarshaller = context.createUnmarshaller();
var unmarshalled = unmarshaller.unmarshalString('<root><c:condition xmlns:c="http://us.xomplex"><a:condition xmlns:a="http://us.xomplexio">one</a:condition><operator>2</operator><a:condition xmlns:a="http://us.xomplexio"><fieldName>unmra</fieldName><fieldTest>Beneed</fieldTest></a:condition><a:condition xmlns:a="http://us.xomplexio">four</a:condition><operator>AND</operator><operator>4</operator></c:condition></root>');
console.log("unmarshalled");
console.log(unmarshalled);
var marshaller = context.createMarshaller();
var marshalled = marshaller.marshalString({
name: {
localPart: 'root'
},
value: {
'c:condition': [
"9",
{ name:
{
localPart: 'atoms'
},
fieldName: "rating",
fieldTest: "equals",
fieldValues: {
fieldValue: ["563"]
} ,
TYPE_NAME: 'MyModule.SubAtomic'
},
"AND",
{ name:
{
localPart: 'atoms'
},
fieldName: "price",
fieldTest: "between",
fieldValues: {
fieldValue: ["150", "300"]
} ,
TYPE_NAME: 'MyModule.SubAtomic'
}] //end of c:condition value
}
});
console.log(marshalled);
完成!谢谢@lexicore,您的指导帮了大忙!
预期的 XML 格式如下:
<c:condition>
<a:condition>
<fieldname>fieldName</fieldname>
<fieldtest>fieldTest</fieldtest>
<fieldvalues>
<fieldvalue>fieldValue</fieldvalue>
</fieldvalues>
</a:condition>
<operator>operator</operator>
<a:condition>
...<some>
</a:condition>
</c:condition>
<a:condition>
和 <operator>
都需要在 collection
中,因为两者都可以有 XML 请求所需的数量。
EDITED Type:elements
代码如下:
{{
var MyModule = {
name: 'MyModule',
typeInfos: [{
type: 'classInfo',
localName: 'ElementsType',
propertyInfos: [{
type: 'elements',
name: 'c:condition',
wrapperElementName: {
localPart: 'condition',
namespaceURI: 'http://us.xomplex',
prefix: 'c'
},
collection: true,
elementTypeInfos: [{
elementName: {
localPart: 'condition',
namespaceURI: 'http://us.xomplexio',
prefix: 'a'
},
typeInfo: 'MyModule.SubAtomic'
}, {
elementName: 'operator',
typeInfo: 'String'
}]
}]
},
{
type: 'classInfo',
localName: 'SubAtomic',
propertyInfos:[{
type: 'element',
name: 'fieldName',
elementName: 'fieldName',
typeInfo: 'String'
},
{
type: 'element',
name: 'fieldTest',
elementName: 'fieldTest',
typeInfo: 'String'
}]
}],
elementInfos: [{
elementName: 'root',
typeInfo: 'MyModule.ElementsType'
}]
};
console.log("creating unmarsaller");
var context = new Jsonix.Context([MyModule]);
var unmarshaller = context.createUnmarshaller();
var unmarshalled = unmarshaller.unmarshalString('<root><c:condition xmlns:c="http://us.xomplex"><a:condition xmlns:a="http://us.xomplexio">one</a:condition><operator>2</operator><a:condition xmlns:a="http://us.xomplexio"><fieldName>unmra</fieldName><fieldTest>Beneed</fieldTest></a:condition><a:condition xmlns:a="http://us.xomplexio">four</a:condition><operator>AND</operator><operator>4</operator></c:condition></root>');
console.log("unmarshalled");
console.log(unmarshalled);
var marshaller = context.createMarshaller();
var marshalled = marshaller.marshalString({
name: {
localPart: 'root'
},
//Marshalling - not working....
value: {
//'c:condition': ['one', 2, 'unmra', 'four', 10,4]
'c:condition': [
['Field','Test'],9
]
}
});
console.log(marshalled);
}}
EDITED 看起来 elements
可以满足我的要求。解组也有效。现在我只需要弄清楚如何从 Json 格式编组它。任何建议将不胜感激
不完全是你想要的。
您可以使用 elements
or elementReferences
属性 来声明一个集合 属性,它将包含不同的元素作为项目。类似于:
{
type: 'elementRefs',
name: 'acondition',
collection: true,
elementTypeInfos: [{
elementName: 'condition',
typeInfo: 'DOXML.Atomicon'
}, {
elementName: 'operator',
typeInfo: 'String'
}]
}
此 属性 将包含 condition
或 operator
任何数量或订单。但这并不能保证它们的数量相同,或者它总是 condition
后跟 operator
。如果这是您想要的,请考虑将 condition
和 operator
对包装到一个元素中,例如 conditionalOperator
.
这是我解决问题的方法:
var MyModule = {
name: 'MyModule',
typeInfos: [{
type: 'classInfo',
localName: 'ElementsType',
propertyInfos: [{
type: 'elements',
name: 'c:condition',
wrapperElementName: {
localPart: 'condition',
namespaceURI: 'http://us.xomplex',
prefix: 'c'
},
collection: true,
elementTypeInfos: [{
elementName: {
localPart: 'condition',
namespaceURI: 'http://us.xomplexio',
prefix: 'a'
},
typeInfo: 'MyModule.SubAtomic'
}, {
elementName: 'operator',
typeInfo: 'String'
}]
}]
},
{
type: 'classInfo',
localName: 'SubAtomic',
propertyInfos:[{
type: 'element',
name: 'fieldName',
elementName: 'fieldName',
typeInfo: 'String'
},
{
type: 'element',
name: 'fieldTest',
elementName: 'fieldTest',
typeInfo: 'String'
},
{
type: 'element',
name: 'fieldValues',
elementName: 'fieldValues',
typeInfo: 'MyModule.SubSubAtoms'
}]
},
{
type: 'classInfo',
localName: 'SubSubAtoms',
propertyInfos:[
{
type: 'element',
name: 'fieldValue',
collection: true,
elementName: 'fieldValue',
typeInfo: 'String'
}
]
}
],
elementInfos: [{
elementName: 'root',
typeInfo: 'MyModule.ElementsType',
},
{
elementName: 'atoms',
typeInfo:'MyModule.SubAtomic'
}
]
};
console.log("creating unmarsaller");
var context = new Jsonix.Context([MyModule]);
var unmarshaller = context.createUnmarshaller();
var unmarshalled = unmarshaller.unmarshalString('<root><c:condition xmlns:c="http://us.xomplex"><a:condition xmlns:a="http://us.xomplexio">one</a:condition><operator>2</operator><a:condition xmlns:a="http://us.xomplexio"><fieldName>unmra</fieldName><fieldTest>Beneed</fieldTest></a:condition><a:condition xmlns:a="http://us.xomplexio">four</a:condition><operator>AND</operator><operator>4</operator></c:condition></root>');
console.log("unmarshalled");
console.log(unmarshalled);
var marshaller = context.createMarshaller();
var marshalled = marshaller.marshalString({
name: {
localPart: 'root'
},
value: {
'c:condition': [
"9",
{ name:
{
localPart: 'atoms'
},
fieldName: "rating",
fieldTest: "equals",
fieldValues: {
fieldValue: ["563"]
} ,
TYPE_NAME: 'MyModule.SubAtomic'
},
"AND",
{ name:
{
localPart: 'atoms'
},
fieldName: "price",
fieldTest: "between",
fieldValues: {
fieldValue: ["150", "300"]
} ,
TYPE_NAME: 'MyModule.SubAtomic'
}] //end of c:condition value
}
});
console.log(marshalled);
完成!谢谢@lexicore,您的指导帮了大忙!