Skip to content

Instantly share code, notes, and snippets.

@navczydev
Created October 13, 2021 00:57
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 navczydev/0a2dc30432141e239b067de66bcf3aab to your computer and use it in GitHub Desktop.
Save navczydev/0a2dc30432141e239b067de66bcf3aab to your computer and use it in GitHub Desktop.
@Serializable
data class DataModel( // properties )
@OptIn(ExperimentalSerializationApi::class)
fun main() {
URL("http://api.plos.org/search?q=title:DNA").openStream().use {
val jsonData = Json.decodeFromStream<DataModel>(it) // read JSON from a URL
println(jsonData)
FileOutputStream(File("dataModel.json")).use { // save to a file
Json.encodeToStream(jsonData, it)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment