Skip to content

Commit d3c9986

Browse files
committed
enhanced object2String
1 parent 755348c commit d3c9986

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

utilcode/lib/src/main/java/com/blankj/utilcode/util/LogUtils.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -347,8 +347,6 @@ private static String formatObject(Object object) {
347347
return iFormatter.format(object);
348348
}
349349
}
350-
if (object instanceof String || object instanceof JSONObject || object instanceof JSONArray)
351-
return object.toString();
352350
if (object.getClass().isArray()) return LogFormatter.array2String(object);
353351
if (object instanceof Throwable) return LogFormatter.throwable2String((Throwable) object);
354352
if (object instanceof Bundle) return LogFormatter.bundle2String((Bundle) object);
@@ -1009,6 +1007,10 @@ static String collection2String(Collection collection) {
10091007
}
10101008

10111009
static String object2String(Object object) {
1010+
if (object instanceof String || object instanceof JSONObject ||
1011+
object instanceof JSONArray) {
1012+
return object.toString();
1013+
}
10121014
Class<?> clazz = object.getClass();
10131015
List<Field> tmp = Arrays.asList(clazz.getDeclaredFields());
10141016
ArrayList<Field> list = new ArrayList<>(tmp);
@@ -1028,13 +1030,14 @@ static String object2String(Object object) {
10281030
Field[] a = new Field[list.size()];
10291031
Field[] fields = list.toArray(a);
10301032
JSONObject jsonObject = new JSONObject();
1031-
for (Field field : fields) {
1032-
String fieldName = field.getName();
1033-
try {
1033+
try {
1034+
jsonObject.put("object", object.toString());
1035+
for (Field field : fields) {
1036+
String fieldName = field.getName();
10341037
Object obj = field.get(object);
10351038
jsonObject.put(fieldName, obj);
1036-
} catch (Exception ignore) {
10371039
}
1040+
} catch (Exception ignore) {
10381041
}
10391042
return jsonObject.toString();
10401043
}

0 commit comments

Comments
 (0)