MoshiJsonSerializer

class MoshiJsonSerializer(moshi: Moshi, customize: JsonAdapter<*>.(type: TypeRef<*>) -> JsonAdapter<*>) : JsonSerializer

A JSON serializer backed by Moshi.

Requires adding Moshi 1.x as an additional dependency of your project. Maven coordinates of the additional dependency:

<dependency>
<groupId>com.squareup.moshi</groupId>
<artifactId>moshi-kotlin</artifactId>
<version>${moshi.version}</version>
</dependency>

Sample code for configuring the Couchbase cluster environment using Moshi in reflection mode:

val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
val cluster = Cluster.connect(connectionString, username, password) {
jsonSerializer = MoshiJsonSerializer(moshi)
}

To use Moshi's code generation mode, don't add KotlinJsonAdapterFactory, and instead look to the Moshi documentation for details on how to run the code generator as part of your build process.

The MoshiJsonSerializer constructor takes an optional lambda for customizing the Moshi JsonAdapter. This lambda is invoked in a context where "this" refers to the default JsonAdapter. The lambda returns the JsonAdapter to use. For example, to configure Moshi to serialize nulls:

    jsonSerializer = MoshiSerializer(moshi) { serializeNulls() }

Constructors

Link copied to clipboard
fun MoshiJsonSerializer(moshi: Moshi, customize: JsonAdapter<*>.(type: TypeRef<*>) -> JsonAdapter<*> = { this })

Functions

Link copied to clipboard
open override fun <T> deserialize(json: ByteArray, type: TypeRef<T>): T
Link copied to clipboard
open override fun <T> serialize(value: T, type: TypeRef<T>): ByteArray