Jer*_*ker 6 java jackson objectmapper
我有一个 JSON:
{
"stringField" : 1234,
"booleanField": true,
"numberField": 1200.00
}
Run Code Online (Sandbox Code Playgroud)
我使用对象映射器将 json 反序列化为:-
@Data
class SomeClass {
String stringField;
boolean booleanField;
float numberField;
}
Run Code Online (Sandbox Code Playgroud)
我希望 objectMapper 抛出错误,因为根据 json 规范,字符串字段的值必须双引号。我怎样才能让 objectMapper 抛出错误?
小智 1
你可以编写自定义字符串反序列化器。(我假设你正在使用spring)
@Configuration
public class JacksonConfiguration {
@Bean
SimpleModule jacksonDeserializerConfiguration() {
SimpleModule module = new SimpleModule();
module.addDeserializer(String.class, new StdDeserializer<String>(String.class) {
@Override
public String deserialize(JsonParser parser, DeserializationContext context)
throws IOException {
if (!parser.hasToken(JsonToken.VALUE_STRING)) {
//throw ex what do u want
throw new RuntimeException("String not include quote");
}
return StringDeserializer.instance.deserialize(parser, context);
}
});
return module;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
327 次 |
| 最近记录: |