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

Add Json Alias annotation #45

merged 2 commits into from
Dec 14, 2022

Conversation

SentryMan
Copy link
Collaborator

Adds a new annotation we can use to specify alternate deserialization names.

@Json
@Getter
@Setter
public class TestClass {

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

  private String s;

  private int i;

  private Integer integer;

  private char ch;

  private Character chara;

  private List<String> list;
}

generated adapter fromJson looks like.

  @Override
  public TestClass fromJson(JsonReader reader) {
    TestClass _$testClass = new TestClass();

    // read json
    reader.beginObject();
    reader.names(names);
    while (reader.hasNextField()) {
      String fieldName = reader.nextField();
      switch (fieldName) {
        case "something":
        case "something2":
        case "alias": {
          _$testClass.setAlias(stringJsonAdapter.fromJson(reader)); break;
        }
        case "s": {
          _$testClass.setS(stringJsonAdapter.fromJson(reader)); break;
        }
        case "i": {
          _$testClass.setI(pintJsonAdapter.fromJson(reader)); break;
        }
        case "integer": {
          _$testClass.setInteger(integerJsonAdapter.fromJson(reader)); break;
        }
 and so on...

Copy link
Contributor

@rbygrave rbygrave left a comment

Choose a reason for hiding this comment

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

I love it

@@ -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

@@ -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"

@rbygrave rbygrave added this to the 1.1 milestone Dec 14, 2022
@rbygrave rbygrave added the enhancement New feature or request label Dec 14, 2022
@rbygrave rbygrave merged commit a348233 into avaje:main Dec 14, 2022
rbygrave added a commit that referenced this pull request Dec 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants