Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/main/java/org/json/CDL.java
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,9 @@ private static boolean shouldQuoteValue(String value, char delimiter) {
}

/**
* Append a row value surrounded by quotes, omitting characters that should
* not appear inside the quoted value.
* Append a row value surrounded by quotes, doubling embedded quotes and
* omitting control characters that should not appear inside the quoted
* value.
*
* @param sb the destination buffer
* @param value the value to append
Expand All @@ -241,7 +242,10 @@ private static void appendQuotedValue(StringBuilder sb, String value) {
int length = value.length();
for (int j = 0; j < length; j += 1) {
char c = value.charAt(j);
if (c >= ' ' && c != '"') {
if (c == '"') {
// escape an embedded quote by doubling it, as the parser expects
sb.append("\"\"");
} else if (c >= ' ') {
sb.append(c);
}
}
Expand Down
17 changes: 15 additions & 2 deletions src/test/java/org/json/junit/CDLTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,19 @@ public void textToJSONArrayAndBackToStringCustomDelimiter() {
JSONArray expectedJsonArray = new JSONArray(EXPECTED_LINES);
Util.compareActualVsExpectedJsonArrays(finalJsonArray, expectedJsonArray);
}


}
/**
* Embedded quotes must survive a CDL.toString round trip.
*/
@Test
public void embeddedQuotesRoundTrip() {
JSONArray jsonArray = new JSONArray();
jsonArray.put(new JSONObject().put("a", "say \"hi\", ok").put("b", "\"start"));
String cdl = CDL.toString(jsonArray);
assertEquals("a,b\n\"say \"\"hi\"\", ok\",\"\"\"start\"\n", cdl);
JSONArray parsed = CDL.toJSONArray(cdl);
assertEquals("say \"hi\", ok", parsed.getJSONObject(0).getString("a"));
assertEquals("\"start", parsed.getJSONObject(0).getString("b"));
}

}
Loading