ObjectMapper.readTree不会在无效输入上引发IOException

问题描述 投票:1回答:1

我正在使用Jackson数据绑定2.9.10。根据文档,它指出:

如果发生解析问题(无效的JSON),将抛出JsonParseException

参见:ObjectMapper.readTree

我有以下测试:

    // Given
    String invalidJson = new POJONode("}{").toString();
    final InputStream mockInputStream = new ByteArrayInputStream(invalidJson.getBytes());

    // When
    myLambda.handleRequest(mockInputStream, mockOutputStream, mockContext);

    // Then
    Expect some stuff to happen in the catch JsonProcessingException bit

但是,readTree将其转换为TextNode,并且不会引发异常...

调试:enter image description here

如何配置ObjectMapper以抛出无效输入?

java jackson jackson-databind
1个回答
0
投票

当打​​印invalidJson变量时,您会看到"}{",它实际上是对应于JSON的有效json.org。您也可以使用jsonformatter等网络工具来确认。直接将invalidJson变量设置为此字符串时,解析器将引发异常:

String invalidJson = "}{";

您应该看到:

Exception in thread "main" com.fasterxml.jackson.core.JsonParseException: Unexpected close marker '}': expected ']' (for root starting at [Source: (String)"}{"; line: 1, column: 0])
 at [Source: (String)"}{"; line: 1, column: 2]
© www.soinside.com 2019 - 2024. All rights reserved.