Moshi adapter for annotated model - android

I'm currently using a Moshi adapter to convert some json raw to a given type. It works fine until I used an annotated model.
I'm guessing I should add another params to my adapter beside Player::class.java but I don't know what.
Here is an exemple:
data class Player(
val username: String,
#Json(name = "lucky number")
val luckyNumber: Int
)
private val playerStubRaw = "{\n" +
" \"username\": \"jesse\",\n" +
" \"lucky number\": 32\n" +
"}"
#Test
fun doSomething() {
val moshi = Moshi.Builder().build()
val player = moshi.adapter(Player::class.java).fromJson(playerStubRaw)
// player.luckyNumber == 0
}
luckyNumber value is 0 and not 32.
Any idea what I should do to make it work?
Thanks in advance,

To work with Kotlin, Moshi requires either the reflective KotlinJsonAdapterFactory (from the moshi-kotlin artifact) or code-gen adapters (from the moshi-kotlin-codegen artifact).
https://github.com/square/moshi#kotlinIn a future release of Moshi, a proper error will be thrown to state this requirement.

With the moshi-kotlin-codegen artifact you also need to add #JsonClass(generateAdapter = true) on the class for the decoding to work correctly and not set the property to a default of 0
So after adding the kotlin-kapt plugin and the dependency kapt "com.squareup.moshi:moshi-kotlin-codegen:1.8.0" to the app build gradle annotate the class as below:
#JsonClass(generateAdapter = true)
data class Player(
val username: String,
#Json(name = "lucky number")
val luckyNumber: Int
)

Related

Moshi-JsonDataException: Required value '%s' missing at $

