uidsonic.fluid-json-coding.0.9.22.source-code.JSONDecoder.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 JSONDecoder : JSONReader {
val context: Context
override fun readValue() =
readValueOfType()
fun readValueOfType(valueType: JSONCodingType): Value
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
): BuilderForSource
}
private class BuilderForCodecsImpl(
private val context: Context
) : BuilderForCodecs {
override fun codecs(
providers: Iterable>,
base: JSONCodecProvider?
) =
BuilderForSourceImpl(
context = context,
codecProvider = JSONCodecProvider(base?.let { providers + it } ?: providers)
)
}
interface BuilderForSource {
fun source(source: JSONReader): Builder
fun source(source: Reader) =
source(JSONReader.build(source))
fun source(source: String) =
source(StringReader(source))
}
private class BuilderForSourceImpl(
private val context: Context,
private val codecProvider: JSONCodecProvider
) : BuilderForSource {
override fun source(source: JSONReader) =
BuilderImpl(
context = context,
codecProvider = codecProvider,
source = source
)
}
interface Builder {
fun build(): JSONDecoder
}
private class BuilderImpl(
private val context: Context,
private val codecProvider: JSONCodecProvider,
private val source: JSONReader
) : Builder {
override fun build() =
StandardDecoder(
context = context,
codecProvider = codecProvider,
source = source
)
}
}
}
fun JSONDecoder<*>.invalidPropertyError(property: String, details: String): Nothing =
schemaError("Invalid value for property '$property': $details")
fun JSONDecoder<*>.invalidValueError(details: String): Nothing =
schemaError("Invalid value: $details")
fun JSONDecoder<*>.missingPropertyError(property: String): Nothing =
schemaError("Missing value for property '$property'")
fun JSONDecoder<*>.parsingError(message: String): Nothing =
throw JSONException.Parsing(
message = message,
offset = offset,
path = path
)
fun JSONDecoder<*>.readValueOrNull() =
if (nextToken != JSONToken.nullValue) readValue() else readNull()
inline fun JSONDecoder<*>.readValueOfType() =
readValueOfType(jsonCodingType())
inline fun JSONDecoder<*>.readValueOfTypeOrNull() =
readValueOfTypeOrNull(jsonCodingType())
fun JSONDecoder<*>.readValueOfTypeOrNull(valueType: JSONCodingType) =
readOrNull { readValueOfType(valueType) }
fun JSONDecoder<*>.schemaError(message: String): Nothing =
throw JSONException.Schema(
message = message,
offset = offset,
path = path
)