mirror of
https://github.com/stleary/JSON-java.git
synced 2025-08-02 11:05:28 -04:00
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:
commit
4917e3579d
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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.
|
||||
*/
|
||||
|
Loading…
x
Reference in New Issue
Block a user