|  | @@ -1,5 +1,7 @@
 | 
	
		
			
			| 1 | 1 |  package com.reactnativenavigation.layout.parse;
 | 
	
		
			
			| 2 | 2 |  
 | 
	
		
			
			|  | 3 | +import android.support.annotation.NonNull;
 | 
	
		
			
			|  | 4 | +
 | 
	
		
			
			| 3 | 5 |  import org.json.JSONArray;
 | 
	
		
			
			| 4 | 6 |  import org.json.JSONObject;
 | 
	
		
			
			| 5 | 7 |  
 | 
	
	
		
			
			|  | @@ -21,9 +23,35 @@ public class LayoutNode {
 | 
	
		
			
			| 21 | 23 |  		}
 | 
	
		
			
			| 22 | 24 |  	}
 | 
	
		
			
			| 23 | 25 |  
 | 
	
		
			
			|  | 26 | +	@SuppressWarnings("unchecked")
 | 
	
		
			
			|  | 27 | +	public static LayoutNode parse(JSONObject layoutTree) {
 | 
	
		
			
			|  | 28 | +		String id = layoutTree.optString("id");
 | 
	
		
			
			|  | 29 | +		LayoutNode.Type type = LayoutNode.Type.fromString(layoutTree.optString("type"));
 | 
	
		
			
			|  | 30 | +		JSONObject data = parseData(layoutTree);
 | 
	
		
			
			|  | 31 | +		List<LayoutNode> children = parseChildren(layoutTree);
 | 
	
		
			
			|  | 32 | +		return new LayoutNode(id, type, data, children);
 | 
	
		
			
			|  | 33 | +	}
 | 
	
		
			
			|  | 34 | +
 | 
	
		
			
			|  | 35 | +	@NonNull
 | 
	
		
			
			|  | 36 | +	private static List<LayoutNode> parseChildren(JSONObject layoutTree) {
 | 
	
		
			
			|  | 37 | +		List<LayoutNode> children = new ArrayList<>();
 | 
	
		
			
			|  | 38 | +		if (layoutTree.has("children")) {
 | 
	
		
			
			|  | 39 | +			JSONArray rawChildren = layoutTree.optJSONArray("children");
 | 
	
		
			
			|  | 40 | +			for (int i = 0; i < rawChildren.length(); i++) {
 | 
	
		
			
			|  | 41 | +				children.add(LayoutNode.parse(rawChildren.optJSONObject(i)));
 | 
	
		
			
			|  | 42 | +			}
 | 
	
		
			
			|  | 43 | +		}
 | 
	
		
			
			|  | 44 | +		return children;
 | 
	
		
			
			|  | 45 | +	}
 | 
	
		
			
			|  | 46 | +
 | 
	
		
			
			|  | 47 | +	private static JSONObject parseData(JSONObject layoutTree) {
 | 
	
		
			
			|  | 48 | +		return layoutTree.has("data") ? layoutTree.optJSONObject("data") : new JSONObject();
 | 
	
		
			
			|  | 49 | +	}
 | 
	
		
			
			|  | 50 | +
 | 
	
		
			
			| 24 | 51 |  	public final String id;
 | 
	
		
			
			| 25 | 52 |  	public final Type type;
 | 
	
		
			
			| 26 | 53 |  	public final JSONObject data;
 | 
	
		
			
			|  | 54 | +
 | 
	
		
			
			| 27 | 55 |  	public final List<LayoutNode> children;
 | 
	
		
			
			| 28 | 56 |  
 | 
	
		
			
			| 29 | 57 |  	public LayoutNode(String id, Type type) {
 | 
	
	
		
			
			|  | @@ -36,27 +64,4 @@ public class LayoutNode {
 | 
	
		
			
			| 36 | 64 |  		this.data = data;
 | 
	
		
			
			| 37 | 65 |  		this.children = children;
 | 
	
		
			
			| 38 | 66 |  	}
 | 
	
		
			
			| 39 |  | -
 | 
	
		
			
			| 40 |  | -	@SuppressWarnings("unchecked")
 | 
	
		
			
			| 41 |  | -	public static LayoutNode fromTree(JSONObject layoutTree) {
 | 
	
		
			
			| 42 |  | -		String id = layoutTree.optString("id");
 | 
	
		
			
			| 43 |  | -		LayoutNode.Type type = LayoutNode.Type.fromString(layoutTree.optString("type"));
 | 
	
		
			
			| 44 |  | -
 | 
	
		
			
			| 45 |  | -		JSONObject data;
 | 
	
		
			
			| 46 |  | -		if (layoutTree.has("data")) {
 | 
	
		
			
			| 47 |  | -			data = layoutTree.optJSONObject("data");
 | 
	
		
			
			| 48 |  | -		} else {
 | 
	
		
			
			| 49 |  | -			data = new JSONObject();
 | 
	
		
			
			| 50 |  | -		}
 | 
	
		
			
			| 51 |  | -
 | 
	
		
			
			| 52 |  | -		List<LayoutNode> children = new ArrayList<>();
 | 
	
		
			
			| 53 |  | -		if (layoutTree.has("children")) {
 | 
	
		
			
			| 54 |  | -			JSONArray rawChildren = layoutTree.optJSONArray("children");
 | 
	
		
			
			| 55 |  | -			for (int i = 0; i < rawChildren.length(); i++) {
 | 
	
		
			
			| 56 |  | -				children.add(LayoutNode.fromTree(rawChildren.optJSONObject(i)));
 | 
	
		
			
			| 57 |  | -			}
 | 
	
		
			
			| 58 |  | -		}
 | 
	
		
			
			| 59 |  | -
 | 
	
		
			
			| 60 |  | -		return new LayoutNode(id, type, data, children);
 | 
	
		
			
			| 61 |  | -	}
 | 
	
		
			
			| 62 | 67 |  }
 |