Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[android] fix ReadableNativeMap.toHashMap() for nested maps and arrays #18455

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.facebook.proguard.annotations.DoNotStrip;

import java.util.HashMap;
import java.util.Iterator;

import com.facebook.infer.annotation.Assertions;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -250,7 +251,31 @@ public HashMap<String, Object> toHashMap() {
}
return hashMap;
}
return getLocalMap();

// we can almost just return getLocalMap(), but we need to convert nested arrays and maps to the
// correct types first
HashMap<String, Object> hashMap = new HashMap<>(getLocalMap());
Iterator iterator = hashMap.keySet().iterator();

while (iterator.hasNext()) {
String key = (String) iterator.next();
switch (getType(key)) {
case Null:
case Boolean:
case Number:
case String:
break;
case Map:
hashMap.put(key, Assertions.assertNotNull(getMap(key)).toHashMap());
break;
case Array:
hashMap.put(key, Assertions.assertNotNull(getArray(key)).toArrayList());
break;
default:
throw new IllegalArgumentException("Could not convert object with key: " + key + ".");
}
}
return hashMap;
}

/**
Expand Down