gsonvalue icon indicating copy to clipboard operation
gsonvalue copied to clipboard

Serializing kotlin classes with methods

Open naixx opened this issue 7 years ago • 2 comments

..will fail.

Just try the test:

data class IgnoreDataMethods @GsonConstructor constructor(val arg1: Int, val arg2: Int) {
    fun fail() = ""
}

There would be additional adapter fields generated and write method will contain java getters, not callableName

public ValueTypeAdapter_IgnoreDataMethods(Gson gson, TypeToken<IgnoreDataMethods> typeToken) {
    this.adapter_arg1 = gson.getAdapter(int.class);
    this.adapter_arg2 = gson.getAdapter(int.class);
    this.adapter_fail = gson.getAdapter(String.class);
    this.adapter_getArg1 = gson.getAdapter(int.class);
    this.adapter_getArg2 = gson.getAdapter(int.class);
  }

  @Override
  public void write(JsonWriter out, IgnoreDataMethods value) throws IOException {
    if (value == null) {
      out.nullValue();
      return;
    }
    out.beginObject();
    out.name("fail");
    adapter_fail.write(out, value.fail());
    out.name("getArg1");
    adapter_getArg1.write(out, value.getArg1());
    out.name("getArg2");
    adapter_getArg2.write(out, value.getArg2());
    out.endObject();
  }

@SerializedName does not help

naixx avatar Apr 29 '18 03:04 naixx

The issue is with value-processor It is needed to strip beans every time for this to fix. @evant , what is the reason that we have

val allBeans = getters.all { it.isBean }

to strip in value processor?

Like AutoValue, prefixes only apply if all getters follow that style.

naixx avatar Apr 30 '18 09:04 naixx

From the comment here https://github.com/google/auto/issues/84#issuecomment-77760148 the talk is only about abstract method (that are considered a property)

naixx avatar Apr 30 '18 11:04 naixx