Created
August 30, 2023 19:07
-
-
Save rgiaviti/0aa78a42a4caa92077c96ac398beeae6 to your computer and use it in GitHub Desktop.
POC CBOR - Kotlin/Jackson
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.fasterxml.jackson.annotation.JsonIgnoreProperties | |
import com.fasterxml.jackson.annotation.JsonProperty | |
@JsonIgnoreProperties(ignoreUnknown = true) | |
data class User( | |
@field:JsonProperty("name") | |
val name: String, | |
@field:JsonProperty("age") | |
val age: Int, | |
@field:JsonProperty("address") | |
val address: Address, | |
@field:JsonProperty("photo") | |
val photo: String | |
) { | |
override fun toString(): String { | |
return "User(name='$name', age=$age, address=$address, photo='$photo')" | |
} | |
} | |
@JsonIgnoreProperties(ignoreUnknown = true) | |
data class Address( | |
@field:JsonProperty("street") | |
val street: String, | |
@field:JsonProperty("city") | |
val city: String, | |
@field:JsonProperty("country") | |
val country: String | |
) { | |
override fun toString(): String { | |
return "Address(street='$street', city='$city', country='$country')" | |
} | |
} | |
import com.fasterxml.jackson.dataformat.cbor.databind.CBORMapper | |
import com.fasterxml.jackson.module.kotlin.registerKotlinModule | |
import java.util.* | |
fun main(args: Array<String>) { | |
val cborMapper = CBORMapper().registerKotlinModule() | |
val user = User( | |
name = "James Hetfield", | |
age = 50, | |
photo = "sss", | |
address = Address( | |
street = "Blackened St", | |
city = "San Francisco", | |
country = "USA" | |
) | |
) | |
val cborUser = cborMapper.writeValueAsBytes(user) | |
println(Base64.getEncoder().encodeToString(cborUser)) | |
val objUser = cborMapper.readValue(cborUser, User::class.java) | |
println(objUser) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment