import java.io.IOException;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
public final class JsonUtils {
private static final Logger LOG = LoggerFactory.getLogger(JsonUtils.class);
private static final ObjectMapper mapper = new ObjectMapper();
private static final JavaType map_str;
static {
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true);
map_str = mapper.getTypeFactory().constructParametricType(Map.class, String.class, String.class);
}
final static public String toJson0(final Object obj) throws JsonProcessingException {
if(obj==null) {
return null;
}
return mapper.writeValueAsString(obj);
}
final static public Map<String, String> toMap0(final String json) throws IOException{
if(StringUtils.isBlank(json)) {
return null;
}
return mapper.readValue(json, map_str);
}
final static public String toJson(final Object obj) {
try {
return toJson0(obj);
} catch (JsonProcessingException e) {
LOG.error("Json Error toJson obj="+obj, e);
}
return null;
}
final static public Map<String, String> toMap(final String json){
try {
return toMap0(json);
} catch (IOException e) {
LOG.error("Json Error toMap json="+json, e);
}
return null;
}
public final static boolean isNoneValue(final String cacheValue) {
return cacheValue.startsWith(SFCons.Cache_Not_DB);
}
public final static String mkNoneValue(String suffix) {
return SFCons.Cache_Not_DB + SFCons.Cache_Not_Divide + suffix;
}
public final static Permission toPm0(final String json) throws IOException {
if(StringUtils.isBlank(json)) {
return null;
}
return mapper.readValue(json, Permission.class);
}
public final static Permission toPm(final String json) {
try {
return toPm0(json);
} catch (IOException e) {
LOG.error("Json Error toPm json="+json, e);
}
return null;
}
public final static CreditUser toCreditUser0(final String json) throws IOException {
if(StringUtils.isBlank(json)) {
return null;
}
return mapper.readValue(json, CreditUser.class);
}
public final static CreditUser toCreditUser(final String json) {
try {
return toCreditUser0(json);
} catch (IOException e) {
LOG.error("Json Error CreditUser json="+json, e);
}
return null;
}
}