Kotlin Gson Deserializing - android

I am receiving a JSON data model that has a map wrapper Table. I'm trying to use generics to pass in the type that is beyond the wrapper but it's not translating well at runtime. Here's an example of my JSON file:
{
"Table": [
{
"paymentmethod_id": 1,
"paymentmethod_description": "Cash",
"paymentmethod_code": "Cash",
"paymentmethod_is_ach_onfile": false,
"paymentmethod_is_element": false,
"paymentmethod_is_reward": false,
"paymentmethod_is_openedgeswipe": false,
"paymentmethod_update_user_id": 1,
"paymentmethod_insert_user_id": 1,
"paymentmethod_insertdate": "2014-10-07 14:53:16",
"paymentmethod_deleted": false,
"paymentmethod_is_mobile_visible": true
}
]
}
The wrapper class I'm using is called Table.
data class Table<T>(
#SerializedName("Table") val models : Array<T>
)
The actual model class is PaymentMethod.
data class PaymentMethod(
#SerializedName("paymentmethod_id") val idNumber : Int = -1
)
I have created a generic data manager class that takes < T > type. I think use subclasses of the data manager to localize the input and results (such as declaring the model class PaymentMethod.
open class NXDataManager<T>(manager: NXNetworkManager? = null, rpc : String?, parameters: List<Pair<String, String>>? = null, method : String = "get")
{
...
open fun sendRequest(completionHandler: (models:Array<T>) -> Unit, errorHandler: (error:FuelError) -> Unit) {
val request = NXNetworkRequest(rpc, parameters, method)
request.send(manager, completionHandler = { s: String ->
val table: Table<T> = Gson().fromJson(s)
completionHandler(table.models)
}, errorHandler = errorHandler)
}
inline fun <reified T> Gson.fromJson(json: String) = this.fromJson<T>(json, object: TypeToken<T>() {}.type)
}
My subclassed data manager specifies the model to parse into.
final public class PaymentMethodsDataManager : NXDataManager<PaymentMethod>
{
constructor () : super("genGetPaymentMethods")
}
When I run the code as:
val table: Table<T> = Gson().fromJson(s)
I get an error message java.lang.ClassCastException: java.lang.Object[] cannot be cast to Networking.PaymentMethod[]. However, when I pass in an explicit type it works as expected--parsing the array into PaymentMethod models:
val table: Table<PaymentMethod> = Gson().fromJson(s)
Any ideas of how I can still use the generic type T?

Data Class :
data class Table<T>(
#SerializedName("Table") val models : Array<T>
)
to JSON:
val gson = Gson()
val json = gson.toJson(table)
from JSON:
val json = getJson()
val table = gson.fromJson(json, Table::class.java)

Method fromJson is generic, so when you call it for Table<T> variable it creates Array<Any> as most suitable. You need to notice that PaymentMethod class extends T generic, but I don't know is it even possible. If you find out how to make it, use something like following:
val table: Table<T> = Gson().fromJson<Table<PaymentMethod>>(s)
In your case I'm using gson adapters. Following function creates object with specified type parameter:
fun getObjectFromString(type: Type, string: String) =
Gson().getAdapter(TypeToken.get(type)).fromJson(string)
To use it write something following:
val table: Table<T> = getObjectFromString(Table<PaymentMethod>::class.java, s) as Table<PaymentMethod>
Update
To avoid spare class cast you can use reified generic function:
inline fun <reified T> getObjectFromString(string: String): T =
getGsonConverter().getAdapter(TypeToken.get(T::class.java)).fromJson(string)!!
In that case using would be easier:
val table: Table<T> = getObjectFromString<Table<PaymentMethod>>(s)
I used first solution in cases where I don't know what type the object would be - I've got only Type variable with information about that object.

java.lang.ClassCastException: java.lang.Object[] cannot be cast to
Networking.PaymentMethod[]
Your JSON is
{
"Table": [
{
"paymentmethod_id": 1,
"paymentmethod_description": "Cash",
"paymentmethod_code": "Cash",
"paymentmethod_is_ach_onfile": false,
"paymentmethod_is_element": false,
"paymentmethod_is_reward": false,
"paymentmethod_is_openedgeswipe": false,
"paymentmethod_update_user_id": 1,
"paymentmethod_insert_user_id": 1,
"paymentmethod_insertdate": "2014-10-07 14:53:16",
"paymentmethod_deleted": false,
"paymentmethod_is_mobile_visible": true
}
]
}
Create a data class, PaymentMethod.
We frequently create classes whose main purpose is to hold data. In
such a class some standard functionality and utility functions are
often mechanically derivable from the data.
data class PaymentMethod(#SerializedName("Table") val table:ArrayList<PaymentData> )
data class PaymentData
(
#SerializedName("paymentmethod_id") val paymentmethod_id: Int,
#SerializedName("paymentmethod_description") val paymentmethod_description: String,
#SerializedName("paymentmethod_code") val paymentmethod_code:String,
#SerializedName("paymentmethod_is_ach_onfile") val paidStatus:Boolean,
#SerializedName("paymentmethod_is_element") val paymentmethod_is_element:Boolean,
#SerializedName("paymentmethod_is_reward") val paymentmethod_is_reward:Boolean,
#SerializedName("paymentmethod_is_openedgeswipe") val paymentmethod_is_openedgeswipe:Boolean,
#SerializedName("paymentmethod_update_user_id") val paymentmethod_update_user_id:Int,
#SerializedName("paymentmethod_insert_user_id") val paymentmethod_insert_user_id:Int,
#SerializedName("paymentmethod_insertdate") val paymentmethod_insertdate:String,
#SerializedName("paymentmethod_deleted") val paymentmethod_deleted:Boolean),
#SerializedName("paymentmethod_is_mobile_visible") val paymentmethod_is_mobile_visible:Boolean
)
You can call this way
val paymentDATA = Gson().fromJson<PaymentMethod>("JSON_RESPONSE", PaymentMethod::class.java)
val _adapterPaymentHistory = paymentDATA.table

Related

Kotlin - Clarification Needed Regarding the Data Classes Copy Function

Hello everyone I am new to Jetpack Compose.
Needed clarification reagarding the usage of Copy Function in kotlin Data Classes.
Here i am using a NetworkFetchState abstract class, which helps me determine the state of the network call.
// Abstract Class
abstract class NetworkFetchState(
val isLoading: Boolean = false,
val isSuccess: Boolean = false,
val isError: Boolean = false,
val error: Throwable? = null,
val errorMessage: String? = null
)
I am creating the data class that is extending this abstract class
data class LoginDataState(
val responseData: LoginResponse? = null
) : NetworkFetchState() // extending the Abstract Class
Now inside the ViewModel Class i am creating a mutable state flow
class MyViewModel:ViewModel(){
// Mutable State Flow of the Data State
private val _loginDataState = MutableStateFlow(LoginDataState())
// readonly value of the __loginDataState
val loginDataState: StateFlow<LoginDataState> get() = _loginDataState
/*
* Here I am performing network calls inside the view model scope
* based on the result from the network call i am trying to update the MutableStateFlow
*/
fun makeNetworkCall(){
// ....
_loginDataState.update { prevState ->
prevState.copy(
// ---- PROBLEM HERE ----
// isLoading, isSuccess.. etc (all other variables from abstract class)
// are not available
)
}
}
}
all the member variables that are extending from abstract class are not visible.
What am i doing wrong?
The .copy function is a function generated by kotlin compiler for all data classes. As per the documentation, it's using only properties declared in the primary constructor.
If you want to change those properties with copy function, you will have to add them to the primary constructor somehow.
// this would work
data class LoginDataState(
val responseData: LoginResponse? = null,
val _isLoading: Boolean = false,
) : NetworkFetchState(isLoading = _isLoading)
// this is probably better
interface NetworkFetchState {
val isLoading: Boolean get() = false
}
data class LoginDataState(
val responseData: LoginResponse? = null,
override val isLoading: Boolean = false,
) : NetworkFetchState

Serialize class with abstract parameter to JSON

I have 3 classes:
GameResultList which is basically ArrayList with some helper methods in it
GameResult with an abstract value gameMode
GameMode
public class GameResultList extends ArrayList<GameResult> {
...
}
class GameResult(
val gameMode: GameMode,
val score: Int,
timeSpentInSeconds: Int,
val completionDateTime: Date
) {
...
}
GameMode class:
abstract class GameMode(
val suggestionsActivated: Boolean,
val screenOrientation: ScreenOrientation // enum: PORTRAIT, HORIZONTAL
) {
...
}
I need to serialize GameResultList into JSON.
Since the parameter gameMode is abstract, Gson throws an exception. After some research, I decided to give Moshi a try. I have added PolymorphicJsonAdapterFactory and KotlinJsonAdapterFactory, but the result is always empty ({}).
How I set up Moshi:
private val moshi =
Moshi.Builder().add(PolymorphicJsonAdapterFactory.of(GameMode::class.java, "GameMode")
.withSubtype(GameOnTime::class.java, "GameOnTime")
.withSubtype(GameOnCount::class.java, "GameOnCount"))
.add(KotlinJsonAdapterFactory())
.build()
private val jsonAdapter: JsonAdapter<GameResultList> = moshi.adapter(GameResultList::class.java)
This returns empty JSON response:
jsonAdapter.toJson(gameResultList)
So how can I serialize the GameResultList? Is there an easy way? Also, it's not necessary to use Moshi, it can be anything else for the sake of easiness.
After some investigation, I found out that the main problem is that array lists require explicit converters.
class GameResultListToJsonAdapter {
#ToJson
fun arrayListToJson(list: GameResultList): List<GameResult> = list
#FromJson
fun arrayListFromJson(list: List<GameResult>): GameResultList = GameResultList(list)
}
Also, there is a problem with handling the Date type, I have replaced it with Long to not make another explicit converter.

More efficient way to model JSON data using retrofit - Kotlin

The data that I want to use has this structure:
{
"1": {
"id": 1,
"name": "Bulbasaur"
},
"2": {
"id": 2,
"name": "Ivysaur"
},
"3": {
"id": 3,
"name": "Venusaur"
}
}
Note:
The number labeling each object matches the id of the Pokémon, not the number of Pokémon
My problem is that when I try to create data classes for this it ends up creating a data class for each object. Not one data class that fits each object. I believe this is due to the number labeling the object(Pokémon) being different for each object.
Is there a way I can format this data in maybe one or two data classes and not over 800?
Ideally I would like the data to be structured like this but it does not work when run.
data class ReleasedPokemonModel(
val id: Int,
val name: String
)
When parsing Json to Object with this special case, you should custom Json Deserializer yourself.
Here I use Gson library to parse Json to Object.
First, create a custom Json Deserializer with Gson. As follows:
PokemonResponse.kt
data class PokemonResponse(
val pokemonMap: List<StringReleasedPokemonModel>
)
data class ReleasedPokemonModel(
val id: Int,
val name: String
)
GsonHelper.kt
object GsonHelper {
fun create(): Gson = GsonBuilder().apply {
registerTypeAdapter(PokemonResponse::class.java, PokemonType())
setLenient()
}.create()
private class PokemonType : JsonDeserializer<PokemonResponse> {
override fun deserialize(
json: JsonElement?,
typeOfT: Type?,
context: JsonDeserializationContext?
): PokemonResponse {
val list = mutableListOf<ReleasedPokemonModel>()
// Get your all key
val keys = json?.asJsonObject?.keySet()
keys?.forEach { key ->
// Get your item with key
val item = Gson().fromJson<ReleasedPokemonModel>(
json.asJsonObject[key],
object : TypeToken<ReleasedPokemonModel>() {}.type
)
list.add(item)
}
return PokemonResponse(list)
}
}
}
Next I will create a GsonConverterFactory so that I can addConvertFactory to Retrofit.
val gsonConverterFactory = GsonConverterFactory.create(GsonHelper.create())
And now I will add retrofit.
val retrofit = Retrofit.Builder()
// Custom your Retrofit
.addConverterFactory(gsonConverterFactory) // Add GsonConverterFactoty
.build()
Finally in ApiService, your response will now return type PokemonResponse.
interface ApiService {
#GET("your_link")
suspend fun getGenres(): PokemonResponse
}
The problem is that there's no JSON array there. it's literally one JSON object with each Pokemon listed as a property. I would recommend that you reformat the JSON beforehand to look like this:
[
{
"id": 1,
"name": "Bulbasaur"
},
{
"id": 2,
"name": "Ivysaur"
},
{
"id": 3,
"name": "Venusaur"
}
]
And then you could model it like this:
data class ReleasedPokemonModel(
val id: Int,
val name: String
)
data class Response(
val items: List<ReleasedPokemonModel>
)
See more here.
And see here for discussion about reformatting the data before handing it to Retrofit.
You can use Map to store the key like the following
data class PokemonResponse(
val pokemonMap:Map<String,ReleasedPokemonModel>
)
data class ReleasedPokemonModel(
val id: Int,
val name: String
)

How to ignore fields when using #Parcelize annotation in Kotlin

I want to ignore a field when using the #Parcelize annotation in Kotlin so that the field is not parceled, since this field does not implement the Parcelable interface.
Starting with this, we get an error because PagedList is not parcelable:
#Parcelize
data class LeaderboardState(
val progressShown: Boolean = true,
val pagedList: PagedList<QUser>? = null
) : Parcelable
Gives:
Type is not directly supported by 'Parcelize'. Annotate the parameter type with '#RawValue' if you want it to be serialized using 'writeValue()'
Marking as #Transient gives the same error as above:
#Parcelize
data class LeaderboardState(
val progressShown: Boolean = true,
//Same error
#Transient
val pagedList: PagedList<QUser>? = null
) : Parcelable
There is an undocumented annotation I found called #IgnoredOnParcel which gives the same error, and a lint error on the annotation:
#Parcelize
data class LeaderboardState(
val progressShown: Boolean = true,
//Same error plus lint error on annotation
#IgnoredOnParcel
val pagedList: PagedList<QUser>? = null
) : Parcelable
The lint error in that case is:
#IgnoredOnParcel' is inapplicable to properties declared in the primary constructor
Is there really no way to do this with #Parcelize?
Use a regular class and move the property out of the primary constructor:
#Parcelize
class LeaderboardState(
val progressShown: Boolean = true,
pagedList: PagedList<QUser>? = null
) : Parcelable {
#IgnoredOnParcel
val pagedList: PagedList<QUser>? = pagedList
}
This is apparently the only solution. Make sure to override equals, hashCode, toString, copy, etc as you need them because they won't be defined for a regular class.
EDIT: Here's another solution so you don't lose the features of the data class and you don't lose the automatic parcelization. I'm using a general example here.
data class Person(
val info: PersonInfo
val items: PagedList<Item>? = null)
#Parcelize
data class PersonInfo(
val firstName: String,
val lastName: String,
val age: Int
) : Parcelable
You save only Person.info and recreate it from that.

Android App crashes as Json element is empty String ("") and not Object

I am working on an android project and using RxAndroid, Retrofit to make API call and retrieve json. The json looks something like following :
{
"result": [
{
"parent": "jhasj",
"u_deviation": "skasks",
"caused_by": "ksks",
"u_owner_mi": {
"link": "https://gddhdd.service-now.com/api/now/v1/table/sys_user/ghtytu",
"value": "ghtytu"
},
"impact": "",
}
]
}
I am using gson to parse the Json. The problem is "u_owner_mi" sometimes reruns empty string "" when there is no value assigned to it. I don't have access to change the return type to null. This is making my app crash as I am expecting an object here.
I get the following error :
Expected BEGIN_OBJECT but was STRING
If you can't modify the server, try replacing the offending line in the server response before passing it to the Gson parser. Something like:
String safeResponse = serverResponse.replace("\"u_owner_mi\": \"\"", "\"u_owner_mi\": null");
Your app (client) code is expecting an object according to a contract specified in the class that you pass to GSON. Your app behaves as it should and crashes loudly. You should consider having your server return "u_owner_mi" : null instead of an empty string, assuming you have control over that. The u_owner_mi field on the client side would have to be a nullable type.
If you don't have the ability to fix the api, you could also write a custom deserializer.
Suppose your result class and sub-object are:
data class Result(
val parent: String,
val owner: Any?
)
data class Owner(
val link: String,
val value: String
)
The deserializer could be:
class ResultDeserializer : JsonDeserializer<Result> {
override fun deserialize(json: JsonElement, typeOfT: Type?, context: JsonDeserializationContext?): Result {
val jsonObject = json.asJsonObject
val ownerProperty = jsonObject.get("owner")
return Result(
parent = jsonObject.get("parent").asString,
owner = if (ownerProperty.isJsonObject) context?.deserialize<Owner>(ownerProperty.asJsonObject, Owner::class.java)
else ownerProperty.asString
)
}
}
Finally, to add the deserializer:
#Test
fun deserialization() {
val gson = GsonBuilder().registerTypeAdapter(Result::class.java, ResultDeserializer()).create()
val result1 = gson.fromJson<Result>(jsonWithObject, Result::class.java)
val result2 = gson.fromJson<Result>(jsonWithEmpty, Result::class.java)
}

Categories

Resources