-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathLayoutNodeParser.java
More file actions
39 lines (31 loc) · 1.15 KB
/
LayoutNodeParser.java
File metadata and controls
39 lines (31 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.reactnativenavigation.options.parsers;
import androidx.annotation.NonNull;
import com.reactnativenavigation.options.LayoutNode;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class LayoutNodeParser {
@SuppressWarnings("unchecked")
public static LayoutNode parse(JSONObject layoutTree) {
String id = layoutTree.optString("id");
LayoutNode.Type type = LayoutNode.Type.valueOf(layoutTree.optString("type"));
JSONObject data = parseData(layoutTree);
List<LayoutNode> children = parseChildren(layoutTree);
return new LayoutNode(id, type, data, children);
}
@NonNull
private static List<LayoutNode> parseChildren(JSONObject layoutTree) {
List<LayoutNode> children = new ArrayList<>();
if (layoutTree.has("children")) {
JSONArray rawChildren = layoutTree.optJSONArray("children");
for (int i = 0; i < rawChildren.length(); i++) {
children.add(parse(rawChildren.optJSONObject(i)));
}
}
return children;
}
private static JSONObject parseData(JSONObject layoutTree) {
return layoutTree.has("data") ? layoutTree.optJSONObject("data") : new JSONObject();
}
}