Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

@rkreis
Copy link
Contributor

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.

Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

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.

Copy link

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
Projects
None yet
3 participants
You can’t perform that action at this time.