JsonAdapter

abstract class JsonAdapter<T>

Converts Java values to JSON, and JSON values to Java.

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

Custom JsonAdapter implementations should be designed to be thread-safe.

Types

Factory
Link copied to clipboard
interface Factory

Functions

failOnUnknown
Link copied to clipboard
fun failOnUnknown(): JsonAdapter<T>
Returns a JSON adapter equal to this, but that throws a JsonDataException when unknown names and values are encountered.This constraint applies to both the top-level message handled by this type adapter as well asto nested messages.
fromJson
Link copied to clipboard
abstract fun fromJson(reader: JsonReader): T
Decodes a nullable instance of type T from the given {@code reader}.
fun fromJson(string: String): T
Decodes a nullable instance of type T from the given {@code string}.
fun fromJson(source: BufferedSource): T
Decodes a nullable instance of type T from the given {@code source}.
fromJsonValue
Link copied to clipboard
fun fromJsonValue(@Nullable() value: Any): T
Decodes a Java value object from {@code value}, which must be comprised of maps, lists,strings, numbers, booleans and nulls.
indent
Link copied to clipboard
open fun indent(indent: String): JsonAdapter<T>
Return a JSON adapter equal to this, but using {@code indent} to control how the result isformatted.
lenient
Link copied to clipboard
fun lenient(): JsonAdapter<T>
Returns a JSON adapter equal to this, but is lenient when reading and writing.
nonNull
Link copied to clipboard
fun nonNull(): JsonAdapter<T>
Returns a JSON adapter equal to this JSON adapter, but that refuses null values.
nullSafe
Link copied to clipboard
fun nullSafe(): JsonAdapter<T>
Returns a JSON adapter equal to this JSON adapter, but with support for reading and writingnulls.
serializeNulls
Link copied to clipboard
fun serializeNulls(): JsonAdapter<T>
Returns a JSON adapter equal to this JSON adapter, but that serializes nulls when encodingJSON.
toJson
Link copied to clipboard
fun toJson(@Nullable() value: T): String
Encodes the given {@code value} into a String and returns it.
abstract fun toJson(writer: JsonWriter, @Nullable() value: T)
Encodes the given {@code value} with the given {@code writer}.
fun toJson(sink: BufferedSink, @Nullable() value: T)
toJsonValue
Link copied to clipboard
fun toJsonValue(@Nullable() value: T): Any
Encodes {@code value} as a Java value object comprised of maps, lists, strings, numbers,booleans, and nulls.

Inheritors

NullSafeJsonAdapter
Link copied to clipboard
NonNullJsonAdapter
Link copied to clipboard