Skip to content

Commit

Permalink
Revert my earlier misguided simplification (need START_OBJECT/ARRAY f…
Browse files Browse the repository at this point in the history
…or values, with native Type Ids too)
  • Loading branch information
cowtowncoder committed Nov 9, 2024
1 parent 201ed2b commit a5f17c3
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions src/main/java/com/fasterxml/jackson/core/JsonGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -1969,47 +1969,48 @@ 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) {
writeStartObject(typeIdDef.forValue);
}
break;
case START_ARRAY:
// should we now set the current object?
writeStartArray();
writeStartArray(typeIdDef.forValue);
break;
default: // otherwise: no start marker
}

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);

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit a5f17c3

Please sign in to comment.