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

uidsonic.fluid-json-coding.0.9.14.source-code.JSONCodingSerializer.kt Maven / Gradle / Ivy

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy