Skip to content

Instantly share code, notes, and snippets.

@avaz
Last active June 21, 2022 16:29
Show Gist options
  • Save avaz/7d73787dd047ea1eefcf232a9909534f to your computer and use it in GitHub Desktop.
Save avaz/7d73787dd047ea1eefcf232a9909534f to your computer and use it in GitHub Desktop.
Example of how to map a MySQL SQL JSON column to a Java Map in JPA
package net.taus.corporate.model.persistence.entities;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.persistence.*;
import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
@Data
@Embeddable
public class ExtraInformation implements Serializable {
@Column(name = "extra_data", columnDefinition = "json")
@Convert(converter = JsonStringToMapConverter.class)
private Map<String, Object> value;
public ExtraInformation() {
this.value = new HashMap<>();
}
public ExtraInformation(final Map<String, Object> value) {
this.value = value;
}
public void putAll(final ExtraInformation extra) {
this.value.putAll(extra.getValue());
}
public static class JsonStringToMapConverter implements AttributeConverter<Map<String, Object>, String> {
private static final Logger LOGGER = LoggerFactory.getLogger(JsonStringToMapConverter.class);
private static final ObjectMapper MAPPER = new ObjectMapper();
@Override
public String convertToDatabaseColumn(Map<String, Object> map) {
try {
return MAPPER.writeValueAsString(map);
}
catch (JsonProcessingException e) {
throw new PersistenceException(String.format("Error to convert %s to json format", map));
}
}
@Override
public Map<String, Object> convertToEntityAttribute(String jsonNodeString) {
Map<String, Object> map = null;
if (Objects.nonNull(jsonNodeString) && !jsonNodeString.isBlank()) {
try {
TypeReference<HashMap<String, Object>> typeRef = new TypeReference<>() {
};
map = MAPPER.readValue(jsonNodeString, typeRef);
}
catch (JsonProcessingException e) {
LOGGER.error("Error parsing jsonNodeString", e);
}
}
return map;
}
}
public static class ExtraInformationSerializer extends StdSerializer<ExtraInformation> {
public ExtraInformationSerializer() {
super(ExtraInformation.class);
}
@Override
public void serialize(final ExtraInformation value, final JsonGenerator gen, final SerializerProvider provider) throws IOException {
gen.writeObject(value.getValue());
}
}
public static class ExtraInformationDeserializer extends StdDeserializer<ExtraInformation> {
public ExtraInformationDeserializer() {
super(ExtraInformation.class);
}
@Override
@SuppressWarnings("unchecked")
public ExtraInformation deserialize(final JsonParser p, final DeserializationContext ctxt) throws IOException {
return new ExtraInformation(p.readValueAs(HashMap.class));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment