Skip to content

Instantly share code, notes, and snippets.

@krishnabhargav
Created October 2, 2019 21:57
Show Gist options
  • Save krishnabhargav/1a61413d6dceaebaf7f43a8af7cde00f to your computer and use it in GitHub Desktop.
Save krishnabhargav/1a61413d6dceaebaf7f43a8af7cde00f to your computer and use it in GitHub Desktop.
import infrastructure.Json
import io.javalin.Javalin
import io.javalin.plugin.json.FromJsonMapper
import io.javalin.plugin.json.JavalinJson
import io.javalin.plugin.json.ToJsonMapper
sealed class Device {
data class Laptop(val model: String) : Device()
data class Phone(val model: String, val carrier: String): Device()
}
class Employee(val device: List<Device>)
fun main() {
val app = Javalin.create().start(7000)
JavalinJson.toJsonMapper = object : ToJsonMapper {
override fun map(obj: Any): String = Json.toJson(obj)
}
JavalinJson.fromJsonMapper = object : FromJsonMapper {
override fun <T> map(json: String, targetClass: Class<T>): T = Json.fromJsonWithClass(json, targetClass)
}
app.get("/") { ctx ->
ctx.result("Hello, World!")
}
app.get("/device") { ctx ->
val devices = listOf(Device.Laptop("Macbook Pro"), Device.Phone("IPhone", "T-Mobile"))
ctx.json(Employee(devices))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment