Kotlin + Arrow + Gson = None? - kotlin

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)]

Related

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

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 }
}
}

How to handle Kotlin Jetpack Paging 3 exceptions?

I am new to kotlin and jetpack, I am requested to handle errors (exceptions) coming from the PagingData, I am not allowed to use Flow, I am only allowed to use LiveData.
This is the Repository:
class GitRepoRepository(private val service: GitRepoApi) {
fun getListData(): LiveData<PagingData<GitRepo>> {
return Pager(
// Configuring how data is loaded by adding additional properties to PagingConfig
config = PagingConfig(
pageSize = 20,
enablePlaceholders = false
),
pagingSourceFactory = {
// Here we are calling the load function of the paging source which is returning a LoadResult
GitRepoPagingSource(service)
}
).liveData
}
}
This is the ViewModel:
class GitRepoViewModel(private val repository: GitRepoRepository) : ViewModel() {
private val _gitReposList = MutableLiveData<PagingData<GitRepo>>()
suspend fun getAllGitRepos(): LiveData<PagingData<GitRepo>> {
val response = repository.getListData().cachedIn(viewModelScope)
_gitReposList.value = response.value
return response
}
}
In the Activity I am doing:
lifecycleScope.launch {
gitRepoViewModel.getAllGitRepos().observe(this#PagingActivity, {
recyclerViewAdapter.submitData(lifecycle, it)
})
}
And this is the Resource class which I created to handle exceptions (please provide me a better one if there is)
data class Resource<out T>(val status: Status, val data: T?, val message: String?) {
companion object {
fun <T> success(data: T?): Resource<T> {
return Resource(Status.SUCCESS, data, null)
}
fun <T> error(msg: String, data: T?): Resource<T> {
return Resource(Status.ERROR, data, msg)
}
fun <T> loading(data: T?): Resource<T> {
return Resource(Status.LOADING, data, null)
}
}
}
As you can see I am using Coroutines and LiveData. I want to be able to return the exception when it occurs from the Repository or the ViewModel to the Activity in order to display the exception or a message based on the exception in a TextView.
Your GitRepoPagingSource should catch retryable errors and pass them forward to Paging as a LoadResult.Error(exception).
class GitRepoPagingSource(..): PagingSource<..>() {
...
override suspend fun load(..): ... {
try {
... // Logic to load data
} catch (retryableError: IOException) {
return LoadResult.Error(retryableError)
}
}
}
This gets exposed to the presenter-side of Paging as LoadState, which can be reacted to via LoadStateAdapter, .addLoadStateListener, etc as well as .retry. All of the presenter APIs from Paging expose these methods, such as PagingDataAdapter: https://developer.android.com/reference/kotlin/androidx/paging/PagingDataAdapter
You gotta pass your error handler to the PagingSource
class MyPagingSource(
private val api: MyApi,
private val onError: (Throwable) -> Unit,
): PagingSource<Int, MyModel>() {
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, YourModel> {
try {
...
} catch(e: Exception) {
onError(e) // <-- pass your error listener here
}
}
}

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()
}

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.