JiBX
  1. JiBX
  2. JIBX-286

strange results with int array

    Details

    • Type: Bug Bug
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Not A Bug
    • Affects Version/s: JiBX 1.2
    • Fix Version/s: None
    • Component/s: core
    • Labels:
      None
    • Number of attachments :
      0

      Description

      Tried to serialize a bean with int[] properties. The xml seems to mixup ints from different properties.
      See snippets below.
      ================================

      public static void main(String[] args) throws Exception
      {
      test.TestBean test = new test.TestBean();
      test.setName(Blah");
      test.setStuff(new int[]

      {1,2,3}

      );
      test.setMoreStuff(new int[]

      {4,5,6}

      );

      IBindingFactory bfact =
      BindingDirectory.getFactory(test.TestBean.class);

      IMarshallingContext mctx = bfact.createMarshallingContext();
      mctx.marshalDocument(test, "UTF-8", null,
      new FileOutputStream("c:
      1.xml"));
      }

      ================================
      <?xml version="1.0" encoding="UTF-8"?>
      <testBean><name>Blah</name><int>1</int><int>2</int><int>3</int><int>4</int><int>5</int><int>6</int></testBean>
      ================================
      <binding name="binding" package="test">
      <mapping abstract="true" type-name="testBean" class="test.TestBean">
      <value style="element" name="name" field="name" usage="optional"/>
      <collection field="stuff" usage="optional">
      <value name="int" type="int"/>
      </collection>
      <collection field="moreStuff" usage="optional">
      <value name="int" type="int"/>
      </collection>
      </mapping>
      <mapping class="test.TestBean" name="testBean">
      <structure map-as="testBean"/>
      </mapping>
      </binding>
      ================================
      public class TestBean
      {
      private String name;
      private int[] stuff;
      private int[] moreStuff;

      public TestBean()
      {
      }

      public String getName()

      { return name; }

      public void setName(String name)

      { this.name = name; }

      public int[] getStuff()

      { return stuff; }

      public void setStuff(int[] stuff)

      { this.stuff = stuff; }

      public int[] getMoreStuff()

      { return moreStuff; }

      public void setMoreStuff(int[] moreStuff)

      { this.moreStuff = moreStuff; }

      }
      -

        Activity

        Hide
        Dennis Sosnoski added a comment -

        The problem here is that your XML representation doesn't distinguish between the two sets of elements (from the two arrays). Here's your binding:

        <binding name="binding" package="test">
        <mapping abstract="true" type-name="testBean" class="test.TestBean">
        <value style="element" name="name" field="name" usage="optional"/>
        <collection field="stuff" usage="optional">
        <value name="int" type="int"/>
        </collection>
        <collection field="moreStuff" usage="optional">
        <value name="int" type="int"/>
        </collection>
        </mapping>
        <mapping class="test.TestBean" name="testBean">
        <structure map-as="testBean"/>
        </mapping>
        </binding>

        The two collections both use <int> elements to represent the values, so when marshalling you'll just get a long list of <int> elements representing both arrays, in order. When you unmarshal, since there's no way to distinguish the two sets all the values will go into the first array.

        To distinguish between the two sets of values, either use different element names (<int1> and <int2>, for instance) or add a wrapper element (by using name="stuff" on the first <collection>, or name="moreStuff" on the second <collection>, or both).

        Show
        Dennis Sosnoski added a comment - The problem here is that your XML representation doesn't distinguish between the two sets of elements (from the two arrays). Here's your binding: <binding name="binding" package="test"> <mapping abstract="true" type-name="testBean" class="test.TestBean"> <value style="element" name="name" field="name" usage="optional"/> <collection field="stuff" usage="optional"> <value name="int" type="int"/> </collection> <collection field="moreStuff" usage="optional"> <value name="int" type="int"/> </collection> </mapping> <mapping class="test.TestBean" name="testBean"> <structure map-as="testBean"/> </mapping> </binding> The two collections both use <int> elements to represent the values, so when marshalling you'll just get a long list of <int> elements representing both arrays, in order. When you unmarshal, since there's no way to distinguish the two sets all the values will go into the first array. To distinguish between the two sets of values, either use different element names (<int1> and <int2>, for instance) or add a wrapper element (by using name="stuff" on the first <collection>, or name="moreStuff" on the second <collection>, or both).
        Dennis Sosnoski made changes -
        Field Original Value New Value
        Resolution Not A Bug [ 6 ]
        Assignee Dennis Sosnoski [ dsosnoski ]
        Status Open [ 1 ] Resolved [ 5 ]
        Dennis Sosnoski made changes -
        Status Resolved [ 5 ] Closed [ 6 ]

          People

          • Assignee:
            Dennis Sosnoski
            Reporter:
            Alex
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: