Skip to content

Add Json Alias annotation #45

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
Dec 14, 2022
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
@@ -0,0 +1,27 @@
package io.avaje.jsonb.generator;

import java.util.List;
import java.util.stream.Collectors;

import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;

final class AliasReader {
private AliasReader() {}

private static final String JSON_ALIAS = "io.avaje.jsonb.Json.JsonAlias";

/** Read the Json.Alias annotation using annotation mirrors. */
static List<String> getAliases(Element element) {
for (final AnnotationMirror mirror : element.getAnnotationMirrors()) {
if (JSON_ALIAS.equals(mirror.getAnnotationType().toString())) {
return mirror.getElementValues().values().stream()
.flatMap(v -> ((List<?>) v.getValue()).stream())
.map(Object::toString)
.map(Util::trimQuotes)
.collect(Collectors.toList());
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class FieldReader {
private boolean constructorParam;
private boolean genericTypeParameter;
private int genericTypeParamPosition;
private final List<String> aliases;

FieldReader(Element element, NamingConvention namingConvention, TypeSubTypeMeta subType, List<String> genericTypeParams) {
addSubType(subType);
Expand All @@ -39,6 +40,7 @@ class FieldReader {
this.propertyName = PropertyReader.name(namingConvention, fieldName, element);
this.publicField = element.getModifiers().contains(Modifier.PUBLIC);
this.rawType = trimAnnotations(element.asType().toString());
this.aliases = AliasReader.getAliases(element);

final PropertyIgnoreReader ignoreReader = new PropertyIgnoreReader(element);
this.unmapped = ignoreReader.unmapped();
Expand Down Expand Up @@ -302,6 +304,15 @@ void writeFromJsonSwitch(Append writer, boolean defaultConstructor, String varNa
if (unmapped) {
return;
}

if (aliases != null) {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whitespace

for (final String alias : aliases) {
writer.append(" case \"%s\":", alias);
writer.eol();
}
}

writer.append(" case \"%s\": {", propertyName).eol();
if (!deserialize) {
writer.append(" reader.skipValue();");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
@Json
public class TestClass {

@Json.JsonAlias({"something", "something2"})
private String alias;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should add a test that uses "something", "something2" and "alias"


private String s;

private int i;
Expand All @@ -19,6 +22,13 @@ public class TestClass {

private List<String> list;

public String getAlias() {
return alias;
}

public void setAlias(String alias) {
this.alias = alias;
}

public String getS() {
return s;
Expand Down
18 changes: 18 additions & 0 deletions jsonb/src/main/java/io/avaje/jsonb/Json.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,24 @@
String value();
}

/**
* Define one or more alternative names for a property accepted
* during deserialization.
*
* <pre>{@code
* @Json.JsonAlias("$code")
* String referenceCode;
*
* }</pre>
*/
@Retention(CLASS)
@Target({ElementType.FIELD})
@interface JsonAlias {

/** One or more secondary names to accept as aliases to the official name. */
String[] value();
}

/**
* Exclude the property from serialization, deserialization or both.
* <p>
Expand Down