How to serialize/deserialize enums in/from a lower case in Ktor? - kotlin

I use Ktor serialization in my app, below is a dependency in build.gradle:
dependencies {
// ...
implementation "io.ktor:ktor-serialization:$ktor_version"
}
And set up it in Application.kt:
fun main(args: Array<String>): Unit = io.ktor.server.netty.EngineMain.main(args)
#Suppress("unused")
fun Application.module(#Suppress("UNUSED_PARAMETER") testing: Boolean = false) {
// ...
install(ContentNegotiation) {
json(Json {
prettyPrint = true
})
}
// ...
}
All works perfectly but enumerations... For example, I have the next one:
enum class EGender(val id: Int) {
FEMALE(1),
MALE(2);
companion object {
fun valueOf(value: Int) = values().find { it.id == value }
}
}
If I will serialise this enum instance, Ktor will output a something like:
{
"gender": "MALE"
}
How to make it in a lower case without renaming enumeration members?
P.S. Also I can't change Int to String type cuz it represents database IDs.

You can add the SerialName annotation for enum constants to override names in JSON:
#kotlinx.serialization.Serializable
enum class EGender(val id: Int) {
#SerialName("female")
FEMALE(1),
#SerialName("male")
MALE(2);
companion object {
fun valueOf(value: Int) = values().find { it.id == value }
}
}

Related

Is it possible to verify at compile time whether the required function is called for the Factory Class in Kotlin?

class ModelFactory {
fun setA() : ModelFactory {
// blabla...
}
fun setB() : ModelFactory {
// blabla...
}
fun setC() : ModelFactory {
// blabla...
}
fun build() : Model {
// An error occurs if any of setA, setB, and setC is not called.
}
}
//example
fun successTest() {
ModelFactory().setA().setB().setC().build() // No error occurs at compile time
}
fun failTest() {
ModelFactory().setA().build() // An error occurs at compile time because setB and setC are not called.
}
It's awkward grammatically, but I think it's been expressed what I want.
I have already implemented an error-raising runtime for this requirement, but I want to check this at compile time.
If possible, I think I should use annotations. But is this really possible at compile time?
With Kotlin, I have been avoiding builder pattern, as we can always specify default values for non-mandatory fields.
If you still want to use a builder pattern, you can use Step builder pattern that expects all mandatory fields to be set before creating the object. Note that each setter method returns the reference of next setter interface. You can have multiple Step builders based on the combination of mandatory fields.
class Model(val a: String = "", val b: String = "", val c: String = "")
class StepBuilder {
companion object {
fun builder(): AStep = Steps()
}
interface AStep {
fun setA(a: String): BStep
}
interface BStep {
fun setB(b: String): CStep
}
interface CStep {
fun setC(c: String): BuildStep
}
interface BuildStep {
//fun setOptionalField(x: String): BuildStep
fun build(): Model
}
class Steps : AStep, BStep, CStep, BuildStep {
private lateinit var a: String
private lateinit var b: String
private lateinit var c: String
override fun setA(a: String): BStep {
this.a = a
return this
}
override fun setB(b: String): CStep {
this.b = b
return this
}
override fun setC(c: String): BuildStep {
this.c = c
return this
}
override fun build() = Model(a, b , c)
}
}
fun main() {
// cannot build until you call all three setters
val model = StepBuilder.builder().setA("A").setB("B").setC("C").build()
}

Kotlinx Serialization - Custom serializer to ignore null value

