All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.avrokotlin.avro4k.AvroGenericDataExtensions.kt Maven / Gradle / Ivy

Go to download

Avro binary format support for kotlin, built on top of kotlinx-serialization

The newest version!
package com.github.avrokotlin.avro4k

import com.github.avrokotlin.avro4k.internal.decoder.generic.AvroValueGenericDecoder
import com.github.avrokotlin.avro4k.internal.encoder.generic.AvroValueGenericEncoder
import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.SerializationStrategy
import kotlinx.serialization.serializer
import org.apache.avro.Schema
import org.apache.avro.generic.GenericContainer

@ExperimentalSerializationApi
public fun  Avro.encodeToGenericData(
    writerSchema: Schema,
    serializer: SerializationStrategy,
    value: T,
): Any? {
    var result: Any? = null
    AvroValueGenericEncoder(this, writerSchema) {
        result = it
    }.encodeSerializableValue(serializer, value)
    return result
}

@ExperimentalSerializationApi
public inline fun  Avro.encodeToGenericData(value: T): Any? {
    val serializer = serializersModule.serializer()
    return encodeToGenericData(schema(serializer), serializer, value)
}

@ExperimentalSerializationApi
public inline fun  Avro.encodeToGenericData(
    writerSchema: Schema,
    value: T,
): Any? {
    val serializer = serializersModule.serializer()
    return encodeToGenericData(writerSchema, serializer, value)
}

@ExperimentalSerializationApi
public fun  Avro.decodeFromGenericData(
    writerSchema: Schema,
    deserializer: DeserializationStrategy,
    value: Any?,
): T {
    return AvroValueGenericDecoder(this, value, writerSchema)
        .decodeSerializableValue(deserializer)
}

@ExperimentalSerializationApi
public inline fun  Avro.decodeFromGenericData(
    writerSchema: Schema,
    value: Any?,
): T {
    val deserializer = serializersModule.serializer()
    return decodeFromGenericData(writerSchema, deserializer, value)
}

@ExperimentalSerializationApi
public inline fun  Avro.decodeFromGenericData(value: GenericContainer?): T? {
    if (value == null) return null
    val deserializer = serializersModule.serializer()
    return decodeFromGenericData(value.schema, deserializer, value)
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy