1919
2020import java .io .ByteArrayOutputStream ;
2121import java .util .ArrayList ;
22+ import java .util .HashMap ;
23+ import java .util .Iterator ;
2224import java .util .List ;
25+ import java .util .Map ;
2326
2427class Helpers {
28+ interface JSONObjectGeneratorWithContext <T > {
29+ JSONObject generateJSONObject (T param , Context context ) throws JSONException ;
30+ }
31+
32+ interface JSONObjectGenerator <T > {
33+ JSONObject generateJSONObject (T param ) throws JSONException ;
34+ }
35+
36+ static <T > JSONArray generateList (List <T > list ) {
37+ JSONArray result = new JSONArray ();
38+ if (list == null ) return result ;
39+ for (T t : list )
40+ if (t != null )
41+ result .put (t );
42+
43+ return result ;
44+ }
45+
46+ static <T > JSONArray generateList (List <T > list , JSONObjectGenerator <T > generator ) throws JSONException {
47+ JSONArray result = new JSONArray ();
48+ if (list == null ) return result ;
49+ for (T t : list )
50+ if (t != null )
51+ result .put (generator .generateJSONObject (t ));
52+
53+ return result ;
54+ }
55+
56+ static <T > JSONArray generateList (List <T > list , JSONObjectGeneratorWithContext <T > generator , Context context ) throws JSONException {
57+ JSONArray result = new JSONArray ();
58+ if (list == null ) return result ;
59+ for (T t : list )
60+ if (t != null )
61+ result .put (generator .generateJSONObject (t , context ));
62+
63+ return result ;
64+ }
65+
66+ static <T > JSONArray generateArray (T [] array ) throws JSONException {
67+ JSONArray result = new JSONArray ();
68+ if (array == null ) return result ;
69+ for (int i = 0 ; i < array .length ; i ++)
70+ result .put (i , array [i ]);
71+
72+ return result ;
73+ }
74+
75+ static <T > JSONArray generateArray (T [] array , JSONObjectGenerator <T > generator ) throws JSONException {
76+ JSONArray result = new JSONArray ();
77+ if (array == null ) return result ;
78+ for (int i = 0 ; i < array .length ; i ++)
79+ result .put (i , generator .generateJSONObject (array [i ]));
80+
81+ return result ;
82+ }
83+
84+ static <T , V > JSONObject generateMap (Map <T , V > map ) throws JSONException {
85+ JSONObject result = new JSONObject ();
86+ if (map == null ) return result ;
87+ for (Map .Entry <T , V > entry : map .entrySet ())
88+ if (entry != null )
89+ result .put (entry .getKey ().toString (), entry .getValue ());
90+ return result ;
91+ }
92+
93+ static JSONArray generateIntArray (int [] array ) throws JSONException {
94+ JSONArray result = new JSONArray ();
95+ if (array == null ) return result ;
96+ for (int i = 0 ; i < array .length ; i ++)
97+ result .put (i , array [i ]);
98+
99+ return result ;
100+ }
101+
102+ static int [] intArrayFromJSON (JSONArray input ) throws JSONException {
103+ int [] result = new int [input .length ()];
104+ for (int i = 0 ; i < input .length (); i ++)
105+ result [i ] = input .getInt (i );
106+
107+ return result ;
108+ }
109+
110+ static JSONArray generateByteArray (byte [] array ) throws JSONException {
111+ JSONArray result = new JSONArray ();
112+ if (array == null ) return result ;
113+ for (int i = 0 ; i < array .length ; i ++)
114+ result .put (i , array [i ]);
115+
116+ return result ;
117+ }
118+
119+ static JSONArray generateLongArray (long [] array ) throws JSONException {
120+ JSONArray result = new JSONArray ();
121+ if (array == null ) return result ;
122+ for (int i = 0 ; i < array .length ; i ++)
123+ result .put (i , array [i ]);
124+
125+ return result ;
126+ }
127+
25128 static Bitmap bitmapFromBase64 (String base64 ) {
26- byte [] decodedString = Base64 .decode (base64 , Base64 .DEFAULT );
129+ byte [] decodedString = Base64 .decode (base64 , Base64 .NO_WRAP );
27130 Bitmap result = BitmapFactory .decodeByteArray (decodedString , 0 , decodedString .length );
28131 int sizeMultiplier = result .getByteCount () / 5000000 ;
29132 if (result .getByteCount () > 5000000 )
@@ -63,13 +166,13 @@ static Bitmap bitmapFromDrawable(Drawable drawable) {
63166 }
64167
65168 static String bitmapToBase64String (Bitmap bitmap ) {
66- if (bitmap == null ) return null ;
169+ if (bitmap == null ) return null ;
67170
68171 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream ();
69172 bitmap .compress (Bitmap .CompressFormat .JPEG , 100 , byteArrayOutputStream );
70173 byte [] byteArray = byteArrayOutputStream .toByteArray ();
71174
72- return Base64 .encodeToString (byteArray , Base64 .DEFAULT );
175+ return Base64 .encodeToString (byteArray , Base64 .NO_WRAP );
73176 }
74177
75178 static Matrix matrixFromFloatArray (float [] floats ) {
@@ -142,4 +245,15 @@ static String[] stringArrayFromJson(JSONArray jsonArray) {
142245 result [i ] = jsonArray .optString (i );
143246 return result ;
144247 }
248+
249+ static Map <String , String > stringMapFromJson (JSONObject input ) throws JSONException {
250+ Map <String , String > result = new HashMap <>();
251+ Iterator <String > keys = input .keys ();
252+ while (keys .hasNext ()) {
253+ String key = keys .next ();
254+ String value = input .getString (key );
255+ result .put (key , value );
256+ }
257+ return result ;
258+ }
145259}
0 commit comments