Skip to content

Commit

Permalink
#177 followup - add test, format and tidy internals
Browse files Browse the repository at this point in the history
Also exclude include fields in fromJson() that are not
used due to being read-only json properties
  • Loading branch information
rbygrave committed Oct 29, 2023
1 parent f09c2d2 commit 5271859
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,11 @@ public record WithName(
String simplePlus,
int myOneRed) {

/**
* A read-only JSON property without an underlying field.
*/
@Json.Property("derived")
public int calculated() {
return myOneRed * 2;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void toFrom() {
WithName bean = new WithName("sim", "simPlus", 42);

String asJson = jsonType.toJson(bean);
assertThat(asJson).isEqualTo("{\"Some Thing Odd\":\"sim\",\"simple-plus\":\"simPlus\",\"my-one-red\":42}");
assertThat(asJson).isEqualTo("{\"Some Thing Odd\":\"sim\",\"simple-plus\":\"simPlus\",\"my-one-red\":42,\"derived\":84}");

WithName fromJson = jsonType.fromJson(asJson);
assertEquals(fromJson, bean);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -424,10 +424,12 @@ private void writeFromJsonImplementation(Append writer, String varName) {
} else {
writer.append(" // variables to read json values into, constructor params don't need _set$ flags").eol();
for (final FieldReader allField : allFields) {
if (isRecord) {
allField.writeFromJsonVariablesRecord(writer);
} else if (allField.includeFromJson()) {
allField.writeFromJsonVariables(writer);
if (allField.includeFromJson()) {
if (isRecord) {
allField.writeFromJsonVariablesRecord(writer);
} else {
allField.writeFromJsonVariables(writer);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,13 @@ final class FieldReader {

final var fieldName = element.getSimpleName().toString();
final var publicField = !isMethod && element.getModifiers().contains(Modifier.PUBLIC);
final var type = isMethod ? ((ExecutableElement) element).getReturnType() : element.asType();

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));
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));

this.aliases = initAliases(element);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,9 @@ final class TypeReader {
if (mixInType == null) {
this.mixInFields = new HashMap<>();
} else {
this.mixInFields =
mixInType.getEnclosedElements().stream()
.filter(e -> e.getKind() == ElementKind.FIELD)
.collect(Collectors.toMap(e -> e.getSimpleName().toString(), e -> e));
this.mixInFields = mixInType.getEnclosedElements().stream()
.filter(e -> e.getKind() == ElementKind.FIELD)
.collect(Collectors.toMap(e -> e.getSimpleName().toString(), e -> e));
}
this.namingConvention = namingConvention;
this.hasJsonAnnotation = JsonPrism.isPresent(baseType) || importedJson(baseType).isPresent();
Expand Down Expand Up @@ -129,11 +128,18 @@ private void readField(Element element, List<FieldReader> localFields) {
optional = true;
}
if (includeField(element)) {
final var frequency = frequencyMap.compute(element.getSimpleName().toString(), (k, v) -> v == null ? 0 : v + 1);
final var frequency = frequency(element.getSimpleName().toString());
localFields.add(new FieldReader(element, namingConvention, currentSubType, genericTypeParams, frequency));
}
}

/**
* Compute and return the frequency of the key / json property name.
*/
private Integer frequency(String key) {
return frequencyMap.compute(key, (k, v) -> v == null ? 0 : v + 1);
}

private boolean includeField(Element element) {
if (extendsThrowable) {
return !element.getModifiers().contains(Modifier.TRANSIENT)
Expand Down Expand Up @@ -186,24 +192,19 @@ private void readMethod(Element element, TypeElement type, List<FieldReader> loc
}
}
}
// 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));
});
// for getter/accessor methods only, not setters
PropertyPrism.getOptionalOn(methodElement).ifPresent(propertyPrism -> {
if (!methodElement.getParameters().isEmpty()) {
logError("Json.Property can only be placed on Getter Methods, but on %s", methodElement);
return;
}

// getter property as simulated read-only field with getter method
final var frequency = frequency(propertyPrism.value());
final var reader = new FieldReader(element, namingConvention, currentSubType, genericTypeParams, frequency);
reader.getterMethod(new MethodReader(methodElement, type));
localFields.add(reader);
});
}

private boolean checkMethod2(ExecutableElement methodElement) {
Expand Down
2 changes: 1 addition & 1 deletion jsonb/src/main/java/io/avaje/jsonb/Json.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@
}

/**
* @deprecated - migrate to {@link Json.Alias}.
* Deprecated - migrate to {@link Json.Alias}.
*/
@Deprecated
@Retention(CLASS)
Expand Down

0 comments on commit 5271859

Please sign in to comment.