Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
uidsonic.fluid-json-coding.0.9.16.source-code.JSONCodingParser.kt Maven / Gradle / Ivy
Go to download
A JSON library written in pure Kotlin (coding extension)
package com.github.fluidsonic.fluid.json
import java.io.Reader
interface JSONCodingParser : JSONParser {
override fun parseList(source: JSONReader, withTermination: Boolean) =
parseValueOfType>(source, withTermination = withTermination)
override fun parseMap(source: JSONReader, withTermination: Boolean) =
parseValueOfType>(source, withTermination = withTermination)
fun parseValueOfTypeOrNull(source: JSONReader, valueType: JSONCodingType, withTermination: Boolean = true): Value?
override fun parseValueOrNull(source: JSONReader, withTermination: Boolean) =
parseValueOfTypeOrNull(source, withTermination = withTermination)
companion object {
fun builder(): BuilderForDecoding =
BuilderForDecodingImpl(context = JSONCodingContext.empty)
fun builder(context: Context): BuilderForDecoding =
BuilderForDecodingImpl(context = context)
val default = builder()
.decodingWith()
.build()
val nonRecursive = builder()
.decodingWith(DefaultJSONCodecs.nonRecursive)
.build()
interface BuilderForDecoding {
fun decodingWith(factory: (source: JSONReader, context: Context) -> JSONDecoder): Builder
fun decodingWith(
vararg providers: JSONCodecProvider,
base: JSONCodecProvider? = JSONCodecProvider.extended
) =
decodingWith(providers = providers.toList(), base = base)
fun decodingWith(
providers: Iterable>,
base: JSONCodecProvider? = JSONCodecProvider.extended
) =
decodingWith { source, context ->
JSONDecoder.builder(context)
.codecs(JSONCodecProvider(providers), base = base)
.source(source)
.build()
}
}
private class BuilderForDecodingImpl(
private val context: Context
) : BuilderForDecoding {
override fun decodingWith(factory: (source: JSONReader, context: Context) -> JSONDecoder) =
BuilderImpl(
context = context,
decoderFactory = factory
)
}
interface Builder {
fun build(): JSONCodingParser
}
private class BuilderImpl(
private val context: Context,
private val decoderFactory: (source: JSONReader, context: Context) -> JSONDecoder
) : Builder {
override fun build() =
StandardCodingParser(
context = context,
decoderFactory = decoderFactory
)
}
}
}
fun JSONCodingParser.parseValue(source: JSONReader, withTermination: Boolean = true) =
parseValueOrNull(source, withTermination = withTermination)
?: throw JSONException.Schema(
message = "Unexpected null value at top-level",
offset = source.offset,
path = source.path
)
fun JSONCodingParser.parseValue(source: Reader, withTermination: Boolean = true) =
parseValue(JSONReader.build(source), withTermination = withTermination)
fun JSONCodingParser.parseValue(source: String) =
parseValue(JSONReader.build(source))
inline fun JSONCodingParser.parseValueOfType(source: JSONReader, withTermination: Boolean = true): Value =
parseValueOfType(source, valueType = jsonCodingType(), withTermination = withTermination)
inline fun JSONCodingParser.parseValueOfType(source: Reader, withTermination: Boolean = true): Value =
parseValueOfType(JSONReader.build(source), withTermination = withTermination)
inline fun JSONCodingParser.parseValueOfType(source: String): Value =
parseValueOfType(JSONReader.build(source))
fun JSONCodingParser.parseValueOfType(source: JSONReader, valueType: JSONCodingType, withTermination: Boolean = true) =
parseValueOfTypeOrNull(source, valueType = valueType, withTermination = withTermination)
?: throw JSONException.Schema(
message = "Unexpected null value at top-level",
offset = source.offset,
path = source.path
)
fun JSONCodingParser.parseValueOfType(source: Reader, valueType: JSONCodingType, withTermination: Boolean = true) =
parseValueOfType(JSONReader.build(source), valueType = valueType, withTermination = withTermination)
fun JSONCodingParser.parseValueOfType(source: String, valueType: JSONCodingType): Value =
parseValueOfType(JSONReader.build(source), valueType = valueType)
inline fun JSONCodingParser.parseValueOfTypeOrNull(source: JSONReader, withTermination: Boolean = true): Value? =
parseValueOfTypeOrNull(source, valueType = jsonCodingType(), withTermination = withTermination)
inline fun JSONCodingParser.parseValueOfTypeOrNull(source: Reader, withTermination: Boolean = true): Value? =
parseValueOfTypeOrNull(JSONReader.build(source), withTermination = withTermination)
inline fun JSONCodingParser.parseValueOfTypeOrNull(source: String): Value? =
parseValueOfTypeOrNull(JSONReader.build(source))
fun JSONCodingParser.parseValueOfTypeOrNull(source: Reader, valueType: JSONCodingType, withTermination: Boolean = true): Value? =
parseValueOfTypeOrNull(JSONReader.build(source), valueType = valueType, withTermination = withTermination)
fun JSONCodingParser.parseValueOfTypeOrNull(source: String, valueType: JSONCodingType): Value? =
parseValueOfTypeOrNull(JSONReader.build(source), valueType = valueType)
fun JSONCodingParser.parseValueOrNull(source: Reader, withTermination: Boolean = true) =
parseValueOrNull(JSONReader.build(source), withTermination = withTermination)
fun JSONCodingParser.parseValueOrNull(source: String) =
parseValueOrNull(JSONReader.build(source))