-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
246 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
...n/java/com/infobip/jackson/SingleArgumentPropertiesCreatorModeAnnotationIntrospector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.infobip.jackson; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.databind.cfg.MapperConfig; | ||
import com.fasterxml.jackson.databind.introspect.*; | ||
|
||
import java.lang.reflect.Field; | ||
import java.util.Objects; | ||
import java.util.stream.Stream; | ||
|
||
public class SingleArgumentPropertiesCreatorModeAnnotationIntrospector extends NopAnnotationIntrospector { | ||
|
||
@Override | ||
public JsonCreator.Mode findCreatorAnnotation(MapperConfig<?> config, Annotated a) { | ||
if (!(a instanceof AnnotatedConstructor)) { | ||
return null; | ||
} | ||
|
||
AnnotatedConstructor annotatedConstructor = (AnnotatedConstructor) a; | ||
|
||
if (annotatedConstructor.getParameterCount() != 1) { | ||
return null; | ||
} | ||
|
||
Class<?> declaringClass = annotatedConstructor.getDeclaringClass(); | ||
|
||
if (declaringClass.getDeclaredConstructors().length > 1) { | ||
return null; | ||
} | ||
|
||
if (doesntHaveMatchingFieldAndParameter(annotatedConstructor, declaringClass)) { | ||
return null; | ||
} | ||
|
||
return JsonCreator.Mode.PROPERTIES; | ||
} | ||
|
||
private boolean doesntHaveMatchingFieldAndParameter(AnnotatedConstructor annotatedConstructor, | ||
Class<?> declaringClass) { | ||
String parameterName = annotatedConstructor.getAnnotated().getParameters()[0].getName(); | ||
return Stream.of(declaringClass.getDeclaredFields()) | ||
.noneMatch(field -> doesFieldMatchParameter(field, parameterName)); | ||
} | ||
|
||
private boolean doesFieldMatchParameter(Field field, String parameterName) { | ||
|
||
JsonProperty annotation = field.getAnnotation(JsonProperty.class); | ||
|
||
if (Objects.nonNull(annotation) && annotation.value().equals(parameterName)) { | ||
return true; | ||
} | ||
|
||
return field.getName().equals(parameterName); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.