I can't figure out why nested object aren't clearly parsed from BeanIo.
The parsing is based on "fixedlength", so positional.
Give these pojos:
@Record(minOccurs=1)
@Data
public class Address {
@Field(length=2)//,at=1)
private String prov;
@Field(length=6)//at=2)
private String city;
}
and
@Record(minOccurs=1)
@Data
public class Employee {
@Field(length=6)//,at=1)
private String firstName;
private List<Address> addresses;
@Field(length=6)//at=2)
private String lastName;
}
I've made this class to attempt both input and output operations to clearify the problem:
public class BeanioTest {
public static void main(String[] args) {
Employee emp = new Employee();
emp.setFirstName("Joe");
emp.setLastName("Black");
List<Address> addresses = new ArrayList<>();
Address address1 = new Address();
address1.setCity("PARIS");
addresses.add(address1);
Address address2 = new Address();
address2.setCity("MILAN");
addresses.add(address2);
emp.setAddresses(addresses);
String marshalled = marshaller( emp);
System.out.println(marshalled);
Employee empUnm = unmarshaller(marshalled);
System.out.println(empUnm.toString());
}
public static Employee unmarshaller(String emp) {
StreamFactory factory = StreamFactory.newInstance();
StreamBuilder builder = new StreamBuilder("s1")
.format("fixedlength")
.addRecord(Employee.class)
.addRecord(Address.class);
factory.define(builder);
Unmarshaller unmarshaller = factory.createUnmarshaller("s1");
Employee unmarshalled = (Employee) unmarshaller.unmarshal(emp);
return unmarshalled;
}
public static String marshaller(Employee emp) {
StreamFactory factory = StreamFactory.newInstance();
StreamBuilder builder = new StreamBuilder("Tm")
.format("fixedlength")
.addRecord(Employee.class)
.addRecord(Address.class);
factory.define(builder);
Marshaller marshaller = factory.createMarshaller("Tm");
String marshalled = marshaller.marshal(emp).toString();
return marshalled;
}
}
Bu the input and output won't take care of nested colletion, console output looks like this:
Joe Black
Employee(firstName=Joe, addresses=null, lastName=Black)
I've tested various attempt but can't figure out. Pls point me in the right direction.