Skip to content

Commit

Permalink
fix airbytevalue serialize
Browse files Browse the repository at this point in the history
  • Loading branch information
edgao committed Jan 10, 2025
1 parent 05b6d0b commit c524258
Showing 1 changed file with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
import com.fasterxml.jackson.module.afterburner.AfterburnerModule
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
import io.airbyte.cdk.load.data.NullValue
import io.airbyte.cdk.load.data.ObjectValue
import java.io.InputStream

object Jsons : ObjectMapper() {
Expand All @@ -35,7 +37,15 @@ object Jsons : ObjectMapper() {
}

fun <T> T.serializeToString(): String {
return Jsons.writeValueAsString(this)
return when (this) {
// Handle null specially - otherwise jackson sees it as an empty object
// and returns "{}"
is NullValue -> return "null"
// no idea why this is necessary, but jackson throws an exception if you try to serialize
// the ObjectValue directly
is ObjectValue -> return Jsons.writeValueAsString(this.values)
else -> Jsons.writeValueAsString(this)
}
}

fun <T> InputStream.readIntoClass(klass: Class<T>): T =
Expand All @@ -54,5 +64,10 @@ fun <T> String.deserializeToClass(klass: Class<T>): T {
}

fun Any.serializeToJsonBytes(): ByteArray {
return Jsons.writeValueAsBytes(this)
// same special cases as serializeToString()
return when (this) {
is NullValue -> return "null".toByteArray(Charsets.UTF_8)
is ObjectValue -> return Jsons.writeValueAsBytes(this.values)
else -> Jsons.writeValueAsBytes(this)
}
}

0 comments on commit c524258

Please sign in to comment.