uidsonic.fluid-json-coding.0.9.22.source-code.JSONCodingSerializer.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
interface JSONCodingSerializer : JSONSerializer {
override fun serializeValue(value: Any?, destination: JSONWriter, withTermination: Boolean)
companion object {
fun builder(): BuilderForEncoding =
BuilderForEncodingImpl(context = JSONCodingContext.empty)
fun builder(context: Context): BuilderForEncoding =
BuilderForEncodingImpl(context = context)
val default = builder()
.encodingWith()
.build()
val nonRecursive = builder()
.encodingWith(DefaultJSONCodecs.nonRecursive)
.build()
interface BuilderForEncoding {
fun encodingWith(factory: (destination: JSONWriter, context: Context) -> JSONEncoder): Builder
fun encodingWith(
vararg providers: JSONCodecProvider,
base: JSONCodecProvider? = JSONCodecProvider.extended
) =
encodingWith(providers = providers.toList(), base = base)
fun encodingWith(
providers: Iterable>,
base: JSONCodecProvider? = JSONCodecProvider.extended
) =
encodingWith { destination, context ->
JSONEncoder.builder(context)
.codecs(JSONCodecProvider(providers), base = base)
.destination(destination)
.build()
}
}
private class BuilderForEncodingImpl(
private val context: Context
) : BuilderForEncoding {
override fun encodingWith(factory: (source: JSONWriter, context: Context) -> JSONEncoder) =
BuilderImpl(
context = context,
encoderFactory = factory
)
}
interface Builder {
fun build(): JSONCodingSerializer
}
private class BuilderImpl(
private val context: Context,
private val encoderFactory: (source: JSONWriter, context: Context) -> JSONEncoder
) : Builder {
override fun build() =
StandardCodingSerializer(
context = context,
encoderFactory = encoderFactory
)
}
}
}