@@ -259,7 +259,7 @@ private JSONObject parseBundleToJson(Bundle bundle) {
259259 bundle
260260 .keySet ()
261261 .iterator ()
262- .forEachRemaining (key -> setValueInJson (key , json , bundle ));
262+ .forEachRemaining (key -> setValueInJsonSafe (key , json , bundle ));
263263 return json ;
264264
265265 } catch (Exception exception ) {
@@ -269,53 +269,56 @@ private JSONObject parseBundleToJson(Bundle bundle) {
269269 return null ;
270270 }
271271
272- private void setValueInJson (String key , JSONObject json , Bundle bundle ) {
273- try {
274- Object value = bundle .get (key );
275-
276- if (value instanceof Bitmap ) {
277- json .put (key , parseBitmapImageToBase64 ((Bitmap ) value , false ));
278- return ;
279- }
280-
281- if (value instanceof Bundle ) {
282- json .put (key , parseBundleToJson ((Bundle ) value ));
283- return ;
284- }
285-
286- if (isBundleList (value )) {
287- JSONArray jsonArray = ((List <Bundle >) value )
288- .stream ()
289- .map (this ::parseBundleToJson )
290- .collect (Collector .of (JSONArray ::new , JSONArray ::put , JSONArray ::put ));
291-
292- json .put (key , jsonArray );
293- return ;
294- }
295-
296- Optional <List <?>> primitiveListOpt = asPrimitiveList (value );
297- if (primitiveListOpt .isPresent ()) {
298- // ODK/Enketo models a primitive multi-value list (e.g. a select_multiple question) as a
299- // space-delimited string.
300- String nodeList = primitiveListOpt
272+ private void setValueInJson (String key , JSONObject json , Bundle bundle ) throws IOException , JSONException {
273+ Object value = bundle .get (key ); // NOSONAR
274+
275+ if (value instanceof Bitmap bitmap ) {
276+ json .put (key , parseBitmapImageToBase64 (bitmap , false ));
277+ return ;
278+ }
279+
280+ if (value instanceof Bundle b ) {
281+ json .put (key , parseBundleToJson (b ));
282+ return ;
283+ }
284+
285+ if (isBundleList (value )) {
286+ JSONArray jsonArray = ((List <Bundle >) value )
301287 .stream ()
302- .flatMap (List ::stream )
303- .map (Object ::toString )
304- .map (s -> s .replace (" " , "_" ))
305- .collect (Collectors .joining (" " ));
306- json .put (key , nodeList );
307- return ;
308- }
309-
310- Optional <Uri > imagePath = getImageUri (value );
311- if (imagePath .isPresent ()) {
312- boolean keepFullResolution = bundle .getBoolean ("sampleImage" , false );
313- json .put (key , getImageFromStoragePath (imagePath .get (), keepFullResolution ));
314- return ;
315- }
316-
317- json .put (key , JSONObject .wrap (value ));
288+ .map (this ::parseBundleToJson )
289+ .collect (Collector .of (JSONArray ::new , JSONArray ::put , JSONArray ::put ));
290+
291+ json .put (key , jsonArray );
292+ return ;
293+ }
318294
295+ Optional <List <?>> primitiveListOpt = asPrimitiveList (value );
296+ if (primitiveListOpt .isPresent ()) {
297+ // ODK/Enketo models a primitive multi-value list (e.g. a select_multiple question) as a
298+ // space-delimited string.
299+ String nodeList = primitiveListOpt
300+ .stream ()
301+ .flatMap (List ::stream )
302+ .map (Object ::toString )
303+ .map (s -> s .replace (" " , "_" ))
304+ .collect (Collectors .joining (" " ));
305+ json .put (key , nodeList );
306+ return ;
307+ }
308+
309+ Optional <Uri > imagePath = getImageUri (value );
310+ if (imagePath .isPresent ()) {
311+ boolean keepFullResolution = bundle .getBoolean ("sampleImage" , false );
312+ json .put (key , getImageFromStoragePath (imagePath .get (), keepFullResolution ));
313+ return ;
314+ }
315+
316+ json .put (key , JSONObject .wrap (value ));
317+ }
318+
319+ private void setValueInJsonSafe (String key , JSONObject json , Bundle bundle ) {
320+ try {
321+ setValueInJson (key , json , bundle );
319322 } catch (Exception exception ) {
320323 error (exception , "ChtExternalApp :: Problem parsing bundle to json. Key=%s, Bundle=%s" , key , bundle );
321324 }
@@ -342,26 +345,24 @@ private boolean isPrimitive(Object value) {
342345 value instanceof Character ;
343346 }
344347
345- private Optional <List <?>> asPrimitiveList (Object value ) {
346- if (value != null && value .getClass ().isArray ()) {
347- // Java utility methods are not great when mixing primitive and object arrays.
348- // So, we manually convert any array to a List<Object>.
349- int len = java .lang .reflect .Array .getLength (value );
350- List <Object > list = new ArrayList <>(len );
351- for (int i = 0 ; i < len ; i ++) {
352- list .add (java .lang .reflect .Array .get (value , i ));
353- }
354- value = list ;
355- }
356- if (
357- !(value instanceof List )
358- || ((List <?>) value ).isEmpty ()
359- || !isPrimitive (((List <?>) value ).get (0 ))
360- ) {
361- return Optional .empty ();
348+ private List <?> getArrayAsList (Object array ) {
349+ int len = java .lang .reflect .Array .getLength (array );
350+ List <Object > list = new ArrayList <>(len );
351+ for (int i = 0 ; i < len ; i ++) {
352+ list .add (java .lang .reflect .Array .get (array , i ));
362353 }
354+ return list ;
355+ }
363356
364- return Optional .of ((List <?>) value );
357+ private Optional <List <?>> asPrimitiveList (Object value ) {
358+ return Optional
359+ .ofNullable (value )
360+ .map (v -> v .getClass ().isArray () ? getArrayAsList (v ) : v )
361+ .filter (List .class ::isInstance )
362+ .map (v -> (List <?>) v )
363+ .filter (v -> !v .isEmpty ())
364+ .filter (v -> isPrimitive (v .get (0 )))
365+ .map (v -> (List <?>) v );
365366 }
366367
367368 private Optional <Uri > getImageUri (Object value ) {
0 commit comments