33
33
import java .io .StringWriter ;
34
34
import java .lang .annotation .Retention ;
35
35
import java .lang .annotation .RetentionPolicy ;
36
+ import java .lang .reflect .Array ;
36
37
import java .lang .reflect .ParameterizedType ;
37
38
import java .lang .reflect .Type ;
38
39
import java .net .UnknownHostException ;
39
40
import java .text .ParseException ;
40
41
import java .text .SimpleDateFormat ;
42
+ import java .util .Collection ;
41
43
import java .util .Date ;
42
44
import java .util .Formatter ;
43
45
import java .util .Iterator ;
44
46
import java .util .Locale ;
47
+ import java .util .Map ;
45
48
import java .util .Set ;
46
49
import java .util .concurrent .ExecutorService ;
47
50
import java .util .concurrent .Executors ;
@@ -181,19 +184,19 @@ public static void file(@TYPE final int type, final String tag, final Object con
181
184
log (FILE | type , tag , content );
182
185
}
183
186
184
- public static void json (final String content ) {
187
+ public static void json (final Object content ) {
185
188
log (JSON | D , CONFIG .mGlobalTag , content );
186
189
}
187
190
188
- public static void json (@ TYPE final int type , final String content ) {
191
+ public static void json (@ TYPE final int type , final Object content ) {
189
192
log (JSON | type , CONFIG .mGlobalTag , content );
190
193
}
191
194
192
- public static void json (final String tag , final String content ) {
195
+ public static void json (final String tag , final Object content ) {
193
196
log (JSON | D , tag , content );
194
197
}
195
198
196
- public static void json (@ TYPE final int type , final String tag , final String content ) {
199
+ public static void json (@ TYPE final int type , final String tag , final Object content ) {
197
200
log (JSON | type , tag , content );
198
201
}
199
202
@@ -330,7 +333,7 @@ private static String processBody(final int type, final Object... contents) {
330
333
331
334
private static String formatObject (int type , Object object ) {
332
335
if (object == null ) return NULL ;
333
- if (type == JSON ) return LogFormatter .formatJson (object . toString () );
336
+ if (type == JSON ) return LogFormatter .object2Json (object );
334
337
if (type == XML ) return LogFormatter .formatXml (object .toString ());
335
338
return formatObject (object );
336
339
}
@@ -344,9 +347,6 @@ private static String formatObject(Object object) {
344
347
return iFormatter .format (object );
345
348
}
346
349
}
347
- if (object instanceof Throwable ) return LogFormatter .throwable2String ((Throwable ) object );
348
- if (object instanceof Bundle ) return LogFormatter .bundle2String ((Bundle ) object );
349
- if (object instanceof Intent ) return LogFormatter .intent2String ((Intent ) object );
350
350
return LogFormatter .object2String (object );
351
351
}
352
352
@@ -782,17 +782,19 @@ private static class TagHead {
782
782
}
783
783
784
784
private static class LogFormatter {
785
- static String formatJson (String json ) {
786
- try {
787
- if (json .startsWith ("{" )) {
788
- json = new JSONObject (json ).toString (4 );
789
- } else if (json .startsWith ("[" )) {
790
- json = new JSONArray (json ).toString (4 );
791
- }
792
- } catch (JSONException e ) {
793
- e .printStackTrace ();
794
- }
795
- return json ;
785
+
786
+ 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 );
790
+ if (object instanceof Throwable ) return throwable2String ((Throwable ) object );
791
+ if (object instanceof Bundle ) return bundle2String ((Bundle ) object );
792
+ if (object instanceof Intent ) return intent2String ((Intent ) object );
793
+ return object .toString ();
794
+ }
795
+
796
+ static String object2Json (Object object ) {
797
+ return formatJson (GSON .toJson (object ));
796
798
}
797
799
798
800
static String formatXml (String xml ) {
@@ -810,7 +812,7 @@ static String formatXml(String xml) {
810
812
return xml ;
811
813
}
812
814
813
- static String throwable2String (final Throwable e ) {
815
+ private static String throwable2String (final Throwable e ) {
814
816
Throwable t = e ;
815
817
while (t != null ) {
816
818
if (t instanceof UnknownHostException ) {
@@ -830,7 +832,7 @@ static String throwable2String(final Throwable e) {
830
832
return sw .toString ();
831
833
}
832
834
833
- static String bundle2String (Bundle bundle ) {
835
+ private static String bundle2String (Bundle bundle ) {
834
836
Iterator <String > iterator = bundle .keySet ().iterator ();
835
837
if (!iterator .hasNext ()) {
836
838
return "Bundle {}" ;
@@ -851,7 +853,7 @@ static String bundle2String(Bundle bundle) {
851
853
}
852
854
}
853
855
854
- static String intent2String (Intent intent ) {
856
+ private static String intent2String (Intent intent ) {
855
857
StringBuilder sb = new StringBuilder (128 );
856
858
sb .append ("Intent { " );
857
859
boolean first = true ;
@@ -961,8 +963,17 @@ static String intent2String(Intent intent) {
961
963
return sb .toString ();
962
964
}
963
965
964
- static String object2String (Object object ) {
965
- return formatJson (GSON .toJson (object ));
966
+ private static String formatJson (String json ) {
967
+ try {
968
+ if (json .startsWith ("{" )) {
969
+ json = new JSONObject (json ).toString (4 );
970
+ } else if (json .startsWith ("[" )) {
971
+ json = new JSONArray (json ).toString (4 );
972
+ }
973
+ } catch (JSONException e ) {
974
+ e .printStackTrace ();
975
+ }
976
+ return json ;
966
977
}
967
978
968
979
@ RequiresApi (api = Build .VERSION_CODES .JELLY_BEAN )
0 commit comments