Skip to content

Commit

Permalink
Add release notes; streamline code; minor logic changes (since I'm fa…
Browse files Browse the repository at this point in the history
…miliar with intent)
  • Loading branch information
cowtowncoder committed Nov 8, 2024
1 parent c6bc936 commit 201ed2b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
5 changes: 5 additions & 0 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -456,3 +456,8 @@ Jared Stehler (@jaredstehler)
Zhanghao (@zhangOranges)
* Contributed #1305: Make helper methods of `WriterBasedJsonGenerator` non-final to allow overriding
(2.18.0)

Eduard Gomoliako (@Gems)
* Contributed #1356: Make `JsonGenerator::writeTypePrefix` method to not write a
`WRAPPER_ARRAY` when `typeIdDef.id == null`
(2.19.0)
3 changes: 3 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ a pure JSON library.
2.19.0 (not yet released)

#1328: Optimize handling of `JsonPointer.head()`
#1356: Make `JsonGenerator::writeTypePrefix` method to not write a
`WRAPPER_ARRAY` when `typeIdDef.id == null`
(contributed by Eduard G)

2.18.1 (28-Oct-2024)

Expand Down
29 changes: 18 additions & 11 deletions src/main/java/com/fasterxml/jackson/core/JsonGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -1969,39 +1969,47 @@ public void writeTypeId(Object id) throws IOException {
*/
public WritableTypeId writeTypePrefix(WritableTypeId typeIdDef) throws IOException
{
final boolean hasStartObjectWritten = canWriteTypeId()
// just rely on native type output method (sub-classes likely to override)
? writeTypePrefixWithTypeId(typeIdDef)
// No native type id; write wrappers
: writeTypePrefixWrapper(typeIdDef);
// 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);

// and finally possible start marker for value itself:
switch (typeIdDef.valueShape) {
case START_OBJECT:
if (!hasStartObjectWritten)
if (!wasStartObjectWritten) {
writeStartObject(typeIdDef.forValue);
}
break;
case START_ARRAY:
// should we now set the current object?
writeStartArray();
break;
default: // otherwise: no start marker
}

return typeIdDef;
}

private boolean writeTypePrefixWithTypeId(WritableTypeId typeIdDef) throws IOException {
// @since 2.19
protected WritableTypeId _writeNativeTypePrefix(WritableTypeId typeIdDef) throws IOException {
typeIdDef.wrapperWritten = false;
writeTypeId(typeIdDef.id);

return false;
return typeIdDef;
}

/**
* Writes a wrapper for the type id; if necessary.
*
* @return True if start of an object has been written, False otherwise.
*
* @since 2.19
*/
private boolean writeTypePrefixWrapper(WritableTypeId typeIdDef) throws IOException {
protected boolean _writeTypePrefixWrapper(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 All @@ -2017,7 +2025,6 @@ private boolean writeTypePrefixWrapper(WritableTypeId typeIdDef) throws IOExcept
typeIdDef.include = incl = WritableTypeId.Inclusion.WRAPPER_ARRAY;
}


typeIdDef.wrapperWritten = true;

switch (incl) {
Expand Down

0 comments on commit 201ed2b

Please sign in to comment.