JiBX
  1. JiBX
  2. JIBX-408

Marshalling and unmarshalling enums uses toString and valueOf by default, which is wrong by definition of enum

    Details

    • Type: Bug Bug
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: JiBX 1.2.2
    • Fix Version/s: JiBX 1.2.3
    • Component/s: core
    • Labels:
      None
    • Number of attachments :
      0

      Description

      JiBX uses toString() method of an enum to marshall the enum and then again it uses valueOf() to unmarshall it by default. The problem is, that while toString()-method of an enum returns name() by default (that is the enum name as defined in the source code), toString() is actually to be used to provide human readable representation for the enum. On the other hand method name() always returns the enum name as defined in the source code. Actually valueOf()-method needs to be given this name, not the name returned by toString() in order to success.

      So the right method pair is not toString() and valueOf() but name() and valueOf(). Using name() and valueOf() it is possible to override the human readable representations (eg. if locale changes) but still the XML-representation stays the same.

      Current workaround is to use enum-value-method="name", but better solution would be fix the problem.

        Activity

        Dennis Sosnoski made changes -
        Field Original Value New Value
        Assignee Dennis Sosnoski [ dsosnoski ]
        Hide
        Dennis Sosnoski added a comment -

        Corrected the code to use name() as the default serialization method for enums, and to add an xmlValue() method when code generating enums where the XML text is different from the Java name (rather than overriding toString()).

        Show
        Dennis Sosnoski added a comment - Corrected the code to use name() as the default serialization method for enums, and to add an xmlValue() method when code generating enums where the XML text is different from the Java name (rather than overriding toString()).
        Dennis Sosnoski made changes -
        Status Open [ 1 ] Resolved [ 5 ]
        Fix Version/s JiBX 1.2.3 [ 16349 ]
        Resolution Fixed [ 1 ]
        Dennis Sosnoski made changes -
        Status Resolved [ 5 ] Closed [ 6 ]

          People

          • Assignee:
            Dennis Sosnoski
            Reporter:
            Markus Sunela
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: