package <%= java_package %> import org.json.JSONArray import org.json.JSONObject /** * Immutable data class representing one node in the Dala component tree. * Parsed from the JSON binary produced by Dala.Renderer on the BEAM. */ data class DalaNode( val type: String, val props: Map, val children: List ) /** Recursively parse a JSONObject into a DalaNode tree. */ fun JSONObject.toDalaNode(): DalaNode { val propsMap = mutableMapOf() val propsObj = optJSONObject("props") ?: JSONObject() for (key in propsObj.keys()) { propsMap[key] = propsObj.get(key) } val childList = mutableListOf() val childArr = optJSONArray("children") ?: JSONArray() for (i in 0 until childArr.length()) { childList.add(childArr.getJSONObject(i).toDalaNode()) } return DalaNode( type = getString("type"), props = propsMap, children = childList ) }