22
33import androidx .annotation .Nullable ;
44
5+ import com .facebook .react .bridge .WritableArray ;
6+ import com .facebook .react .bridge .WritableMap ;
7+ import com .facebook .react .bridge .WritableNativeArray ;
8+ import com .facebook .react .bridge .WritableNativeMap ;
9+
10+ import org .json .JSONArray ;
11+ import org .json .JSONException ;
512import org .json .JSONObject ;
613
14+ import java .util .Iterator ;
15+
716public class RNIterableInternal {
817
918 private static String TAG = "RNIterableInternal" ;
@@ -17,7 +26,17 @@ public static String getUserId() {
1726 }
1827
1928 public static JSONObject getInAppMessageJson (IterableInAppMessage message ) {
20- return message .toJSONObject ();
29+ JSONObject messageJson = message .toJSONObject ();
30+
31+ if (message .getCustomPayload () != null ) {
32+ try {
33+ messageJson .put (IterableConstants .ITERABLE_IN_APP_CUSTOM_PAYLOAD , convertJsonToMap (message .getCustomPayload ()));
34+ } catch (JSONException e ) {
35+ // something crazy happened if we're here so we'll just skip
36+ }
37+ }
38+
39+ return messageJson ;
2140 }
2241
2342 public static IterableInAppMessage getMessageById (String messageId ) {
@@ -32,4 +51,56 @@ public static void setAttributionInfo(IterableAttributionInfo attributionInfo) {
3251 IterableApi .getInstance ().setAttributionInfo (attributionInfo );
3352 }
3453
54+ // obtained from https://gist.github.com/viperwarp/2beb6bbefcc268dee7ad
55+ // copied over from Serialization.java for expediency, this can definitely be reorganized better
56+
57+ static WritableMap convertJsonToMap (JSONObject jsonObject ) throws JSONException {
58+ WritableMap map = new WritableNativeMap ();
59+
60+ Iterator <String > iterator = jsonObject .keys ();
61+ while (iterator .hasNext ()) {
62+ String key = iterator .next ();
63+ Object value = jsonObject .get (key );
64+ if (value instanceof JSONObject ) {
65+ map .putMap (key , convertJsonToMap ((JSONObject ) value ));
66+ } else if (value instanceof JSONArray ) {
67+ map .putArray (key , convertJsonToArray ((JSONArray ) value ));
68+ } else if (value instanceof Boolean ) {
69+ map .putBoolean (key , (Boolean ) value );
70+ } else if (value instanceof Integer ) {
71+ map .putInt (key , (Integer ) value );
72+ } else if (value instanceof Double ) {
73+ map .putDouble (key , (Double ) value );
74+ } else if (value instanceof String ) {
75+ map .putString (key , (String ) value );
76+ } else {
77+ map .putString (key , value .toString ());
78+ }
79+ }
80+ return map ;
81+ }
82+
83+ static WritableArray convertJsonToArray (JSONArray jsonArray ) throws JSONException {
84+ WritableArray array = new WritableNativeArray ();
85+
86+ for (int i = 0 ; i < jsonArray .length (); i ++) {
87+ Object value = jsonArray .get (i );
88+ if (value instanceof JSONObject ) {
89+ array .pushMap (convertJsonToMap ((JSONObject ) value ));
90+ } else if (value instanceof JSONArray ) {
91+ array .pushArray (convertJsonToArray ((JSONArray ) value ));
92+ } else if (value instanceof Boolean ) {
93+ array .pushBoolean ((Boolean ) value );
94+ } else if (value instanceof Integer ) {
95+ array .pushInt ((Integer ) value );
96+ } else if (value instanceof Double ) {
97+ array .pushDouble ((Double ) value );
98+ } else if (value instanceof String ) {
99+ array .pushString ((String ) value );
100+ } else {
101+ array .pushString (value .toString ());
102+ }
103+ }
104+ return array ;
105+ }
35106}
0 commit comments