diff --git a/src/main/java/com/fasterxml/jackson/core/JsonGenerator.java b/src/main/java/com/fasterxml/jackson/core/JsonGenerator.java index 58437fadda..cee202f465 100644 --- a/src/main/java/com/fasterxml/jackson/core/JsonGenerator.java +++ b/src/main/java/com/fasterxml/jackson/core/JsonGenerator.java @@ -1969,16 +1969,12 @@ public void writeTypeId(Object id) throws IOException { */ public WritableTypeId writeTypePrefix(WritableTypeId typeIdDef) throws IOException { - // First: is this a native type id? If so, just use write method, be done - if (canWriteTypeId()) { - // just rely on native type output method (sub-classes likely to override) - return _writeNativeTypePrefix(typeIdDef); - } - - // No native type id; write wrappers - final boolean wasStartObjectWritten = _writeTypePrefixWrapper(typeIdDef); + // Are native type ids allowed? If so, use them; otherwise, use wrappers + final boolean wasStartObjectWritten = canWriteTypeId() + ? _writeTypePrefixUsingNative(typeIdDef) + : _writeTypePrefixUsingWrapper(typeIdDef); - // and finally possible start marker for value itself: + // And then possible start marker for value itself: switch (typeIdDef.valueShape) { case START_OBJECT: if (!wasStartObjectWritten) { @@ -1986,8 +1982,7 @@ public WritableTypeId writeTypePrefix(WritableTypeId typeIdDef) throws IOExcepti } break; case START_ARRAY: - // should we now set the current object? - writeStartArray(); + writeStartArray(typeIdDef.forValue); break; default: // otherwise: no start marker } @@ -1995,21 +1990,27 @@ public WritableTypeId writeTypePrefix(WritableTypeId typeIdDef) throws IOExcepti return typeIdDef; } - // @since 2.19 - protected WritableTypeId _writeNativeTypePrefix(WritableTypeId typeIdDef) throws IOException { + /** + * Writes a native type id (when supported by format). + * + * @return True if start of an object has been written, False otherwise. + * + * @since 2.19 + */ + protected boolean _writeTypePrefixUsingNative(WritableTypeId typeIdDef) throws IOException { typeIdDef.wrapperWritten = false; writeTypeId(typeIdDef.id); - return typeIdDef; + return false; } /** - * Writes a wrapper for the type id; if necessary. + * Writes a wrapper for the type id if necessary. * - * @return True if start of an object has been written, False otherwise. + * @return True if start of an object has been written, false otherwise. * * @since 2.19 */ - protected boolean _writeTypePrefixWrapper(WritableTypeId typeIdDef) throws IOException { + protected boolean _writeTypePrefixUsingWrapper(WritableTypeId typeIdDef) throws IOException { // Normally we only support String type ids (non-String reserved for native type ids) final String id = Objects.toString(typeIdDef.id, null); @@ -2044,7 +2045,8 @@ protected boolean _writeTypePrefixWrapper(WritableTypeId typeIdDef) throws IOExc return true; case WRAPPER_OBJECT: - // NOTE: this is wrapper, not directly related to value to output, so don't pass + // NOTE: this is wrapper, not directly related to value to output, so + // do NOT pass "typeIdDef.forValue" writeStartObject(); writeFieldName(id); break;