Instead of XML configuration file used by BeanIO can we use JSON Object

28 views Asked by At

BeanIO uses an XML configuration file, called a mapping file, to define how bean objects are bound to records. Below is a mapping file, named mapping.xml, that could be used to read the sample employee file and unmarshall records into Employee objects.

<beanio xmlns="http://www.beanio.org/2012/03" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.beanio.org/2012/03 http://www.beanio.org/2012/03/mapping.xsd">

  <stream name="employeeFile" format="csv">
    <record name="employee" class="example.Employee">
      <field name="firstName" />
      <field name="lastName" />
      <field name="title" />
      <field name="salary" />
      <field name="hireDate" format="MMddyyyy" />
    </record>
  </stream>
</beanio>

The same mapping file can be used to write, or marshall, Employee objects to a file or output stream. One of the project requirement, instead of xml file as configuration we need to use JSON object, where JSON was provided at run time. The problem with XML configuration is that we cant update it once the jar is deployed in Production. I'm getting below error

Exception in thread "main" org.beanio.BeanIOException: Failed to load 'mapping.json' from the file system

Also please suggest how can we use a configuration file for different schema for each record in csv. E.g. if we've 20 records in file and there are three types of records in that file. E.g.

Record 1 - Name, Age, Gender Record 2 - City, State, Country, PIN Record 3 - Phone-No, Email-ID

If Record is different getting below error

Invalid record: DataRecord
org.beanio.InvalidRecordException: Invalid 'DataRecord' record at line 2
 ==> Invalid 'city':  Invalid field length, expected 10 characters
 ==> Invalid 'state':  Expected minimum 1 occurrences
 ==> Invalid 'pinCode':  Expected minimum 1 occurrences

I'm also facing issue with max occurrence, that unbounded is not working as max occurrence hence I need to provide the exact count.

0

There are 0 answers