react-native-navigation的迁移库

LayoutNodeParser.java 1.1KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package com.reactnativenavigation.parse.parsers;
  2. import android.support.annotation.NonNull;
  3. import com.reactnativenavigation.parse.LayoutNode;
  4. import org.json.JSONArray;
  5. import org.json.JSONObject;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. public class LayoutNodeParser {
  9. @SuppressWarnings("unchecked")
  10. public static LayoutNode parse(JSONObject layoutTree) {
  11. String id = layoutTree.optString("id");
  12. LayoutNode.Type type = LayoutNode.Type.valueOf(layoutTree.optString("type"));
  13. JSONObject data = parseData(layoutTree);
  14. List<LayoutNode> children = parseChildren(layoutTree);
  15. return new LayoutNode(id, type, data, children);
  16. }
  17. @NonNull
  18. private static List<LayoutNode> parseChildren(JSONObject layoutTree) {
  19. List<LayoutNode> children = new ArrayList<>();
  20. if (layoutTree.has("children")) {
  21. JSONArray rawChildren = layoutTree.optJSONArray("children");
  22. for (int i = 0; i < rawChildren.length(); i++) {
  23. children.add(parse(rawChildren.optJSONObject(i)));
  24. }
  25. }
  26. return children;
  27. }
  28. private static JSONObject parseData(JSONObject layoutTree) {
  29. return layoutTree.has("data") ? layoutTree.optJSONObject("data") : new JSONObject();
  30. }
  31. }