Skip to content
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

@Property methods now implicitly override fields #217

Merged
merged 16 commits into from
Mar 15, 2024

Conversation

SentryMan
Copy link
Collaborator

@SentryMan SentryMan commented Mar 7, 2024

  • automatically disables field serialization if a @Property method targeting the same field is located (the getter method will serialize instead)
  • can even change the type of the accessor

Now this:

@Json
public class OptionalAccess {

  String stringy;

  @Property("stringy")
  public Optional<String> stringy() {
    return Optional.of(stringy);
  }

  public void stringy(String stringy) {
    this.stringy = stringy;
  }
}

will generate like this:

public final class OptionalAccessJsonAdapter implements JsonAdapter<OptionalAccess> , ViewBuilderAware {
...

  private final JsonAdapter<String> stringJsonAdapter;
  private final JsonAdapter<Optional<String>> optionalStringJsonAdapter;
  private final PropertyNames names;

  public OptionalAccessJsonAdapter(Jsonb jsonb) {
    this.stringJsonAdapter = jsonb.adapter(String.class);
    this.optionalStringJsonAdapter = jsonb.adapter(Types.optionalOf(String.class));
    this.names = jsonb.properties("stringy");
  }
...

  @Override
  public void build(ViewBuilder builder, String name, MethodHandle handle) {
    builder.beginObject(name, handle);
    builder.add("stringy", optionalStringJsonAdapter, builder.method(OptionalAccess.class, "stringy", java.util.Optional.class));
    builder.endObject();
  }

  @Override
  public void toJson(JsonWriter writer, OptionalAccess optionalAccess) {
    writer.beginObject(names);
    writer.name(0);
    optionalStringJsonAdapter.toJson(writer, optionalAccess.stringy());
    writer.endObject();
  }

  @Override
  public OptionalAccess fromJson(JsonReader reader) {
    OptionalAccess _$optionalAccess = new OptionalAccess();

    // read json
    reader.beginObject(names);
    while (reader.hasNextField()) {
      final String fieldName = reader.nextField();
      switch (fieldName) {
        case "stringy": 
          _$optionalAccess.stringy(stringJsonAdapter.fromJson(reader));
          break;

        default:
          reader.unmappedField(fieldName);
          reader.skipValue();
      }
    }
    reader.endObject();

    return _$optionalAccess;
  }
}

part of #216

@SentryMan SentryMan enabled auto-merge March 7, 2024 16:33
@SentryMan SentryMan added the enhancement New feature or request label Mar 7, 2024
@SentryMan SentryMan self-assigned this Mar 7, 2024
@SentryMan SentryMan requested a review from rbygrave March 7, 2024 16:44
@SentryMan SentryMan added this to the 1.11 milestone Mar 7, 2024
@Auties00
Copy link

Auties00 commented Mar 8, 2024

I'll take a look this night to see if I can help add support for views, we might also want to consider OptionalInt, OptionaLong, OptionalDouble

@SentryMan SentryMan changed the title Support Optional Accessors @Property methods can now override fields Mar 10, 2024
@SentryMan SentryMan requested a review from rbygrave March 10, 2024 05:29
@SentryMan SentryMan changed the title @Property methods can now override fields @Property methods now implicitly override fields Mar 10, 2024
@SentryMan
Copy link
Collaborator Author

found another way to do it.

@SentryMan
Copy link
Collaborator Author

Yeah, I'm good with this. @rbygrave please review when you get time

@rbygrave rbygrave disabled auto-merge March 15, 2024 10:06
@rbygrave rbygrave merged commit d30f827 into avaje:main Mar 15, 2024
2 checks passed
@SentryMan SentryMan deleted the optional branch March 20, 2024 15:35
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.

3 participants