mirror of
https://github.com/stleary/JSON-java.git
synced 2025-08-03 03:15:32 -04:00
fixed issue #943 Csv parsing skip last row if last line is missing newline
This commit is contained in:
parent
76ee4312b3
commit
b2943b8fd0
@ -100,11 +100,15 @@ public class CDL {
|
|||||||
for (;;) {
|
for (;;) {
|
||||||
String value = getValue(x,delimiter);
|
String value = getValue(x,delimiter);
|
||||||
char c = x.next();
|
char c = x.next();
|
||||||
if (value == null ||
|
if (value != null) {
|
||||||
(ja.length() == 0 && value.length() == 0 && c != delimiter)) {
|
ja.put(value);
|
||||||
|
} else if (ja.length() == 0 && c != delimiter) {
|
||||||
return null;
|
return null;
|
||||||
|
} else {
|
||||||
|
// This line accounts for CSV ending with no newline
|
||||||
|
ja.put("");
|
||||||
}
|
}
|
||||||
ja.put(value);
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
if (c == delimiter) {
|
if (c == delimiter) {
|
||||||
break;
|
break;
|
||||||
@ -307,6 +311,17 @@ public class CDL {
|
|||||||
if (ja.length() == 0) {
|
if (ja.length() == 0) {
|
||||||
return null;
|
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;
|
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.
|
* 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