Skip to content

Commit 99c8da6

Browse files
committed
Fix #2025
1 parent b6eaec1 commit 99c8da6

File tree

1 file changed

+103
-49
lines changed

1 file changed

+103
-49
lines changed

src/main/java/com/fasterxml/jackson/databind/jsontype/TypeSerializer.java

Lines changed: 103 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
*<p>
1919
* NOTE: version 2.9 contains significant attempt at simplifying interface,
2020
* as well as giving format implementation (via {@link JsonGenerator}) more
21-
* control over actual serialization details.
21+
* control over actual serialization details. Minor changes are required to change
22+
* call pattern so that return value of "prefix" write needs to be passed to "suffix"
23+
* write.
2224
*/
2325
public abstract class TypeSerializer
2426
{
@@ -119,6 +121,12 @@ public WritableTypeId typeId(Object value, Class<?> typeForId,
119121
* Method called to write initial part of type information for given
120122
* value, along with possible wrapping to use: details are specified
121123
* by `typeId` argument.
124+
* Note that for structured types (Object, Array), this call will add
125+
* necessary start token so it should NOT be explicitly written, unlike
126+
* with non-type-id value writes.
127+
*<p>
128+
* See {@link #writeTypeSuffix(JsonGenerator, WritableTypeId)} for a complete
129+
* example of typical usage.
122130
*
123131
* @param g Generator to use for outputting type id and possible wrapping
124132
* @param typeId Details of what type id is to be written, how.
@@ -129,6 +137,25 @@ public abstract WritableTypeId writeTypePrefix(JsonGenerator g,
129137
WritableTypeId typeId) throws IOException;
130138

131139
/**
140+
* Method that should be called after {@link #writeTypePrefix(JsonGenerator, WritableTypeId)}
141+
* and matching value write have called, passing {@link WritableTypeId} returned.
142+
* Usual idiom is:
143+
*<pre>
144+
* // Indicator generator that type identifier may be needed; generator may write
145+
* // one as suggested, modify information, or take some other action
146+
* // NOTE! For Object/Array types, this will ALSO write start marker!
147+
* WritableTypeId typeIdDef = typeSer.writeTypePrefix(gen,
148+
* typeSer.typeId(value, JsonToken.START_OBJECT));
149+
*
150+
* // serializing actual value for which TypeId may have been written... like
151+
* // NOTE: do NOT write START_OBJECT before OR END_OBJECT after:
152+
* g.writeStringField("message", "Hello, world!"
153+
*
154+
* // matching type suffix call to let generator chance to add suffix, if any
155+
* // NOTE! For Object/Array types, this will ALSO write end marker!
156+
* typeSer.writeTypeSuffix(gen, typeIdDef);
157+
*</pre>
158+
*
132159
* @since 2.9
133160
*/
134161
public abstract WritableTypeId writeTypeSuffix(JsonGenerator g,
@@ -141,110 +168,107 @@ public abstract WritableTypeId writeTypeSuffix(JsonGenerator g,
141168
*/
142169

143170
/**
144-
* Method called to write initial part of type information for given
145-
* value, when it will be output as scalar JSON value (not as JSON
146-
* Object or Array).
147-
* This means that the context after call cannot be that of JSON Object;
148-
* it may be Array or root context.
149-
*
150-
* @param value Value that will be serialized, for which type information is
151-
* to be written
152-
* @param g Generator to use for writing type information
171+
* DEPRECATED: now equivalent to:
172+
*{@code writeTypePrefix(g, typeId(value, JsonToken.VALUE_STRING));}.
173+
* See {@link #writeTypePrefix} for more info.
174+
*
175+
* @deprecated Since 2.9 use {@link #writeTypePrefix(JsonGenerator, WritableTypeId) instead
153176
*/
154177
@Deprecated // since 2.9
155178
public void writeTypePrefixForScalar(Object value, JsonGenerator g) throws IOException {
156179
writeTypePrefix(g, typeId(value, JsonToken.VALUE_STRING));
157180
}
158181

159182
/**
160-
* Method called to write initial part of type information for given
161-
* value, when it will be output as JSON Object value (not as JSON
162-
* Array or scalar).
163-
* This means that context after call must be JSON Object, meaning that
164-
* caller can then proceed to output field entries.
165-
*
166-
* @param value Value that will be serialized, for which type information is
167-
* to be written
168-
* @param g Generator to use for writing type information
183+
* DEPRECATED: now equivalent to:
184+
*{@code writeTypePrefix(g, typeId(value, JsonToken.START_OBJECT));}.
185+
* See {@link #writeTypePrefix} for more info.
186+
*
187+
* @deprecated Since 2.9 use {@link #writeTypePrefix(JsonGenerator, WritableTypeId) instead
169188
*/
170189
@Deprecated // since 2.9
171190
public void writeTypePrefixForObject(Object value, JsonGenerator g) throws IOException {
172191
writeTypePrefix(g, typeId(value, JsonToken.START_OBJECT));
173192
}
174193

175194
/**
176-
* Method called to write initial part of type information for given
177-
* value, when it will be output as JSON Array value (not as JSON
178-
* Object or scalar).
179-
* This means that context after call must be JSON Array, that is, there
180-
* must be an open START_ARRAY to write contents in.
181-
*
182-
* @param value Value that will be serialized, for which type information is
183-
* to be written
184-
* @param g Generator to use for writing type information
195+
* DEPRECATED: now equivalent to:
196+
*{@code writeTypePrefix(g, typeId(value, JsonToken.START_ARRAY));}.
197+
* See {@link #writeTypePrefix} for more info.
198+
*
199+
* @deprecated Since 2.9 use {@link #writeTypePrefix(JsonGenerator, WritableTypeId) instead
185200
*/
186201
@Deprecated // since 2.9
187202
public void writeTypePrefixForArray(Object value, JsonGenerator g) throws IOException {
188203
writeTypePrefix(g, typeId(value, JsonToken.START_ARRAY));
189204
}
190205

191206
/**
192-
* Method called after value has been serialized, to close any scopes opened
193-
* by earlier matching call to {@link #writeTypePrefixForScalar}.
194-
* Actual action to take may depend on various factors, but has to match with
195-
* action {@link #writeTypePrefixForScalar} did (close array or object; or do nothing).
207+
* DEPRECATED: now equivalent to:
208+
*{@code writeTypeSuffix(g, typeId(value, JsonToken.VALUE_STRING));}.
209+
* See {@link #writeTypeSuffix} for more info.
210+
*
211+
* @deprecated Since 2.9 use {@link #writeTypeSuffix(JsonGenerator, WritableTypeId) instead
196212
*/
197213
@Deprecated // since 2.9
198214
public void writeTypeSuffixForScalar(Object value, JsonGenerator g) throws IOException {
199215
_writeLegacySuffix(g, typeId(value, JsonToken.VALUE_STRING));
200216
}
201217

202218
/**
203-
* Method called after value has been serialized, to close any scopes opened
204-
* by earlier matching call to {@link #writeTypePrefixForObject}.
205-
* It needs to write closing END_OBJECT marker, and any other decoration
206-
* that needs to be matched.
219+
* DEPRECATED: now equivalent to:
220+
*{@code writeTypeSuffix(g, typeId(value, JsonToken.START_OBJECT));}.
221+
* See {@link #writeTypeSuffix} for more info.
222+
*
223+
* @deprecated Since 2.9 use {@link #writeTypeSuffix(JsonGenerator, WritableTypeId) instead
207224
*/
208225
@Deprecated // since 2.9
209226
public void writeTypeSuffixForObject(Object value, JsonGenerator g) throws IOException {
210227
_writeLegacySuffix(g, typeId(value, JsonToken.START_OBJECT));
211228
}
212229

213230
/**
214-
* Method called after value has been serialized, to close any scopes opened
215-
* by earlier matching call to {@link #writeTypeSuffixForScalar}.
216-
* It needs to write closing END_ARRAY marker, and any other decoration
217-
* that needs to be matched.
231+
* DEPRECATED: now equivalent to:
232+
*{@code writeTypeSuffix(g, typeId(value, JsonToken.START_ARRAY));}.
233+
* See {@link #writeTypeSuffix} for more info.
234+
*
235+
* @deprecated Since 2.9 use {@link #writeTypeSuffix(JsonGenerator, WritableTypeId) instead
218236
*/
219237
@Deprecated // since 2.9
220238
public void writeTypeSuffixForArray(Object value, JsonGenerator g) throws IOException {
221239
_writeLegacySuffix(g, typeId(value, JsonToken.START_ARRAY));
222240
}
223241

224242
/**
225-
* Alternative version of the prefix-for-scalar method, which is given
226-
* actual type to use (instead of using exact type of the value); typically
227-
* a super type of actual value type
243+
* DEPRECATED: now equivalent to:
244+
*{@code writeTypePrefix(g, typeId(value, type, JsonToken.VALUE_STRING));}.
245+
* See {@link #writeTypePrefix} for more info.
246+
*
247+
* @deprecated Since 2.9 use {@link #writeTypePrefix(JsonGenerator, WritableTypeId) instead
228248
*/
229249
@Deprecated // since 2.9
230250
public void writeTypePrefixForScalar(Object value, JsonGenerator g, Class<?> type) throws IOException {
231251
writeTypePrefix(g, typeId(value, type, JsonToken.VALUE_STRING));
232252
}
233253

234254
/**
235-
* Alternative version of the prefix-for-object method, which is given
236-
* actual type to use (instead of using exact type of the value); typically
237-
* a super type of actual value type
255+
* DEPRECATED: now equivalent to:
256+
*{@code writeTypePrefix(g, typeId(value, type, JsonToken.START_OBJECT));}.
257+
* See {@link #writeTypePrefix} for more info.
258+
*
259+
* @deprecated Since 2.9 use {@link #writeTypePrefix(JsonGenerator, WritableTypeId) instead
238260
*/
239261
@Deprecated // since 2.9
240262
public void writeTypePrefixForObject(Object value, JsonGenerator g, Class<?> type) throws IOException {
241263
writeTypePrefix(g, typeId(value, type, JsonToken.START_OBJECT));
242264
}
243265

244266
/**
245-
* Alternative version of the prefix-for-array method, which is given
246-
* actual type to use (instead of using exact type of the value); typically
247-
* a super type of actual value type
267+
* DEPRECATED: now equivalent to:
268+
*{@code writeTypePrefix(g, typeId(value, type, JsonToken.START_ARRAY));}.
269+
* See {@link #writeTypePrefix} for more info.
270+
*
271+
* @deprecated Since 2.9 use {@link #writeTypePrefix(JsonGenerator, WritableTypeId) instead
248272
*/
249273
@Deprecated // since 2.9
250274
public void writeTypePrefixForArray(Object value, JsonGenerator g, Class<?> type) throws IOException {
@@ -257,31 +281,61 @@ public void writeTypePrefixForArray(Object value, JsonGenerator g, Class<?> type
257281
/**********************************************************
258282
*/
259283

284+
/**
285+
* DEPRECATED: now equivalent to:
286+
*{@code writeTypePrefix(g, typeId(value, JsonToken.VALUE_STRING, typeId));}.
287+
* See {@link #writeTypePrefix} for more info.
288+
*
289+
* @deprecated Since 2.9 use {@link #writeTypePrefix(JsonGenerator, WritableTypeId) instead
290+
*/
260291
@Deprecated // since 2.9
261292
public void writeCustomTypePrefixForScalar(Object value, JsonGenerator g, String typeId) throws IOException {
262293
writeTypePrefix(g, typeId(value, JsonToken.VALUE_STRING, typeId));
263294
}
264295

296+
/**
297+
* DEPRECATED: now equivalent to:
298+
*{@code writeTypePrefix(g, typeId(value, JsonToken.START_OBJECT, typeId));}.
299+
* See {@link #writeTypePrefix} for more info.
300+
*
301+
* @deprecated Since 2.9 use {@link #writeTypePrefix(JsonGenerator, WritableTypeId) instead
302+
*/
265303
@Deprecated // since 2.9
266304
public void writeCustomTypePrefixForObject(Object value, JsonGenerator g, String typeId) throws IOException {
267305
writeTypePrefix(g, typeId(value, JsonToken.START_OBJECT, typeId));
268306
}
269307

308+
/**
309+
* DEPRECATED: now equivalent to:
310+
*{@code writeTypePrefix(g, typeId(value, JsonToken.START_ARRAY, typeId));}.
311+
* See {@link #writeTypePrefix} for more info.
312+
*
313+
* @deprecated Since 2.9 use {@link #writeTypePrefix(JsonGenerator, WritableTypeId) instead
314+
*/
270315
@Deprecated // since 2.9
271316
public void writeCustomTypePrefixForArray(Object value, JsonGenerator g, String typeId) throws IOException {
272317
writeTypePrefix(g, typeId(value, JsonToken.START_ARRAY, typeId));
273318
}
274319

320+
/**
321+
* @deprecated Since 2.9 use {@link #writeTypeSuffix(JsonGenerator, WritableTypeId) instead
322+
*/
275323
@Deprecated // since 2.9
276324
public void writeCustomTypeSuffixForScalar(Object value, JsonGenerator g, String typeId) throws IOException {
277325
_writeLegacySuffix(g, typeId(value, JsonToken.VALUE_STRING, typeId));
278326
}
279327

328+
/**
329+
* @deprecated Since 2.9 use {@link #writeTypeSuffix(JsonGenerator, WritableTypeId) instead
330+
*/
280331
@Deprecated // since 2.9
281332
public void writeCustomTypeSuffixForObject(Object value, JsonGenerator g, String typeId) throws IOException {
282333
_writeLegacySuffix(g, typeId(value, JsonToken.START_OBJECT, typeId));
283334
}
284335

336+
/**
337+
* @deprecated Since 2.9 use {@link #writeTypeSuffix(JsonGenerator, WritableTypeId) instead
338+
*/
285339
@Deprecated // since 2.9
286340
public void writeCustomTypeSuffixForArray(Object value, JsonGenerator g, String typeId) throws IOException {
287341
_writeLegacySuffix(g, typeId(value, JsonToken.START_ARRAY, typeId));

0 commit comments

Comments
 (0)