react-native-navigation的迁移库

BundleConverter.java 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package com.reactnativenavigation.bridge;
  2. import android.os.Bundle;
  3. import com.facebook.react.bridge.ReadableArray;
  4. import com.facebook.react.bridge.ReadableMap;
  5. import com.facebook.react.bridge.ReadableMapKeySetIterator;
  6. public class BundleConverter {
  7. public static Bundle toBundle(ReadableMap map) {
  8. Bundle bundle = new Bundle();
  9. ReadableMapKeySetIterator it = map.keySetIterator();
  10. while (it.hasNextKey()) {
  11. String key = it.nextKey();
  12. switch (map.getType(key)) {
  13. case Null:
  14. break;
  15. case Boolean:
  16. bundle.putBoolean(key, map.getBoolean(key));
  17. break;
  18. case Number:
  19. putNumber(bundle, map, key);
  20. break;
  21. case String:
  22. bundle.putString(key, map.getString(key));
  23. break;
  24. case Map:
  25. bundle.putBundle(key, toBundle(map.getMap(key)));
  26. break;
  27. case Array:
  28. bundle.putBundle(key, toBundle(map.getArray(key)));
  29. break;
  30. default:
  31. break;
  32. }
  33. }
  34. return bundle;
  35. }
  36. private static void putNumber(Bundle bundle, ReadableMap map, String key) {
  37. try {
  38. bundle.putInt(key, map.getInt(key));
  39. } catch (Exception e) {
  40. bundle.putDouble(key, map.getDouble(key));
  41. }
  42. }
  43. public static Bundle toBundle(ReadableArray array) {
  44. Bundle bundle = new Bundle();
  45. for (int i = 0; i < array.size(); i++) {
  46. String key = String.valueOf(i);
  47. switch (array.getType(i)) {
  48. case Null:
  49. break;
  50. case Boolean:
  51. bundle.putBoolean(key, array.getBoolean(i));
  52. break;
  53. case Number:
  54. bundle.putDouble(key, array.getDouble(i));
  55. break;
  56. case String:
  57. bundle.putString(key, array.getString(i));
  58. break;
  59. case Map:
  60. bundle.putBundle(key, toBundle(array.getMap(i)));
  61. break;
  62. case Array:
  63. bundle.putBundle(key, toBundle(array.getArray(i)));
  64. break;
  65. default:
  66. break;
  67. }
  68. }
  69. return bundle;
  70. }
  71. }