Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > XStream and default field values.

Reply
Thread Tools

XStream and default field values.

 
 
Daniel Pitts
Guest
Posts: n/a
 
      08-16-2010
Hello everyone.

Is there anyway to tell XStream to treat missing elements as empty
elements? Or, even better, let corresponding fields keep their value
from object initialization?

My explanation on why this would be useful follows.

I'm using XStream to deserialize a Java object. The XML format is
actually a published interface (at least internally) which can evolve
slowly over time.

The XStream process is replacing a Spring Binding process, and we have a
lot of framework/library code that makes certain assumptions about the
bean contents/structure. One of the assumptions is that certain
properties are not null, even if they are optional. XStream seems to be
setting the fields (which are currently final) to null, if there isn't a
corresponding value in the XML. This means that all processed XML will
need to have the optional elements present, or it will cause problems.

for example:
// Foo.java
import java.io.Serializable;
public class Foo implements Serializable {
private final Bar bar = new Bar;
private String myString;

public void doSomething() {
bar.doSomething();
}
}

// Bar.java
import java.io.Serializable;
public class Bar implements Serializable {
public void doSomething() {
System.out.println("Working!");
}
}

// Main.java
public class Main {
private static String xml =
"<foo>" +
"<myString>This is the value of myString!</myString>" +
"</foo>";

public static void main(String...args) {
XStream xStream = new XStream();
xStream.aliasType("foo", Foo.class);
Foo foo = (Foo)xStream.fromXML(xml);
foo.doSomething(); // NPE for bar.doSomething();
}
}


--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
 
Reply With Quote
 
 
 
 
Daniel Pitts
Guest
Posts: n/a
 
      08-16-2010
On 8/15/2010 10:43 PM, Daniel Pitts wrote:
> Hello everyone.
>
> Is there anyway to tell XStream to treat missing elements as empty
> elements? Or, even better, let corresponding fields keep their value
> from object initialization?

It looks like I may want to register a JavaBeanConverter, I'll follow up
tomorrow with what I find.

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
 
Reply With Quote
 
 
 
 
Daniel Pitts
Guest
Posts: n/a
 
      08-16-2010
On 8/15/2010 10:51 PM, Daniel Pitts wrote:
> On 8/15/2010 10:43 PM, Daniel Pitts wrote:
>> Hello everyone.
>>
>> Is there anyway to tell XStream to treat missing elements as empty
>> elements? Or, even better, let corresponding fields keep their value
>> from object initialization?

> It looks like I may want to register a JavaBeanConverter, I'll follow up
> tomorrow with what I find.

It looks like XStream is not the correct approach for me at this time.
The JavaBeanConverter assumes every property that is in the XML should
be writable, where as my model assumes final embedded beans with their
own properties. My approach works perfectly for Spring binding, but I'd
have to write a very sophisticated Converter for it to work in XStream.

Someday I may do so, as an exercise, but today my solution is to drop
XStream and convert the XML to a Map with the same conventions that
Spring expects. Actually, I think XStream could be redesigned to be
more flexible and extensible. Maybe when I have some spare time. Yeah,
right, spare time.


--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
XStream vs XMLGregorianCalendar bw Java 8 01-31-2011 01:34 PM
Using XStream to write out/read in XML memory issues k3n85 Java 0 07-15-2009 07:16 PM
Using XStream to write out/read in XML memory issues k3n85 XML 0 07-15-2009 07:15 PM
XStream: streaming XML transformations Alain Frisch XML 0 06-28-2006 04:18 PM
Lightyear XSTREAM VoIP digital phone service mukes@mail.com Computer Support 0 09-19-2004 12:51 AM



Advertisments