I was working with Moshi to pull JSON data from an API and map it to my DTO Data classes when I encountered this error:
FATAL EXCEPTION: main Process: com.plcoding.weatherapp, PID: 9706
com.squareup.moshi.JsonDataException: Required value 'weatherData'
missing at $ at
com.squareup.moshi.internal.Util.missingProperty(Util.java:649) at
com.squareup.moshi.kotlin.reflect.KotlinJsonAdapter.fromJson(KotlinJsonAdapter.kt:103)
at
com.squareup.moshi.internal.NullSafeJsonAdapter.fromJson(NullSafeJsonAdapter.java:41)
My DTO are annotated with #field:Json(name = "xyz")
#JsonClass(generateAdapter = true)
data class WeatherDataDTO(
#field:Json(name = "time")
val times: List<String>,
#field:Json(name = "temperature_2m")
val temperatures: List<Double>)
I have enclosed the above DTO in another DTO.
JsonClass(generateAdapter = true)
data class WeatherDTO(
#field:Json(name = "hourly")
val weatherData: WeatherDataDTO
)
I am using the latest Retrofit and Moshi Libs on Kotlin 1.6.10
// Retrofit, Core Moshi JSON Library and Moshi's Kotlin support and converter factory
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation "com.squareup.moshi:moshi:1.12.0"
implementation "com.squareup.moshi:moshi-kotlin:1.12.0"
implementation 'com.squareup.retrofit2:converter-moshi:2.9.0'
My Json endpoint looks this:
{
"latitude": 42,
"longitude": 36,
"generationtime_ms": 0.3489255905151367,
"hourly": {
"time": [],
"temperature_2m": []
.....
}
Have struggled with this error for days but cannot find a solution.
Removing #field:Json() in favor of just #Json(name = "xyz") did the trick for me.
My DTO now looks like this and is working fine!
#JsonClass(generateAdapter = true)
data class WeatherDataDTO(
#Json(name = "time")
val times: List<String>,
#Json(name = "temperature_2m")
val temperatures: List<Double>)
For more info on #field:Json(name = "xyz") vs #Json(name = "xyz") you can review this StackOverflow question.
I also found this article to be helpful in case the Moshi-JSON Exception persists.
Happy Coding ...
In my case I was migrating from gson to moshi and first I got the exception:
java.lang.IllegalArgumentException: Cannot serialize Kotlin type com.xxx.Spot. Reflective serialization of Kotlin classes without using kotlin-reflect has undefined and unexpected behavior. Please use KotlinJsonAdapter from the moshi-kotlin artifact or use code gen from the moshi-kotlin-codegen artifact.
Then I resolved it with adding #JsonClass(generateAdapter = true) on data class like your WeatherDataDTO that you did and right after that came the exception you mentioned in this post.
I have done what you posted in your answer, added the #Json(name) but because my data class fields that was getting the errors were null, I was getting that same error.
In my data class I had somewhat like this:
#JsonClass(generateAdapter = true)
data class WeatherDataDTO(
#Json(name = "time")
val times: List<OtherDataClass>
)
And the OtherDataClass:
#JsonClass(generateAdapter = true)
data class OtherDataClass(
#Json(name = "time")
val problematicString: String, //This was causing the problem if it was null
//and the String was non-nullable(without a "?" questionmark beside of String)
)
What I did in OtherDataClass was making problematicString nullable:
#JsonClass(generateAdapter = true)
data class OtherDataClass(
#Json(name = "problemString")
val problematicString: String? = null, // Made it nullable and initialized it as a null.
//...other variables...
)
Also I got the information from this stack overflow question
I hope this helps someone with the same exception.

Moshi/retrofit error with one single Int field after enabling R8 minify

Having trouble with parsing one single field from JSON response after enabling minify, with minify disabled all works correctly:
retrofit API call:
#FormUrlEncoded
#POST("/api/test")
fun test(#Field <some String fields>
): Observable<Response<TestListing>>
wrapped in repo
override fun test(<some String fields>): Observable<Response<TestListing>> {
return api.test(<some String fields>)
.subscribeOn(schedulers.io())
}
model:
data class TestListing (
#Json(name = "success") val success:Int,
#Json(name = "user") val user: TestUser?
)
TestUser class
data class TestUser(
#Json(name = "id") val id: Int,
#Json(name = "email") val email: String,
#Json(name = "name") val name: String,
#Json(name = "key") val remix_userkey: String,
#Json(name = "downloads_limit") val downloads_limit: Int?,
<some other fields>
)
and finally calling it in a viewModel
fun test(<some String fields>){
compositeDisposable.add(testRepo.test(<some String variables>)
.subscribeOn(schedulers.io())
.observeOn(schedulers.main())
.subscribe ({ testList ->
testListDebug.postValue(testList)
if (testList.isSuccessful) user.postValue(userList.body()?.user)
else {<some error posting>}
})
{ throwable -> <some actions>})
}
So without minifyEnabled it parses this JSON
{"success":1,"user":{"id":"123456","email":"test#test.com","name":"Test","remix_userkey":"abcd123abcd","downloads_limit":15}}
correctly, after I enable minify - id field is always 0.
Same JSON, but somehow it wraps in retrofit Response already with id=0 in the body(all other fields are parsed correctly)
example of testListDebug value from debugger after API call
Tried adding all library rules in proguard-rules.pro file, but with no effect; also tried adding #Keep annotation to TestUser class and renaming id field
Where I can dig from here? Is it something regarding Moshi or Retrofit/Okhttp?
Figured it out - needed to keep a custom moshi annotation class, which was used for parsing some field(which sometimes Int and sometimes Boolean) in other API calls and which was not used here. After adding keep annotation to it id is parsed fine
Very strange behavior since this annotation was not used here

Moshi adapter creation failure: "requires explicit JsonAdapter to be registered"

var wall= ArrayList<VKWall>()
try {
val response = r.getString("response") as String
val moshi = Moshi.Builder().build()
val type: Type = Types.newParameterizedType(
ArrayList::class.java,
VKWall::class.java
)
val jsonAdapter: JsonAdapter<ArrayList<VKWall>> = moshi.adapter(type)
wall = jsonAdapter.fromJson(response)!!
} catch (e: JSONException){}
return wall
It can not create adapter. Debuger can`t execute this string and goes to exception of function over this code
val jsonAdapter: JsonAdapter<ArrayList<VKWall>> = moshi.adapter(type)
I`am trying to do everything like there
https://github.com/square/moshi
Platform java.util.ArrayList<com.e.app.fragments.vk_tabs.WallFragment.DataPackage.VKWall> (with no annotations) requires explicit JsonAdapter to be registered
#Parcelize
#JsonClass(
generateAdapter = true)
data class VKWall (
// val UserName:String="",
// val UserSurname:String="",
#Json(name = "text")
val Text:String="" ,
// val attachments: Attachments?,
// val copyright: String="",
// val repost: Repost?
):Parcelable
{
}
The problem is in that moshi don't have adapter for yours class VKWall. To resolve this you could add KotlinJsonAdapterFactory that based on reflection:
val moshi = Moshi.Builder()
// ... add your own JsonAdapters and factories ...
.add(KotlinJsonAdapterFactory())
.build()
Or you could use generated adapter like this:
// Annotate yours class #JsonClass(generateAdapter = true)
#JsonClass(generateAdapter = true)
class VKWall(
....
)
More documentation about this https://github.com/square/moshi#kotlin
More about yours problem https://www.zacsweers.dev/a-closer-look-at-moshi-1-9/
Now, for Kotlin classes, you either need to use code gen,
KotlinJsonAdapterFactory, or supply your own custom JsonAdapter.
This is a potentially dangerous change! In projects using code gen,
there are cases where Kotlin classes could have (appeared to) Just
Work™️ before if you forgot to annotate them with #JsonClass. These
will fail at runtime now. If you're worried about this, I suggest
using Moshi 1.9 only in debug builds for a period of time to tease
these out before releasing production builds with it.

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.

