fix strict json parsing

This commit is contained in:
NikkyAI 2018-11-24 23:39:54 +01:00
parent 04bc439880
commit a0b0e3d24d
2 changed files with 8 additions and 8 deletions

View File

@ -29,13 +29,17 @@ data class ApiMessage(
) {
fun encode(): String {
return JSON.stringify(Companion, this)
return JSON.nonstrict.stringify(ApiMessage.serializer(), this)
}
override fun toString(): String = encode()
@Serializer(forClass = ApiMessage::class)
companion object {
val USER_ACTION = "user_action"
val JOIN_LEAVE = "join_leave"
override fun serialize(output: Encoder, obj: ApiMessage) {
val elemOutput = output.beginStructure(descriptor)
obj.username.takeIf { it.isNotEmpty() }?.let {
@ -77,12 +81,8 @@ data class ApiMessage(
elemOutput.endStructure(descriptor)
}
val USER_ACTION = "user_action"
val JOIN_LEAVE = "join_leave"
fun decode(json: String): ApiMessage {
return JSON.nonstrict.parse(Companion, json)
fun decode(input: String): ApiMessage {
return JSON.nonstrict.parse(ApiMessage.serializer(), input)
}
}
}

View File

@ -110,7 +110,7 @@ open class MessageHandler : CoroutineScope {
when (result) {
is Result.Success -> {
val messages: List<ApiMessage> = JSON.parse(ApiMessage.list, result.value)
val messages: List<ApiMessage> = JSON.nonstrict.parse(ApiMessage.list, result.value)
messages.forEach { msg ->
logger.trace("skipping $msg")
}