英文:
Throw error if strings are not double quoted while using jackson objectmapper deserialization
问题
我有一个JSON:
{
"stringField" : 1234,
"booleanField": true,
"numberField": 1200.00
}
我使用对象映射器将JSON反序列化为:
@Data
class SomeClass {
String stringField;
boolean booleanField;
float numberField;
}
我希望对象映射器抛出错误,因为根据JSON规范,字符串字段的值必须用双引号括起来。如何让对象映射器抛出错误?
英文:
I have a JSON:
{
"stringField" : 1234,
"booleanField": true,
"numberField": 1200.00
}
I use object mapper to deserialize the json into:-
@Data
class SomeClass {
String stringField;
boolean booleanField;
float numberField;
}
I would like the objectMapper to throw an error because, the values for String fields must be double quoted according to the json spec. How can i get objectMapper to throw an error?
答案1
得分: 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 new RuntimeException("字符串不包含引号");
}
return StringDeserializer.instance.deserialize(parser, context);
}
});
return module;
}
}
英文:
You can write custom string deserializer.(i assume you are using 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;
}
}
答案2
得分: 0
这应该可以解决你的问题。
class SomeClass {
@JsonDeserialize(using=ForceStringDeserializer.class)
public String stringField;
public boolean booleanField;
public float numberField;
}
class ForceStringDeserializer extends JsonDeserializer<String> {
@Override
public String deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
if (jsonParser.getCurrentToken() != JsonToken.VALUE_STRING) {
throw deserializationContext.wrongTokenException(jsonParser, JsonToken.VALUE_STRING, "尝试将整数解析为字符串,但是这是被禁止的");
}
return jsonParser.getValueAsString();
}
}
英文:
This should fix your issue.
class SomeClass {
@JsonDeserialize(using=ForceStringDeserializer.class)
public String stringField;
public boolean booleanField;
public float numberField;
}
class ForceStringDeserializer extends JsonDeserializer<String> {
@Override
public String deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
if (jsonParser.getCurrentToken() != JsonToken.VALUE_STRING) {
throw deserializationContext.wrongTokenException(jsonParser, JsonToken.VALUE_STRING, "Attempted to parse Integer to String but this is forbidden");
}
return jsonParser.getValueAsString();
}
}
答案3
得分: 0
你只需要像这样设置 jackson objectmapper
JsonFactory factory = new JsonFactory();
factory.disable(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES);
ObjectMapper mapper = new ObjectMapper(factory)
这样在序列化/反序列化过程中应该会抛出错误。
英文:
You just need to setup jackson objectmapper like this
JsonFactory factory = new JsonFactory();
factory.disable(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES);
ObjectMapper mapper = new ObjectMapper(factory)
This should throw error during serialization/deserilaization
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论