1 package net.pterodactylus.sone.web.ajax
3 import com.fasterxml.jackson.annotation.JsonAnyGetter
4 import com.fasterxml.jackson.databind.JsonNode
5 import com.fasterxml.jackson.databind.node.BooleanNode
6 import com.fasterxml.jackson.databind.node.IntNode
7 import com.fasterxml.jackson.databind.node.TextNode
10 * JSON return object for AJAX requests.
12 open class JsonReturnObject(val isSuccess: Boolean) {
14 private val values = mutableMapOf<String, JsonNode?>()
16 val content: Map<String, Any?>
17 @JsonAnyGetter get() = values
19 operator fun get(key: String) = values[key]
21 fun put(key: String, value: String?) = apply {
22 values[key] = TextNode.valueOf(value)
25 fun put(key: String, value: Int) = apply {
26 values[key] = IntNode.valueOf(value)
29 fun put(key: String, value: Boolean) = apply {
30 values[key] = BooleanNode.valueOf(value)
33 fun put(key: String, value: JsonNode) = apply {
37 override fun hashCode(): Int {
38 return isSuccess.hashCode() xor content.hashCode()
41 override fun equals(other: Any?) =
42 (other as? JsonReturnObject)?.let {
43 it.isSuccess == isSuccess && it.content == content