Skip to content

Commit 4e245eb

Browse files
authored
Implement #845: add writeComment() in ToXmlGenerator (#846)
1 parent 5431701 commit 4e245eb

File tree

3 files changed

+108
-18
lines changed

3 files changed

+108
-18
lines changed

release-notes/VERSION

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ Version: 3.x (for earlier see VERSION-2.x)
114114
#802: Serialization with Polymorphisme and `EXTERNAL_PROPERTY` = duplicate property
115115
(reported by @ Adrien-dev25 )
116116
(fix by @cowtowncoder, w/ Claude code)
117+
#845: Implement `JsonGenerator` methods `writeComment()` and `canWriteComments()`
118+
(implemented by @cowtowncoder, w/ Claude code)
117119

118120
3.1.1 (27-Mar-2026)
119121

src/main/java/tools/jackson/dataformat/xml/ser/ToXmlGenerator.java

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -253,21 +253,30 @@ public void initGenerator() throws JacksonException
253253

254254
/*
255255
/**********************************************************************
256-
/* Overridden output state handling methods
256+
/* Overrides: capability introspection
257257
/**********************************************************************
258258
*/
259-
260-
@Override
261-
public final TokenStreamContext streamWriteContext() { return _streamWriteContext; }
259+
260+
@Override // @since 3.2
261+
public boolean canWriteComments() { return true; }
262+
263+
// Base class impl fine:
264+
//@Override public boolean canWriteObjectId() { return false; }
265+
266+
// Base class impl fine:
267+
//@Override public boolean canOmitProperties() { return true; }
268+
269+
// Base class impl fine:
270+
//@Override public boolean canWriteTypeId() { return false; }
262271

263272
@Override
264-
public final Object currentValue() {
265-
return _streamWriteContext.currentValue();
273+
public boolean has(StreamWriteCapability capability) {
274+
return DEFAULT_TEXTUAL_WRITE_CAPABILITIES.isEnabled(capability);
266275
}
267276

268277
@Override
269-
public final void assignCurrentValue(Object v) {
270-
_streamWriteContext.assignCurrentValue(v);
278+
public JacksonFeatureSet<StreamWriteCapability> streamWriteCapabilities() {
279+
return DEFAULT_TEXTUAL_WRITE_CAPABILITIES;
271280
}
272281

273282
/*
@@ -297,6 +306,25 @@ public int streamWriteOutputBuffered() {
297306
return -1;
298307
}
299308

309+
/*
310+
/**********************************************************************
311+
/* Overridden output state handling methods
312+
/**********************************************************************
313+
*/
314+
315+
@Override
316+
public final TokenStreamContext streamWriteContext() { return _streamWriteContext; }
317+
318+
@Override
319+
public final Object currentValue() {
320+
return _streamWriteContext.currentValue();
321+
}
322+
323+
@Override
324+
public final void assignCurrentValue(Object v) {
325+
_streamWriteContext.assignCurrentValue(v);
326+
}
327+
300328
/*
301329
/**********************************************************************
302330
/* Extended API, configuration
@@ -316,16 +344,6 @@ public ToXmlGenerator configure(XmlWriteFeature f, boolean state) {
316344
return this;
317345
}
318346

319-
@Override
320-
public boolean has(StreamWriteCapability capability) {
321-
return DEFAULT_TEXTUAL_WRITE_CAPABILITIES.isEnabled(capability);
322-
}
323-
324-
@Override
325-
public JacksonFeatureSet<StreamWriteCapability> streamWriteCapabilities() {
326-
return DEFAULT_TEXTUAL_WRITE_CAPABILITIES;
327-
}
328-
329347
public boolean inRoot() {
330348
return _streamWriteContext.inRoot();
331349
}
@@ -906,6 +924,27 @@ public JsonGenerator writeRaw(char c) throws JacksonException
906924
return writeRaw(String.valueOf(c));
907925
}
908926

927+
/*
928+
/**********************************************************************
929+
/* Output method implementations, comments
930+
/**********************************************************************
931+
*/
932+
933+
@Override // @since 3.2
934+
public JsonGenerator writeComment(String comment) throws JacksonException
935+
{
936+
try {
937+
if (comment != null) {
938+
_xmlWriter.writeComment(comment);
939+
} else {
940+
_xmlWriter.writeSpace("\n");
941+
}
942+
} catch (XMLStreamException e) {
943+
StaxUtil.throwAsWriteException(e, this);
944+
}
945+
return this;
946+
}
947+
909948
/*
910949
/**********************************************************************
911950
/* Output method implementations, base64-encoded binary

src/test/java/tools/jackson/dataformat/xml/stream/XmlGeneratorTest.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import tools.jackson.dataformat.xml.ser.ToXmlGenerator;
1313

1414
import static org.junit.jupiter.api.Assertions.assertEquals;
15+
import static org.junit.jupiter.api.Assertions.assertTrue;
1516

1617
public class XmlGeneratorTest extends XmlTestUtil
1718
{
@@ -312,4 +313,52 @@ public void testRawCharArratAttribute() throws Exception
312313
xml = removeSjsxpNamespace(xml);
313314
assertEquals("<root attr=\"value\"/>", xml);
314315
}
316+
317+
// [dataformat-xml#845]
318+
@Test
319+
public void testCanWriteComments() throws Exception
320+
{
321+
StringWriter out = new StringWriter();
322+
ToXmlGenerator gen = (ToXmlGenerator) MAPPER.createGenerator(out);
323+
assertTrue(gen.canWriteComments());
324+
// Need to write something to avoid empty document error on close
325+
gen.setNextName(new QName("root"));
326+
gen.writeStartObject();
327+
gen.writeEndObject();
328+
gen.close();
329+
}
330+
331+
// [dataformat-xml#845]
332+
@Test
333+
public void testWriteCommentInObject() throws Exception
334+
{
335+
StringWriter out = new StringWriter();
336+
ToXmlGenerator gen = (ToXmlGenerator) MAPPER.createGenerator(out);
337+
gen.setNextName(new QName("root"));
338+
gen.writeStartObject();
339+
gen.writeComment("a comment");
340+
gen.writeName("elem");
341+
gen.writeString("value");
342+
gen.writeEndObject();
343+
gen.close();
344+
String xml = removeSjsxpNamespace(out.toString());
345+
assertEquals("<root><!--a comment--><elem>value</elem></root>", xml);
346+
}
347+
348+
// [dataformat-xml#845]
349+
@Test
350+
public void testWriteCommentNullWritesEmptyLine() throws Exception
351+
{
352+
StringWriter out = new StringWriter();
353+
ToXmlGenerator gen = (ToXmlGenerator) MAPPER.createGenerator(out);
354+
gen.setNextName(new QName("root"));
355+
gen.writeStartObject();
356+
gen.writeComment(null);
357+
gen.writeName("elem");
358+
gen.writeString("value");
359+
gen.writeEndObject();
360+
gen.close();
361+
String xml = removeSjsxpNamespace(out.toString());
362+
assertEquals("<root>\n<elem>value</elem></root>", xml);
363+
}
315364
}

0 commit comments

Comments
 (0)