Browse Source

migrate to ReadableMap.toHashMap

Daniel Zlotin 6 years ago
parent
commit
781997ad4e

+ 0
- 85
lib/android/app/src/main/java/com/reactnativenavigation/parse/parsers/JSONParser.java View File

@@ -1,85 +0,0 @@
1
-package com.reactnativenavigation.parse.parsers;
2
-
3
-import com.facebook.react.bridge.ReadableArray;
4
-import com.facebook.react.bridge.ReadableMap;
5
-import com.facebook.react.bridge.ReadableMapKeySetIterator;
6
-
7
-import org.json.JSONArray;
8
-import org.json.JSONException;
9
-import org.json.JSONObject;
10
-
11
-public class JSONParser {
12
-	public static JSONObject parse(ReadableMap map) {
13
-		try {
14
-			ReadableMapKeySetIterator it = map.keySetIterator();
15
-			JSONObject result = new JSONObject();
16
-			while (it.hasNextKey()) {
17
-				String key = it.nextKey();
18
-				switch (map.getType(key)) {
19
-					case String:
20
-						result.put(key, map.getString(key));
21
-						break;
22
-					case Number:
23
-						result.put(key, parseNumber(map, key));
24
-						break;
25
-					case Boolean:
26
-						result.put(key, map.getBoolean(key));
27
-						break;
28
-					case Array:
29
-						result.put(key, parse(map.getArray(key)));
30
-						break;
31
-					case Map:
32
-						result.put(key, parse(map.getMap(key)));
33
-						break;
34
-					default:
35
-						break;
36
-				}
37
-			}
38
-			return result;
39
-		} catch (JSONException e) {
40
-			throw new RuntimeException(e);
41
-		}
42
-	}
43
-
44
-	public static JSONArray parse(ReadableArray arr) {
45
-		JSONArray result = new JSONArray();
46
-		for (int i = 0; i < arr.size(); i++) {
47
-			switch (arr.getType(i)) {
48
-				case String:
49
-					result.put(arr.getString(i));
50
-					break;
51
-				case Number:
52
-					result.put(parseNumber(arr, i));
53
-					break;
54
-				case Boolean:
55
-					result.put(arr.getBoolean(i));
56
-					break;
57
-				case Array:
58
-					result.put(parse(arr.getArray(i)));
59
-					break;
60
-				case Map:
61
-					result.put(parse(arr.getMap(i)));
62
-					break;
63
-				default:
64
-					break;
65
-			}
66
-		}
67
-		return result;
68
-	}
69
-
70
-	private static Object parseNumber(ReadableMap map, String key) {
71
-		try {
72
-			return map.getInt(key);
73
-		} catch (Exception e) {
74
-			return map.getDouble(key);
75
-		}
76
-	}
77
-
78
-	private static Object parseNumber(ReadableArray arr, int index) {
79
-		try {
80
-			return arr.getInt(index);
81
-		} catch (Exception e) {
82
-			return arr.getDouble(index);
83
-		}
84
-	}
85
-}

+ 8
- 7
lib/android/app/src/main/java/com/reactnativenavigation/react/NavigationModule.java View File

@@ -9,7 +9,6 @@ import com.facebook.react.bridge.ReactContextBaseJavaModule;
9 9
 import com.facebook.react.bridge.ReactMethod;
10 10
 import com.facebook.react.bridge.ReadableMap;
11 11
 import com.reactnativenavigation.NavigationActivity;
12
-import com.reactnativenavigation.parse.parsers.JSONParser;
13 12
 import com.reactnativenavigation.parse.LayoutFactory;
14 13
 import com.reactnativenavigation.parse.LayoutNode;
15 14
 import com.reactnativenavigation.parse.parsers.LayoutNodeParser;
@@ -19,6 +18,8 @@ import com.reactnativenavigation.utils.UiThread;
19 18
 import com.reactnativenavigation.viewcontrollers.Navigator;
20 19
 import com.reactnativenavigation.viewcontrollers.ViewController;
21 20
 
