Skip to content

Fix #5103: pass Object size to writeStartObject() for ObjectNode serialization #5118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Project: jackson-databind

#4136: Drop deprecated (in 2.12) `PropertyNamingStrategy` implementations
from 2.20
#5103: Use `writeStartObject(Object forValue, int size)` for `ObjectNode`
serialization

2.19.0 (24-Apr-2025)

Expand Down
20 changes: 10 additions & 10 deletions src/main/java/com/fasterxml/jackson/databind/node/ObjectNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,6 @@ protected ArrayNode _withArrayAddTailProperty(JsonPointer tail, boolean preferIn
return putObject(propName)._withArrayAddTailProperty(tail, preferIndex);
}


/*
/**********************************************************
/* Overrides for JsonSerializable.Base
Expand Down Expand Up @@ -489,24 +488,25 @@ public List<JsonNode> findParents(String propertyName, List<JsonNode> foundSoFar
*/
@SuppressWarnings("deprecation")
@Override
public void serialize(JsonGenerator g, SerializerProvider provider)
public void serialize(JsonGenerator g, SerializerProvider ctxt)
throws IOException
{
if (provider != null) {
boolean trimEmptyArray = !provider.isEnabled(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS);
boolean skipNulls = !provider.isEnabled(JsonNodeFeature.WRITE_NULL_PROPERTIES);
if (ctxt != null) {
boolean trimEmptyArray = !ctxt.isEnabled(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS);
boolean skipNulls = !ctxt.isEnabled(JsonNodeFeature.WRITE_NULL_PROPERTIES);
if (trimEmptyArray || skipNulls) {
g.writeStartObject(this);
serializeFilteredContents(g, provider, trimEmptyArray, skipNulls);
serializeFilteredContents(g, ctxt, trimEmptyArray, skipNulls);
g.writeEndObject();
return;
}
}
g.writeStartObject(this);
for (Map.Entry<String, JsonNode> en : _contentsToSerialize(provider).entrySet()) {
JsonNode value = en.getValue();
Map<String, JsonNode> contents = _contentsToSerialize(ctxt);
// 25-Apr-2025, tatu: [databind#5103] Pass size (some formats can optimize)
g.writeStartObject(this, contents.size());
for (Map.Entry<String, JsonNode> en : contents.entrySet()) {
g.writeFieldName(en.getKey());
value.serialize(g, provider);
en.getValue().serialize(g, ctxt);
}
g.writeEndObject();
}
Expand Down