JSONML.toString() and XML.toString() omit escaping for literal newlines (\n), causing whitespace loss in downstream XML parsers (Regression from #1070)
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Idoneità per principianti
- 45/100
Direzione di ricerca
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.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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
- Lingua principale
- Java
- Stelle
- 4.7k
- Fork
- 2.6k
- Merge medio
- 4g 11h
- PR unite (30g)
- 7
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Nessun modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di stleary/JSON-java
-
JSONObject.toString does not use an Enum's `toString()` but its `name()`Forse di nuovo libera @XIAYM-gh l’ha presa 46 giorni fa e non c’è nessuna pull request aperta. ApertaFix before the next release
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
-
Investigating
Difficoltà 5/5 Più di una settimana Idoneità per principianti 45/100
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 72/100
-
Active discussion
Difficoltà 5/5 Più di una settimana Idoneità per principianti 35/100
-
JSONObject.toString() throws StackOverflowError (not JSONException) on self-referential cyclesForse di nuovo libera Una pull request per questa issue è stata chiusa senza essere unita. ApertaActive discussion
Difficoltà 4/5 3-5 giorni Idoneità per principianti 56/100
Tutte le issue di stleary/JSON-java
Issue simili
-
pdfium-drift
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 82/100
Stirling-Tools/JPDFium#176 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
hiero-ledger/hiero-consensus-node#27712 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 61/100
Vakore/ZappierGames#162 ·
-
Improve README documentation clarity and fix minor wording issuesForse già presa @simplesilence l’ha presa oggi. Aperta
Difficoltà 1/5 1-3 ore Idoneità per principianti 84/100
resilience4j/resilience4j#2567 ·
I maintainer di solito rispondono entro 6 giorni
-
Priority/High
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
wso2/dpdp-accelerator#417 ·
I maintainer di solito rispondono entro 1 giorno