Skip to content

Commit 5f2b093

Browse files
committed
Fix #673
1 parent dfa3b11 commit 5f2b093

File tree

3 files changed

+78
-48
lines changed

3 files changed

+78
-48
lines changed

release-notes/VERSION-2.x

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ JSON library.
1919
#664: Add `StreamWriteException` type to eventually replace `JsonGenerationException`
2020
#671: Add `getCurrentLocation()`/`getTokenLocation()` to replace
2121
`currentLocation()`/`currentTokenLocation()`
22+
#673: Replace `JsonGenerator.writeObject()` (and related) with `writePOJO()`
2223

2324
2.12.1 (08-Jan-2021)
2425

src/main/java/com/fasterxml/jackson/core/JsonGenerator.java

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2007,17 +2007,32 @@ public WritableTypeId writeTypeSuffix(WritableTypeId typeIdDef) throws IOExcepti
20072007
*/
20082008

20092009
/**
2010-
* Method for writing given Java object (POJO) as Json.
2010+
* Method for writing given Java object (POJO) as JSON.
20112011
* Exactly how the object gets written depends on object
2012-
* in question (ad on codec, its configuration); for most
2013-
* beans it will result in JSON Object, but for others JSON
2012+
* in question (and on codec, its configuration); for
2013+
* typical POJOs it will result in JSON Object, but for others JSON
20142014
* Array, or String or numeric value (and for nulls, JSON
2015-
* null literal.
2016-
* <b>NOTE</b>: generator must have its <b>object codec</b>
2015+
* null literal).
2016+
* <b>NOTE</b>: generator must have its {@code ObjectCodec}
20172017
* set to non-null value; for generators created by a mapping
20182018
* factory this is the case, for others not.
20192019
*
2020-
* @param pojo General POJO value to write
2020+
* @param pojo Java value (usually POJO) to write
2021+
*
2022+
* @throws IOException if there is either an underlying I/O problem or encoding
2023+
* issue at format layer
2024+
*
2025+
* @since 2.13 (to eventually replace {@link #writeObject(Object)}
2026+
*/
2027+
public void writePOJO(Object pojo) throws IOException {
2028+
writeObject(pojo);
2029+
}
2030+
2031+
// TODO: deprecate in 2.14 or later
2032+
/**
2033+
* Older alias for {@link #writePOJO(Object)}
2034+
*
2035+
* @param pojo Java value (usually POJO) to write
20212036
*
20222037
* @throws IOException if there is either an underlying I/O problem or encoding
20232038
* issue at format layer
@@ -2327,6 +2342,20 @@ public void writeObjectFieldStart(String fieldName) throws IOException {
23272342
* @throws IOException if there is either an underlying I/O problem or encoding
23282343
* issue at format layer
23292344
*/
2345+
public void writePOJOField(String fieldName, Object pojo) throws IOException {
2346+
writeObjectField(fieldName, pojo);
2347+
}
2348+
2349+
// TODO: deprecate in 2.14 or later
2350+
/**
2351+
* Older alias for {@link #writePOJOField}
2352+
*
2353+
* @param fieldName Name of the field to write
2354+
* @param pojo POJO value of the field to write
2355+
*
2356+
* @throws IOException if there is either an underlying I/O problem or encoding
2357+
* issue at format layer
2358+
*/
23302359
public void writeObjectField(String fieldName, Object pojo) throws IOException {
23312360
writeFieldName(fieldName);
23322361
writeObject(pojo);

src/main/java/com/fasterxml/jackson/core/util/JsonGeneratorDelegate.java

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ public class JsonGeneratorDelegate extends JsonGenerator
2424
protected boolean delegateCopyMethods;
2525

2626
/*
27-
/**********************************************************
27+
/**********************************************************************
2828
/* Construction, initialization
29-
/**********************************************************
29+
/**********************************************************************
3030
*/
3131

3232
public JsonGeneratorDelegate(JsonGenerator d) {
@@ -55,9 +55,9 @@ public void setCurrentValue(Object v) {
5555
}
5656

5757
/*
58-
/**********************************************************
58+
/**********************************************************************
5959
/* Public API, metadata
60-
/**********************************************************
60+
/**********************************************************************
6161
*/
6262

6363
@Override public ObjectCodec getCodec() { return delegate.getCodec(); }
@@ -74,9 +74,9 @@ public void setCurrentValue(Object v) {
7474
@Override public int getOutputBuffered() { return delegate.getOutputBuffered(); }
7575

7676
/*
77-
/**********************************************************
78-
/* Public API, capability introspection (since 2.3, mostly)
79-
/**********************************************************
77+
/**********************************************************************
78+
/* Public API, capability introspection
79+
/**********************************************************************
8080
*/
8181

8282
@Override
@@ -103,9 +103,9 @@ public JacksonFeatureSet<StreamWriteCapability> getWriteCapabilities() {
103103
}
104104

105105
/*
106-
/**********************************************************
106+
/**********************************************************************
107107
/* Public API, configuration
108-
/**********************************************************
108+
/**********************************************************************
109109
*/
110110

111111
@Override
@@ -149,9 +149,9 @@ public JsonGenerator overrideFormatFeatures(int values, int mask) {
149149
}
150150

151151
/*
152-
/**********************************************************
152+
/**********************************************************************
153153
/* Configuring generator
154-
/**********************************************************
154+
/**********************************************************************
155155
*/
156156

157157
@Override
@@ -186,9 +186,9 @@ public JsonGenerator setPrettyPrinter(PrettyPrinter pp) {
186186
return this; }
187187

188188
/*
189-
/**********************************************************
189+
/**********************************************************************
190190
/* Public API, write methods, structural
191-
/**********************************************************
191+
/**********************************************************************
192192
*/
193193

194194
@Override
@@ -257,9 +257,9 @@ public void writeArray(String[] array, int offset, int length) throws IOExceptio
257257
}
258258

259259
/*
260-
/**********************************************************
260+
/**********************************************************************
261261
/* Public API, write methods, text/String values
262-
/**********************************************************
262+
/**********************************************************************
263263
*/
264264

265265
@Override
@@ -283,9 +283,9 @@ public void writeString(Reader reader, int len) throws IOException {
283283
public void writeUTF8String(byte[] text, int offset, int length) throws IOException { delegate.writeUTF8String(text, offset, length); }
284284

285285
/*
286-
/**********************************************************
286+
/**********************************************************************
287287
/* Public API, write methods, binary/raw content
288-
/**********************************************************
288+
/**********************************************************************
289289
*/
290290

291291
@Override
@@ -319,9 +319,9 @@ public void writeString(Reader reader, int len) throws IOException {
319319
public int writeBinary(Base64Variant b64variant, InputStream data, int dataLength) throws IOException { return delegate.writeBinary(b64variant, data, dataLength); }
320320

321321
/*
322-
/**********************************************************
322+
/**********************************************************************
323323
/* Public API, write methods, other value types
324-
/**********************************************************
324+
/**********************************************************************
325325
*/
326326

327327
@Override
@@ -358,9 +358,9 @@ public void writeString(Reader reader, int len) throws IOException {
358358
public void writeNull() throws IOException { delegate.writeNull(); }
359359

360360
/*
361-
/**********************************************************
361+
/**********************************************************************
362362
/* Public API, convenience field-write methods
363-
/**********************************************************
363+
/**********************************************************************
364364
*/
365365

366366
// 04-Oct-2019, tatu: Reminder: these should NOT be delegated, unless matching
@@ -375,6 +375,7 @@ public void writeString(Reader reader, int len) throws IOException {
375375
// public void writeArrayFieldStart(String fieldName) throws IOException {
376376
// public void writeObjectFieldStart(String fieldName) throws IOException {
377377
// public void writeObjectField(String fieldName, Object pojo) throws IOException {
378+
// public void writePOJOField(String fieldName, Object pojo) throws IOException {
378379

379380
// Sole exception being this method as it is not a "combo" method
380381

@@ -384,9 +385,9 @@ public void writeOmittedField(String fieldName) throws IOException {
384385
}
385386

386387
/*
387-
/**********************************************************
388+
/**********************************************************************
388389
/* Public API, write methods, Native Ids
389-
/**********************************************************
390+
/**********************************************************************
390391
*/
391392

392393
@Override
@@ -402,11 +403,16 @@ public void writeOmittedField(String fieldName) throws IOException {
402403
public void writeEmbeddedObject(Object object) throws IOException { delegate.writeEmbeddedObject(object); }
403404

404405
/*
405-
/**********************************************************
406+
/**********************************************************************
406407
/* Public API, write methods, serializing Java objects
407-
/**********************************************************
408+
/**********************************************************************
408409
*/
409-
410+
411+
@Override // since 2.13
412+
public void writePOJO(Object pojo) throws IOException {
413+
writeObject(pojo);
414+
}
415+
410416
@Override
411417
public void writeObject(Object pojo) throws IOException {
412418
if (delegateCopyMethods) {
@@ -444,17 +450,17 @@ public void writeTree(TreeNode tree) throws IOException {
444450
}
445451

446452
/*
447-
/**********************************************************
453+
/**********************************************************************
448454
/* Public API, convenience field write methods
449-
/**********************************************************
455+
/**********************************************************************
450456
*/
451457

452458
// // These are fine, just delegate to other methods...
453459

454460
/*
455-
/**********************************************************
461+
/**********************************************************************
456462
/* Public API, copy-through methods
457-
/**********************************************************
463+
/**********************************************************************
458464
*/
459465

460466
@Override
@@ -470,34 +476,28 @@ public void copyCurrentStructure(JsonParser p) throws IOException {
470476
}
471477

472478
/*
473-
/**********************************************************
479+
/**********************************************************************
474480
/* Public API, context access
475-
/**********************************************************
481+
/**********************************************************************
476482
*/
477483

478484
@Override public JsonStreamContext getOutputContext() { return delegate.getOutputContext(); }
479485

480486
/*
481-
/**********************************************************
487+
/**********************************************************************
482488
/* Public API, buffer handling
483-
/**********************************************************
489+
/**********************************************************************
484490
*/
485491

486492
@Override public void flush() throws IOException { delegate.flush(); }
487493
@Override public void close() throws IOException { delegate.close(); }
488494

489-
/*
490-
/**********************************************************
491-
/* Closeable implementation
492-
/**********************************************************
493-
*/
494-
495495
@Override public boolean isClosed() { return delegate.isClosed(); }
496496

497497
/*
498-
/**********************************************************
498+
/**********************************************************************
499499
/* Extended API
500-
/**********************************************************
500+
/**********************************************************************
501501
*/
502502

503503
@Deprecated // since 2.11

0 commit comments

Comments
 (0)