Skip to content

Commit a56a1f4

Browse files
committed
see 01/29 log
1 parent 96e51ef commit a56a1f4

File tree

5 files changed

+47
-17
lines changed

5 files changed

+47
-17
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
* `19/01/28` [fix] KeyboardUtils#fixSoftInputLeaks didn't work on the device of HuaWei.
2-
* `19/01/27` [upd] demo
1+
* `19/01/29` [fix] LogUtils format json when json not start with '{'. Publish v1.23.3.
2+
* `19/01/28` [fix] KeyboardUtils#fixSoftInputLeaks don't work on the device of HuaWei.
3+
* `19/01/26` [fix] NetworkUtils#getNetworkType.
34
* `19/01/25` [add] CloneUtils, PermissionUtils support request permission of WRITE_SETTINGS and DRAW_OVERLAYS. Publish v1.23.2.
45
* `19/01/24` [add] BrightnessUtils and FlashlightUtils.
56
* `19/01/23` [add] Modify the demo of utilcode use kotlin. Publish v1.23.1.

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,15 @@ public static String formatJson(final String json) {
211211

212212
public static String formatJson(final String json, final int indentSpaces) {
213213
try {
214-
if (json.startsWith("{")) {
215-
return new JSONObject(json).toString(indentSpaces);
216-
} else if (json.startsWith("[")) {
217-
return new JSONArray(json).toString(indentSpaces);
214+
for (int i = 0, len = json.length(); i < len; i++) {
215+
char c = json.charAt(i);
216+
if (c == '{') {
217+
return new JSONObject(json).toString(indentSpaces);
218+
} else if (c == '[') {
219+
return new JSONArray(json).toString(indentSpaces);
220+
} else if (!Character.isWhitespace(c)) {
221+
return json;
222+
}
218223
}
219224
} catch (JSONException e) {
220225
e.printStackTrace();

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,10 +1030,15 @@ private static String intent2String(Intent intent) {
10301030

10311031
private static String formatJson(String json) {
10321032
try {
1033-
if (json.startsWith("{")) {
1034-
json = new JSONObject(json).toString(2);
1035-
} else if (json.startsWith("[")) {
1036-
json = new JSONArray(json).toString(2);
1033+
for (int i = 0, len = json.length(); i < len; i++) {
1034+
char c = json.charAt(i);
1035+
if (c == '{') {
1036+
return new JSONObject(json).toString(2);
1037+
} else if (c == '[') {
1038+
return new JSONArray(json).toString(2);
1039+
} else if (!Character.isWhitespace(c)) {
1040+
return json;
1041+
}
10371042
}
10381043
} catch (JSONException e) {
10391044
e.printStackTrace();

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ static class Result<T> {
3535

3636
@Override
3737
public String toString() {
38-
return "{\"code\":" + code + ",\"message\":" + message + ",\"data\":" + data + "}";
38+
return "{\"code\":" + primitive2String(code) +
39+
",\"message\":" + primitive2String(message) +
40+
",\"data\":" + primitive2String(data) + "}";
3941
}
4042
}
4143

@@ -51,7 +53,15 @@ static class Person {
5153

5254
@Override
5355
public String toString() {
54-
return "{\"name\":" + name + ",\"gender\":" + gender + ",\"address\":" + address + "}";
56+
return "{\"name\":" + primitive2String(name) +
57+
",\"gender\":" + primitive2String(gender) +
58+
",\"address\":" + primitive2String(address) + "}";
5559
}
5660
}
61+
62+
private static String primitive2String(final Object obj) {
63+
if (obj == null) return "null";
64+
if (obj instanceof CharSequence) return "\"" + obj.toString() + "\"";
65+
return obj.toString();
66+
}
5767
}

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717
public class LogUtilsTest extends BaseTest {
1818

19-
private static final String JSON = "{\"tools\": [{ \"name\":\"css format\" , \"site\":\"http://tools.w3cschool.cn/code/css\" },{ \"name\":\"JSON format\" , \"site\":\"http://tools.w3cschool.cn/code/JSON\" },{ \"name\":\"pwd check\" , \"site\":\"http://tools.w3cschool.cn/password/my_password_safe\" }]}";
19+
private static final String JSON = "\r\n{\"tools\": [{ \"name\":\"css format\" , \"site\":\"http://tools.w3cschool.cn/code/css\" },{ \"name\":\"JSON format\" , \"site\":\"http://tools.w3cschool.cn/code/JSON\" },{ \"name\":\"pwd check\" , \"site\":\"http://tools.w3cschool.cn/password/my_password_safe\" }]}";
2020
private static final String XML = "<books><book><author>Jack Herrington</author><title>PHP Hacks</title><publisher>O'Reilly</publisher></book><book><author>Jack Herrington</author><title>Podcasting Hacks</title><publisher>O'Reilly</publisher></book></books>";
2121
private static final int[] ONE_D_ARRAY = new int[]{1, 2, 3};
2222
private static final int[][] TWO_D_ARRAY = new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
@@ -166,10 +166,6 @@ public void testObject() {
166166
LogUtils.d((Object) TWO_D_ARRAY);
167167
LogUtils.d(LIST);
168168
LogUtils.d(MAP);
169-
Object o = GsonUtils.fromJson(GsonUtils.toJson(LIST), GsonUtils.getListType(String.class));
170-
System.out.println(o);
171-
172-
173169
}
174170

175171
static class Person {
@@ -193,5 +189,18 @@ public boolean equals(Object obj) {
193189
private static boolean equals(final Object o1, final Object o2) {
194190
return o1 == o2 || (o1 != null && o1.equals(o2));
195191
}
192+
193+
@Override
194+
public String toString() {
195+
return "{\"name\":" + primitive2String(name) +
196+
",\"gender\":" + primitive2String(gender) +
197+
",\"address\":" + primitive2String(address) + "}";
198+
}
199+
}
200+
201+
private static String primitive2String(final Object obj) {
202+
if (obj == null) return "null";
203+
if (obj instanceof CharSequence) return "\"" + obj.toString() + "\"";
204+
return obj.toString();
196205
}
197206
}

0 commit comments

Comments
 (0)