How to reasign values dynamically in room? - android

I have a data class, where as a JSON from backend I get string as key for example "ready".
if key = "ready", then it should fullfil the rest of the data like:
key = "ready"
order = 1
isReady = true
isClosed = false
isFinished = false
I hope I could do it dynamically like that:
#TypeConverters(Converters::class)
#Entity(tableName = "statemanager")
data class StateManager (
#PrimaryKey
var key: String = "",
var order: Int = 0,
var isReady: Boolean,
var isClosed: Boolean,
var isFinished: Boolean,
) {
init {
order = getOrderFromKey(key)
isReady= key == READY
isClosed = key == CLOSED
isFinished= key == FINISHED
}
companion object {
fun getOrderFromKey(key: String): Int {
return when (key) {
READY -> 1
CLOSED -> 2
FINISHED -> 3
else -> throw IllegalArgumentException()
}
}
}
}
I needed to write converter so it is:
class Converters {
private val gson = GsonBuilder().serializeNulls().create()
#TypeConverter
fun convertManagerToString(type: StateManager?): String =
gson.toJson(type)
#TypeConverter
fun convertFromJSONToManager(key: String): StateManager? =
gson.fromJson(key, StateManager::class.java)
}
I've got error. This data is part of another data class where I use it like
#ColumnInfo(name = "state")
var state: StateManager
error:
Expected BEGIN_OBJECT but was STRING at line 1 column 361
EDIT:
I have fixed it, but IMO it not nice way. I use #JSONAdapter adnotation and there I have changed my string to json.asString and everything works, but do you know nicer way to do it? How to do it without "#JSONAdapter"?

Related

Any data type not working in data class kotlin

