If you have a function that return the Enum from t...
# kondor-json
u
If you have a function that return the Enum from the label you can write a JTitleType converter:
Copy code
object JTitleType : JStringRepresentable<TitleType>() {
    override val cons: (String) -> TitleType = ???
    override val render: (TitleType) -> String = TitleType::label
}
r
Yes I have the function
Copy code
enum class TitleType(val label: String) {
    Movie("movie"), Series("series"), Episoode("episode");

    companion object {
        fun fromLabel(label: String) = values().firstOrNull { it.label == label}
    }
}
but returns a null able type and JStringRepresetable requires an Any… well i could make it throw instead (although I would prefer null) but that does not fix everything still can see to write the
JTitleRequest
type
to use it…. That would be somethig like:
Copy code
data class TitleRequest(
    val id: String,
    val type: TitleType?
)

enum class TitleType(val label: String) {
    Movie("movie"), Series("series"), Episoode("episode");

    companion object {
        fun fromLabel(label: String) = values().first { it.label == label}
    }
}

object JTitleType : JStringRepresentable<TitleType>() {
    override val cons: (String) -> TitleType = ::fromLabel
    override val render: (TitleType) -> String = TitleType::label
}

object JTitleRequest : JAny<TitleRequest>() {
    private val id by str(TitleRequest::id)

    private val type by str(???)

    override fun JsonNodeObject.deserializeOrThrow(): TitleRequest =
        TitleRequest(
            id = +id,
            type = TitleType.fromLabel(+type)
        )
}
u
reg the nullable -> so in case of an unknown label you do want a null? There is no support for that but I can think about it.
Copy code
data class TitleRequest(
    val id: String,
    val type: TitleType?
)

enum class TitleType(val label: String) {
    Movie("movie"), Series("series"), Episoode("episode");

    companion object {
        fun fromLabel(label: String) = values().first { it.label == label}
    }
}

object JTitleType : JStringRepresentable<TitleType>() {
    override val cons: (String) -> TitleType = ::fromLabel
    override val render: (TitleType) -> String = TitleType::label
}

object JTitleRequest : JAny<TitleRequest>() {
    private val id by str(TitleRequest::id)

    private val type by str(TitleRequest::type)

    override fun JsonNodeObject.deserializeOrThrow(): TitleRequest =
        TitleRequest(
            id = +id,
            type = +type
        )
}
this compiles to me, what's the problem?
r
Thanks. but yet
Copy code
private val type by str(TitleRequest::type)
doesn't do the job
Copy code
@Test
    fun aTest() {
        val inValue = TitleRequest("tom", TitleType.Movie)
        val json = JTitleRequest.toPrettyJson(inValue)

        json shouldBe """
            {
              "id": "tom",
              "type": "movie"
            }
        """.trimIndent()
    }
fails as it encodes the Enum name
Movies
instead the label
movie
👍 1
Copy code
expected:<"{
  "id": "tom",
  "type": "movie"
}"> but was:<"{
  "id": "tom",
  "type": "Movie"
}">