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

uidsonic.fluid-json-coding.0.9.22.source-code.JSONEncoder.kt Maven / Gradle / Ivy

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy