Skip to content

Instantly share code, notes, and snippets.

@mikeseif
Created January 14, 2019 22:01
Show Gist options
  • Save mikeseif/0a50a6fcb7c7c355a834471da16e9955 to your computer and use it in GitHub Desktop.
Save mikeseif/0a50a6fcb7c7c355a834471da16e9955 to your computer and use it in GitHub Desktop.
Live Template for CDoc<T, R, L>,
@Serializable
data class $NAME$(override val version: String,
override val href: String,
override val attributes: $ATTRTYPE$,
override val items: List<$ITEMTYPE$>,
override val links: $LINKTYPE$): CDoc<$ATTRTYPE$, $ITEMTYPE$, $LINKTYPE$> {
@Serializer(forClass = $NAME$::class)
companion object: KSerializer<$NAME$> {
@ImplicitReflectionSerializer
override fun deserialize(input: Decoder): $NAME$ {
val jsonReader = input as? JSON.JsonInput ?: throw SerializationException("This class can be loaded only by JSON")
val tree = jsonReader.readAsTree() as? JsonObject ?: throw SerializationException("Expected JSON object")
val itemsJo = tree["items"].jsonArray
val items = itemsJo.parseToFailableList<$ITEMTYPE$>()
val version = tree["version"].toString()
val href = tree["href"].toString()
val attributes = JSON.nonstrict.parse($ATTRTYPE$::class.serializer(), tree["attributes"].toString())
val links = JSON.nonstrict.parse($LINKTYPE$::class.serializer(), tree["links"].toString())
return $NAME$(version, href, attributes, items, links)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment