unpack

inline fun <T : Any> Any.unpack(): T(source)

Unpacks this Any message into a message of type T.

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, an IllegalArgumentException is thrown.

Example:

val any: Any = receiveFromNetwork()
if (any.contains<Timestamp>()) {
val timestamp = any.unpack<Timestamp>()
processTimestamp(timestamp)
}

Return

the unpacked message of type T

Type Parameters

T

the protobuf message type to unpack to

See also

Throws

if this Any does not contain a message of type T


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

Unpacks this Any message into a message of class kClass.

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, an IllegalArgumentException is thrown.

Example:

val any: Any = receiveFromNetwork()
if (any.contains<Timestamp>()) {
val timestamp = any.unpack(Timestamp::class)
processTimestamp(timestamp)
}

Return

the unpacked message of type T

Parameters

kClass

the protobuf message class to unpack to

See also

Throws

if this Any does not contain a message of class kClass


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

Unpacks this Any message into a message of type kType.

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, an IllegalArgumentException is thrown.

Example:

val any: Any = receiveFromNetwork()
if (any.contains<Timestamp>()) {
val timestamp = any.unpack<Timestamp>(typeOf<Timestamp>())
processTimestamp(timestamp)
}

Return

the unpacked message of type T

Parameters

kType

the protobuf message type to unpack to

See also

Throws

if this Any does not contain a message of type kType