JSONML.toString() and XML.toString() omit escaping for literal newlines (\n), causing whitespace loss in downstream XML parsers (Regression from #1070)
還沒有人認領這個 Issue。
評估
研究方向
Start with XML.mustEscape(int cp) and XML.escape in the XML class, then trace how JSONML.toString and XML.toString reach them. The known regression is that 0xA, 0xD and 0x9 no longer get escaped on output, while numeric references like must still parse on input. Check the existing tests for the #1059 and #1070 behaviour before changing anything. Done means literal newlines, carriage returns and tabs in values serialize as numeric entities, and the parsing tests still pass.
由索引模型根據 Issue 內容生成。
描述
Hello,
When a JSONObject containing a string with newline characters (\n) is converted to XML using JSONML.toString() (or XML.toString()), the newlines are output as literal newline characters instead of being XML-escaped (e.g., to ).
Because literal newlines in attribute values or specific text nodes are subject to W3C Newline Normalization by downstream XML parsers, the structural formatting and whitespace are completely lost or altered when the generated XML is parsed by another system.
This behavior is a side effect of the fix introduced in PR #1070 (which solved #1059). While PR #1070 correctly allowed whitespace numeric character references ( ) to be accepted during parsing (deserialization), it altered XML.mustEscape(int cp) to return false for 0xA, 0xD, and 0x9. As a result, the serialization path (XML.toString / JSONML.toString) now skips escaping for literal newlines, causing a regression in how whitespaces are preserved during export.
Reproducible Example / Code Snippet
import org.json.JSONObject;
import org.json.JSONML;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class XMLNewlineEscapingTest {
@Test
public void testJSONMLToStringPreservesNewlines() {
String jsonString = "{\"tagName\":\"MyTag\", \"value\":\"Lettuce\\nTomato\\nMustard\"}";
JSONObject jo = new JSONObject(jsonString);
String xmlOutput = JSONML.toString(jo);
System.out.println("Generated XML:\n" + xmlOutput);
// Assert
// Expected: Newlines should be escaped as to survive XML normalization
// Actual: It outputs raw, literal newlines <MyTag value="Lettuce\nTomato..."/>
assertTrue(xmlOutput.contains(" "), "XML should escape literal newlines to preserve them.");
}
}
Expected Behavior
When serializing to XML, literal control characters like \n (0xA), \r (0xD), and \t (0x9) within string values should be escaped into their corresponding numeric character entities ( , , 	) to prevent downstream XML parsers from stripping or normalizing them.
Actual Behavior
The generated XML string contains raw literal line breaks, which are collapsed or lost according to standard XML whitespace processing rules when loaded by any standard XML parser.
Suggested Root Cause & Fix Hint
The serialization path (XML.escape) heavily relies on XML.mustEscape(int cp). Currently, XML.mustEscape combines both "is this character legally allowed in an XML document?" (W3C range) and "does this character need to be escaped in text/attributes?".
The library needs to differentiate between the deserialization path (where is valid and shouldn't throw an exception) and the serialization path (where a literal 0xA needs to be actively escaped to ensure data integrity).
Best regards,
Sebastian
- 主要語言
- Java
- 星號
- 4.7k
- 分支
- 2.6k
- 平均合併
- 4 天 11 小時
- 30 天內合併 PR
- 7
環境準備
- 沒有 Dockerfile 或 Docker Compose 檔案
- 沒有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
stleary/JSON-java 的其他 Issue
-
JSONObject.toString does not use an Enum's `toString()` but its `name()`可能重新可做 @XIAYM-gh 於 43 天前認領,目前沒有進行中的 PR。 未關閉Fix before the next release
難度 2/5 1-3 小時 新手友好度 78/100
-
Investigating
難度 5/5 一週以上 新手友好度 45/100
-
難度 3/5 1-2 天 新手友好度 72/100
-
Active discussion
難度 5/5 一週以上 新手友好度 35/100
-
JSONObject.toString() throws StackOverflowError (not JSONException) on self-referential cycles可能重新可做 關聯的 PR 已關閉且未合併。 未關閉Active discussion
難度 4/5 3-5 天 新手友好度 56/100
查看 stleary/JSON-java 的全部 Issue
相似的 Issue
-
agent:working documentation enhancement go:build lane:needs-decision
難度 2/5 1-3 小時 新手友好度 72/100
Sanne/incus-spawn#1435 · 3 則留言 ·
維護者通常 1 天內回覆
-
enhancement good first issue up-for-grabs
難度 2/5 1-3 小時 新手友好度 80/100
AnikethTS/ChangeRadar#22 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 72/100
openai/openai-java#1202 ·
維護者通常 1 天內回覆
-
admin sql type: bug
難度 2/5 1-3 小時 新手友好度 72/100
維護者通常 2 天內回覆
-
library-new-request priority
難度 2/5 1-3 小時 新手友好度 70/100
oracle/graalvm-reachability-metadata#10911 · 1 則留言 ·
維護者通常 1 天內回覆