Moshi Json Serializer
class MoshiJsonSerializer(moshi: Moshi, customize: JsonAdapter<*>.(type: TypeRef<*>) -> JsonAdapter<*>) : JsonSerializer
Content copied to clipboard
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>
Content copied to clipboard
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)
}
Content copied to clipboard
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() }
Content copied to clipboard
Constructors
Link copied to clipboard
fun MoshiJsonSerializer(moshi: Moshi, customize: JsonAdapter<*>.(type: TypeRef<*>) -> JsonAdapter<*> = { this })
Content copied to clipboard