io.github.rcarlosdasilva.kits.json.JacksonHelper Maven / Gradle / Ivy
package io.github.rcarlosdasilva.kits.json;
import java.io.File;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Strings;
/**
* JSON工具的Jackson实现
*
* @author Dean Zhao ([email protected])
*/
public class JacksonHelper extends JsonHelper {
private final ObjectMapper normalOM;
JacksonHelper() {
normalOM = new ObjectMapper();
}
@Override
public String toJson(Object obj) {
return toJson(obj, Object.class);
}
@Override
public String toJson(T obj, Class clazz) {
try {
return normalOM.writerFor(clazz).writeValueAsString(obj);
} catch (JsonProcessingException ex) {
ex.printStackTrace();
return null;
}
}
@Override
public boolean writeJosnToFile(String path, Object obj) {
return writeJsonToFile(path, obj, Object.class);
}
@Override
public boolean writeJosnToFile(File file, Object obj) {
return writeJsonToFile(file, obj, Object.class);
}
@Override
public boolean writeJsonToFile(String path, T obj, Class clazz) {
if (Strings.isNullOrEmpty(path)) {
return false;
}
File file = new File(path);
return writeJsonToFile(file, obj, clazz);
}
@Override
public boolean writeJsonToFile(File file, T obj, Class clazz) {
if (obj == null || file == null || file.isDirectory()) {
return false;
}
try {
normalOM.writeValue(file, obj);
return true;
} catch (IOException ex) {
ex.printStackTrace();
}
return false;
}
@Override
public Object fromJson(String json) {
return fromJson(json, Object.class);
}
@Override
public T fromJson(String json, Class clazz) {
try {
return normalOM.readerFor(clazz).readValue(json);
} catch (JsonProcessingException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
return null;
}
@Override
public Object readJsonFromFile(String path) {
return readJsonFromFile(path, Object.class);
}
@Override
public Object readJsonFromFile(File file) {
return readJsonFromFile(file, Object.class);
}
@Override
public T readJsonFromFile(String path, Class clazz) {
if (Strings.isNullOrEmpty(path)) {
return null;
}
File file = new File(path);
return readJsonFromFile(file, clazz);
}
@Override
public T readJsonFromFile(File file, Class clazz) {
if (file == null || !file.exists() || file.isDirectory()) {
return null;
}
try {
return normalOM.readerFor(clazz).readValue(file);
} catch (IOException ex) {
ex.printStackTrace();
}
return null;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy