Skip to content

Commit a2e92bc

Browse files
committed
Fix #785: Make JsonGenerator.writeXxx() methods chainable
1 parent 2656f4a commit a2e92bc

File tree

10 files changed

+628
-390
lines changed

10 files changed

+628
-390
lines changed

release-notes/VERSION

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ JSON library.
3636
#680: Allow use of `java.nio.file.Path` as parser source, generator target
3737
(contributed by Sven D)
3838
#689: Remove existing "request payload" functionality
39+
#785: Make `JsonGenerator.writeXxx()` methods chainable
3940
#793: Rename "com.fasterxml.jackson" -> "tools.jackson"
4041
- Rename `JsonGenerator.Feature.AUTO_CLOSE_JSON_CONTENT` as `AUTO_CLOSE_CONTENT`
4142
- Add `TreeCodec.nullNode()`, `TreeNode.isNull()` methods

src/main/java/tools/jackson/core/JsonGenerator.java

+92-86
Large diffs are not rendered by default.

src/main/java/tools/jackson/core/base/GeneratorBase.java

+32-29
Original file line numberDiff line numberDiff line change
@@ -152,20 +152,20 @@ public final JsonGenerator configure(StreamWriteFeature f, boolean state) {
152152
/**********************************************************************
153153
*/
154154

155-
//public void writeStartArray()
156-
//public void writeEndArray()
157-
//public void writeStartObject()
158-
//public void writeEndObject()
155+
//public JsonGenerator writeStartArray()
156+
//public JsonGenerator writeEndArray()
157+
//public JsonGenerator writeStartObject()
158+
//public JsonGenerator writeEndObject()
159159

160160
@Override
161-
public void writeStartArray(Object forValue, int size) throws JacksonException {
162-
writeStartArray(forValue);
161+
public JsonGenerator writeStartArray(Object forValue, int size) throws JacksonException {
162+
return writeStartArray(forValue);
163163
}
164164

165165
@Override
166-
public void writeStartObject(Object forValue, int size) throws JacksonException
166+
public JsonGenerator writeStartObject(Object forValue, int size) throws JacksonException
167167
{
168-
writeStartObject(forValue);
168+
return writeStartObject(forValue);
169169
}
170170

171171
/*
@@ -174,47 +174,48 @@ public void writeStartObject(Object forValue, int size) throws JacksonException
174174
/**********************************************************************
175175
*/
176176

177-
@Override public void writeName(SerializableString name) throws JacksonException {
178-
writeName(name.getValue());
177+
@Override
178+
public JsonGenerator writeName(SerializableString name) throws JacksonException {
179+
return writeName(name.getValue());
179180
}
180181

181-
//public abstract void writeString(String text);
182+
//public abstract JsonGenerator writeString(String text);
182183

183-
//public abstract void writeString(char[] text, int offset, int len);
184+
//public abstract JsonGenerator writeString(char[] text, int offset, int len);
184185

185186
@Override
186-
public void writeString(Reader reader, int len) throws JacksonException {
187+
public JsonGenerator writeString(Reader reader, int len) throws JacksonException {
187188
// Let's implement this as "unsupported" to make it easier to add new parser impls
188-
_reportUnsupportedOperation();
189+
return _reportUnsupportedOperation();
189190
}
190-
191-
//public abstract void writeRaw(String text);
192191

193-
//public abstract void writeRaw(char[] text, int offset, int len);
192+
//public abstract JsonGenerator writeRaw(String text);
193+
194+
//public abstract JsonGenerator writeRaw(char[] text, int offset, int len);
194195

195196
@Override
196-
public void writeString(SerializableString text) throws JacksonException {
197-
writeString(text.getValue());
197+
public JsonGenerator writeString(SerializableString text) throws JacksonException {
198+
return writeString(text.getValue());
198199
}
199200

200-
@Override public void writeRawValue(String text) throws JacksonException {
201+
@Override public JsonGenerator writeRawValue(String text) throws JacksonException {
201202
_verifyValueWrite("write raw value");
202-
writeRaw(text);
203+
return writeRaw(text);
203204
}
204205

205-
@Override public void writeRawValue(String text, int offset, int len) throws JacksonException {
206+
@Override public JsonGenerator writeRawValue(String text, int offset, int len) throws JacksonException {
206207
_verifyValueWrite("write raw value");
207-
writeRaw(text, offset, len);
208+
return writeRaw(text, offset, len);
208209
}
209210

210-
@Override public void writeRawValue(char[] text, int offset, int len) throws JacksonException {
211+
@Override public JsonGenerator writeRawValue(char[] text, int offset, int len) throws JacksonException {
211212
_verifyValueWrite("write raw value");
212-
writeRaw(text, offset, len);
213+
return writeRaw(text, offset, len);
213214
}
214215

215-
@Override public void writeRawValue(SerializableString text) throws JacksonException {
216+
@Override public JsonGenerator writeRawValue(SerializableString text) throws JacksonException {
216217
_verifyValueWrite("write raw value");
217-
writeRaw(text);
218+
return writeRaw(text);
218219
}
219220

220221
@Override
@@ -249,7 +250,7 @@ public abstract void writeNull()
249250
*/
250251

251252
@Override
252-
public void writePOJO(Object value) throws JacksonException {
253+
public JsonGenerator writePOJO(Object value) throws JacksonException {
253254
if (value == null) {
254255
// important: call method that does check value write:
255256
writeNull();
@@ -259,16 +260,18 @@ public void writePOJO(Object value) throws JacksonException {
259260
// state would advance causing exception later on
260261
_objectWriteContext.writeValue(this, value);
261262
}
263+
return this;
262264
}
263265

264266
@Override
265-
public void writeTree(TreeNode rootNode) throws JacksonException {
267+
public JsonGenerator writeTree(TreeNode rootNode) throws JacksonException {
266268
// As with 'writeObject()', we are not to check if write would work
267269
if (rootNode == null) {
268270
writeNull();
269271
} else {
270272
_objectWriteContext.writeTree(this, rootNode);
271273
}
274+
return this;
272275
}
273276

274277
/*

0 commit comments

Comments
 (0)