3333import java .io .StringWriter ;
3434import java .lang .annotation .Retention ;
3535import java .lang .annotation .RetentionPolicy ;
36+ import java .lang .reflect .Array ;
3637import java .lang .reflect .ParameterizedType ;
3738import java .lang .reflect .Type ;
3839import java .net .UnknownHostException ;
3940import java .text .ParseException ;
4041import java .text .SimpleDateFormat ;
42+ import java .util .Collection ;
4143import java .util .Date ;
4244import java .util .Formatter ;
4345import java .util .Iterator ;
4446import java .util .Locale ;
47+ import java .util .Map ;
4548import java .util .Set ;
4649import java .util .concurrent .ExecutorService ;
4750import java .util .concurrent .Executors ;
@@ -181,19 +184,19 @@ public static void file(@TYPE final int type, final String tag, final Object con
181184 log (FILE | type , tag , content );
182185 }
183186
184- public static void json (final String content ) {
187+ public static void json (final Object content ) {
185188 log (JSON | D , CONFIG .mGlobalTag , content );
186189 }
187190
188- public static void json (@ TYPE final int type , final String content ) {
191+ public static void json (@ TYPE final int type , final Object content ) {
189192 log (JSON | type , CONFIG .mGlobalTag , content );
190193 }
191194
192- public static void json (final String tag , final String content ) {
195+ public static void json (final String tag , final Object content ) {
193196 log (JSON | D , tag , content );
194197 }
195198
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 ) {
197200 log (JSON | type , tag , content );
198201 }
199202
@@ -330,7 +333,7 @@ private static String processBody(final int type, final Object... contents) {
330333
331334 private static String formatObject (int type , Object object ) {
332335 if (object == null ) return NULL ;
333- if (type == JSON ) return LogFormatter .formatJson (object . toString () );
336+ if (type == JSON ) return LogFormatter .object2Json (object );
334337 if (type == XML ) return LogFormatter .formatXml (object .toString ());
335338 return formatObject (object );
336339 }
@@ -344,9 +347,6 @@ private static String formatObject(Object object) {
344347 return iFormatter .format (object );
345348 }
346349 }
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 );
350350 return LogFormatter .object2String (object );
351351 }
352352
@@ -782,17 +782,19 @@ private static class TagHead {
782782 }
783783
784784 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 ));
796798 }
797799
798800 static String formatXml (String xml ) {
@@ -810,7 +812,7 @@ static String formatXml(String xml) {
810812 return xml ;
811813 }
812814
813- static String throwable2String (final Throwable e ) {
815+ private static String throwable2String (final Throwable e ) {
814816 Throwable t = e ;
815817 while (t != null ) {
816818 if (t instanceof UnknownHostException ) {
@@ -830,7 +832,7 @@ static String throwable2String(final Throwable e) {
830832 return sw .toString ();
831833 }
832834
833- static String bundle2String (Bundle bundle ) {
835+ private static String bundle2String (Bundle bundle ) {
834836 Iterator <String > iterator = bundle .keySet ().iterator ();
835837 if (!iterator .hasNext ()) {
836838 return "Bundle {}" ;
@@ -851,7 +853,7 @@ static String bundle2String(Bundle bundle) {
851853 }
852854 }
853855
854- static String intent2String (Intent intent ) {
856+ private static String intent2String (Intent intent ) {
855857 StringBuilder sb = new StringBuilder (128 );
856858 sb .append ("Intent { " );
857859 boolean first = true ;
@@ -961,8 +963,17 @@ static String intent2String(Intent intent) {
961963 return sb .toString ();
962964 }
963965
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 ;
966977 }
967978
968979 @ RequiresApi (api = Build .VERSION_CODES .JELLY_BEAN )
0 commit comments