See what Google Play services use to safely display zero lines:
String NULL = "SAFE_PARCELABLE_NULL_STRING";
This is technically not an empty string; the team decided that semantically this string is zero.
We use similar methods when working with lists. First we writeInt
with the length of the list. It can be -1
so that the list is empty.
Whether -1
can be used to represent the absence of an integer depends on your schema. In your use case -1
valid value? If so, you should find another value to represent your null value, such as Int.MIN
or Int.MAX
.
If all of these values are in the valid range, use a different value, for example:
val hasValue: Boolean = value != null val hasValueByte: Byte = if (value != null) 1 else 0 dest.writeByte(hasValueByte) if (hasValue) dest.writeInt(value)
Do not forget about one of these cases when unmarshalling!
val parcelHasValueByte = source.readByte() val parcelHasValue = parcelHasValueByte != 0 if (parcelHasValue) { value = source.readInt() } else { value = null }
source share