Hi I have a kotlin data class, initially it had 20...
# android
u
Hi I have a kotlin data class, initially it had 200 parameters in constructor and was working fine, at some point I got it updated as the data is coming from server and I need to add as many new parameters gets added to one JSON object and then I did. But as soon as parameters increased to 300 it started giving error like Bad class exception I am now somewhat confused. So to overcome this i created a plain POJO class in Java and it is working fine. Is there any documentation or solution I apply and use kotlin data class instead of POJO Am I missed something? Currently my server returning more than 500 strings in one JSON object
blob thinking upside down 1
blob nervous 2
😱 9
h
I would recommend reorganize server response. Using so much fields in a object is bad practice
u
Unfortunately I have no control over server Just because of this POJO class I created my whole code base is now not 100% kotlin That is my major concern. I didn't mean I don't like Java but as discussed in the beginning of my project, it will be 100% kotlin
z
The problem with a data class of many properties is that it would have a constructor that has the same # of parameters, and a JVM method can only have 255 parameters (a limitation that’s rarely a problem). So if you want to have a Kotlin class with this many properties, you need to place them in the body, and it can’t be a data class:
Copy code
class Test {
  val a0: Int = 0
  val a1: Int = 0
  .....
}
✔️ 1
Of course I also do agree that this API is far from ideal to work with, but if you go with the above, at least you can make it work 😄 Depending on what you use to parse data into this class, be careful with the default
0
values potentially being used in the result.
h
You could use
lateinit
or delegates to prevent using the default value
c
Alternatively, you might be better off receiving the response as just as
Map<String, String>
and wrapping the response into a domain object using the map as a property delegate https://kotlinlang.org/docs/delegated-properties.html#storing-properties-in-a-map
👍 4
☝️ 1
h
Or better, write a custom serializer, which groups the attributes
u
Thanks @zsmb I converted my data class to kotlin class with attributes in body and it worked. I am using Retrofit for parsing with Gson and everything is good now My retrofit interface returns direct instance of it. No need to do anything to parse or map. I will talk to my server team for this long response but for now I am happy.
Other suggestions also welcome And I do that map to property delegate things in some cases where required to generate response without creating data class For default values, it is not required to handle as i am caching the response coming from server and not calling this api again and again
j
Are you using those 300 properties? if not, you only need to create a data class with those ones
☝️ 5
And config your serializer to ignore unknown properties
g
even if you use all those properties, do you really need class to access them, it looks as some kind map structure as suggested above by Casey. Even without data class it looks like a complete overkill to have a class with 300 properties and what is even worse is maintain it
u
by using map I need to remember the property to fetch and that may lead to error if I made a typo with the help of class I can easily access any property like classInstance.property1
g
Do you access all those 300 properties from your code?
u
Yes, and currently these are 500+ properties
g
So you have code which actually using 500 properties? It’s indeed some serious scalability problem out there 🙂
1
u
I can’t share the exact logic as it is my company code but I can tell you a little bit This gets initialised when the app launched and then used over different places of app by the help of shared viewmodel that holds this class as livedata
s
You should initiate variable. E.g: var a:String?="", var b:Int = 0 If some parameters not come from server, it'll handle your exception.
There are also many solutions to overcome your issue.
Also you should only use required parameter, not all 300/500.
d
This gets initialised when the app launched and then used over different places of app by the help of shared viewmodel that holds this class as livedata
Sounds like a god-class of configurations to me. You should really consider separating it out in a feature-specific manner apart from other things. Not every one of those 300/500 properties should be required right at app start, some can be fetched on a need-to basis surely (?)
u
@Devanshu Kaushik Actually the api returns all properties in one JSON object so I not found this technique useful to collect some properties in one fragment then in other fragment i need different properties so call api again and collect different properties.
g
If you need it on different screens in different situations, I would instead load this json, saved it to database or any other storage and only serialize it to particular data classes with properties which are related on particular screens/use cases with only required fields