mini-cards/app/src/main/java/com/programaker/api/data/ProgramakerCustomBlock.kt

74 lines
2.4 KiB
Kotlin
Raw Normal View History

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<ProgramakerCustomBlockArgument>?,
val block_type: String?,
val block_result_type: String?,
var bridge_id: String?,
2020-05-29 10:38:22 +00:00
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
2020-05-29 10:38:22 +00:00
)
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"),
2020-05-29 10:38:22 +00:00
obj.optString("block_result_type"),
obj.optString("bridge_id"),
2020-05-29 10:38:22 +00:00
ProgramakerCustomBlockSaveTo.deserialize(obj.optJSONObject("save_to")),
obj.optString("key"),
ProgramakerCustomBlockSubkeyDefinition.deserialize(obj.optJSONObject("subkey"))
)
return block
}
}
}