encodeToProblemValue
fun <T> Json.encodeToProblemValue(serializer: SerializationStrategy<T>, value: T): ProblemValue(source)
Encodes value to a standalone ProblemValue. ProblemBuilder.extension does the same thing for one whole extension member; this is for callers building a ProblemValue that is not itself a member, such as one element of a ProblemArray.
Samples
val errors =
listOf(
FieldError(field = "#/age", message = "must be a positive integer"),
FieldError(field = "#/profile/color", message = "must be 'green', 'red' or 'blue'"),
)
return problem {
type = "https://example.net/validation-error"
title = "Your request is not valid."
// Each element is a typed object, not hand-built ProblemPrimitive/ProblemObject calls.
val array: ProblemValue = ProblemArray(errors.map { Json.encodeToProblemValue(it) })
extension("errors", array)
}Content copied to clipboard
Encodes value to a standalone ProblemValue, with the serializer resolved from T.