Because a single converter instance can convert mu...
# klaxon
k
Because a single converter instance can convert multiple different types.
a
Can't we just use implementation parametrized with
Any
in this case (e.g.
DefaultConverter
), and we will have types in other cases?
Copy code
import com.beust.klaxon.JsonValue

interface Test<T> {
    
    fun fromJson(jv: JsonValue): T
    
    fun toJson(value: T): JsonValue
}

class TestImpl: Test<Any> {
    override fun fromJson(jv: JsonValue): Any {
        TODO("not implemented")
    }

    override fun toJson(value: Any): JsonValue {
        TODO("not implemented") 
    }
}
k
But what about the
canConvert
function then?
And if you add that you might as well remove the illusion of type safety entirely.
a
Copy code
override fun canConvert(cls: Class<*>) = true
seems absurd too. With generic interface you can create custom converter that returns object of concrete class, I see type safety here
k
Is that how you implement that method? Its supposed to be
... = cls == MyType::class
or something like that.
Or for multiple types
= cls == MyType::class.java || cls == MyOtherType::class.java
.
a
k
Well that's a special case since it's the fallback one, but it's indeed strange that it returns
true
.
For custom converter implementations you usually don't do that
a
Thanks for clarification) however it seems somewhat strange that you're writing converter that creates objects of single class and after convertion you must to cast this object. Need time to reconcile with this)
k
If you want you can write a wrapper abstract class that takes in the correct class and then places more restrictions on the functions.
c
FYI, I went back and forth between making the converter generic or not, and it was generic in the previous version of Klaxon. However, I decided that the current implementation is more flexible
👍 2