79624962

Date: 2025-05-16 10:18:53
Score: 0.5
Natty:
Report link

In Java:

Gson gson = new GsonBuilder().registerTypeHierarchyAdapter(Integer.class, new StrictStringDeserializer())
.create();

//below line will tell gson that all incoming object would me a map with key as string and value as Ineteger for any json - this one is important so gsonn will aply our custom JsonDeserializer to our Map class instead of using default one

 final Type type = new TypeToken<Map<String, Integer>>() {}.getType();

 String str1 =
                   """
                   {"key": 123}
                   """;
        Map map = gson.fromJson(str1, type); 

public class StrictStringDeserializer implements JsonDeserializer<Integer> {
    @Override
    public Integer deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
       if (jsonElement.toString().startsWith("\"")) {
           throw new JsonParseException("bad");
       }
        return jsonElement.getAsInt();
    }
}

// Cheers! )
Reasons:
  • Blacklisted phrase (1): Cheers
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: user3072571