unpackOrNull

inline fun <T : Any> Any.unpackOrNull(): T?(source)

Unpacks this Any message into a message of type T, or returns null if the type does not match.

This method deserializes the content of this Any message and returns it as an instance of type T. Before unpacking, it verifies that the type URL matches the expected message type. If the type URL does not match, null is returned instead of throwing an exception.

Example:

val any: Any = receiveFromNetwork()
val timestamp = any.unpackOrNull<Timestamp>()
if (timestamp != null) {
processTimestamp(timestamp)
}

Return

the unpacked message of type T, or null if this Any does not contain a message of type T

Type Parameters

T

the protobuf message type to unpack to

See also


fun <T : Any> Any.unpackOrNull(kType: KType): T?(source)

Unpacks this Any message into a message of type kType, or returns null if the type does not match.

This method deserializes the content of this Any message and returns it as an instance of type kType. Before unpacking, it verifies that the type URL matches the expected message type. If the type URL does not match, null is returned instead of throwing an exception.

Example:

val any: Any = receiveFromNetwork()
val timestamp = any.unpackOrNull<Timestamp>(typeOf<Timestamp>())
if (timestamp != null) {
processTimestamp(timestamp)
}

Return

the unpacked message of type T, or null if this Any does not contain a message of type kType

Parameters

kType

the protobuf message type to unpack to

See also


fun <T : Any> Any.unpackOrNull(kClass: KClass<T>): T?(source)

Unpacks this Any message into a message of class kClass, or returns null if the type does not match.

This method deserializes the content of this Any message and returns it as an instance of class kClass. Before unpacking, it verifies that the type URL matches the expected message type. If the type URL does not match, null is returned instead of throwing an exception.

Example:

val any: Any = receiveFromNetwork()
val timestamp = any.unpackOrNull(Timestamp::class)
if (timestamp != null) {
processTimestamp(timestamp)
}

Return

the unpacked message of type T, or null if this Any does not contain a message of class kClass

Parameters

kClass

the protobuf message class to unpack to

See also