Moshi

Coordinates binding between JSON values and Java objects.

Moshi instances are thread-safe, meaning multiple threads can safely use a single instance concurrently.

class Moshi

Constructors

Moshi
Link copied to clipboard
open fun Moshi(builder: Moshi.Builder)

Types

Builder
Link copied to clipboard
class Builder
Lookup
Link copied to clipboard

This class implements {@code JsonAdapter} so it can be used as a stub for re-entrant calls.

class Lookup<T> : JsonAdapter<T>
LookupChain
Link copied to clipboard

A possibly-reentrant chain of lookups for JSON adapters.

We keep track of the current stack of lookups: we may start by looking up the JSON adapter for Employee, re-enter looking for the JSON adapter of HomeAddress, and re-enter again looking up the JSON adapter of PostalCode. If any of these lookups fail we can provide a stack trace with all of the lookups.

Sometimes a JSON adapter factory depends on its own product; either directly or indirectly. To make this work, we offer a JSON adapter stub while the final adapter is being computed. When it is ready, we wire the stub to that finished adapter. This is necessary in self-referential object models, such as an {@code Employee} class that has a {@code List} field for an organization's management hierarchy.

This class defers putting any JSON adapters in the cache until the topmost JSON adapter has successfully been computed. That way we don't pollute the cache with incomplete stubs, or adapters that may transitively depend on incomplete stubs.

class LookupChain

Functions

adapter
Link copied to clipboard
open fun <T> adapter(type: Class<T>): JsonAdapter<T>
open fun <T> adapter(type: Type, annotationTypes: Array<Class<out Annotation>>): JsonAdapter<T>
open fun <T> adapter(type: Type, annotationType: Class<out Annotation>): JsonAdapter<T>
open fun <T> adapter(type: Type, annotations: Set<out Annotation>): JsonAdapter<T>

Returns a JSON adapter for {@code type} , creating it if necessary.

open fun <T> adapter(type: Type): JsonAdapter<T>

An optional field name associated with this type. The field name is used as a hint for better adapter lookup error messages for nested structures.

open fun <T> adapter(type: Type, annotations: Set<out Annotation>, fieldName: String): JsonAdapter<T>
newAdapterFactory
Link copied to clipboard
open fun <T> newAdapterFactory(type: Type, jsonAdapter: JsonAdapter<T>): JsonAdapter.Factory
open fun <T> newAdapterFactory(type: Type, annotation: Class<out Annotation>, jsonAdapter: JsonAdapter<T>): JsonAdapter.Factory
newBuilder
Link copied to clipboard

Returns a new builder containing all custom factories used by the current instance.

open fun newBuilder(): Moshi.Builder
nextAdapter
Link copied to clipboard
open fun <T> nextAdapter(skipPast: JsonAdapter.Factory, type: Type, annotations: Set<out Annotation>): JsonAdapter<T>

Properties

BUILT_IN_FACTORIES
Link copied to clipboard
val BUILT_IN_FACTORIES: List<JsonAdapter.Factory>

Extensions

adapter
Link copied to clipboard
inline fun <T> Moshi.adapter(): JsonAdapter<T>
fun <T> Moshi.adapter(ktype: KType): JsonAdapter<T>