lodsve.core.json.JacksonConverter Maven / Gradle / Ivy
/*
* Copyright (C) 2018 Sun.Hao
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
package lodsve.core.json;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lodsve.core.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;
import java.io.IOException;
import java.util.Collections;
import java.util.Map;
/**
* jackson JsonUtils impl.
*
* @author sunhao(sunhao.java @ gmail.com)
* @version V1.0, 2017-12-28-0028 14:36
*/
public class JacksonConverter implements JsonConverter {
private final static Logger logger = LoggerFactory.getLogger(JacksonConverter.class);
private final static ObjectMapper OBJECT_MAPPER = new ObjectMapper();
@Override
public String toJson(Object obj) {
Assert.notNull(obj);
try {
return OBJECT_MAPPER.writeValueAsString(obj);
} catch (JsonProcessingException e) {
if (logger.isErrorEnabled()) {
logger.error(e.getMessage(), e);
}
return StringUtils.EMPTY;
}
}
@Override
public T toObject(String json, Class clazz) {
Assert.hasText(json);
Assert.notNull(clazz);
try {
return OBJECT_MAPPER.readValue(json, clazz);
} catch (IOException e) {
if (logger.isErrorEnabled()) {
logger.error(e.getMessage(), e);
}
return null;
}
}
@Override
public Map toMap(String json) {
Assert.hasText(json);
try {
return OBJECT_MAPPER.readValue(json, new TypeReference