Skip to content

Commit 933ebc8

Browse files
committed
see 01/08 log
1 parent 368926c commit 933ebc8

File tree

4 files changed

+50
-32
lines changed

4 files changed

+50
-32
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,4 +204,21 @@ private static <T> T getValueByType(final String json,
204204
return defaultValue;
205205
}
206206
}
207+
208+
public static String formatJson(final String json) {
209+
return formatJson(json, 4);
210+
}
211+
212+
public static String formatJson(final String json, final int indentSpaces) {
213+
try {
214+
if (json.startsWith("{")) {
215+
return new JSONObject(json).toString(indentSpaces);
216+
} else if (json.startsWith("[")) {
217+
return new JSONArray(json).toString(indentSpaces);
218+
}
219+
} catch (JSONException e) {
220+
e.printStackTrace();
221+
}
222+
return json;
223+
}
207224
}

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

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@
3838
import java.net.UnknownHostException;
3939
import java.text.ParseException;
4040
import java.text.SimpleDateFormat;
41+
import java.util.Arrays;
4142
import java.util.Collection;
43+
import java.util.Collections;
4244
import java.util.Date;
4345
import java.util.Formatter;
4446
import java.util.Iterator;
@@ -784,9 +786,7 @@ private static class TagHead {
784786
private static class LogFormatter {
785787

786788
static String object2String(Object object) {
787-
if (object.getClass().isArray()) return object2Json(object);
788-
if (object instanceof Collection) return object2Json(object);
789-
if (object instanceof Map) return object2Json(object);
789+
if (object.getClass().isArray()) return array2String(object);
790790
if (object instanceof Throwable) return throwable2String((Throwable) object);
791791
if (object instanceof Bundle) return bundle2String((Bundle) object);
792792
if (object instanceof Intent) return intent2String((Intent) object);
@@ -798,7 +798,7 @@ static String object2Json(Object object) {
798798
return formatJson(object.toString());
799799
}
800800
try {
801-
return GSON.toJson(object);
801+
return formatJson(GSON.toJson(object));
802802
} catch (Throwable t) {
803803
return object.toString();
804804
}
@@ -1021,6 +1021,29 @@ private static void clipData2String(ClipData clipData, StringBuilder sb) {
10211021
sb.append("NULL");
10221022
sb.append("}");
10231023
}
1024+
1025+
private static String array2String(Object object) {
1026+
if (object instanceof Object[]) {
1027+
return Arrays.deepToString((Object[]) object);
1028+
} else if (object instanceof boolean[]) {
1029+
return Arrays.toString((boolean[]) object);
1030+
} else if (object instanceof byte[]) {
1031+
return Arrays.toString((byte[]) object);
1032+
} else if (object instanceof char[]) {
1033+
return Arrays.toString((char[]) object);
1034+
} else if (object instanceof double[]) {
1035+
return Arrays.toString((double[]) object);
1036+
} else if (object instanceof float[]) {
1037+
return Arrays.toString((float[]) object);
1038+
} else if (object instanceof int[]) {
1039+
return Arrays.toString((int[]) object);
1040+
} else if (object instanceof long[]) {
1041+
return Arrays.toString((long[]) object);
1042+
} else if (object instanceof short[]) {
1043+
return Arrays.toString((short[]) object);
1044+
}
1045+
throw new IllegalArgumentException("Array has incompatible type: " + object.getClass());
1046+
}
10241047
}
10251048

10261049
private static <T> Class getTypeClassFromParadigm(final IFormatter<T> formatter) {

utilcode/lib/src/test/java/com/blankj/utilcode/util/BaseTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.robolectric.RuntimeEnvironment;
88
import org.robolectric.annotation.Config;
99
import org.robolectric.shadows.ShadowLog;
10+
import org.robolectric.shadows.ShadowLooper;
1011

1112
/**
1213
* <pre>
@@ -22,6 +23,8 @@ public class BaseTest {
2223

2324
public BaseTest() {
2425
ShadowLog.stream = System.out;
26+
ReflectUtils.reflect("com.blankj.utilcode.util.ThreadUtils$Deliver")
27+
.field("MAIN_HANDLER", null);
2528
Utils.init(RuntimeEnvironment.application);
2629
}
2730

utilcode/lib/src/test/java/com/blankj/utilcode/util/LogUtilsTest.java

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class LogUtilsTest extends BaseTest {
3131
private static final int[] ONE_D_ARRAY = new int[]{1, 2, 3};
3232
private static final int[][] TWO_D_ARRAY = new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
3333
private static final ArrayList<Object> LIST = new ArrayList<>();
34-
private static final Map<String, String> MAP = new HashMap<>();
34+
private static final Map<String, Object> MAP = new HashMap<>();
3535

3636
@Test
3737
public void testV() {
@@ -172,13 +172,11 @@ public void testObject() {
172172
LIST.add(new Application());
173173

174174
MAP.put("name", "AndroidUtilCode");
175-
MAP.put("class", "LogUtils");
175+
MAP.put("class", new Application());
176176
LogUtils.d((Object) ONE_D_ARRAY);
177177
LogUtils.d((Object) TWO_D_ARRAY);
178-
LogUtils.d(formatJson(collection2String(LIST)));
178+
LogUtils.d(LIST);
179179
LogUtils.d(MAP);
180-
181-
LogUtils.d(array2String(TWO_D_ARRAY));
182180
}
183181

184182
static String collection2String(Collection collection) {
@@ -206,29 +204,6 @@ private static String formatJson(String json) {
206204
return json;
207205
}
208206

209-
static String array2String(Object object) {
210-
if (object instanceof Object[]) {
211-
return Arrays.deepToString((Object[]) object);
212-
} else if (object instanceof boolean[]) {
213-
return Arrays.toString((boolean[]) object);
214-
} else if (object instanceof byte[]) {
215-
return Arrays.toString((byte[]) object);
216-
} else if (object instanceof char[]) {
217-
return Arrays.toString((char[]) object);
218-
} else if (object instanceof double[]) {
219-
return Arrays.toString((double[]) object);
220-
} else if (object instanceof float[]) {
221-
return Arrays.toString((float[]) object);
222-
} else if (object instanceof int[]) {
223-
return Arrays.toString((int[]) object);
224-
} else if (object instanceof long[]) {
225-
return Arrays.toString((long[]) object);
226-
} else if (object instanceof short[]) {
227-
return Arrays.toString((short[]) object);
228-
}
229-
throw new IllegalArgumentException("Array has incompatible type: " + object.getClass());
230-
}
231-
232207

233208
static class Person {
234209

0 commit comments

Comments
 (0)