Skip to content

Commit

Permalink
Merge pull request #107 from emartech/MV-744-inbox-action
Browse files Browse the repository at this point in the history
feat: update MapUtil and ArrayUtil for ArrayList mapping
  • Loading branch information
eduzatoni authored Aug 8, 2024
2 parents 89d477b + 01b98ad commit dc03f52
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
27 changes: 27 additions & 0 deletions android/src/main/java/com/emarsys/rnwrapper/ArrayUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Map;

public class ArrayUtil {
Expand Down Expand Up @@ -122,6 +123,32 @@ public static WritableArray toWritableArray(Object[] array) {
return writableArray;
}

public static WritableArray arrayListToWritableArray(ArrayList array) {
WritableArray writableArray = Arguments.createArray();

for (int i = 0; i < array.size(); i++) {
Object value = array.get(i);

if (value == null) {
writableArray.pushNull();
} else if (value instanceof Boolean) {
writableArray.pushBoolean((Boolean) value);
} else if (value instanceof Double) {
writableArray.pushDouble((Double) value);
} else if (value instanceof Integer) {
writableArray.pushInt((Integer) value);
} else if (value instanceof String) {
writableArray.pushString((String) value);
} else if (value instanceof Map) {
writableArray.pushMap(MapUtil.toWritableMap((Map<String, Object>) value));
} else if (value instanceof ArrayList) {
writableArray.pushArray(ArrayUtil.arrayListToWritableArray((ArrayList) value));
}
}

return writableArray;
}

public static WritableArray jsonArrayToWritableArray(JSONArray jsonArray) {
WritableArray writableArray = new WritableNativeArray();

Expand Down
3 changes: 3 additions & 0 deletions android/src/main/java/com/emarsys/rnwrapper/MapUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.json.JSONObject;

import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
Expand Down Expand Up @@ -125,6 +126,8 @@ public static WritableMap toWritableMap(Map<String, Object> map) {
writableMap.putString((String) pair.getKey(), (String) value);
} else if (value instanceof Map) {
writableMap.putMap((String) pair.getKey(), MapUtil.toWritableMap((Map<String, Object>) value));
} else if (value instanceof ArrayList) {
writableMap.putArray((String) pair.getKey(), ArrayUtil.arrayListToWritableArray((ArrayList) value));
} else if (value.getClass() != null && value.getClass().isArray()) {
writableMap.putArray((String) pair.getKey(), ArrayUtil.toWritableArray((Object[]) value));
}
Expand Down

0 comments on commit dc03f52

Please sign in to comment.