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

uidsonic.fluid-json-coding.0.9.16.source-code.JSONCodingParser.kt Maven / Gradle / Ivy

There is a newer version: 0.9.24
Show newest version
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))




© 2015 - 2024 Weber Informatics LLC | Privacy Policy