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

Serializing null AutoValue object #28

Closed
niqo01 opened this issue Apr 19, 2016 · 3 comments
Closed

Serializing null AutoValue object #28

niqo01 opened this issue Apr 19, 2016 · 3 comments

Comments

@niqo01
Copy link

niqo01 commented Apr 19, 2016

I am converting slowly my value objects to AutoValue with Gson extension.
I am having an issue for the following case:
(Pseudo code)

// Not an AutoValue (yet)
class A{
 B b;
}

@AutoValue
class B{
  public abstract Boolean enabled();

  public static TypeAdapter<B> typeAdapter(Gson gson) {
    return new AutoValue_B.GsonTypeAdapter(gson);
  }
}

When I am trying to serialize A with gson and B is null I am getting a NPE on the following line if (object.enabled() != null) inside $AutoValue_B:

    @Override
    public void write(JsonWriter jsonWriter, B object) throws IOException {
      jsonWriter.beginObject();
      if (object.enabled() != null) {
        jsonWriter.name("enabled");
        enabledAdapter.write(jsonWriter, object.enabled());
      }
      jsonWriter.endObject();
    }

because object is null.

@JakeWharton
Copy link
Contributor

Call .nullSafe() on the TypeAdapter before registering it.

On Mon, Apr 18, 2016, 9:41 PM Nicolas Milliard notifications@github.com
wrote:

I am converting slowly my value objects to AutoValue with Gson extension.
I am having an issue for the following case:
(Pseudo code)

// Not an AutoValue (yet)
class A{
B b;
}

@autovalue
class B{
public abstract Boolean enabled();

public static TypeAdapter typeAdapter(Gson gson) {
return new AutoValue_B.GsonTypeAdapter(gson);
}
}

When I am trying to serialize A with gson and B is null I am getting a NPE
on the following line if (object.enabled() != null) inside $AutoValue_B:

@Override
public void write(JsonWriter jsonWriter, B object) throws IOException {
  jsonWriter.beginObject();
  if (object.enabled() != null) {
    jsonWriter.name("enabled");
    enabledAdapter.write(jsonWriter, object.enabled());
  }
  jsonWriter.endObject();
}

because object is null.


You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub
#28

@JakeWharton
Copy link
Contributor

Keep in mind it returns a new null-safe instance which needs registered,
not modifies the existing one.

On Mon, Apr 18, 2016, 9:51 PM Jake Wharton jakewharton@gmail.com wrote:

Call .nullSafe() on the TypeAdapter before registering it.

On Mon, Apr 18, 2016, 9:41 PM Nicolas Milliard notifications@github.com
wrote:

I am converting slowly my value objects to AutoValue with Gson extension.
I am having an issue for the following case:
(Pseudo code)

// Not an AutoValue (yet)
class A{
B b;
}

@autovalue
class B{
public abstract Boolean enabled();

public static TypeAdapter typeAdapter(Gson gson) {
return new AutoValue_B.GsonTypeAdapter(gson);
}
}

When I am trying to serialize A with gson and B is null I am getting a
NPE on the following line if (object.enabled() != null) inside
$AutoValue_B:

@Override
public void write(JsonWriter jsonWriter, B object) throws IOException {
  jsonWriter.beginObject();
  if (object.enabled() != null) {
    jsonWriter.name("enabled");
    enabledAdapter.write(jsonWriter, object.enabled());
  }
  jsonWriter.endObject();
}

because object is null.


You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub
#28

@niqo01
Copy link
Author

niqo01 commented Apr 19, 2016

Thanks @JakeWharton. I missed that useful API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants