6

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?

Smile
  • 3,832
  • 3
  • 25
  • 39
Jerald Baker
  • 1,121
  • 1
  • 12
  • 48
  • Doesn't this throw some error already since this is simply invalid JSON (based on the missing quotes) – luk2302 Sep 28 '20 at 11:35
  • it's not an invalid json right? Example, boolean values should be without quotes, numbers should be without quotes.. – Jerald Baker Sep 28 '20 at 11:38
  • Booleans and numbers (and null) are exactly the two things allowed without quotes, "asdadasd" is not true, false, null or a number -> invalid. Check https://jsonlint.com/ – luk2302 Sep 28 '20 at 11:40
  • Okay, sorry my bad. I have edited my question. The String field is now 1234. and this must be double quoted as the the field of the POJO is of type String. and i expect objectMapper to throw an error.. can i enforce it? – Jerald Baker Sep 28 '20 at 11:44
  • Which version of `Jackson` do you use? – Michał Ziober Sep 29 '20 at 15:43
  • jackson-databind: 2.11.1 – Jerald Baker Sep 30 '20 at 06:34
  • 1
    I think this [feature](https://github.com/FasterXML/jackson-databind/issues/2113) is slated be released as part of Jackson 2.12.x. Till it is released, answers on this question should suffice. – Smile Oct 07 '20 at 11:57

3 Answers3

1

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;
    }
}
divilipir
  • 882
  • 6
  • 17
0

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();
    }
}
Smile
  • 3,832
  • 3
  • 25
  • 39
SauriBabu
  • 414
  • 6
  • 15
  • 2
    Why it's is down voted ? I have spent hours to figure this and tried at my side before Posting . Its perfectly working solution. – SauriBabu Oct 06 '20 at 16:23
0

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

Rohitdev
  • 866
  • 6
  • 15