Created
August 22, 2019 09:02
-
-
Save talalUcef/9aa66d707ac62f957b4b45812d2acaa4 to your computer and use it in GitHub Desktop.
Jackson empry Array to Map deserialization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.red.bol.serializer; | |
import com.fasterxml.jackson.core.JsonParser; | |
import com.fasterxml.jackson.databind.DeserializationContext; | |
import com.fasterxml.jackson.databind.JsonDeserializer; | |
import org.apache.commons.lang3.StringUtils; | |
import java.io.IOException; | |
import java.util.HashMap; | |
import java.util.Map; | |
public class ArrayToMapDeserializer extends JsonDeserializer<Map<?, ?>> { | |
@Override | |
public Map<?, ?> deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException { | |
final String text = jsonParser.getText(); | |
if (StringUtils.isBlank(text) || text.contains("[")) { | |
return new HashMap<>(); | |
} | |
return jsonParser.readValueAs(Map.class); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@JsonDeserialize(using = ArrayToMapDeserializer.class) | |
private Map<?, ?> map; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment