1. XStream
  2. XSTR-592

omitField only ignores field on deserialization if field non-existent


    • Type: Improvement Improvement
    • Status: Closed Closed
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: 1.3
    • Fix Version/s: 1.4
    • Component/s: Core
    • Labels:
    • JDK version and platform:
      jdk 1.6.0_16


      omitField does work during deserialization. However, it was designed to be a way to ignore those elements in the XML that correspond to non-existent fields (so an exception is not thrown). If you attempt to omit a field, but that field does still exist in the object being deserialized, the omitField call is ignored and the field is populated.

      This isn't necessarily a bug, but a small lack of capability and at the very least unintuitive. Would be nice if omitField always omitted the fields it has been called with, regardless of whether or not they exist in the class, or at least if there was some way in the API to specify that we want this additional omission behavior.

      Quote from Jorg:

      "I had a closer look and it turns out, that the omitField declaration only helps to omit XML tags that have no equivalent in the deserialized object (otherwise XStream would throw an exception). If the field exists, the omitField declaration is not even checked. You may open a JIRA issue for this.

      The only workaround I can currently think of is a custom converter based on the ReflectionConverter that uses a filter for the ReflectionProvider..."

      Example Program:

      public class XStreamOmitTest
      public static void main(String[] args)

      { XStream xstream = new XStream(); A a = new A(); a.x = "field 1"; a.y = "field 2"; String xml = xstream.toXML(a); System.out.println(xml); xstream.omitField(A.class, "y"); A a2 = (A) xstream.fromXML(xml); System.out.println(a2.x); // prints "field 1" System.out.println(a2.y); // prints "field 2" (instead of "null") // The field 'y' should be uninitialized if it's // being skipped on deserialization, correct? }

      public static class A

      { public String x; public String y; }


        Issue Links


          • Assignee:
            Jörg Schaible
            Derek Trumbo
          • Votes:
            1 Vote for this issue
            1 Start watching this issue


            • Created: