javacsvbean-io

BeanIO reading field from CSV which represents a collection


I have a CSV file with ; delimiter, but in one of the fields it represents objects with | delimiter:

field1;field2;field3;"field-4.1;field-4.2|field-4.1;field-4.2";field5;field6;

So the problem is with field4 which represents multiples objects separated by pipe.

Actually my template is like this:

           <record name="fields" class="br.com.beanio.MyPojo">
                <field name="field1"/>
                <field name="field2"/>
                <field name="field3"/>
                <segment name="fields4" minOccurs="0" collection="list" class="br.com.beanio.MySubPojo">
                    <field name="field4-1" />
                    <field name="field4-2" />
                </segment>
                <field name="field5"/>
                <field name="field6"/>
            </record>

I tried with different properties to identify when field4 represents a new object, but didn't work.

How could I read field 4 using beanIO templating?

Thanks in advance


Solution

  • My solution was creating a typeHandler for the field:

    public class MyCustomTypeHandler implements TypeHandler {
    

    In my template I use the typeHandler:

    <field name="fieldWithList" typeHandler="MyCustomTypeHandler"/>
    

    So inside parse method in the typeHandler I could process the field:

            if (nonNull(text)) {
                asList(text.split("\\|")).forEach(str -> {
                    String[] fields = str.split(";", -1);
                    // process and add items to the list
                });
            }