org.http4k.format.write.kt Maven / Gradle / Ivy
package org.http4k.format
import com.squareup.moshi.JsonWriter
fun JsonWriter.string(name: String, theValue: String?) {
name(name)
theValue?.also(::value) ?: nullValue()
}
fun JsonWriter.number(name: String, theValue: Number?) {
name(name)
theValue?.also(::value) ?: nullValue()
}
fun JsonWriter.boolean(name: String, theValue: Boolean?) {
name(name)
theValue?.also(::value) ?: nullValue()
}
fun JsonWriter.obj(name: String, theValue: Map?) {
name(name)
obj(theValue) {
entries.forEach { string(it.key, it.value?.toString()) }
}
}
fun JsonWriter.obj(name: String, obj: T?, fn: T.() -> Unit) {
name(name)
obj(obj, fn)
}
fun JsonWriter.obj(obj: T?, fn: T.() -> Unit) {
when (obj) {
null -> nullValue()
else -> {
beginObject()
obj.fn()
endObject()
}
}
}
fun JsonWriter.list(name: String, theValue: List?) {
name(name)
list(theValue, ::value)
}
fun JsonWriter.list(name: String, list: List?, fn: T.() -> Unit) {
name(name)
list(list, fn)
}
fun JsonWriter.list(list: List?, fn: T.() -> Unit) {
when (list) {
null -> nullValue()
else -> {
beginArray()
list.forEach(fn)
endArray()
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy