Merge pull request #962 from marilynel/master

Fix: handles edge case 'no \n at end of csv dataset + empty last column'
This commit is contained in:
Sean Leary 2025-03-26 20:24:01 -05:00 committed by GitHub
commit 4917e3579d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 45 additions and 3 deletions

View File

@ -100,11 +100,15 @@ public class CDL {
for (;;) {
String value = getValue(x,delimiter);
char c = x.next();
if (value == null ||
(ja.length() == 0 && value.length() == 0 && c != delimiter)) {
if (value != null) {
ja.put(value);
} else if (ja.length() == 0 && c != delimiter) {
return null;
} else {
// This line accounts for CSV ending with no newline
ja.put("");
}
ja.put(value);
for (;;) {
if (c == delimiter) {
break;
@ -307,6 +311,17 @@ public class CDL {
if (ja.length() == 0) {
return null;
}
// The following block accounts for empty datasets (no keys or vals)
if (ja.length() == 1) {
JSONObject j = ja.getJSONObject(0);
if (j.length() == 1) {
String key = j.keys().next();
if ("".equals(key) && "".equals(j.get(key))) {
return null;
}
}
}
return ja;
}

View File

@ -168,6 +168,33 @@ public class CDLTest {
}
}
/**
* Csv parsing skip last row if last field of this row is empty #943
*/
@Test
public void csvParsingCatchesLastRow(){
String data = "Field 1,Field 2,Field 3\n" +
"value11,value12,\n" +
"value21,value22,";
JSONArray jsonArray = CDL.toJSONArray(data);
JSONArray expectedJsonArray = new JSONArray();
JSONObject jsonObject = new JSONObject();
jsonObject.put("Field 1", "value11");
jsonObject.put("Field 2", "value12");
jsonObject.put("Field 3", "");
expectedJsonArray.put(jsonObject);
jsonObject = new JSONObject();
jsonObject.put("Field 1", "value21");
jsonObject.put("Field 2", "value22");
jsonObject.put("Field 3", "");
expectedJsonArray.put(jsonObject);
Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
}
/**
* Assert that there is no error for a single escaped quote within a properly embedded quote.
*/