com.bybutter.sisyphus.protobuf.primitives.ValueExtension.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sisyphus-jackson-protobuf Show documentation
Show all versions of sisyphus-jackson-protobuf Show documentation
Jackson support for Sisyphus protobuf runtime customized message
package com.bybutter.sisyphus.protobuf.primitives
import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.node.ObjectNode
fun JsonNode.toProto(): Value = Value {
when {
[email protected] -> {
listValue = ListValue {
values += [email protected] { it.toProto() }
}
}
[email protected] -> {
structValue = Struct {
for ((field, node) in [email protected]()) {
fields[field] = node.toProto()
}
}
}
[email protected] -> {
numberValue = [email protected]()
}
[email protected] -> {
boolValue = [email protected]()
}
[email protected] -> {
val text = [email protected]()
text.toDoubleOrNull()?.let {
numberValue = it
} ?: text.toBooleanOrNull()?.let {
boolValue = it
} ?: run {
stringValue = text
}
}
[email protected] -> {
nullValue = NullValue.NULL_VALUE
}
else -> throw UnsupportedOperationException("Unknown json type '$this'.")
}
}
fun ObjectNode.toProto(): Struct = Struct {
for ((field, node) in [email protected]()) {
fields[field] = node.toProto()
}
}
fun String.toBooleanOrNull(): Boolean? {
return if (this.equals("true", true) || this.equals("false", true)) {
this.toBoolean()
} else {
null
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy