Throw error if strings are not double quoted while using jackson objectmapper deserialization

huangapple go评论99阅读模式
英文:

Throw error if strings are not double quoted while using jackson objectmapper deserialization

问题

  1. 我有一个JSON
  2. {
  3. "stringField" : 1234,
  4. "booleanField": true,
  5. "numberField": 1200.00
  6. }
  7. 我使用对象映射器将JSON反序列化为:
  8. @Data
  9. class SomeClass {
  10. String stringField;
  11. boolean booleanField;
  12. float numberField;
  13. }
  14. 我希望对象映射器抛出错误,因为根据JSON规范,字符串字段的值必须用双引号括起来。如何让对象映射器抛出错误?
英文:

I have a JSON:

  1. {
  2. "stringField" : 1234,
  3. "booleanField": true,
  4. "numberField": 1200.00
  5. }

I use object mapper to deserialize the json into:-

  1. @Data
  2. class SomeClass {
  3. String stringField;
  4. boolean booleanField;
  5. float numberField;
  6. }

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)

  1. @Configuration
  2. public class JacksonConfiguration {
  3. @Bean
  4. SimpleModule jacksonDeserializerConfiguration() {
  5. SimpleModule module = new SimpleModule();
  6. module.addDeserializer(String.class, new StdDeserializer<String>(String.class) {
  7. @Override
  8. public String deserialize(JsonParser parser, DeserializationContext context)
  9. throws IOException {
  10. if (!parser.hasToken(JsonToken.VALUE_STRING)) {
  11. // 抛出异常,你想要什么样的异常
  12. throw new RuntimeException("字符串不包含引号");
  13. }
  14. return StringDeserializer.instance.deserialize(parser, context);
  15. }
  16. });
  17. return module;
  18. }
  19. }
英文:

You can write custom string deserializer.(i assume you are using spring)

  1. @Configuration
  2. public class JacksonConfiguration {
  3. @Bean
  4. SimpleModule jacksonDeserializerConfiguration() {
  5. SimpleModule module = new SimpleModule();
  6. module.addDeserializer(String.class, new StdDeserializer&lt;String&gt;(String.class) {
  7. @Override
  8. public String deserialize(JsonParser parser, DeserializationContext context)
  9. throws IOException {
  10. if (!parser.hasToken(JsonToken.VALUE_STRING)) {
  11. //throw ex what do u want
  12. throw new RuntimeException(&quot;String not include quote&quot;);
  13. }
  14. return StringDeserializer.instance.deserialize(parser, context);
  15. }
  16. });
  17. return module;
  18. }
  19. }

答案2

得分: 0

这应该可以解决你的问题。

  1. class SomeClass {
  2. @JsonDeserialize(using=ForceStringDeserializer.class)
  3. public String stringField;
  4. public boolean booleanField;
  5. public float numberField;
  6. }
  7. class ForceStringDeserializer extends JsonDeserializer<String> {
  8. @Override
  9. public String deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
  10. if (jsonParser.getCurrentToken() != JsonToken.VALUE_STRING) {
  11. throw deserializationContext.wrongTokenException(jsonParser, JsonToken.VALUE_STRING, "尝试将整数解析为字符串,但是这是被禁止的");
  12. }
  13. return jsonParser.getValueAsString();
  14. }
  15. }
英文:

This should fix your issue.

  1. class SomeClass {
  2. @JsonDeserialize(using=ForceStringDeserializer.class)
  3. public String stringField;
  4. public boolean booleanField;
  5. public float numberField;
  6. }
  7. class ForceStringDeserializer extends JsonDeserializer&lt;String&gt; {
  8. @Override
  9. public String deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
  10. if (jsonParser.getCurrentToken() != JsonToken.VALUE_STRING) {
  11. throw deserializationContext.wrongTokenException(jsonParser, JsonToken.VALUE_STRING, &quot;Attempted to parse Integer to String but this is forbidden&quot;);
  12. }
  13. return jsonParser.getValueAsString();
  14. }
  15. }

答案3

得分: 0

你只需要像这样设置 jackson objectmapper

  1. JsonFactory factory = new JsonFactory();
  2. factory.disable(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES);
  3. ObjectMapper mapper = new ObjectMapper(factory)

这样在序列化/反序列化过程中应该会抛出错误。

英文:

You just need to setup jackson objectmapper like this

  1. JsonFactory factory = new JsonFactory();
  2. factory.disable(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES);
  3. ObjectMapper mapper = new ObjectMapper(factory)

This should throw error during serialization/deserilaization

huangapple
  • 本文由 发表于 2020年9月28日 19:34:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/64101409.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定