🔥 Remove identifier from plugin connector interface
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / utils / Json.kt
1 package net.pterodactylus.sone.utils
2
3 import com.fasterxml.jackson.databind.JsonNode
4 import com.fasterxml.jackson.databind.node.ArrayNode
5 import com.fasterxml.jackson.databind.node.JsonNodeFactory.instance
6 import com.fasterxml.jackson.databind.node.ObjectNode
7
8 fun jsonObject(block: ObjectNode.() -> Unit): ObjectNode = ObjectNode(instance).apply(block)
9
10 fun jsonObject(vararg properties: Pair<String, Any?>) = jsonObject {
11         properties.forEach {
12                 it.second.let { value ->
13                         when (value) {
14                                 is String -> put(it.first, value)
15                                 is Int -> put(it.first, value)
16                                 is Long -> put(it.first, value)
17                                 is Boolean -> put(it.first, value)
18                                 else -> Unit
19                         }
20                 }
21         }
22 }
23
24 fun jsonArray(vararg objects: String?): ArrayNode = objects.fold(ArrayNode(instance), ArrayNode::add)
25 fun jsonArray(vararg objects: JsonNode?): ArrayNode = objects.fold(ArrayNode(instance), ArrayNode::add)
26
27 fun Iterable<ObjectNode>.toArray(): ArrayNode = fold(ArrayNode(instance), ArrayNode::add)