Giter VIP home page Giter VIP logo

Comments (3)

JakeWharton avatar JakeWharton commented on July 20, 2024

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

On Mon, Apr 18, 2016, 9:41 PM Nicolas Milliard [email protected]
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

from auto-value-gson.

JakeWharton avatar JakeWharton commented on July 20, 2024

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 [email protected] wrote:

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

On Mon, Apr 18, 2016, 9:41 PM Nicolas Milliard [email protected]
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

from auto-value-gson.

niqo01 avatar niqo01 commented on July 20, 2024

Thanks @JakeWharton. I missed that useful API.

from auto-value-gson.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.