Let's say I'm having a class like:
#Serializable
data class MyClass(
#SerialName("a") val a: String?,
#SerialName("b") val b: String
)
Assume the a is null and b's value is "b value", then Json.stringify(MyClass.serializer(), this) produces:
{ "a": null, "b": "b value" }
Basically if a is null, I wanted to get this:
{ "b": "b value" }
From some research I found this is currently not doable out of the box with Kotlinx Serialization so I was trying to build a custom serializer to explicitly ignore null value. I followed the guide from here but couldn't make a correct one.
Can someone please shed my some light? Thanks.
You can use explicitNulls = false
example:
#OptIn(ExperimentalSerializationApi::class)
val format = Json { explicitNulls = false }
#Serializable
data class Project(
val name: String,
val language: String,
val version: String? = "1.3.0",
val website: String?,
)
fun main() {
val data = Project("kotlinx.serialization", "Kotlin", null, null)
val json = format.encodeToString(data)
println(json) // {"name":"kotlinx.serialization","language":"Kotlin"}
}
https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/json.md#explicit-nulls
Use encodeDefaults = false property in JsonConfiguration and it won't serialize nulls (or other optional values)
Try this (not tested, just based on adapting the example):
#Serializable
data class MyClass(val a: String?, val b: String) {
#Serializer(forClass = MyClass::class)
companion object : KSerializer<MyClass> {
override val descriptor: SerialDescriptor = object : SerialClassDescImpl("MyClass") {
init {
addElement("a")
addElement("b")
}
}
override fun serialize(encoder: Encoder, obj: MyClass) {
encoder.beginStructure(descriptor).run {
obj.a?.let { encodeStringElement(descriptor, 0, obj.a) }
encodeStringElement(descriptor, 1, obj.b)
endStructure(descriptor)
}
}
override fun deserialize(decoder: Decoder): MyClass {
var a: String? = null
var b = ""
decoder.beginStructure(descriptor).run {
loop# while (true) {
when (val i = decodeElementIndex(descriptor)) {
CompositeDecoder.READ_DONE -> break#loop
0 -> a = decodeStringElement(descriptor, i)
1 -> b = decodeStringElement(descriptor, i)
else -> throw SerializationException("Unknown index $i")
}
}
endStructure(descriptor)
}
return MyClass(a, b)
}
}
}
Since I was also struggling with this one let me share with you the solution I found that is per property and does not require to create serializer for the whole class.
class ExcludeIfNullSerializer : KSerializer<String?> {
override fun deserialize(decoder: Decoder): String {
return decoder.decodeString()
}
override val descriptor: SerialDescriptor
get() = PrimitiveSerialDescriptor("ExcludeNullString", PrimitiveKind.STRING)
override fun serialize(encoder: Encoder, value: String?) {
if (value != null) {
encoder.encodeString(value)
}
}
}
will work as expected with the following class
#Serializable
class TestDto(
#SerialName("someString")
val someString: String,
#SerialName("id")
#EncodeDefault(EncodeDefault.Mode.NEVER)
#Serializable(with = ExcludeIfNullSerializer::class)
val id: String? = null
)
Note the #EncodeDefault(EncodeDefault.Mode.NEVER) is crucial here in case you using JsonBuilder with encodeDefaults = true, as in this case the serialization library will still add the 'id' json key even if the value of id field is null unless using this annotation.
JsonConfiguration is deprecated in favor of Json {} builder since kotlinx.serialization 1.0.0-RC according to its changelog.
Now you have to code like this:
val json = Json { encodeDefaults = false }
val body = json.encodeToString(someSerializableObject)
As of now, for anyone seeing this pos today, default values are not serialized (see https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/basic-serialization.md#defaults-are-not-encoded-by-default)
So you simply add to set a default null value, and it will not be serialized.

Kotlin + Arrow + Gson = None?

I have a model in Kotlin of a simple library of Books and Borrowers where a Book is checked out if it has a Borrower. I use Arrow Option to encode the absence/presence of a Borrower:
data class Borrower(val name: Name, val maxBooks: MaxBooks)
data class Book(val title: String, val author: String, val borrower: Option<Borrower> = None)
I am having trouble serializing/deserializing these objects to/from JSON in Gson - specifically the representation of an Option<Borrower> to a JSON null within a Book:
[
{
"title": "Book100",
"author": "Author100",
"borrower": {
"name": "Borrower100",
"maxBooks": 100
}
},
{
"title": "Book200",
"author": "Author200",
"borrower": null
}
]
My deserialize code:
fun jsonStringToBooks(jsonString: String): List<Book> {
val gson = Gson()
return try {
gson.fromJson(jsonString, object : TypeToken<List<Book>>() {}.type)
} catch (e: Exception) {
emptyList()
}
}
I get an empty list. The nearly identical jsonStringToBorrowers works fine.
Can someone point me in the right direction?
Would using a different JSON library like kotlinx.serialization or Klaxon be a better idea and how do they do the null <-> None thing?
Thank you!
The issue is a bit hidden by the fact that you don't log the exception before returning an empty list. If you logged that exception you would have gotten this:
java.lang.RuntimeException: Failed to invoke private arrow.core.Option() with no args
This means that Gson doesn't know how to create an Option class because it has no public empty constructor. Indeed, Option is a sealed class (hence abstract) having 2 concrete children classes: Some and None. In order to get an instance of Option you should use one of the factory methods, like Option.just(xxx) or Option.empty() among the others.
Now, in order to fix your code you need to tell Gson how to deserialize an Option class. To do that, you need to register a type adapter to your gson object.
A possible implementation is the following:
class OptionTypeAdapter<E>(private val adapter: TypeAdapter<E>) : TypeAdapter<Option<E>>() {
#Throws(IOException::class)
override fun write(out: JsonWriter, value: Option<E>) {
when (value) {
is Some -> adapter.write(out, value.t)
is None -> out.nullValue()
}
}
#Throws(IOException::class)
override fun read(input: JsonReader): Option<E> {
val peek = input.peek()
return if (peek != JsonToken.NULL) {
Option.just(adapter.read(input))
} else {
input.nextNull()
Option.empty()
}
}
companion object {
fun getFactory() = object : TypeAdapterFactory {
override fun <T> create(gson: Gson, type: TypeToken<T>): TypeAdapter<T>? {
val rawType = type.rawType as Class<*>
if (rawType != Option::class.java) {
return null
}
val parameterizedType = type.type as ParameterizedType
val actualType = parameterizedType.actualTypeArguments[0]
val adapter = gson.getAdapter(TypeToken.get(actualType))
return OptionTypeAdapter(adapter) as TypeAdapter<T>
}
}
}
}
You can use it in the following way:
fun main(args: Array<String>) {
val gson = GsonBuilder()
.registerTypeAdapterFactory(OptionTypeAdapter.getFactory())
.create()
val result: List<Book> = try {
gson.fromJson(json, TypeToken.getParameterized(List::class.java, Book::class.java).type)
} catch (e: Exception) {
e.printStackTrace()
emptyList()
}
println(result)
}
That code outputs:
[Book(title=Book100, author=Author100, borrower=Some(Borrower(name=Borrower100, maxBooks=100))), Book(title=Book200, author=Author200, borrower=None)]

Binding custom data type transmit null to converter

I'm using Jooq and Kotlin in my project. I have object EventEnvelope in which field of type Event is composed. I want to store this field as JSON in my DB (postgres). I prepared jooq custom datatype bindings and converter as it is described here -> https://www.jooq.org/doc/3.10/manual/code-generation/custom-data-type-bindings/
Below I paste converter, binding and gradle generator code.
My questions are:
Is it ok to use kotlin non null types with jooq bindings?
Is this configuration ok? What should I change?
When I want to store value my converter gets null in from func. I don't why is that.
I am out of ideas what should I do to fix it.
class JSONEventConverter constructor(
private val objectMapper: ObjectMapper,
private val schemaMatcher: SchemaMatcher
) : Converter<Any, Event> {
override fun from(databaseObject: Any): Event {
return schemaMatcher.parse(databaseObject.toString())
}
override fun to(userObject: Event): Any {
return objectMapper.writeValueAsString(userObject)
}
override fun fromType(): Class<Any> {
return Any::class.java
}
override fun toType(): Class<Event> {
return Event::class.java
}
companion object {
fun create(): JSONEventConverter {
return JSONEventConverter(jacksonObjectMapper(),
SchemaMatcher.create())
}
}
}
class PostgresJSONEventBinding : Binding<Any, Event> {
override fun register(ctx: BindingRegisterContext<Event>?) {
ctx!!.statement().registerOutParameter(ctx.index(), Types.VARCHAR)
}
override fun sql(ctx: BindingSQLContext<Event>?) {
ctx!!.render().visit(DSL.`val`(ctx.convert(converter())
.value())).sql("::json")
}
override fun converter(): Converter<Any, Event> {
return JSONEventConverter.create()
}
override fun get(ctx: BindingGetResultSetContext<Event>?) {
ctx!!.convert(converter())
.value(ctx.resultSet().getString(ctx.index()))
}
override fun get(ctx: BindingGetStatementContext<Event>?) {
ctx!!.convert(converter())
.value(ctx.statement().getString(ctx.index()))
}
override fun get(ctx: BindingGetSQLInputContext<Event>?) {
throw SQLFeatureNotSupportedException()
}
override fun set(ctx: BindingSetStatementContext<Event>?) {
ctx!!.statement().setString(ctx.index(),
Objects.toString(ctx.convert(converter()).value(), null))
}
override fun set(ctx: BindingSetSQLOutputContext<Event>?) {
throw SQLFeatureNotSupportedException()
}
}
generator {
name = 'org.jooq.util.DefaultGenerator'
strategy {
name = 'org.jooq.util.DefaultGeneratorStrategy'
}
database {
name = 'org.jooq.util.postgres.PostgresDatabase'
schemata {
schema {
inputSchema = someSchema
}
schema {
inputSchema = otherSchema
}
}
forcedTypes {
forcedType {
userType = 'package.Event'
binding = 'package.PostgresJSONEventBinding'
expression = 'someSchema\\.event_store\\.event'
}
}
}
generate {
relations = true
deprecated = false
records = true
immutablePojos = true
fluentSetters = true
}
target {
packageName = appName
}
}
Is it ok to use kotlin non null types with jooq bindings?
jOOQ (or any Java library) will not respect your Kotlin non-nullable guarantees and might produce null values where you wouldn't expect them. So, perhaps it's not a good idea after all.
At the interface between jOOQ and your code, you must ensure yourself that this cannot happen.
Is this configuration ok? What should I change?
That's an open ended question. If you have any specific questions, please ask.
When I want to store value my converter gets null in from func. I don't why is that.
There are not enough infos in your question to help you about this
Ok so in my case it was about java-kotlin interoperability between nullable types in Java and non-null types in kotlin. All I had to do was implementing converter using nullable types in kotlin (the ones with ?).
Correct converter look like this:
class JSONEventConverter constructor(
private val objectMapper: ObjectMapper,
private val schemaMatcher: SchemaMatcher
) : Converter<Any, Event> {
override fun from(databaseObject: Any?): Event? {
return databaseObject?.let { schemaMatcher.parse(it.toString()) }
}
override fun to(userObject: Event?): Any? {
return userObject?.let { objectMapper.writeValueAsString(it) }
}
override fun fromType(): Class<Any> {
return Any::class.java
}
override fun toType(): Class<Event> {
return Event::class.java
}
companion object {
fun create(): JSONEventConverter {
return JSONEventConverter(serializingObjectMapper(),
SchemaMatcher.create())
}
}
}

Kotlin DSL for creating json objects (without creating garbage)

I am trying to create a DSL for creating JSONObjects. Here is a builder class and a sample usage:
import org.json.JSONObject
fun json(build: JsonObjectBuilder.() -> Unit): JSONObject {
val builder = JsonObjectBuilder()
builder.build()
return builder.json
}
class JsonObjectBuilder {
val json = JSONObject()
infix fun <T> String.To(value: T) {
json.put(this, value)
}
}
fun main(args: Array<String>) {
val jsonObject =
json {
"name" To "ilkin"
"age" To 37
"male" To true
"contact" To json {
"city" To "istanbul"
"email" To "xxx#yyy.com"
}
}
println(jsonObject)
}
The output of the above code is :
{"contact":{"city":"istanbul","email":"xxx#yyy.com"},"name":"ilkin","age":37,"male":true}
It works as expected. But it creates an additional JsonObjectBuilder instance every time it creates a json object. Is it possible to write a DSL for creating json objects without extra garbage?
You can use a Deque as a stack to track your current JSONObject context with a single JsonObjectBuilder:
fun json(build: JsonObjectBuilder.() -> Unit): JSONObject {
return JsonObjectBuilder().json(build)
}
class JsonObjectBuilder {
private val deque: Deque<JSONObject> = ArrayDeque()
fun json(build: JsonObjectBuilder.() -> Unit): JSONObject {
deque.push(JSONObject())
this.build()
return deque.pop()
}
infix fun <T> String.To(value: T) {
deque.peek().put(this, value)
}
}
fun main(args: Array<String>) {
val jsonObject =
json {
"name" To "ilkin"
"age" To 37
"male" To true
"contact" To json {
"city" To "istanbul"
"email" To "xxx#yyy.com"
}
}
println(jsonObject)
}
Example output:
{"contact":{"city":"istanbul","email":"xxx#yyy.com"},"name":"ilkin","age":37,"male":true}
Calling json and build across multiple threads on a single JsonObjectBuilder would be problematic but that shouldn't be a problem for your use case.
Do you need a DSL? You lose the ability to enforce String keys, but vanilla Kotlin isn't that bad :)
JSONObject(mapOf(
"name" to "ilkin",
"age" to 37,
"male" to true,
"contact" to mapOf(
"city" to "istanbul",
"email" to "xxx#yyy.com"
)
))
Updated on Jan 11 2023:
Replaced infix fun String.to(json: Json -> Unit) with infix fun String.to(json: Json.() -> Unit) which uses Json block as receiver and invokes after a Json object is created. So no longe need to add Json key inside Json object.
I am not sure if I get the question correctly. You don't want a builder?
import org.json.JSONArray
import org.json.JSONObject
class Json() : JSONObject() {
constructor(init: Json.() -> Unit) : this() {
this.init()
}
infix fun String.to(json: Json.() -> Unit) {
put(this, Json().apply(json))
}
infix fun <T> String.to(value: T) {
put(this, value)
}
infix fun <T> String.to(values: List<T>) {
put(this, JSONArray().apply {
values.forEach { put(it) }
})
}
}
fun main(args: Array<String>) {
val json = Json {
"name" to "Roy"
"body" to {
"height" to 173
"weight" to 80
}
"cars" to listOf(
"Tesla"
"Porsche"
"BMW"
"Ferrari"
)
}
println(json)
}
You will get
{
"name": "Roy",
"body": {
"weight": 80,
"height": 173
},
"cars": [
"Tesla",
"Porsche",
"BMW",
"Ferrari"
]
}
Yes, it is possible if you don't need any intermediate representation of the nodes, and if the context is always the same (the recursive calls are no different from each other). This can be done by writing the output immediately.
However, this severely increases code complexity, because you have to process your DSL calls right away without storing them anywhere (again, to avoid redundant objects).
Example (see its demo here):
class JsonContext internal constructor() {
internal val output = StringBuilder()
private var indentation = 4
private fun StringBuilder.indent() = apply {
for (i in 1..indentation)
append(' ')
}
private var needsSeparator = false
private fun StringBuilder.separator() = apply {
if (needsSeparator) append(",\n")
}
infix fun String.to(value: Any) {
output.separator().indent().append("\"$this\": \"$value\"")
needsSeparator = true
}
infix fun String.toJson(block: JsonContext.() -> Unit) {
output.separator().indent().append("\"$this\": {\n")
indentation += 4
needsSeparator = false
block(this#JsonContext)
needsSeparator = true
indentation -= 4
output.append("\n").indent().append("}")
}
}
fun json(block: JsonContext.() -> Unit) = JsonContext().run {
block()
"{\n" + output.toString() + "\n}"
}
val j = json {
"a" to 1
"b" to "abc"
"c" toJson {
"d" to 123
"e" toJson {
"f" to "g"
}
}
}
If you don't need indentation but only valid JSON, this can be easily simplified, though.
You can make the json { } and .toJson { } functions inline to get rid even of the lambda classes and thus you achieve almost zero object overhead (one JsonContext and the StringBuilder with its buffers are still allocated), but that would require you to change the visibility modifiers of the members these functions use: public inline functions can only access public or #PublishedApi internal members.
Found another solution. You can just inherit JSONObject class without need to create other objects.
class Json() : JSONObject() {
constructor(init: Json.() -> Unit) : this() {
this.init()
}
infix fun <T> String.To(value: T) {
put(this, value)
}
}
fun main(args: Array<String>) {
val jsonObject =
Json {
"name" To "ilkin"
"age" To 37
"male" To true
"contact" To Json {
"city" To "istanbul"
"email" To "xxx#yyy.com"
}
}
println(jsonObject)
}
The output of code will be the same.
{"contact":{"city":"istanbul","email":"xxx#yyy.com"},"name":"ilkin","age":37,"male":true}
UPD: If you use gson library you can look at this awesome library. It doesn't create any garbage, source code is easy to read and understand.
You could use a library such as https://github.com/holgerbrandl/jsonbuilder to build json with
val myJson = json {
"size" to 0
"array" to arrayOf(1,2,3)
"aggs" to {
"num_destinations" to {
"cardinality" to {
"field" to "DestCountry"
}
}
}
}
Disclaimer: I'm the author of the library.