21
+import org.json.JSONObject;
22
+
22 23
 public class NavigationModule extends ReactContextBaseJavaModule {
23 24
 	private static final String NAME = "RNNBridgeModule";
24 25
 	private final ReactInstanceManager reactInstanceManager;
@@ -36,7 +37,7 @@ public class NavigationModule extends ReactContextBaseJavaModule {
36 37
 
37 38
 	@ReactMethod
38 39
 	public void setRoot(final ReadableMap rawLayoutTree, final Promise promise) {
39
-		final LayoutNode layoutTree = LayoutNodeParser.parse(JSONParser.parse(rawLayoutTree));
40
+		final LayoutNode layoutTree = LayoutNodeParser.parse(new JSONObject(rawLayoutTree.toHashMap()));
40 41
 		handle(() -> {
41 42
             final ViewController viewController = newLayoutFactory().create(layoutTree);
42 43
             navigator().setRoot(viewController, promise);
@@ -45,19 +46,19 @@ public class NavigationModule extends ReactContextBaseJavaModule {
45 46
 
46 47
 	@ReactMethod
47 48
 	public void setDefaultOptions(final ReadableMap options) {
48
-        final Options defaultOptions = Options.parse(new TypefaceLoader(activity()), JSONParser.parse(options));
49
+        final Options defaultOptions = Options.parse(new TypefaceLoader(activity()), new JSONObject(options.toHashMap()));
49 50
         handle(() -> navigator().setDefaultOptions(defaultOptions));
50 51
     }
51 52
 
52 53
 	@ReactMethod
53 54
 	public void setOptions(final String onComponentId, final ReadableMap options) {
54
-		final Options navOptions = Options.parse(new TypefaceLoader(activity()), JSONParser.parse(options));
55
+		final Options navOptions = Options.parse(new TypefaceLoader(activity()), new JSONObject(options.toHashMap()));
55 56
 		handle(() -> navigator().setOptions(onComponentId, navOptions));
56 57
 	}
57 58
 
58 59
 	@ReactMethod
59 60
 	public void push(final String onComponentId, final ReadableMap rawLayoutTree, final Promise promise) {
60
-		final LayoutNode layoutTree = LayoutNodeParser.parse(JSONParser.parse(rawLayoutTree));
61
+		final LayoutNode layoutTree = LayoutNodeParser.parse(new JSONObject(rawLayoutTree.toHashMap()));
61 62
 		handle(() -> {
62 63
             final ViewController viewController = newLayoutFactory().create(layoutTree);
63 64
             navigator().push(onComponentId, viewController, promise);
@@ -81,7 +82,7 @@ public class NavigationModule extends ReactContextBaseJavaModule {
81 82
 
82 83
 	@ReactMethod
83 84
 	public void showModal(final ReadableMap rawLayoutTree, final Promise promise) {
84
-		final LayoutNode layoutTree = LayoutNodeParser.parse(JSONParser.parse(rawLayoutTree));
85
+		final LayoutNode layoutTree = LayoutNodeParser.parse(new JSONObject(rawLayoutTree.toHashMap()));
85 86
 		handle(() -> {
86 87
             final ViewController viewController = newLayoutFactory().create(layoutTree);
87 88
             navigator().showModal(viewController, promise);
@@ -100,7 +101,7 @@ public class NavigationModule extends ReactContextBaseJavaModule {
100 101
 
101 102
 	@ReactMethod
102 103
 	public void showOverlay(final ReadableMap rawLayoutTree) {
103
-        final LayoutNode layoutTree = LayoutNodeParser.parse(JSONParser.parse(rawLayoutTree));
104
+        final LayoutNode layoutTree = LayoutNodeParser.parse(new JSONObject(rawLayoutTree.toHashMap()));
104 105
         handle(() -> {
105 106
             final ViewController viewController = newLayoutFactory().create(layoutTree);
106 107
             navigator().showOverlay(viewController);

+ 0
- 63
lib/android/app/src/test/java/com/reactnativenavigation/parse/JSONParserTest.java View File

@@ -1,63 +0,0 @@
1
-package com.reactnativenavigation.parse;
2
-
3
-import com.facebook.react.bridge.*;
4
-import com.reactnativenavigation.*;
5
-import com.reactnativenavigation.parse.parsers.JSONParser;
6
-
7
-import org.json.*;
8
-import org.junit.*;
9
-
10
-import static org.assertj.core.api.Java6Assertions.*;
11
-
12
-public class JSONParserTest extends BaseTest {
13
-    @Test
14
-    public void parsesMap() throws Exception {
15
-        JavaOnlyMap input = new JavaOnlyMap();
16
-        input.putString("keyString", "stringValue");
17
-        input.putInt("keyInt", 123);
18
-        input.putDouble("keyDouble", 123.456);
19
-        input.putBoolean("keyBoolean", true);
20
-        input.putArray("keyArray", new JavaOnlyArray());
21
-        input.putMap("keyMap", new JavaOnlyMap());
22
-        input.putNull("bla");
23
-
24
-        JSONObject result = JSONParser.parse(input);
25
-
26
-
27
-        assertThat(result.keys()).containsOnly(
28
-                "keyString",
29
-                "keyInt",
30
-                "keyDouble",
31
-                "keyBoolean",
32
-                "keyMap",
33
-                "keyArray");
34
-
35
-        assertThat(result.get("keyString")).isEqualTo("stringValue");
36
-        assertThat(result.get("keyInt")).isEqualTo(123);
37
-        assertThat(result.get("keyDouble")).isEqualTo(123.456);
38
-        assertThat(result.get("keyBoolean")).isEqualTo(true);
39
-        assertThat(result.getJSONObject("keyMap").keys()).isEmpty();
40
-        assertThat(result.getJSONArray("keyArray").length()).isZero();
41
-    }
42
-
43
-    @Test
44
-    public void parsesArrays() throws Exception {
45
-        JavaOnlyArray input = new JavaOnlyArray();
46
-        input.pushString("Hello");
47
-        input.pushInt(123);
48
-        input.pushDouble(123.456);
49
-        input.pushBoolean(true);
50
-        input.pushArray(new JavaOnlyArray());
51
-        input.pushMap(new JavaOnlyMap());
52
-        input.pushNull();
53
-
54
-        JSONArray result = JSONParser.parse(input);
55
-        assertThat(result.length()).isEqualTo(6);
56
-        assertThat(result.get(0)).isEqualTo("Hello");
57
-        assertThat(result.get(1)).isEqualTo(123);
58
-        assertThat(result.get(2)).isEqualTo(123.456);
59
-        assertThat(result.get(3)).isEqualTo(true);
60
-        assertThat(result.getJSONArray(4).length()).isZero();
61
-        assertThat(result.getJSONObject(5).keys()).isEmpty();
62
-    }
63
-}