uidsonic.fluid-json-coding.0.9.22.source-code.JSONEncoder.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fluid-json-coding Show documentation
Show all versions of fluid-json-coding Show documentation
A JSON library written in pure Kotlin (coding extension)
package com.github.fluidsonic.fluid.json
import java.io.*
interface JSONEncoder : JSONWriter {
val context: Context
companion object {
fun builder(): BuilderForCodecs =
BuilderForCodecsImpl(context = JSONCodingContext.empty)
fun builder(context: Context): BuilderForCodecs =
BuilderForCodecsImpl(context = context)
interface BuilderForCodecs {
fun codecs(
vararg providers: JSONCodecProvider,
base: JSONCodecProvider? = JSONCodecProvider.extended
) =
codecs(providers = providers.toList(), base = base)
fun codecs(
providers: Iterable>,
base: JSONCodecProvider? = JSONCodecProvider.extended
): BuilderForDestination
}
private class BuilderForCodecsImpl(
private val context: Context
) : BuilderForCodecs {
override fun codecs(
providers: Iterable>,
base: JSONCodecProvider?
) =
BuilderForDestinationImpl(
context = context,
codecProvider = JSONCodecProvider(base?.let { providers + it } ?: providers)
)
}
interface BuilderForDestination {
fun destination(destination: JSONWriter): Builder
fun destination(destination: Writer) =
destination(JSONWriter.build(destination))
}
private class BuilderForDestinationImpl(
private val context: Context,
private val codecProvider: JSONCodecProvider
) : BuilderForDestination {
override fun destination(destination: JSONWriter) =
BuilderImpl(
context = context,
codecProvider = codecProvider,
destination = destination
)
}
interface Builder {
fun build(): JSONEncoder
}
private class BuilderImpl(
private val context: Context,
private val codecProvider: JSONCodecProvider,
private val destination: JSONWriter
) : Builder {
override fun build() =
StandardEncoder(
context = context,
codecProvider = codecProvider,
destination = destination
)
}
}
}
fun JSONEncoder<*>.serializationError(message: String): Nothing =
throw JSONException.Serialization(
message = message,
path = path
)