MainActivity.kt
package com.cfsuman.composenetwork
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.cfsuman.composenetwork.ui.theme.ComposeNetworkTheme
import kotlinx.serialization.Serializable
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
ComposeNetworkTheme {
Scaffold(
topBar = {
TopAppBar(
title = {
Text("Serialization Decode From String")
}
)
},
content = { MainContent()}
)
}
}
}
}
@Composable
fun MainContent() {
val format = Json{
isLenient = true
}
val employeeJsonString = "{ firstName: Jenny, lastName: Jones," +
"dept: Accounting, salary:1200, age:28}"
val employee = format.decodeFromString<Employee>(employeeJsonString)
Column(Modifier.fillMaxSize().padding(24.dp)) {
Text(
text = "${employee.firstName} ${employee.lastName}" +
"\nDepartment: ${employee.dept}" +
"\nSalary: ${employee.salary} USD" +
"\nAge: ${employee.age}",
style = MaterialTheme.typography.h5
)
}
}
@Serializable
data class Employee(
val firstName: String,
val lastName: String,
val dept: String,
val salary: Int,
val age: Int
)

- jetpack compose - How to use TabRow
- jetpack compose - TabRow indicator color
- jetpack compose - Get screen width height in dp
- jetpack compose - Get screen layout direction
- jetpack compose - How to hide status bar
- jetpack compose - How to hide navigation bar
- jetpack compose - Get text from url
- jetpack compose ktor - How to pass parameters
- jetpack compose - Kotlinx serialization ignore unknown keys
- jetpack compose - Kotlinx serialization alternative json names
- jetpack compose - Kotlinx serialization handle null values
- jetpack compose - Kotlinx serialization encode defaults
- jetpack compose - Kotlinx serialization allow special floating point values
- jetpack compose - Kotlinx serialization parse to json element
- jetpack compose - Kotlinx serialization build json element