JavaBeanConverter can't serialize null values when the property's default value is non-null #5

Closed
rkreis opened this Issue May 7, 2015 · 4 comments

Comments

Projects
None yet
3 participants
@rkreis
Contributor

rkreis commented May 7, 2015

As far as I can see, XStream has no way of representing null values (although often requested). While this is no problem for fields with a default value of null, or when the initialization is skipped, it makes JavaBeanConverter fail in simple cases like the following:
Test case where a bean can not be serialized and then deserialized correctly (TestBeanConverter.testNullValue fails while all other tests pass)

My current workaround is adding an is-null attribute to properties that are null . It would be nice to accomplish this without breaking XStream compatibility.

@joehni

This comment has been minimized.

Show comment
Hide comment
@joehni

joehni May 7, 2015

Member

What works is this:

    public static class ExplicitlyInitialized {
        private Integer i = new Integer(3);
        private String s = "test";

        public Integer getValue() {
            return this.i;
        }

        public void setValue(Integer value) {
            this.i = value;
        }

        public String getString() {
            return s;
        }

        public void setString(String s) {
            this.s = s;
        }
    }

    public void testExplicitNullValue() {
        XStream xstream = new XStream();
        xstream.registerConverter(
            new JavaBeanConverter(xstream.getMapper()), XStream.PRIORITY_LOW);
        String input = ""
            + "<explicit>\n"
            + "  <value class=\"null\"/>\n"
            + "</explicit>";
        xstream.alias("explicit", ExplicitlyInitialized.class);

        ExplicitlyInitialized explicit = (ExplicitlyInitialized)xstream.fromXML(input);
        assertNull(explicit.getValue());
        assertEquals("test", explicit.getString());
    }

But this only demonstrated that it is already possible to deserialize a null value. As long as the JavaBeanConverter does omit tags for null values when marshalling, it is not of great help. Thanks for heads-up.

Member

joehni commented May 7, 2015

What works is this:

    public static class ExplicitlyInitialized {
        private Integer i = new Integer(3);
        private String s = "test";

        public Integer getValue() {
            return this.i;
        }

        public void setValue(Integer value) {
            this.i = value;
        }

        public String getString() {
            return s;
        }

        public void setString(String s) {
            this.s = s;
        }
    }

    public void testExplicitNullValue() {
        XStream xstream = new XStream();
        xstream.registerConverter(
            new JavaBeanConverter(xstream.getMapper()), XStream.PRIORITY_LOW);
        String input = ""
            + "<explicit>\n"
            + "  <value class=\"null\"/>\n"
            + "</explicit>";
        xstream.alias("explicit", ExplicitlyInitialized.class);

        ExplicitlyInitialized explicit = (ExplicitlyInitialized)xstream.fromXML(input);
        assertNull(explicit.getValue());
        assertEquals("test", explicit.getString());
    }

But this only demonstrated that it is already possible to deserialize a null value. As long as the JavaBeanConverter does omit tags for null values when marshalling, it is not of great help. Thanks for heads-up.

@rkreis

This comment has been minimized.

Show comment
Hide comment
@rkreis

rkreis May 8, 2015

Contributor

That's already very helpful, as it enables me to keep my output compatible to plain XStream.

Should I modify JavaBeanConverter and open a PR?

Contributor

rkreis commented May 8, 2015

That's already very helpful, as it enables me to keep my output compatible to plain XStream.

Should I modify JavaBeanConverter and open a PR?

@joehni

This comment has been minimized.

Show comment
Hide comment
@joehni

joehni May 10, 2015

Member

Yes, please, PR is welcome. We're still busy saving all our projects from Codehaus ...

Member

joehni commented May 10, 2015

Yes, please, PR is welcome. We're still busy saving all our projects from Codehaus ...

@joehni joehni closed this in faa89f7 May 20, 2015

joehni added a commit that referenced this issue May 20, 2015

@joehni joehni added the bug label Jun 18, 2015

@joehni joehni added this to the 1.4.x milestone Jun 18, 2015

@joehni joehni modified the milestones: 1.4.x, 1.4.9 Mar 16, 2016

@shybbq

This comment has been minimized.

Show comment
Hide comment
@shybbq

shybbq Jun 7, 2018

nice thank y

shybbq commented Jun 7, 2018

nice thank y

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment