查询windows或者linux上 支持的所有字体
最近在搞画图啥的 简单记录下 查询所有支持的字体
public static void main(String[] args) throws Exception {
GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fontName = environment.getAvailableFontFamilyNames();
for (int i = 0; i < fontName.length; i++) {
System.out.println(fontName[i]);
}
// 对象转json
String in = JsonUtils.toJSONString(fonts);
}
对象转json 通用方法啥的 也 记录下
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import com.taiden.common.constant.LocalDateTimeConstant;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import java.io.InputStream;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;
@Slf4j
public class JsonUtils {
// 加载速度太慢了,放在静态代码块中
// private static final ObjectMapper mapper = new ObjectMapper();
private static ObjectMapper mapper;
/**
* 设置一些通用的属性
*/
static {
mapper = new ObjectMapper();
// 如果json中有新增的字段并且是实体类类中不存在的,不报错
// mapper.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false);
// 如果存在未知属性,则忽略不报错
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// 允许key没有双引号
mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
// 允许key有单引号
mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
// 允许整数以0开头
mapper.configure(JsonParser.Feature.ALLOW_NUMERIC_LEADING_ZEROS, true);
// 允许字符串中存在回车换行控制符
mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
mapper.disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE);
JavaTimeModule javaTimeModule = new JavaTimeModule();
javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(LocalDateTimeConstant.defaultDateTimeFormatter));
javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(LocalDateTimeConstant.defaultDateFormatter));
javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(LocalDateTimeConstant.defaultTimeFormatter));
javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(LocalDateTimeConstant.defaultDateTimeFormatter));
javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(LocalDateTimeConstant.defaultDateFormatter));
javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(LocalDateTimeConstant.defaultTimeFormatter));
mapper.registerModule(javaTimeModule);
}
public static String toJSONString(Object obj) {
return obj != null ? toJSONString(obj, () -> "", false) : "";
}
public static String toFormatJSONString(Object obj) {
return obj != null ? toJSONString(obj, () -> "", true) : "";
}
public static String toJSONString(Object obj, Supplier<String> defaultSupplier, boolean format) {
try {
if (obj == null) {
return defaultSupplier.get();
}
if (obj instanceof String) {
return obj.toString();
}
if (obj instanceof Number) {
return obj.toString();
}
if (format) {
return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
}
return mapper.writeValueAsString(obj);
} catch (Throwable e) {
log.error(String.format("toJSONString %s", obj != null ? obj.toString() : "null"), e);
}
return defaultSupplier.get();
}
/**
* 将对象转换为JSON纯文本
*/
public static String toJSONPlainString(Object obj) {
String replaceJson = toJSONString(obj).replaceAll("\"", "\\\\\"");
return "\"" + replaceJson + "\"";
}
public static <T> T toJavaObject(String value, Class<T> tClass) {
return StringUtils.isNotBlank(value) ? toJavaObject(value, tClass, () -> null) : null;
}
public static <T> T toJavaObject(Object obj, Class<T> tClass) {
return obj != null ? toJavaObject(toJSONString(obj), tClass, () -> null) : null;
}
public static <T> T toJavaObject(InputStream stream, Class<T> tClass) {
try {
return mapper.readValue(stream, tClass);
} catch (Throwable e) {
log.error(String.format("toJavaObject exception: \n %s\n %s", stream, tClass), e);
}
return null;
}
public static <T> T toJavaObject(String value, Class<T> tClass, Supplier<T> defaultSupplier) {
try {
if (StringUtils.isBlank(value)) {
return defaultSupplier.get();
}
return mapper.readValue(value, tClass);
} catch (Throwable e) {
log.error(String.format("toJavaObject exception: \n %s\n %s", value, tClass), e);
}
return defaultSupplier.get();
}
public static <T> List<T> toJavaObjectList(String value, Class<T> tClass) {
return StringUtils.isNotBlank(value) ? toJavaObjectList(value, tClass, () -> null) : null;
}
public static <T> List<T> toJavaObjectList(Object obj, Class<T> tClass) {
return obj != null ? toJavaObjectList(toJSONString(obj), tClass, () -> null) : null;
}
public static <T> List<T> toJavaObjectList(String value, Class<T> tClass, Supplier<List<T>> defaultSupplier) {
try {
if (StringUtils.isBlank(value)) {
return defaultSupplier.get();
}
JavaType javaType = mapper.getTypeFactory().constructParametricType(List.class, tClass);
return mapper.readValue(value, javaType);
} catch (Throwable e) {
log.error(String.format("toJavaObjectList exception \n%s\n%s", value, tClass), e);
}
return defaultSupplier.get();
}
// 简单地直接用json复制或者转换(Cloneable)
public static <T> T jsonCopy(Object obj, Class<T> tClass) {
return obj != null ? toJavaObject(toJSONString(obj), tClass) : null;
}
public static Map<String, Object> toMap(String value) {
return StringUtils.isNotBlank(value) ? toMap(value, () -> null) : null;
}
public static Map<String, Object> toMap(Object value) {
return value != null ? toMap(value, () -> null) : null;
}
public static Map<String, Object> toMap(Object value, Supplier<Map<String, Object>> defaultSupplier) {
if (value == null) {
return defaultSupplier.get();
}
try {
if (value instanceof Map) {
return (Map<String, Object>) value;
}
} catch (Exception e) {
log.info("fail to convert" + toJSONString(value), e);
}
return toMap(toJSONString(value), defaultSupplier);
}
public static Map<String, Object> toMap(String value, Supplier<Map<String, Object>> defaultSupplier) {
if (StringUtils.isBlank(value)) {
return defaultSupplier.get();
}
try {
return toJavaObject(value, LinkedHashMap.class);
} catch (Exception e) {
log.error(String.format("toMap exception\n%s", value), e);
}
return defaultSupplier.get();
}
public static List<Object> toList(String value) {
return StringUtils.isNotBlank(value) ? toList(value, () -> null) : null;
}
public static List<Object> toList(Object value) {
return value != null ? toList(value, () -> null) : null;
}
public static List<Object> toList(String value, Supplier<List<Object>> defaultSuppler) {
if (StringUtils.isBlank(value)) {
return defaultSuppler.get();
}
try {
return toJavaObject(value, List.class);
} catch (Exception e) {
log.error("toList exception\n" + value, e);
}
return defaultSuppler.get();
}
public static List<Object> toList(Object value, Supplier<List<Object>> defaultSuppler) {
if (value == null) {
return defaultSuppler.get();
}
if (value instanceof List) {
return (List<Object>) value;
}
return toList(toJSONString(value), defaultSuppler);
}
public static long getLong(Map<String, Object> map, String key) {
if (MapUtils.isEmpty(map)) {
return 0L;
}
String valueStr = String.valueOf(map.get(key));
if (StringUtils.isBlank(valueStr) || !StringUtils.isNumeric(valueStr)) {
return 0L;
}
return Long.valueOf(valueStr);
}
public static int getInt(Map<String, Object> map, String key) {
if (MapUtils.isEmpty(map)) {
return 0;
}
String valueStr = String.valueOf(map.get(key));
if (StringUtils.isBlank(valueStr) || !StringUtils.isNumeric(valueStr)) {
return 0;
}
return Integer.valueOf(valueStr);
}
}