Cannot find setter for field - using Kotlin with Room database

I'm integrating with the Room persistence library. I have a data class in Kotlin like:
#Entity(tableName = "story")
data class Story (
#PrimaryKey val id: Long,
val by: String,
val descendants: Int,
val score: Int,
val time: Long,
val title: String,
val type: String,
val url: String
)
The #Entity and #PrimaryKey annotations are for the Room library. When I try to build, it is failing with error:
Error:Cannot find setter for field.
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
I also tried providing a default constructor:
#Entity(tableName = "story")
data class Story (
#PrimaryKey val id: Long,
val by: String,
val descendants: Int,
val score: Int,
val time: Long,
val title: String,
val type: String,
val url: String
) {
constructor() : this(0, "", 0, 0, 0, "", "", "")
}
But this doesn't work as well. A thing to note is that it works if I convert this Kotlin class into a Java class with getters and setters. Any help is appreciated!
Since your fields are marked with val, they are effectively final and don't have setter fields.
Try switching out the val with var.
You might also need to initialize the fields.
#Entity(tableName = "story")
data class Story (
#PrimaryKey var id: Long? = null,
var by: String = "",
var descendants: Int = 0,
var score: Int = 0,
var time: Long = 0L,
var title: String = "",
var type: String = "",
var url: String = ""
)
EDIT
The above solution is a general fix for this error in Kotlin when using Kotlin with other Java libraries like Hibernate where i've seen this as well. If you want to keep immutability with Room, see some of the other answers which may be more specific to your case.
In some cases immutability with Java libraries is simply not working at all and while making sad developer noises, you have to switch that val for a var unfortunately.
Hey I don't know if everyone know or not, but you can not have column which is starting from is into Room.
For example you can't have like this
#Entity(tableName = "user")
data class User (
#PrimaryKey var id: Long? = null,
var userName: String = "",
var isConnectedToFB: Boolean = false,
)
If you have #Ignore field in the data class constructor you need to move it to class body like this:
#Entity(primaryKeys = ["id"])
data class User(
#field:SerializedName("id")
val id: Int,
#field:SerializedName("name")
val name: String,
#field:SerializedName("age")
val age: Int
) {
#Ignore
val testme: String?
}
All kudos go to marianperca on GitHub: https://github.com/android/architecture-components-samples/issues/421#issuecomment-442763610
There is an issue in room db library java code generation.
I was using optional field isFavorite. It gives me same error then I change my field name to favorite then compiled.
before
var isFavorite: Int? = 0,
after changing working fine
var favorite: Int? = 0,
Thanks
According to https://stackoverflow.com/a/46753804/2914140 if you have an autogenerated primary key, you should write so:
#Entity(tableName = "story")
data class Story (
val by: String,
val descendants: Int,
val score: Int,
val time: Long,
val title: String,
val type: String,
val url: String
) {
#PrimaryKey(autoGenerate = true)
var id: Int = 0
}
Note that #PrimaryKey is written inside the class body and contains modifier var.
If you later want to update a row in a database with different parameters, use these lines:
val newStory = story.copy(by = "new author", title = "new title") // Cannot use "id" in object cloning
newStory.id = story.id
dao.update(newStory)
UPDATE
I still don't use AndroidX, and Room is 'android.arch.persistence.room:runtime:1.1.1'.
You can extend this class from Serializable. But if you want to extend it from Parcelable, you will get a warning (over id variable): Property would not be serialized inro a 'Parcel'. Add '#IgnoredOnParcel' annotation to remove this warning:
Then I moved an id from the body to the constructor. In Kotlin I use #Parcelize to create Parcelable classes:
#Parcelize
#Entity(tableName = "story")
data class Story (
#PrimaryKey(autoGenerate = true)
var id: Int = 0,
val by: String,
val descendants: Int,
val score: Int,
val time: Long,
val title: String,
val type: String,
val url: String
) : Parcelable
Had this error in Java.
You cannot have a column starting with is or is_ in Java.
Try renaming the column.
Another solution:
You either have to pass the field in the constructor and initialize it with the constructor argument, or create a setter for it.
Example:
public MyEntity(String name, ...) {
this.name = name;
...
}
public void setName(String name) {
this.name = name;
}
This error will be thrown if your column starts with Is:
#ColumnInfo(name = "IsHandicapLeague")
#NonNull
var isHandicapLeague: String = "Y"
Add a default set() function to eliminate
fun setIsHandicapLeague(flag:String) {
isHandicapLeague = flag
}
Just make the variables mutable, change val into var for Kotlin, Or private into public for Java
This is a bug and is fixed in Room 2.1.0-alpha01
https://developer.android.com/jetpack/docs/release-notes#october_8_2018
Bug Fixes
Room will now properly use Kotlin’s primary constructor in
data classes avoiding the need to declare the fields as vars.
b/105769985
I've found that another cause of this compilation error can be due to the use of the Room's #Ignore annotation on fields of your entity data class:
#Entity(tableName = "foo")
data class Foo(
// Okay
#PrimaryKey
val id: String,
// Okay
val bar: String,
// Annotation causes compilation error, all fields of data class report
// the "Cannot find setter for field" error when Ignore is present
#Ignore
val causeserror: String
)
The same error also seems to happens when using the #Transient annotation.
I've noticed this issue using version 2.2.2 of Room:
// build.gradle file
dependencies {
...
kapt "androidx.room:room-compiler:2.2.2"
...
}
Hope that helps someone!
You can try to rename id variable to another name. It worked for me ;
var id: Long? = null
to
var workerId: Long? = null
If you have to name as id and you are using retrofit, then you may need to add SerializedName("id")
Another cause of this may be the naming of the field. If you use any of the pre-defined keywords, you will get the same error.
For instance, you can not name your column "is_active".
Reference: http://www.sqlite.org/lang_keywords.html
It seems like Room and Kotlin versions need to be matched. I have same issue with Room 2.3.0 and Kotlin 1.6.10 but it's ok with Kotlin 1.5.20. It looks ok after I updated Room to 2.4.2.
https://youtrack.jetbrains.com/issue/KT-45883
Also there is a possible solution to use #JvmOverloads constructor for better Java compability.
Updating Room library to the latest version 2.4.2 solve the issue
The correct way to fix this issue would be simply updating to Room v2.4.3 or higher.
Workaround
If you're running on an older version of Room, one that uses an old version of the kotlinx-metadata-jvm library which doesn't understand 1.5.x metadata, a simple workaround would be adding the following line to your build.gradle:
kapt "org.jetbrains.kotlinx:kotlinx-metadata-jvm:0.5.0"
Source: https://youtrack.jetbrains.com/issue/KT-45883/KAPT-Cannot-find-setter-for-field-compiling-projects-with-Room-db-breaks-using-150-M2
Just an update if somebody comes across this thread in 2019, after spending hours digging online on why this should work, but it doesn't.
Using val works as expected if you are using the AndroidX version ( androidx.room:room-<any>:2.*) but it doesn't when using the old android.arch.persistence.room:<any>:1.1.1 and it seems that version 2.* wasn't released on this latter repo.
Edit: typos
If you want the val immutability available for your entity, it is possible.
You should update to AndroidX room current version.
Check for the related issue here it is marked as Won't Fix
Now they have release a fix related to the issue with version 2.0.0-beta01
Now you can use immutable val with default value e.g:
#Entity("tbl_abc")
data class Abc(
#PrimaryKey
val id: Int = 0,
val isFavourite: Boolean = false
)
Previously, the above snippet will throw an error of Cannot find setter for field. Changing into var is a great workaround, but I prefer for the entity class to be immutable from outside invocation
You can now start your field with is but you can't have a number next to the is like : is2FooSelected, you have to rename to isTwoFooSelected.
I think that the variable we wrote as id is getting mixed up with the id in the system. Therefore, when I define it as uuid, my error is resolved. I think it will be solved too. Also, try using var instead of val.
#PrimaryKey(autoGenerate = true)
var uuid:Int=0
Just use var instead of val and if you are using private keyword, make it public.
#Entity(tableName = "story")
data class Story (
#PrimaryKey val id: Long,
var by: String,
var descendants: Int,
var score: Int,
var time: Long,
var title: String,
var type: String,
var url: String
)

Categories

Resources