Current section

Files

Jump to
dala_new priv templates dala.new android app src main java MobNode.kt.eex
Raw

priv/templates/dala.new/android/app/src/main/java/MobNode.kt.eex

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<String, Any?>,
val children: List<DalaNode>
)
/** Recursively parse a JSONObject into a DalaNode tree. */
fun JSONObject.toDalaNode(): DalaNode {
val propsMap = mutableMapOf<String, Any?>()
val propsObj = optJSONObject("props") ?: JSONObject()
for (key in propsObj.keys()) {
propsMap[key] = propsObj.get(key)
}
val childList = mutableListOf<DalaNode>()
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
)
}