我正在使用Jackson数据绑定2.9.10
。根据文档,它指出:
如果发生解析问题(无效的JSON),将抛出JsonParseException
我有以下测试:
// 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
,并且不会引发异常...
如何配置ObjectMapper以抛出无效输入?
当打印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]