gerrenta.blogg.se

Kotlin map not null
Kotlin map not null




It’s not “well-behaved.” And yet, it was working fine up till now. How does Gson handle poorly-behaved classes?īut this data class doesn’t have a no-args constructor.

  • Doesn’t matter whether public or private.
  • Well-behaved classes that are meant for serialization and deserialization should have a no-argument constructor. While deserializing an Object, Gson needs to create a default instance of the class. Well, let’s step back and ask: How did this work in the first place, when we didn’t encounter bogus data? Gson’s docs on “Writing an Instance Creator” say: If you slurp in, Gson will apparently poke a null value into your not-null field.
  • While deserializing, a missing entry in JSON results in setting the corresponding field in the object to its default value: null for object types, zero for numeric types, and false for booleans.
  • While serializing, a null field is omitted from the output.
  • This implementation handles nulls correctly. Gson makes slurping in JSON as objects painless. There’s one more actor in this drama: Gson. Nope, it was Retrofit2’s little helper: Gson.

    kotlin map not null

    And, indeed, the corresponding field proved to be missing from the API response.īut why did this not error out at that point? How did an Invitation ever get created with a null placeName property in the first place? Kotlin told us that would be impossible, but exception logging doesn’t lie.

    kotlin map not null

    So the bogus data probably came from there.

    kotlin map not null

    How does a not-null property wind up null? The exception that shouldn’t have been possible had left the database locked, and ultimately, the app had crashed. While I might personally prefer sticking with null-safe calls and elvis operators, it sounds like you'd prefer cleaner code at the call site (especially considering you know these keys exist and have associated non-null values).E/AndroidRuntime: FATAL EXCEPTION: pool-2-thread-2Ī: database is locked (code 5): retrycount exceeded MapOf() is providing a Map with no guarantees for the presence of a key- something which is kind of expected especially considering the Java implementation of Map.






    Kotlin map not null