Hello everyone, I think I had a solution for the f...
# jackson-kotlin
p
Hello everyone, I think I had a solution for the following problem some time ago but I can’t think of one right now, so here I am: I have the following structure:
Copy code
sealed class ApiResult {
    data class Error(val errors: List<ApiError>) : ApiResult() {
        data class ApiError(
            val propertyName: String,
            val errorMessage: String,
            val errorCode: String,
        )
    }
}
And I want to serialize some response json into 
ApiResult.Error
. The Json looks like that:
Copy code
[
  {
    "propertyName": "...",
    "errorMessage": "...",
    "errorCode": "..."
  }
]
So I have an object with holds a list of elements. Instantiating the object manually would look like that:
Copy code
ApiResult.Error(objectMapper.readValue(""))
But I want to serialize directly into
ApiResultError
. Usually I would do something along the lines of:
Copy code
class ApiErrors: ArrayList<ApiErrors.ApiError>() {
        data class ApiError(
            val propertyName: String,
            val errorMessage: String,
            val errorCode: String,
        )
So, implementing the ArrayList. This does not work inside a sealed class. Any idea? Thanks in advance!
d
You might be able to make your error class look like this and get the behavior you want
That pair of annotations kind-of makes the
Error
object transparent
p
Unfortunately,
@JsonCreator
is not applicable to target ‘class’
d
Split out the constructor and apply it to that
Untitled
p
Ah, I should’ve thought about that too. Thanks, works!
d
It’s a weird syntax, not Kotlin’s best work (though necessary optimization for the more common use case)