Skip to content

Commit 5a1d8b8

Browse files
authored
Fix #5103: pass Object size to writeStartObject() for ObjectNode serialization (#5118)
1 parent 4d20831 commit 5a1d8b8

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

release-notes/VERSION-2.x

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Project: jackson-databind
88

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

1214
2.19.0 (24-Apr-2025)
1315

src/main/java/com/fasterxml/jackson/databind/node/ObjectNode.java

+10-10
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,6 @@ protected ArrayNode _withArrayAddTailProperty(JsonPointer tail, boolean preferIn
233233
return putObject(propName)._withArrayAddTailProperty(tail, preferIndex);
234234
}
235235

236-
237236
/*
238237
/**********************************************************
239238
/* Overrides for JsonSerializable.Base
@@ -489,24 +488,25 @@ public List<JsonNode> findParents(String propertyName, List<JsonNode> foundSoFar
489488
*/
490489
@SuppressWarnings("deprecation")
491490
@Override
492-
public void serialize(JsonGenerator g, SerializerProvider provider)
491+
public void serialize(JsonGenerator g, SerializerProvider ctxt)
493492
throws IOException
494493
{
495-
if (provider != null) {
496-
boolean trimEmptyArray = !provider.isEnabled(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS);
497-
boolean skipNulls = !provider.isEnabled(JsonNodeFeature.WRITE_NULL_PROPERTIES);
494+
if (ctxt != null) {
495+
boolean trimEmptyArray = !ctxt.isEnabled(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS);
496+
boolean skipNulls = !ctxt.isEnabled(JsonNodeFeature.WRITE_NULL_PROPERTIES);
498497
if (trimEmptyArray || skipNulls) {
499498
g.writeStartObject(this);
500-
serializeFilteredContents(g, provider, trimEmptyArray, skipNulls);
499+
serializeFilteredContents(g, ctxt, trimEmptyArray, skipNulls);
501500
g.writeEndObject();
502501
return;
503502
}
504503
}
505-
g.writeStartObject(this);
506-
for (Map.Entry<String, JsonNode> en : _contentsToSerialize(provider).entrySet()) {
507-
JsonNode value = en.getValue();
504+
Map<String, JsonNode> contents = _contentsToSerialize(ctxt);
505+
// 25-Apr-2025, tatu: [databind#5103] Pass size (some formats can optimize)
506+
g.writeStartObject(this, contents.size());
507+
for (Map.Entry<String, JsonNode> en : contents.entrySet()) {
508508
g.writeFieldName(en.getKey());
509-
value.serialize(g, provider);
509+
en.getValue().serialize(g, ctxt);
510510
}
511511
g.writeEndObject();
512512
}

0 commit comments

Comments
 (0)