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

View File

@ -110,7 +110,7 @@ open class MessageHandler : CoroutineScope {
when (result) { when (result) {
is Result.Success -> { 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 -> messages.forEach { msg ->
logger.trace("skipping $msg") logger.trace("skipping $msg")
} }