I just started to learn kotlin.
This is my data class.
data class UserModel(
#SerializedName("Id")
val id: Int = 0,
#SerializedName("myKey")
val myKey: Boolean? = false
//var myKey: Any?
While I use simple as val myKey: Boolean? = false then my app is working and able to run . But when i use var myKey: Any? and run the app it give me compilation error. I have checked by commenting the code for every line. I found the issue occurs because of this var myKey: Any?
So kindly, if any one knows about this. Advanced help would be appreciated !
try from
#SerializedName("myKey")
val myKey: Boolean? = false
into
#SerializedName("myKey")
val myKey: any? = false
example:
data class UserModel(
#SerializedName("Id")
val id: Int = 0,
#SerializedName("myKey")
val myKey: any? = false
)
make sure to do some type check and cast when accessing the data class such as
val userModel = UserModel()
when(userModel.myKey){
is Boolean -> {
// do stuff when myKey type of Boolean
if(userModel.myKey) println("key is false")
else println("key is true")
}
is String -> {
// do stuff when myKey type of String
println(userModel.myKey)
}
}

Is it possible to same key with differ class and data type in data class kotlin android?

I have one issue about code data class kotlin android.
How to implement server response? sometimes I get String value or sometime get Object class.
class CMSRespTemp {
data class CMSRespApi(
val status: Boolean = false,
val message: String = "",
val data: String as Data
)
data class Data(
val cms_id: String = "",
val cms_content: String = ""
)
}
When I implement only Data class it works, like this val data: Data or val data: String. But I need together Data and String with key only data.
Is it possible?
When having multiple type for same variable, we can use Any type which is equivalent to Object type in java. So solution is like below :
class CMSRespTemp {
data class CMSRespApi(
val status: Boolean = false,
val message: String = "",
var data: Any? = null // changed it to var from val, so that we can change it's type runtime if required
)
data class Data(
val cms_id: String = "",
val cms_content: String = ""
)
}
And when accessing that variable, one can simply cast like below :
val apiResponse : CMSRespApi //= some API response here from network call
when (apiResponse.data) {
is String -> {
// apiResponse.data will be smart-casted to String here
}
else -> {
val responseData = Gson().fromJson<CMSRespApi.Data>(
Gson().toJsonTree(apiResponse.data),
CMSRespApi.Data::class.java
)
}
}
After 12 Hrs spend and got the solution my self,
val getResultCon = getSerCont.result // response Any
val gson = Gson()
val jsonElement = gson.toJsonTree(getResultCon)
val resultData = gson.fromJson(jsonElement, SearchContactApi.Result::class.java)
Convert your data string to toJsonTree and fromJson with model class then got result.

How to copy a property between 2 lists of different types using declarative Kotlin?

Context
Using a declarative approach in Kotlin, need to copy a single name property from List of User objects to a List of UserDetail objects based on matching id properties as shown below:
val users = Arrays.asList(
User(1, "a"),
User(2, "b")
)
val details = Arrays.asList(
UserDetail(1),
UserDetail(2)
)
val detailsWithName = copyNameToUser(users, details)
Models are:
class User {
var id = -1;
var name = "" // given for all Users
constructor(id: Int, name: String)
// ...
}
class UserDetail {
var id = -1;
var name = "" // blank for all UserDetails
constructor(id: Int)
// ...
}
Problem
Tried to use a declarative approach via forEach iterable function:
fun copyNameToDetails(users: List<User>, details: List<UserDetail>): List<UserDetail> {
details.forEach(d ->
users.forEach(u ->
if (d.id == u.id) {
d.name = u.name
}
)
)
return details
}
This can be achieved in Java as shown below:
private static List<UserDetail> copyNameToDetails(List<User> users, List<UserDetail> details) {
for (UserDetail d: details) {
for (User u : users) {
if (d.id == u.id) {
d.name = u.name;
}
}
}
return details;
}
Question
How can this be done in Kotlin using a declarative approach?
You make too many iterations over both lists (users.size * details.size) so creating a hashmap can fix it a bit:
fun copyNameToUsers(users: List<User>, details: List<UserDetail>): List<UserDetail> {
val usersById = users.associate { it.id to it }
details.forEach { d ->
usersById[d.id]?.let { d.name = it.name }
}
return details
}
An other approach with non mutable values :
data class User(val id: Int = -1, val name: String = "")
data class UserDetail(val id: Int = -1, val name: String = "")
private fun List<UserDetail>.copyNameToUser(users: List<User>): List<UserDetail> = map { userDetail ->
users.firstOrNull { userDetail.id == it.id }?.let { userDetail.copy(name = it.name) } ?: userDetail
}

Serialized Data Class combined with built-in modifications

I am working on updating the parsing of an API response that uses a Serialized Data Class to parse the JSON response. The serialization works perfectly fine right now, but the new data that I'm attempting to parse into data class is not fully reliant on data in the json. Here is what I mean by that:
The data class is Career, and the new data I need to parse is a set of skills and each have a rating. The json data is very simple and contains the skills as such:
{
// other career data
...
"mathematics_skill": 8,
"critical_thinking_skill": 6
... // the remaining skills
}
Using straight serialization, I would only be able to store the data as such:
data class Career(
// Other career data
#serializableName("mathematic_skill") val mathSkill: Int,
#serializableName("critical_thinking_skill") val mathSkill: Int,
// remaining skills
)
However, I would like to store all skills in an array variable of a custom skills class that not only contains the rating, but also the name of the skill and a color. Basically, when I access the skills data of a career, I would like to access it like such:
val careerMathSkill = career.skills[0]
val mathRating = careerMathSkill.rating
val mathColor = careerMathSkill.color
Is it possible to use the serialized data from the data class to add non-serialized data to the same data class? (Sorry for the weird wording, not sure how else to explain it)
EDIT: Here is what I have:
class CareersRemote(
#SerializedName("careers") val careers: List<Career>
) {
companion object {
fun parseResponse(response: Response<CareersRemote>): CareersResponse {
return if (response.isSuccessful) {
response.body()!!.format()
} else
CareersResponse(listOf(CareersResponse.ErrorType.Generic()))
}
}
fun format(): CareersResponse {
val careers = topCareers.map {
Career(
id = it.id,
title = it.title,
)
}.toMutableList()
return CareersResponse(CareersResponse.SuccessData(careers = careers))
}
data class Career(
#SerializedName("id") val id: String,
#SerializedName("title") val title: String,
)
}
Here is what I am hoping to do in a way
class CareersRemote(
#SerializedName("careers") val careers: List<Career>
) {
companion object {
fun parseResponse(response: Response<CareersRemote>): CareersResponse {
return if (response.isSuccessful) {
response.body()!!.format()
} else
CareersResponse(listOf(CareersResponse.ErrorType.Generic()))
}
}
fun format(): CareersResponse {
val careers = topCareers.map {
Career(
id = it.id,
title = it.title,
)
}.toMutableList()
return CareersResponse(CareersResponse.SuccessData(careers = careers))
}
data class Career(
#SerializedName("id") val id: String,
#SerializedName("title") val title: String,
// skills array that will need to be filled out based on the data I got in the json
var skills: List<Skill>
)
}
EDIT: The suggested solution
class CareersRemote(
#SerializedName("careers") val careers: List<Career>
) {
companion object {
fun parseResponse(response: Response<CareersRemote>): CareersResponse {
return if (response.isSuccessful) {
response.body()!!.format()
} else
CareersResponse(listOf(CareersResponse.ErrorType.Generic()))
}
}
fun format(): CareersResponse {
val careers = topCareers.map {
Career(
id = it.id,
title = it.title,
)
}.toMutableList()
return CareersResponse(CareersResponse.SuccessData(careers = careers))
}
data class Career(
#SerializedName("id") val id: String,
#SerializedName("title") val title: String,
#SerializedName("math_skill") val mathSkill: Int
#SerializedName("other_skill") val mathSkill: Int
) {
var skills: List<Skill> = {
val mathSkill = Skill(name: "Math", rating: mathSkill, color: /**some color*/)
val otherSkill = Skill(name: "Other", rating: otherSkill, color: /**some color*/)
return listOf(mathSkill, otherSkill)
}
}
}
Yes, you can create a custom JsonDeserializer to modify how the JSON is parsed.
Here is a basic example of what that would look like.
class CareerDeserializer : JsonDeserializer<Career> {
override fun deserialize(json: JsonElement, typeOfT: Type, context: JsonDeserializationContext): Career {
val obj = json.asJsonObject
// standard career data
val id = obj.get("id")?.asString
val name = obj.get("name").asString
// making a Skill object
val skill = Skill(
obj.get("mathematic_skill").asInt,
obj.get("critical_thinking_skill").asInt,
obj.get("swimming_skill").asInt
// etc
)
return Career(id, name, skill)
}
}
And make sure to register that within your GsonBuilder.
val gson = GsonBuilder()
.registerTypeAdapter(Career::class.java, CareerDeserializer())
.create()
Note, you'll also have to create a JsonSerializer if you want to go the other way too.
Edit:
However, if you're just looking to change the syntax of how you're accessing that data, you can do something like this.
data class Career(
// Other career data
val mathSkill: Int,
val thinkSkill: Int
// remaining skills
) {
val skills: List<Int>
get() = listOf(mathSkill, thinkSkill)
}
This would give you a skills list back whenever you needed it, and it would be created when you accessed it, so you won't have to worry about the data being out of sync. This would allow you to access your data as such.
career.skills[0] // get the math skill.
And you can take this another step further by adding a get operator to your Career class.
data class Career(
// Other career data
val mathSkill: Int,
val thinkSkill: Int
// remaining skills
) {
...
operator fun get(pos: Int) = skills[pos]
}
Now, you can simply do
career[0] // get the math skill.
Warning, this is dangerous because you're accessing an Array so you could get OutOfBoundsExceptions. Use constants to help you out.
Edit 2:
val skills = {
listOf(Skill("Math", mathSkill, /** some color */ ),
Skill("Other", otherSkill, /** some color */ ))
}

default value in kotlin data class not work when server send null for that value in moshi

I use Moshi for parse json from server. if server send null for item default value not set! but server not send that item default value set.
json:
{"percentChange": null,"change": "-2500.00","value": "130000","name": null}
data class:
#JsonClass(generateAdapter = true) data class Reference(val name:String? = "-",val value: Double,val change: Double,val percentChange: Double? = -10.0,)
but data for name and percentChange is null that should "-" for name and "-10.0" for percentChange. if server not send name and percentChange, default value work, but if send that null default value not work!
I use converter-moshi:2.4.0 and retrofit:2.4.0
This is working as intended because the null literal as a value for a key in JSON is semantically different than the absence of the key and value.
You can make a custom JsonAdapter for your use case.
#JsonClass(generateAdapter = true)
data class Reference(
#Name val name: String = "-",
val value: Double,
val change: Double,
val percentChange: Double? = -10.0
) {
#Retention(RUNTIME)
#JsonQualifier
annotation class Name
companion object {
#Name #FromJson fun fromJson(reader: JsonReader, delegate: JsonAdapter<String>): String {
if (reader.peek() == JsonReader.Token.NULL) {
reader.nextNull<Unit>()
return "-"
}
return delegate.fromJson(reader)!!
}
#ToJson fun toJson(#Name name: String): String {
return name
}
}
}
#Test fun reference() {
val moshi = Moshi.Builder()
.add(Reference)
.build()
val adapter = moshi.adapter(Reference::class.java)
val decoded = Reference("-", 130_000.toDouble(), (-2_500).toDouble(), null)
assertThat(adapter.fromJson(
"""{"percentChange": null,"change": "-2500.00","value": "130000"}"""))
.isEqualTo(decoded)
assertThat(adapter.fromJson(
"""{"percentChange": null,"change": "-2500.00","value": "130000","name": null}"""))
.isEqualTo(decoded)
}

Categories

Resources