Unmarshal blank strings to null with BeanIO

305 views
Skip to first unread message

Virginie Desamoury

unread,
Jan 4, 2016, 8:48:02 AM1/4/16
to beanio-users

Hi there,


BeanIO reference guide states that for a fixed-length stream:

if required is set to false, spaces are unmarshalled to a null field value regardless of the padding character.


So if I understand this sentence correctly, it means that the test below should pass for this pojo:

@Record
public class Pojo {

    @Field(length = 5, required = false)
    String field;

    // constructor, getters, setters
}


The test:

@Test
public void test(){

    StreamFactory factory = StreamFactory.newInstance();
    factory.define(new StreamBuilder("pojo")
    .format("fixedlength")
    .addRecord(Pojo.class));

    Unmarshaller unmarshaller = factory.createUnmarshaller("pojo");

    Pojo pojo = (Pojo) unmarshaller.unmarshal("     "); // 5 spaces
    assertNull(pojo.field);

}

But it fails, the 5 spaces are unmarshalled as an empty string. What am I missing? How can I unmarshal spaces to a null String?

(question originally asked on stackoverflow).

Vitalii Tymchyshyn

unread,
Jan 4, 2016, 2:00:33 PM1/4/16
to beanio-users

Well, you can always try trim="true" lazy="true".


Пн, 4 січ. 2016 08:48 Virginie Desamoury <vdesa...@gmail.com> пише:
--
You received this message because you are subscribed to the Google Groups "beanio-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to beanio+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages