Skip to content

Instantly share code, notes, and snippets.

@jitinsharma
Created January 25, 2018 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jitinsharma/8805cf63ba3b371b657531d55d3fd6c5 to your computer and use it in GitHub Desktop.
Save jitinsharma/8805cf63ba3b371b657531d55d3fd6c5 to your computer and use it in GitHub Desktop.
package io.github.jitinsharma
import kotlinx.serialization.*
import kotlinx.serialization.json.JSON
@Serializable
data class Destination(
var name : String? = "",
var country : String? = "",
var code : Int = 0,
@Optional
var isMetro : Boolean = false,
@Transient
var favorite : Boolean = false
)
@Serializable
data class Country(var name : String = "",
@SerialName("hemisphereCode")
var hCode : Int = -1) {
@Serializer(forClass = Country::class)
companion object : KSerializer<Country>{
override fun load(input: KInput): Country {
TODO("Write your own scheme to handle input")
}
override fun save(output: KOutput, obj: Country) {
TODO("Write your own scheme to handle output")
}
override val serialClassDesc: KSerialClassDesc
get() = TODO()
}
}
class DestinationClass {
companion object {
fun findDestination() {
val delhi = Destination(name = "Delhi", country = "India", code = 0)
println(delhi)
//Destination(name=New York, country=India, code=0, favorite=false)
val delhiAsString = JSON.stringify(delhi)
println(delhiAsString)
//{"name":"Delhi","country":"India","code":0}
val paris = JSON.unquoted.parse<Destination>(
"{name:Paris,country:France,code:10}")
println(paris)
//Destination(name=Paris, country=France, code=10, favorite=false)
var barcelona = JSON.unquoted.parse<Destination>(
"{name:Barcelona,country:Spain,code:5}")
println(barcelona)
// Only optional and transient missing, so serialization works
//Destination(name=Paris, country=France, code=10, isMetro=false, favorite=false)
barcelona = JSON.unquoted.parse(
"{name:Barcelona,country:Spain,code:5,isMetro:true}")
println(barcelona)
//Destination(name=Barcelona, country=Spain, code=5, isMetro=true, favorite=false)
//Optional property isMetro is updated but Transient property favorite remains same
/*barcelona = JSON.unquoted.parse(
"{name:Barcelona,code:5,isMetro=true,favorite=true}")
//This will break as "country" is a required field.*/
val newYork = JSON.parse<Destination>(
"{\"name\":\"New York\",\"country\":\"USA\",\"code\":3}"
)
println(newYork)
//Destination(name=New York, country=USA, code=3, favorite=false)
val newYorkAsMap : Map<String,Any> = Mapper.map(newYork)
val newnewYork = Mapper.unmap<Destination>(newYorkAsMap)
println(newYorkAsMap)
//{name=New York, country=USA, code=3}
val newDelhi = CustomTransformer.transform(delhi)
println(newDelhi)
}
object CustomTransformer : ValueTransformer() {
override fun transformStringValue(desc: KSerialClassDesc, index: Int, value: String): String =
value.toLowerCase()
override fun transformIntValue(desc: KSerialClassDesc, index: Int, value: Int): Int =
when(value) {
0 -> 1
else -> super.transformIntValue(desc, index, value)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment