public static Gson createGsonMapper() { return new GsonBuilder() .serializeNulls() .registerTypeAdapter(Map.class, new JsonDeserializer<Map<String,Object>>() { public Map<String, Object> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) { Map<String, Object> map = new HashMap<>(); for (Map.Entry<String, JsonElement> entry : getObject(json).entrySet()) { if (entry != null) { String key = entry.getKey(); JsonElement jsonElement = entry.getValue(); if (jsonElement != null && jsonElement.isJsonNull()) { map.put(key, null); } else if (jsonElement != null && jsonElement.isJsonPrimitive()) { Object rawValue = asPrimitiveObject((JsonPrimitive) jsonElement); if (rawValue != null) { map.put(key, rawValue); } } } } return map; } }) .create(); }
public static Gson createGsonMapper() { return new GsonBuilder() .serializeNulls() .registerTypeAdapter(Map.class, new JsonDeserializer<Map<String,Object>>() { public Map<String, Object> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) { Map<String, Object> map = new HashMap<>(); for (Map.Entry<String, JsonElement> entry : getObject(json).entrySet()) { if (entry != null) { String key = entry.getKey(); JsonElement jsonElement = entry.getValue(); if (jsonElement != null && jsonElement.isJsonNull()) { map.put(key, null); } else if (jsonElement != null && jsonElement.isJsonPrimitive()) { Object rawValue = asPrimitiveObject((JsonPrimitive) jsonElement); if (rawValue != null) { map.put(key, rawValue); } } } } return map; } }) .create(); }