fix endless parsing

This commit is contained in:
a10zn8
2024-03-01 12:48:45 +03:00
parent 10cbb999eb
commit 3c475c63cd
2 changed files with 35 additions and 12 deletions

View File

@@ -164,19 +164,30 @@ abstract class ResponseParser<T> {
break break
} }
val token = parser.nextToken() val token = parser.nextToken()
if (token == JsonToken.START_OBJECT) { when (token) {
null -> {
return null
}
JsonToken.START_OBJECT -> {
inited = true inited = true
count++ count++
continue continue
} }
if (token == JsonToken.END_OBJECT) { JsonToken.END_OBJECT -> {
count-- count--
continue continue
} }
if (token == JsonToken.VALUE_NULL) { JsonToken.VALUE_NULL -> {
// error is just null
return null return null
} }
JsonToken.VALUE_STRING -> {
if (!inited) {
message = parser.valueAsString
break
}
}
else -> {}
}
val field = parser.currentName() val field = parser.currentName()
if (field == "code" && token == JsonToken.VALUE_NUMBER_INT) { if (field == "code" && token == JsonToken.VALUE_NUMBER_INT) {
code = parser.intValue code = parser.intValue

View File

@@ -223,4 +223,16 @@ class ResponseRpcParserSpec extends Specification {
!act.hasResult() !act.hasResult()
} }
def "Parse non object error"() {
setup:
def json = '{"jsonrpc": "2.0", "id": 1, "error": "plain error"}'
when:
def act = parser.parse(json.getBytes())
then:
act.error != null
act.error.message == "plain error"
act.hasError()
!act.hasResult()
}
} }