codingKit
codingKit

Reputation: 13

Jsonix - Is it possible to create a collection and declare 2 or more elements in the property declarations?

The expected XML format is as per below:

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

The <a:condition> and the <operator> both needs to be in a collection as both can have as many as required by the XML request.

EDITED Type:elements code as below:

{{
    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 Looks like elements can fulfill my requirements. Unmarshalling works too. Now I only need to figure out how to marshall it from Json format. Any advice will be greatly appreciated

Upvotes: 0

Views: 72

Answers (2)

codingKit
codingKit

Reputation: 13

This is my solution to the problem:

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);

Done! Thank you @lexicore, your guides were a great help!

Upvotes: 0

lexicore
lexicore

Reputation: 43671

Not exactly as you want it.

You can use elements or elementReferences property to declare a collection property which would contain different elements as items. Something like:

{
    type: 'elementRefs',
    name: 'acondition',
    collection: true,
    elementTypeInfos: [{
        elementName: 'condition',
        typeInfo: 'DOXML.Atomicon'
    }, {
        elementName: 'operator',
        typeInfo: 'String'
    }]
}

This property would contain condition or operator ins any quantity or order. But this does not guarantee that you have the same number of them or that it's always condition followed by operator. If this is what you want, consider wrapping condition and operator pair into an element like conditionalOperator.

Upvotes: 0

Related Questions