-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from yindz/dev
Dev
- Loading branch information
Showing
15 changed files
with
628 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
src/main/java/com/apifan/common/random/util/JsonUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package com.apifan.common.random.util; | ||
|
||
import com.apifan.common.random.util.json.JsonConverter; | ||
import com.apifan.common.random.util.json.impl.FastjsonConverter; | ||
import com.apifan.common.random.util.json.impl.GsonConverter; | ||
import com.apifan.common.random.util.json.impl.JacksonConverter; | ||
import com.google.common.base.Preconditions; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* JSON工具类 | ||
* | ||
* <p> | ||
* 从 1.0.16 起,不再强依赖 jackson; <br> | ||
* 支持目前常用的以下3种json第三方库: jackson/fastjson/gson <br> | ||
* 检测顺序:jackson>fastjson>gson <br> | ||
* 注意: 不要忘记手动添加依赖 | ||
* </p> | ||
* | ||
* @author yin | ||
* @since 1.0.16 | ||
*/ | ||
public class JsonUtils { | ||
private static final Logger log = LoggerFactory.getLogger(JsonUtils.class); | ||
|
||
private static final JsonConverter jsonConverter; | ||
|
||
static { | ||
jsonConverter = getRealJsonConverter(); | ||
} | ||
|
||
/** | ||
* 对象转换为JSON字符串 | ||
* | ||
* @param obj 对象 | ||
* @return JSON字符串 | ||
*/ | ||
public static String toJson(Object obj) { | ||
Preconditions.checkNotNull(obj, "对象为空"); | ||
return jsonConverter.toJson(obj); | ||
} | ||
|
||
/** | ||
* 解析JSON字符串并转换为单个对象 | ||
* | ||
* @param text 待解析的JSON字符串 | ||
* @param targetClass 目标类 | ||
* @param <T> 泛型 | ||
* @return 对象 | ||
*/ | ||
public static <T> T parseObject(String text, Class<T> targetClass) { | ||
Preconditions.checkArgument(StringUtils.isNotBlank(text), "待解析的JSON字符串为空"); | ||
Preconditions.checkNotNull(targetClass, "目标类为空"); | ||
return jsonConverter.parseObject(text, targetClass); | ||
} | ||
|
||
/** | ||
* 解析JSON字符串并转换为对象列表 | ||
* | ||
* @param text 待解析的JSON字符串 | ||
* @param targetClass 目标类 | ||
* @param <T> 泛型 | ||
* @return 对象列表 | ||
*/ | ||
public static <T> List<T> parseObjectList(String text, Class<T> targetClass) { | ||
Preconditions.checkArgument(StringUtils.isNotBlank(text), "待解析的JSON字符串为空"); | ||
Preconditions.checkNotNull(targetClass, "目标类为空"); | ||
return jsonConverter.parseObjectList(text, targetClass); | ||
} | ||
|
||
/** | ||
* 解析JSON字符串并转换为Map列表 | ||
* | ||
* @param text 待解析的JSON字符串 | ||
* @return Map列表 | ||
*/ | ||
public static List<Map<String, Object>> parseMapList(String text) { | ||
Preconditions.checkArgument(StringUtils.isNotBlank(text), "待解析的JSON字符串为空"); | ||
return jsonConverter.parseMapList(text); | ||
} | ||
|
||
/** | ||
* 获取JSON转换器实例 | ||
* | ||
* @return JSON转换器实例 | ||
*/ | ||
private static JsonConverter getRealJsonConverter() { | ||
if (ResourceUtils.isClassLoaded("com.fasterxml.jackson.databind.ObjectMapper")) { | ||
log.info("将使用 jackson"); | ||
return new JacksonConverter(); | ||
} else if (ResourceUtils.isClassLoaded("com.alibaba.fastjson.JSON")) { | ||
log.info("将使用 fastjson"); | ||
return new FastjsonConverter(); | ||
} else if (ResourceUtils.isClassLoaded("com.google.gson.Gson")) { | ||
log.info("将使用 gson"); | ||
return new GsonConverter(); | ||
} else { | ||
throw new RuntimeException("没有找到可用的JSON库"); | ||
} | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
src/main/java/com/apifan/common/random/util/PinyinUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.apifan.common.random.util; | ||
|
||
import com.apifan.common.random.util.pinyin.PinyinConverter; | ||
import com.apifan.common.random.util.pinyin.impl.Pinyin4jConverter; | ||
import com.apifan.common.random.util.pinyin.impl.TinyPinyinConverter; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* 拼音工具类 | ||
* <p> | ||
* 从 1.0.16 起,不再强依赖 tinypinyin; <br> | ||
* 支持目前常用的以下2种第三方库: tinypinyin/pinyin4j <br> | ||
* 检测顺序:tinypinyin>pinyin4j <br> | ||
* 注意: 不要忘记手动添加依赖 | ||
* </p> | ||
* | ||
* @author yin | ||
* @since 1.0.16 | ||
*/ | ||
public class PinyinUtils { | ||
private static final Logger log = LoggerFactory.getLogger(PinyinUtils.class); | ||
|
||
private static final PinyinConverter pinyinConverter; | ||
|
||
static { | ||
pinyinConverter = getRealPinyinConverter(); | ||
} | ||
|
||
/** | ||
* 转换成拼音 | ||
* | ||
* @param src 原始字符串 | ||
* @param toLowerCase 是否转换为小写 | ||
* @return 拼音 | ||
*/ | ||
public static String toPinyin(String src, boolean toLowerCase) { | ||
return pinyinConverter.toPinyin(src, toLowerCase); | ||
} | ||
|
||
/** | ||
* 获取拼音转换器实例 | ||
* | ||
* @return 拼音转换器实例 | ||
*/ | ||
private static PinyinConverter getRealPinyinConverter() { | ||
if (ResourceUtils.isClassLoaded("com.github.promeg.pinyinhelper.Pinyin")) { | ||
log.info("将使用 tinypinyin"); | ||
return new TinyPinyinConverter(); | ||
} else if (ResourceUtils.isClassLoaded("net.sourceforge.pinyin4j.PinyinHelper")) { | ||
log.info("将使用 pinyin4j"); | ||
return new Pinyin4jConverter(); | ||
} else { | ||
throw new RuntimeException("没有找到可用的拼音库"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.