I was playing with parsing CSV files into JSON using CDL library from org.json.CDL and I observed a strange behavior.
When I pass a CSV with an empty last value to the toJSONArray method, it's omitting the last row.
package com.my.test;
import org.json.CDL;
import org.json.JSONException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
@ExtendWith(MockitoExtension.class)
class CDLTest {
@Test
void testCDL_empty_column_fail() throws JSONException {
final String SIMPLE_CSV_EMPTY_COLUMN_STRING = "Number,Name,Empty\n" +
"1,Allice,\n" +
"2,Chris,\n" +
"3,Bob,";
List<Object> jsonObjects = CDL.toJSONArray(SIMPLE_CSV_EMPTY_COLUMN_STRING).toList();
assertAll(
() -> assertNotNull(jsonObjects),
() -> assertEquals(3, jsonObjects.size())
);
}
}
It's failing as well for that String:
final String SIMPLE_CSV_EMPTY_COLUMN_STRING = "Number,Name,Empty\n" +
"1,Allice,not\n" +
"2,Chris,empty\n" +
"3,Bob,";
Do you have any idea how can I fix it? For learning purposes, I'd prefer to stick to this library and just fix it codewise. I was thinking about extending CDL and reimplementing method with a bug. Do you have any other idea, or maybe someone already fixed it?
Versions:
<org.json.version>20190722</org.json.version> and
Java 11
EDIT:
When I add a new line "\n" to the row "3,Bob,\n";, it parses correctly, but I think the assumption that CSV needs to end with a new line is wrong, is it?
The reason for this behavior is line 108 in CDL rowToJSONArray which assumes that the row needs something at the end:
if (value == null ||
(ja.length() == 0 && value.length() == 0 && c != ',')) {