JsonSupport

Utility object for JSON serialization and deserialization support with configurable strategies.

The primary goals of this object include:

  • Providing a default configured instance of Json with common settings suitable for most use cases.

  • Registering value encoders for handling serialization and deserialization of given types.

  • Supporting @Serializable Kotlin classes for JSON transformation through the ValueEncoder interface.

The default JSON configuration includes:

  • Snake-case naming strategy for property names.

  • Ignoring unknown keys during deserialization.

  • Encoding default values, omitting explicit nulls while encoding.

Functions:

  • encoders: Creates a ValueEncoderRegistry by generating encoders for a set of classes.

  • encoders: Generates encoders for a specific class and its optional sealed subclasses.

  • encoder: Constructs a single ValueEncoder for serialization and deserialization of a specific class.

Functions

Link copied to clipboard
fun <T : @Serializable Any> encoder(clazz: KClass<T>, json: Json = defaultJson): ValueEncoder<T>

Creates a ValueEncoder for a specified Kotlin class with optional JSON configuration.

Link copied to clipboard
fun encoders(types: Set<KClass<*>>, json: Json = defaultJson): ValueEncoderRegistry

Generates and registers encoders for a set of types, including their sealed subclasses, with a given JSON serializer.

fun <T : @Serializable Any> encoders(clazz: KClass<T>, subclasses: Set<KClass<*>> = emptySet(), json: Json = defaultJson): List<Pair<KClass<*>, ValueEncoder<T>>>

Generates encoders for a given class and its optional subclasses using the specified JSON configuration.