I have a file as an input which contain a json Array :
[ {
...,
...
},
{
...,
...
},
{
...,
...
}
]
I want to read
This is the record separator policy I wrote starting from your suggestions and from the default implementation. I use an internal plain string representation for the read record, but i found out very simple to parse the JSON with codehaus jettison JSON object.
public class JsonRecordSeparatorPolicy extends SimpleRecordSeparatorPolicy {
/**
* True if the line can be parsed to a JSON object.
*
* @see RecordSeparatorPolicy#isEndOfRecord(String)
*/
@Override
public boolean isEndOfRecord(String line) {
return StringUtils.countOccurrencesOf(line, "{") == StringUtils.countOccurrencesOf(line, "}")
&& (line.trim().endsWith("}") || line.trim().endsWith(",") || line.trim().endsWith("]") );
}
@Override
public String postProcess(String record) {
if(record.startsWith("[")) record = record.substring(1);
if(record.endsWith("]")) record = record.substring(0, record.length()-1);
if(record.endsWith(",")) record = record.substring(0, record.length()-1);
return super.postProcess(record);
}
}