Skip to content

Commit

Permalink
Implemented first part of #1376, allow disabling of "any getter"
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Nov 22, 2016
1 parent d0daf23 commit 6155a19
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,7 @@ public PropertyName findNameForSerialization(Annotated a) {
*
* @since 2.9
*/
public Boolean findAsValueAnnotation(Annotated a) {
public Boolean hasAsValue(Annotated a) {
// 20-Nov-2016, tatu: Delegate in 2.9; remove redirect from later versions
if (a instanceof AnnotatedMethod) {
if (hasAsValueAnnotation((AnnotatedMethod) a)) {
Expand All @@ -873,14 +873,6 @@ public Boolean findAsValueAnnotation(Annotated a) {
return null;
}

/**
* @deprecated Since 2.9 Use {@link #findAsValueAnnotation(Annotated)} instead.
*/
@Deprecated // since 2.9
public boolean hasAsValueAnnotation(AnnotatedMethod am) {
return false;
}

/**
* Method for checking whether given method has an annotation
* that suggests that the method is to serve as "any setter";
Expand All @@ -889,9 +881,18 @@ public boolean hasAsValueAnnotation(AnnotatedMethod am) {
*
* @return True if such annotation is found (and is not disabled),
* false otherwise
*
* @since 2.9
*/
public boolean hasAnyGetterAnnotation(AnnotatedMethod am) {
return false;
public Boolean hasAnyGetter(Annotated a) {

// 21-Nov-2016, tatu: Delegate in 2.9; remove redirect from later versions
if (a instanceof AnnotatedMethod) {
if (hasAnyGetterAnnotation((AnnotatedMethod) a)) {
return true;
}
}
return null;
}

/**
Expand Down Expand Up @@ -939,6 +940,22 @@ public String findEnumValue(Enum<?> value) {
return value.name();
}

/**
* @deprecated Since 2.9 Use {@link #hasAsValue(Annotated)} instead.
*/
@Deprecated // since 2.9
public boolean hasAsValueAnnotation(AnnotatedMethod am) {
return false;
}

/**
* @deprecated Since 2.9 Use {@link #hasAnyGetter} instead
*/
@Deprecated
public boolean hasAnyGetterAnnotation(AnnotatedMethod am) {
return false;
}

/*
/**********************************************************
/* Deserialization: general annotations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -565,17 +565,21 @@ public PropertyName findNameForSerialization(Annotated a) {
}

@Override
public Boolean findAsValueAnnotation(Annotated a) {
Boolean b = _primary.findAsValueAnnotation(a);
public Boolean hasAsValue(Annotated a) {
Boolean b = _primary.hasAsValue(a);
if (b == null) {
b = _secondary.findAsValueAnnotation(a);
b = _secondary.hasAsValue(a);
}
return b;
}

@Override
public boolean hasAnyGetterAnnotation(AnnotatedMethod am) {
return _primary.hasAnyGetterAnnotation(am) || _secondary.hasAnyGetterAnnotation(am);
public Boolean hasAnyGetter(Annotated a) {
Boolean b = _primary.hasAnyGetter(a);
if (b == null) {
b = _secondary.hasAnyGetter(a);
}
return b;
}

@Override
Expand Down Expand Up @@ -605,6 +609,12 @@ public boolean hasAsValueAnnotation(AnnotatedMethod am) {
return _primary.hasAsValueAnnotation(am) || _secondary.hasAsValueAnnotation(am);
}

@Override
@Deprecated // since 2.9
public boolean hasAnyGetterAnnotation(AnnotatedMethod am) {
return _primary.hasAnyGetterAnnotation(am) || _secondary.hasAnyGetterAnnotation(am);
}

// // // Deserialization: general annotations

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -972,16 +972,26 @@ public PropertyName findNameForSerialization(Annotated a)
return null;
}

@Override
public Boolean findAsValueAnnotation(Annotated a) {
@Override // since 2.9
public Boolean hasAsValue(Annotated a) {
JsonValue ann = _findAnnotation(a, JsonValue.class);
if (ann == null) {
return null;
}
return ann.value();
}

@Override // since 2.9
public Boolean hasAnyGetter(Annotated a) {
JsonAnyGetter ann = _findAnnotation(a, JsonAnyGetter.class);
if (ann == null) {
return null;
}
return ann.enabled();
}

@Override
@Deprecated // since 2.9
public boolean hasAnyGetterAnnotation(AnnotatedMethod am)
{
// No dedicated disabling; regular @JsonIgnore used if needs to be ignored (handled separately)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ protected void _addFields(Map<String, POJOPropertyBuilder> props)
} else {
implName = ai.findImplicitPropertyName(f);
// @JsonValue?
if (Boolean.TRUE.equals(ai.findAsValueAnnotation(f))) {
if (Boolean.TRUE.equals(ai.hasAsValue(f))) {
if (_jsonValueAccessors == null) {
_jsonValueAccessors = new LinkedList<>();
}
Expand Down Expand Up @@ -570,15 +570,16 @@ protected void _addGetterMethod(Map<String, POJOPropertyBuilder> props,

// any getter?
if (ai != null) {
if (ai.hasAnyGetterAnnotation(m)) {
// @JsonAnyGetter?
if (Boolean.TRUE.equals(ai.hasAnyGetter(m))) {
if (_anyGetters == null) {
_anyGetters = new LinkedList<AnnotatedMember>();
}
_anyGetters.add(m);
return;
}
// @JsonValue?
if (Boolean.TRUE.equals(ai.findAsValueAnnotation(m))) {
if (Boolean.TRUE.equals(ai.hasAsValue(m))) {
if (_jsonValueAccessors == null) {
_jsonValueAccessors = new LinkedList<>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ public Map<String,Integer> any() {
}
}

// For [databind#1376]: allow disabling any-getter
static class NotEvenAnyBean extends AnyOnlyBean
{
@JsonAnyGetter(enabled=false)
@Override
public Map<String,Integer> any() {
throw new RuntimeException("Should not get called!)");
}

public int getValue() { return 42; }
}

static class MapAsAny
{
protected Map<String,Object> stuff = new LinkedHashMap<String,Object>();
Expand Down Expand Up @@ -127,7 +139,7 @@ public void serialize(String value, JsonGenerator gen,

private final ObjectMapper MAPPER = new ObjectMapper();

public void testSimpleJsonValue() throws Exception
public void testSimpleAnyBean() throws Exception
{
String json = MAPPER.writeValueAsString(new Bean());
Map<?,?> map = MAPPER.readValue(json, Map.class);
Expand All @@ -153,6 +165,12 @@ public void testAnyOnly() throws Exception
assertEquals("{\"a\":3}", json);
}

public void testAnyDisabling() throws Exception
{
String json = MAPPER.writeValueAsString(new NotEvenAnyBean());
assertEquals(aposToQuotes("{'value':42}"), json);
}

// Trying to repro [databind#577]
public void testAnyWithNull() throws Exception
{
Expand Down

0 comments on commit 6155a19

Please sign in to comment.