|
8 | 8 | import com.fasterxml.jackson.core.*;
|
9 | 9 | import com.fasterxml.jackson.core.type.WritableTypeId;
|
10 | 10 | import com.fasterxml.jackson.databind.*;
|
| 11 | +import com.fasterxml.jackson.databind.cfg.JsonNodeFeature; |
11 | 12 | import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
|
12 | 13 | import com.fasterxml.jackson.databind.util.RawValue;
|
13 | 14 |
|
@@ -302,59 +303,87 @@ public List<JsonNode> findParents(String propertyName, List<JsonNode> foundSoFar
|
302 | 303 | * Method that can be called to serialize this node and
|
303 | 304 | * all of its descendants using specified JSON generator.
|
304 | 305 | */
|
| 306 | + @SuppressWarnings("deprecation") |
305 | 307 | @Override
|
306 | 308 | public void serialize(JsonGenerator g, SerializerProvider provider)
|
307 | 309 | throws IOException
|
308 | 310 | {
|
309 |
| - @SuppressWarnings("deprecation") |
310 |
| - boolean trimEmptyArray = (provider != null) && |
311 |
| - !provider.isEnabled(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS); |
| 311 | + if (provider != null) { |
| 312 | + boolean trimEmptyArray = !provider.isEnabled(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS); |
| 313 | + boolean skipNulls = !provider.isEnabled(JsonNodeFeature.WRITE_NULL_PROPERTIES); |
| 314 | + if (trimEmptyArray || skipNulls) { |
| 315 | + g.writeStartObject(this); |
| 316 | + serializeFilteredContents(g, provider, trimEmptyArray, skipNulls); |
| 317 | + g.writeEndObject(); |
| 318 | + return; |
| 319 | + } |
| 320 | + } |
312 | 321 | g.writeStartObject(this);
|
313 | 322 | for (Map.Entry<String, JsonNode> en : _children.entrySet()) {
|
314 |
| - /* 17-Feb-2009, tatu: Can we trust that all nodes will always |
315 |
| - * extend BaseJsonNode? Or if not, at least implement |
316 |
| - * JsonSerializable? Let's start with former, change if |
317 |
| - * we must. |
318 |
| - */ |
319 |
| - BaseJsonNode value = (BaseJsonNode) en.getValue(); |
320 |
| - |
321 |
| - // as per [databind#867], see if WRITE_EMPTY_JSON_ARRAYS feature is disabled, |
322 |
| - // if the feature is disabled, then should not write an empty array |
323 |
| - // to the output, so continue to the next element in the iteration |
324 |
| - if (trimEmptyArray && value.isArray() && value.isEmpty(provider)) { |
325 |
| - continue; |
326 |
| - } |
| 323 | + JsonNode value = en.getValue(); |
327 | 324 | g.writeFieldName(en.getKey());
|
328 | 325 | value.serialize(g, provider);
|
329 | 326 | }
|
330 | 327 | g.writeEndObject();
|
331 | 328 | }
|
332 | 329 |
|
| 330 | + @SuppressWarnings("deprecation") |
333 | 331 | @Override
|
334 | 332 | public void serializeWithType(JsonGenerator g, SerializerProvider provider,
|
335 | 333 | TypeSerializer typeSer)
|
336 | 334 | throws IOException
|
337 | 335 | {
|
338 |
| - @SuppressWarnings("deprecation") |
339 |
| - boolean trimEmptyArray = (provider != null) && |
340 |
| - !provider.isEnabled(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS); |
| 336 | + boolean trimEmptyArray = false; |
| 337 | + boolean skipNulls = false; |
| 338 | + if (provider != null) { |
| 339 | + trimEmptyArray = !provider.isEnabled(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS); |
| 340 | + skipNulls = !provider.isEnabled(JsonNodeFeature.WRITE_NULL_PROPERTIES); |
| 341 | + } |
341 | 342 |
|
342 | 343 | WritableTypeId typeIdDef = typeSer.writeTypePrefix(g,
|
343 | 344 | typeSer.typeId(this, JsonToken.START_OBJECT));
|
| 345 | + |
| 346 | + if (trimEmptyArray || skipNulls) { |
| 347 | + serializeFilteredContents(g, provider, trimEmptyArray, skipNulls); |
| 348 | + } else { |
| 349 | + for (Map.Entry<String, JsonNode> en : _children.entrySet()) { |
| 350 | + JsonNode value = en.getValue(); |
| 351 | + g.writeFieldName(en.getKey()); |
| 352 | + value.serialize(g, provider); |
| 353 | + } |
| 354 | + } |
| 355 | + typeSer.writeTypeSuffix(g, typeIdDef); |
| 356 | + } |
| 357 | + |
| 358 | + /** |
| 359 | + * Helper method shared and called by {@link #serialize} and {@link #serializeWithType} |
| 360 | + * in cases where actual filtering is needed based on configuration. |
| 361 | + * |
| 362 | + * @since 2.14 |
| 363 | + */ |
| 364 | + protected void serializeFilteredContents(final JsonGenerator g, final SerializerProvider provider, |
| 365 | + final boolean trimEmptyArray, final boolean skipNulls) |
| 366 | + throws IOException |
| 367 | + { |
344 | 368 | for (Map.Entry<String, JsonNode> en : _children.entrySet()) {
|
| 369 | + // 17-Feb-2009, tatu: Can we trust that all nodes will always |
| 370 | + // extend BaseJsonNode? Or if not, at least implement |
| 371 | + // JsonSerializable? Let's start with former, change if |
| 372 | + // we must. |
345 | 373 | BaseJsonNode value = (BaseJsonNode) en.getValue();
|
346 | 374 |
|
347 |
| - // check if WRITE_EMPTY_JSON_ARRAYS feature is disabled, |
| 375 | + // as per [databind#867], see if WRITE_EMPTY_JSON_ARRAYS feature is disabled, |
348 | 376 | // if the feature is disabled, then should not write an empty array
|
349 | 377 | // to the output, so continue to the next element in the iteration
|
350 | 378 | if (trimEmptyArray && value.isArray() && value.isEmpty(provider)) {
|
| 379 | + continue; |
| 380 | + } |
| 381 | + if (skipNulls && value.isNull()) { |
351 | 382 | continue;
|
352 | 383 | }
|
353 |
| - |
354 | 384 | g.writeFieldName(en.getKey());
|
355 | 385 | value.serialize(g, provider);
|
356 | 386 | }
|
357 |
| - typeSer.writeTypeSuffix(g, typeIdDef); |
358 | 387 | }
|
359 | 388 |
|
360 | 389 | /*
|
|
0 commit comments