Add test for DI constructability of TrustAjaxPage
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / web / ajax / JsonReturnObject.kt
1 package net.pterodactylus.sone.web.ajax
2
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
8
9 /**
10  * JSON return object for AJAX requests.
11  */
12 open class JsonReturnObject(val isSuccess: Boolean) {
13
14         private val values = mutableMapOf<String, JsonNode?>()
15
16         val content: Map<String, Any?>
17                 @JsonAnyGetter get() = values
18
19         operator fun get(key: String) = values[key]
20
21         fun put(key: String, value: String?) = apply {
22                 values[key] = TextNode.valueOf(value)
23         }
24
25         fun put(key: String, value: Int) = apply {
26                 values[key] = IntNode.valueOf(value)
27         }
28
29         fun put(key: String, value: Boolean) = apply {
30                 values[key] = BooleanNode.valueOf(value)
31         }
32
33         fun put(key: String, value: JsonNode) = apply {
34                 values[key] = value
35         }
36
37         override fun hashCode(): Int {
38                 return isSuccess.hashCode() xor content.hashCode()
39         }
40
41         override fun equals(other: Any?) =
42                         (other as? JsonReturnObject)?.let {
43                                 it.isSuccess == isSuccess && it.content == content
44                         } ?: false
45
46 }