JSONML.toString() and XML.toString() omit escaping for literal newlines (\n), causing whitespace loss in downstream XML parsers (Regression from #1070)
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức phù hợp với người mới
- 45/100
Hướng nghiên cứu
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.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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
- Ngôn ngữ chính
- Java
- Star
- 4.7k
- Fork
- 2.6k
- Merge trung bình
- 4 ngày 11 giờ
- Pull request đã merge (30 ngày)
- 7
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của stleary/JSON-java
-
JSONObject.toString does not use an Enum's `toString()` but its `name()`Có thể làm lại được @XIAYM-gh đã nhận 44 ngày trước và không có pull request nào đang mở. Đang mởFix before the next release
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
-
Investigating
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 45/100
-
`XML.unescape` rejects the character references `XML.escape` produces for control charactersĐang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 72/100
-
Active discussion
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 35/100
-
JSONObject.toString() throws StackOverflowError (not JSONException) on self-referential cyclesCó thể làm lại được Pull request cho issue này đã bị đóng mà không được merge. Đang mởActive discussion
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 56/100
Tất cả issue của stleary/JSON-java
Issue tương tự
-
False Negative: InnerTypeLastCheck does not report members declared after inner enums or annotationsĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
checkstyle/checkstyle#22027 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug follow-up lane:auto
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 85/100
Sanne/incus-spawn#1497 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 85/100
Maintainer thường phản hồi trong vòng 1 ngày
-
[CI] NettyClientTest.testServerDisconnection may fail with DisconnectExceptionCó thể đã có người làm @yangshangqing95 đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 86/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100