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! )