package com.programaker.api.data import org.json.JSONArray import org.json.JSONObject class ProgramakerCustomBlock( val block_id: String, val function_name: String, val message: String, val arguments: List?, val block_type: String?, val block_result_type: String?, var bridge_id: String?, val save_to: ProgramakerCustomBlockSaveTo?, var key: String?, val subkey: ProgramakerCustomBlockSubkeyDefinition? ) { fun serialize(): JSONObject { val serializedArguments = JSONArray() if (arguments != null) { for (value in arguments) { serializedArguments.put(value.serialize()) } } var saveToSerialized: JSONObject? = null if (save_to != null) { saveToSerialized = save_to.serialize() } val serialized = hashMapOf( "block_id" to block_id, "function_name" to function_name, "message" to message, "arguments" to serializedArguments, "block_type" to block_type, "bridge_id" to bridge_id, "save_to" to saveToSerialized ) if (key != null) { serialized.put("key", key) serialized.put("subkey", subkey?.serialize()) } if (block_result_type != null || key == null) { serialized.put("block_result_type", block_result_type) } return JSONObject(serialized as Map<*, *>) } companion object { @JvmStatic fun deserialize(obj: JSONObject): ProgramakerCustomBlock { var block = ProgramakerCustomBlock( obj.getString("block_id"), obj.getString("function_name"), obj.getString("message"), ProgramakerCustomBlockArgument.deserialize(obj.optJSONArray("arguments")), obj.getString("block_type"), obj.optString("block_result_type"), obj.optString("bridge_id"), ProgramakerCustomBlockSaveTo.deserialize(obj.optJSONObject("save_to")), obj.optString("key"), ProgramakerCustomBlockSubkeyDefinition.deserialize(obj.optJSONObject("subkey")) ) return block } } }