Skip to content

Support @Json.Property Methods #177

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.avaje.jsonb.generator;

import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Modifier;
import java.util.*;

Expand All @@ -24,18 +25,23 @@ final class FieldReader {
addSubType(subType);
final PropertyIgnoreReader ignoreReader = new PropertyIgnoreReader(element);
this.unmapped = ignoreReader.unmapped();
var isMethod = element instanceof ExecutableElement;
this.raw = ignoreReader.raw();
this.serialize = ignoreReader.serialize();
this.deserialize = ignoreReader.deserialize();
this.deserialize = !isMethod && ignoreReader.deserialize();

final var fieldName = element.getSimpleName().toString();
final var publicField = element.getModifiers().contains(Modifier.PUBLIC);
this.property = new FieldProperty(element.asType(), raw, unmapped, genericTypeParams, publicField, fieldName);
final var publicField = !isMethod && element.getModifiers().contains(Modifier.PUBLIC);

var type = isMethod ? ((ExecutableElement) element).getReturnType() : element.asType();

this.property =
new FieldProperty(type, raw, unmapped, genericTypeParams, publicField, fieldName);
this.propertyName =
PropertyPrism.getOptionalOn(element)
.map(PropertyPrism::value)
.map(Util::escapeQuotes)
.orElse(namingConvention.from(fieldName));
PropertyPrism.getOptionalOn(element)
.map(PropertyPrism::value)
.map(Util::escapeQuotes)
.orElse(namingConvention.from(fieldName));

this.aliases = initAliases(element);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ void read(TypeElement type) {
readField(element, localFields);
break;
case METHOD:
readMethod(element, type);
readMethod(element, type, localFields);
break;
}
}
Expand Down Expand Up @@ -169,7 +169,7 @@ private void readConstructor(Element element, TypeElement type) {
}
}

private void readMethod(Element element, TypeElement type) {
private void readMethod(Element element, TypeElement type, List<FieldReader> localFields) {
ExecutableElement methodElement = (ExecutableElement) element;
if (checkMethod2(methodElement)) {
List<? extends VariableElement> parameters = methodElement.getParameters();
Expand All @@ -186,6 +186,24 @@ private void readMethod(Element element, TypeElement type) {
}
}
}
// for reading methods
PropertyPrism.getOptionalOn(methodElement)
.ifPresent(
p -> {
if (!methodElement.getParameters().isEmpty()) {
logError("Json.Property can only be placed on Getter Methods", methodElement);
return;
}

final var frequency =
frequencyMap.compute(p.value(), (k, v) -> v == null ? 0 : v + 1);

final var reader =
new FieldReader(
element, namingConvention, currentSubType, genericTypeParams, frequency);
localFields.add(reader);
reader.getterMethod(new MethodReader(methodElement, type));
});
}

private boolean checkMethod2(ExecutableElement methodElement) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ public class TestClass {

private Optional<String> op;

@Json.Property("what_a_save!")
public String what_a_save() {
return "Chat Disabled";
}

public String getAlias() {
return alias;
}
Expand Down
8 changes: 4 additions & 4 deletions jsonb/src/main/java/io/avaje/jsonb/Json.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
* }</pre>
*/
@Retention(CLASS)
@Target(FIELD)
@Target({FIELD, METHOD})
@interface Property {

/**
Expand Down Expand Up @@ -138,7 +138,7 @@
}

/**
* Deprecate - migrate to {@link Json.Alias}.
* @deprecated - migrate to {@link Json.Alias}.
*/
@Deprecated
@Retention(CLASS)
Expand Down Expand Up @@ -188,7 +188,7 @@
* }</pre>
*/
@Retention(CLASS)
@Target(FIELD)
@Target({FIELD, METHOD})
@interface Unmapped {

}
Expand Down Expand Up @@ -278,7 +278,7 @@
* Marks a String field as containing raw JSON content.
*/
@Retention(CLASS)
@Target(FIELD)
@Target({FIELD, METHOD})
@interface Raw {

}
Expand Down