as.leap.code.impl.LASJsonParser Maven / Gradle / Ivy
package as.leap.code.impl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;
import as.leap.code.LASException;
import java.io.IOException;
import java.util.*;
/**
* Created by stream.
*/
public abstract class LASJsonParser {
private static final ObjectMapper mapper = new ObjectMapper();
private static final TypeFactory typeFactory = TypeFactory.defaultInstance();
public static T asObject(String source, Class clazz) {
try {
return mapper.readValue(source, typeFactory.uncheckedSimpleType(clazz));
} catch (IOException e) {
throw new LASException(e);
}
}
public static T asObject(String source, JavaType type) {
try {
return mapper.readValue(source, type);
} catch (IOException e) {
throw new LASException(e);
}
}
public static JsonNode asJsonNode(String source) {
try {
return mapper.readTree(source);
} catch (IOException e) {
throw new LASException(e);
}
}
public static Map objectToMap(Object object) {
if (object == null) return null;
try {
String jsonStr = mapper.writeValueAsString(object);
return mapper.readValue(jsonStr, Map.class);
} catch (Exception e) {
throw new LASException(e);
}
}
public static List asList(String json) {
return mapper.convertValue(asJsonNode(json), List.class);
}
public static Map asMap(String json) {
return mapper.convertValue(asJsonNode(json), Map.class);
}
public static Map jsonNodeToMap(JsonNode jsonNode) {
return mapper.convertValue(jsonNode, Map.class);
}
public static String asJson(T obj) {
try {
return mapper.writeValueAsString(obj);
} catch (JsonProcessingException e) {
throw new LASException(e);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy