Skip to content

Commit

Permalink
Support @JsonIgnoreProperties to work with @JsonValue (#4070)
Browse files Browse the repository at this point in the history
  • Loading branch information
JooHyukKim authored Oct 1, 2023
1 parent e03eaf0 commit ae77a0c
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 38 deletions.
13 changes: 13 additions & 0 deletions src/main/java/com/fasterxml/jackson/databind/JsonSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.IOException;
import java.util.Iterator;
import java.util.Set;

import com.fasterxml.jackson.core.*;

Expand Down Expand Up @@ -104,6 +105,18 @@ public JsonSerializer<?> withFilterId(Object filterId) {
return this;
}

/**
* Mutant factory method called to create a new instance after excluding specified set of
* properties by name, if there is any.
*
* @return Serializer instance that without specified set of properties to ignore (if any)
* @param ignoredProperties Set of property names to ignore for serialization;
* @since 2.16
*/
public JsonSerializer<?> withIgnoredProperties(Set<String> ignoredProperties) {
return this;
}

/*
/**********************************************************
/* Serialization methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ protected BeanSerializerBase withProperties(BeanPropertyWriter[] properties,
return new BeanSerializer(this, properties, filteredProperties);
}

@Override // @since 2.16
public JsonSerializer<?> withIgnoredProperties(Set<String> toIgnore) {
return new BeanSerializer(this, toIgnore, null);
}

/**
* Implementation has to check whether as-array serialization
* is possible reliably; if (and only if) so, will construct
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.LinkedHashSet;
import java.util.Set;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonTypeInfo.As;

import com.fasterxml.jackson.core.*;
Expand Down Expand Up @@ -418,6 +419,8 @@ protected JsonSerializer<Object> _findDynamicSerializer(SerializerProvider ctxt,
_dynamicSerializers = result.map;
} else {
serializer = ctxt.findPrimaryPropertySerializer(valueClass, _property);
// [databind#3647] : Support `@JsonIgnoreProperties` to work with `@JsonValue`
serializer = (JsonSerializer<Object>) _withIgnoreProperties(ctxt, serializer);
PropertySerializerMap.SerializerAndMapResult result = _dynamicSerializers.addSerializer(valueClass, serializer);
_dynamicSerializers = result.map;
}
Expand All @@ -444,6 +447,24 @@ protected JsonSerializer<Object> _findDynamicSerializer(SerializerProvider ctxt,
*/
}

/**
* Internal helper that configures the provided {@code ser} to ignore properties specified by {@link JsonIgnoreProperties}.
*
* @param ctxt For introspection.
* @param ser Serializer to be configured
* @return Configured serializer with specified properties ignored
* @since 2.16
*/
private JsonSerializer<?> _withIgnoreProperties(SerializerProvider ctxt, JsonSerializer<?> ser) {
final AnnotationIntrospector ai = ctxt.getAnnotationIntrospector();
final SerializationConfig config = ctxt.getConfig();

JsonIgnoreProperties.Value ignorals = ai.findPropertyIgnoralByName(config, _accessor);
Set<String> ignored = ignorals.findIgnoredForSerialization();
ser = ser.withIgnoredProperties(ignored);
return ser;
}

/*
/**********************************************************
/* Standard method overrides
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package com.fasterxml.jackson.databind.ser.filter;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonValue;
import com.fasterxml.jackson.databind.BaseMapTest;
import com.fasterxml.jackson.databind.ObjectMapper;

// [databind#3647] : Support @JsonIgnoreProperties to work with @JsonValue
public class JsonValueIgnore3647Test extends BaseMapTest
{
static class Foo3647 {
public String p1 = "hello";
public String p2 = "world";
}

static class Bar3647 {
@JsonValue
@JsonIgnoreProperties("p1")
public Foo3647 getFoo() {
return new Foo3647();
}
}

@JsonIgnoreProperties({"a"})
static class Bean3647 {
public String a = "hello";
public String b = "world";
}

static class Container3647 {
@JsonValue
@JsonIgnoreProperties("b")
public Bean3647 getBean() {
return new Bean3647();
}
}

static class Base3647 {
public String a = "hello";
public String b = "world";
}

static class BaseContainer3647 {
@JsonValue
public Base3647 getBean() {
return new Base3647();
}
}

static class MixinContainer3647 {
@JsonIgnoreProperties("b")
public Base3647 getBean() {
return new Base3647();
}
}

@JsonIgnoreProperties({"a", "b"})
static class Mixin3647 {
public String a = "hello";
public String b = "world";
}

/*
/**********************************************************************
/* Test methods
/**********************************************************************
*/

private final ObjectMapper MAPPER = newJsonMapper();

public void testIgnorePropsAndJsonValueAtSameLevel() throws Exception
{
assertEquals("{\"p2\":\"world\"}",
MAPPER.writeValueAsString(new Bar3647()));
}

public void testUnionOfIgnorals() throws Exception
{
assertEquals("{}",
MAPPER.writeValueAsString(new Container3647()));
}

public void testMixinContainerAndJsonValue() throws Exception
{
ObjectMapper mapper = jsonMapperBuilder()
.addMixIn(BaseContainer3647.class, MixinContainer3647.class)
.build();

assertEquals("{\"a\":\"hello\"}",
mapper.writeValueAsString(new BaseContainer3647()));
}

public void testMixinAndJsonValue() throws Exception
{
ObjectMapper mapper = jsonMapperBuilder()
.addMixIn(Base3647.class, Mixin3647.class)
.build();

assertEquals("{}",
mapper.writeValueAsString(new Base3647()));
}
}

This file was deleted.

0 comments on commit ae77a0c

Please sign in to comment.