mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix code
This commit is contained in:
@@ -17,9 +17,8 @@ import cn.hutool.core.util.StrUtil;
|
||||
|
||||
/**
|
||||
* 内部JSON工具类,仅用于JSON内部使用
|
||||
*
|
||||
* @author Looly
|
||||
*
|
||||
* @author Looly
|
||||
*/
|
||||
final class InternalJSONUtil {
|
||||
|
||||
@@ -28,17 +27,17 @@ final class InternalJSONUtil {
|
||||
|
||||
/**
|
||||
* 写入值到Writer
|
||||
*
|
||||
* @param writer Writer
|
||||
* @param value 值
|
||||
*
|
||||
* @param writer Writer
|
||||
* @param value 值
|
||||
* @param indentFactor 每一级别的缩进量
|
||||
* @param indent 缩进空格数
|
||||
* @param config 配置项
|
||||
* @param indent 缩进空格数
|
||||
* @param config 配置项
|
||||
* @return Writer
|
||||
* @throws JSONException
|
||||
* @throws IOException
|
||||
* @throws JSONException JSON异常
|
||||
* @throws IOException IO异常
|
||||
*/
|
||||
protected static final Writer writeValue(Writer writer, Object value, int indentFactor, int indent, JSONConfig config) throws JSONException, IOException {
|
||||
protected static Writer writeValue(Writer writer, Object value, int indentFactor, int indent, JSONConfig config) throws JSONException, IOException {
|
||||
if (value == null || value instanceof JSONNull) {
|
||||
writer.write(JSONNull.NULL.toString());
|
||||
} else if (value instanceof JSON) {
|
||||
@@ -67,15 +66,15 @@ final class InternalJSONUtil {
|
||||
}
|
||||
return writer;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 缩进,使用空格符
|
||||
*
|
||||
* @param writer
|
||||
* @param indent
|
||||
* @throws IOException
|
||||
*
|
||||
* @param writer writer
|
||||
* @param indent 随进空格数
|
||||
* @throws IOException IO异常
|
||||
*/
|
||||
protected static final void indent(Writer writer, int indent) throws IOException {
|
||||
protected static void indent(Writer writer, int indent) throws IOException {
|
||||
for (int i = 0; i < indent; i += 1) {
|
||||
writer.write(CharUtil.SPACE);
|
||||
}
|
||||
@@ -83,7 +82,7 @@ final class InternalJSONUtil {
|
||||
|
||||
/**
|
||||
* 如果对象是Number 且是 NaN or infinite,将抛出异常
|
||||
*
|
||||
*
|
||||
* @param obj 被检查的对象
|
||||
* @throws JSONException If o is a non-finite number.
|
||||
*/
|
||||
@@ -182,10 +181,10 @@ final class InternalJSONUtil {
|
||||
/**
|
||||
* 将Property的键转化为JSON形式<br>
|
||||
* 用于识别类似于:com.luxiaolei.package.hutool这类用点隔开的键
|
||||
*
|
||||
*
|
||||
* @param jsonObject JSONObject
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
* @return JSONObject
|
||||
*/
|
||||
protected static JSONObject propertyPut(JSONObject jsonObject, Object key, Object value) {
|
||||
@@ -205,37 +204,36 @@ final class InternalJSONUtil {
|
||||
target.put(path[last], value);
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 默认情况下是否忽略null值的策略选择<br>
|
||||
* JavaBean默认忽略null值,其它对象不忽略
|
||||
*
|
||||
*
|
||||
* @param obj 需要检查的对象
|
||||
* @return 是否忽略null值
|
||||
* @since 4.3.1
|
||||
*/
|
||||
protected static boolean defaultIgnoreNullValue(Object obj) {
|
||||
if(obj instanceof CharSequence || obj instanceof JSONTokener || obj instanceof Map) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
return (false == (obj instanceof CharSequence))//
|
||||
&& (false == (obj instanceof JSONTokener))//
|
||||
&& (false == (obj instanceof Map));
|
||||
}
|
||||
|
||||
/**
|
||||
* 按照给定格式格式化日期,格式为空时返回时间戳字符串
|
||||
*
|
||||
*
|
||||
* @param dateObj Date或者Calendar对象
|
||||
* @param format 格式
|
||||
* @param format 格式
|
||||
* @return 日期字符串
|
||||
*/
|
||||
private static String formatDate(Object dateObj, String format) {
|
||||
if (StrUtil.isNotBlank(format)) {
|
||||
final Date date = (dateObj instanceof Date) ? (Date)dateObj : ((Calendar)dateObj).getTime();
|
||||
final Date date = (dateObj instanceof Date) ? (Date) dateObj : ((Calendar) dateObj).getTime();
|
||||
//用户定义了日期格式
|
||||
return JSONUtil.quote(DateUtil.format(date, format));
|
||||
}
|
||||
|
||||
|
||||
//默认使用时间戳
|
||||
return String.valueOf((dateObj instanceof Date) ? ((Date)dateObj).getTime() : ((Calendar)dateObj).getTimeInMillis());
|
||||
return String.valueOf((dateObj instanceof Date) ? ((Date) dateObj).getTime() : ((Calendar) dateObj).getTimeInMillis());
|
||||
}
|
||||
}
|
||||
|
@@ -34,7 +34,7 @@ public interface JSON extends Cloneable, Serializable{
|
||||
* @see BeanPath#get(Object)
|
||||
* @since 4.0.6
|
||||
*/
|
||||
public Object getByPath(String expression);
|
||||
Object getByPath(String expression);
|
||||
|
||||
/**
|
||||
* 设置表达式指定位置(或filed对应)的值<br>
|
||||
@@ -84,7 +84,7 @@ public interface JSON extends Cloneable, Serializable{
|
||||
* @see BeanPath#get(Object)
|
||||
* @since 4.0.6
|
||||
*/
|
||||
public <T> T getByPath(String expression, Class<T> resultType);
|
||||
<T> T getByPath(String expression, Class<T> resultType);
|
||||
|
||||
/**
|
||||
* 将JSON内容写入Writer,无缩进<br>
|
||||
@@ -94,7 +94,7 @@ public interface JSON extends Cloneable, Serializable{
|
||||
* @return Writer
|
||||
* @throws JSONException JSON相关异常
|
||||
*/
|
||||
public Writer write(Writer writer) throws JSONException;
|
||||
Writer write(Writer writer) throws JSONException;
|
||||
|
||||
/**
|
||||
* 将JSON内容写入Writer<br>
|
||||
@@ -106,7 +106,7 @@ public interface JSON extends Cloneable, Serializable{
|
||||
* @return Writer
|
||||
* @throws JSONException JSON相关异常
|
||||
*/
|
||||
public Writer write(Writer writer, int indentFactor, int indent) throws JSONException;
|
||||
Writer write(Writer writer, int indentFactor, int indent) throws JSONException;
|
||||
|
||||
/**
|
||||
* 转换为JSON字符串
|
||||
@@ -115,7 +115,7 @@ public interface JSON extends Cloneable, Serializable{
|
||||
* @return JSON字符串
|
||||
* @throws JSONException JSON相关异常
|
||||
*/
|
||||
public String toJSONString(int indentFactor) throws JSONException;
|
||||
String toJSONString(int indentFactor) throws JSONException;
|
||||
|
||||
/**
|
||||
* 格式化打印JSON,缩进为4个空格
|
||||
@@ -124,5 +124,5 @@ public interface JSON extends Cloneable, Serializable{
|
||||
* @throws JSONException 包含非法数抛出此异常
|
||||
* @since 3.0.9
|
||||
*/
|
||||
public String toStringPretty() throws JSONException;
|
||||
String toStringPretty() throws JSONException;
|
||||
}
|
||||
|
@@ -82,7 +82,7 @@ public class JSONArray extends JSONGetter<Integer> implements JSON, List<Object>
|
||||
* @since 4.1.19
|
||||
*/
|
||||
public JSONArray(int initialCapacity, JSONConfig config) {
|
||||
this.rawList = new ArrayList<Object>(initialCapacity);
|
||||
this.rawList = new ArrayList<>(initialCapacity);
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
@@ -107,9 +107,7 @@ public class JSONArray extends JSONGetter<Integer> implements JSON, List<Object>
|
||||
*/
|
||||
public JSONArray(Collection<Object> list) {
|
||||
this(list.size());
|
||||
for (Object o : list) {
|
||||
this.add(o);
|
||||
}
|
||||
this.addAll(list);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -311,13 +309,10 @@ public class JSONArray extends JSONGetter<Integer> implements JSON, List<Object>
|
||||
}
|
||||
final JSONArray other = (JSONArray) obj;
|
||||
if (rawList == null) {
|
||||
if (other.rawList != null) {
|
||||
return false;
|
||||
}
|
||||
} else if (!rawList.equals(other.rawList)) {
|
||||
return false;
|
||||
return other.rawList == null;
|
||||
} else {
|
||||
return rawList.equals(other.rawList);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -382,7 +377,7 @@ public class JSONArray extends JSONGetter<Integer> implements JSON, List<Object>
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addAll(Collection<? extends Object> c) {
|
||||
public boolean addAll(Collection<?> c) {
|
||||
if (CollUtil.isEmpty(c)) {
|
||||
return false;
|
||||
}
|
||||
@@ -393,7 +388,7 @@ public class JSONArray extends JSONGetter<Integer> implements JSON, List<Object>
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addAll(int index, Collection<? extends Object> c) {
|
||||
public boolean addAll(int index, Collection<?> c) {
|
||||
if (CollUtil.isEmpty(c)) {
|
||||
return false;
|
||||
}
|
||||
|
@@ -441,8 +441,8 @@ public class JSONObject extends JSONGetter<String> implements JSON, Map<String,
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putAll(Map<? extends String, ? extends Object> m) {
|
||||
for (Entry<? extends String, ? extends Object> entry : m.entrySet()) {
|
||||
public void putAll(Map<? extends String, ?> m) {
|
||||
for (Entry<? extends String, ?> entry : m.entrySet()) {
|
||||
this.put(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
@@ -565,13 +565,10 @@ public class JSONObject extends JSONGetter<String> implements JSON, Map<String,
|
||||
}
|
||||
final JSONObject other = (JSONObject) obj;
|
||||
if (rawHashMap == null) {
|
||||
if (other.rawHashMap != null) {
|
||||
return false;
|
||||
}
|
||||
} else if (!rawHashMap.equals(other.rawHashMap)) {
|
||||
return false;
|
||||
return other.rawHashMap == null;
|
||||
} else {
|
||||
return rawHashMap.equals(other.rawHashMap);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -660,7 +657,7 @@ public class JSONObject extends JSONGetter<String> implements JSON, Map<String,
|
||||
}
|
||||
|
||||
InternalJSONUtil.indent(writer, newIndent);
|
||||
writer.write(JSONUtil.quote(entry.getKey().toString()));
|
||||
writer.write(JSONUtil.quote(entry.getKey()));
|
||||
writer.write(CharUtil.COLON);
|
||||
if (indentFactor > 0) {
|
||||
// 冒号后的空格
|
||||
@@ -682,7 +679,6 @@ public class JSONObject extends JSONGetter<String> implements JSON, Map<String,
|
||||
* Bean对象转Map
|
||||
*
|
||||
* @param bean Bean对象
|
||||
* @param ignoreNullValue 是否忽略空值
|
||||
*/
|
||||
private void populateMap(Object bean) {
|
||||
final Collection<PropDesc> props = BeanUtil.getBeanDesc(bean.getClass()).getProps();
|
||||
@@ -735,7 +731,7 @@ public class JSONObject extends JSONGetter<String> implements JSON, Map<String,
|
||||
}
|
||||
|
||||
final JSONSerializer serializer = GlobalSerializeMapping.getSerializer(source.getClass());
|
||||
if(null != serializer && serializer instanceof JSONObjectSerializer) {
|
||||
if(serializer instanceof JSONObjectSerializer) {
|
||||
// 自定义序列化
|
||||
serializer.serialize(this, source);
|
||||
} else if (source instanceof Map) {
|
||||
|
@@ -12,9 +12,9 @@ import cn.hutool.core.util.StrUtil;
|
||||
public class JSONStrFormater {
|
||||
|
||||
/** 单位缩进字符串。*/
|
||||
private static String SPACE = " ";
|
||||
private static final String SPACE = " ";
|
||||
/** 换行符*/
|
||||
private static char NEW_LINE = StrUtil.C_LF;
|
||||
private static final char NEW_LINE = StrUtil.C_LF;
|
||||
|
||||
/**
|
||||
* 返回格式化JSON字符串。
|
||||
@@ -23,13 +23,13 @@ public class JSONStrFormater {
|
||||
* @return 格式化的JSON字符串。
|
||||
*/
|
||||
public static String format(String json) {
|
||||
final StringBuffer result = new StringBuffer();
|
||||
final StringBuilder result = new StringBuilder();
|
||||
|
||||
Character wrapChar = null;
|
||||
boolean isEscapeMode = false;
|
||||
int length = json.length();
|
||||
int number = 0;
|
||||
char key = 0;
|
||||
char key;
|
||||
for (int i = 0; i < length; i++) {
|
||||
key = json.charAt(i);
|
||||
|
||||
|
@@ -14,5 +14,5 @@ public interface JSONString {
|
||||
*
|
||||
* @return JSON字符串
|
||||
*/
|
||||
public String toJSONString();
|
||||
String toJSONString();
|
||||
}
|
||||
|
@@ -150,7 +150,7 @@ public final class JSONUtil {
|
||||
return null;
|
||||
}
|
||||
|
||||
JSON json = null;
|
||||
JSON json;
|
||||
if (obj instanceof JSON) {
|
||||
json = (JSON) obj;
|
||||
} else if (obj instanceof String) {
|
||||
@@ -648,9 +648,6 @@ public final class JSONUtil {
|
||||
if (object instanceof Date || object instanceof Calendar) {
|
||||
return object;
|
||||
}
|
||||
if (object instanceof Calendar) {
|
||||
return ((Calendar) object).getTimeInMillis();
|
||||
}
|
||||
// 枚举类保存其字符串形式(4.0.2新增)
|
||||
if (object instanceof Enum) {
|
||||
return object.toString();
|
||||
|
@@ -79,7 +79,6 @@ public class JSONArrayTest {
|
||||
List<Exam> list = array.toList(Exam.class);
|
||||
Assert.assertFalse(list.isEmpty());
|
||||
Assert.assertEquals(Exam.class, list.get(0).getClass());
|
||||
;
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -123,7 +122,7 @@ public class JSONArrayTest {
|
||||
JSONArray array = JSONUtil.parseArray(jsonStr);
|
||||
|
||||
Exam[] list = array.toArray(new Exam[0]);
|
||||
Assert.assertFalse(0 == list.length);
|
||||
Assert.assertNotEquals(0, list.length);
|
||||
Assert.assertEquals(Exam.class, list[0].getClass());
|
||||
}
|
||||
|
||||
@@ -136,7 +135,7 @@ public class JSONArrayTest {
|
||||
JSONArray ja = JSONUtil.parseArray(json);
|
||||
|
||||
List<KeyBean> list = ja.toList(KeyBean.class);
|
||||
Assert.assertTrue(null == list.get(0));
|
||||
Assert.assertNull(list.get(0));
|
||||
Assert.assertEquals("avalue", list.get(1).getAkey());
|
||||
Assert.assertEquals("bvalue", list.get(1).getBkey());
|
||||
}
|
||||
@@ -175,7 +174,7 @@ public class JSONArrayTest {
|
||||
return map;
|
||||
}
|
||||
|
||||
class User {
|
||||
static class User {
|
||||
private Integer id;
|
||||
private String name;
|
||||
|
||||
|
@@ -354,7 +354,7 @@ public class JSONObjectTest {
|
||||
Assert.assertEquals("defaultBBB", bbb);
|
||||
}
|
||||
|
||||
public static enum TestEnum {
|
||||
public enum TestEnum {
|
||||
TYPE_A, TYPE_B
|
||||
}
|
||||
|
||||
|
@@ -17,7 +17,7 @@ public class EnvSettingInfo {
|
||||
private String hubRemoteUrl;
|
||||
|
||||
private String reportFolder = "/report";
|
||||
private String screenshotFolder = "/screenshot";;
|
||||
private String screenshotFolder = "/screenshot";
|
||||
|
||||
private String elementFolder = "/config/element/";
|
||||
private String suiteFolder = "/config/suite/";
|
||||
|
Reference in New Issue
Block a user