-
Notifications
You must be signed in to change notification settings - Fork 760
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Moshi 1.5 ignores @Json annotation on Kotlin class properties #315
Comments
Try adding
|
Not an option at the time, it crashes at runtime with |
Please do. Yeah, we wanna make sure this stuff all works well. |
Dam - moshi-kotlin is not yet an option on Android as for the size of the kotlin-reflect dependency - makes me need to go back to 1.4.0 for this reason. |
@ligi Have you tested after proguard? |
@consp1racy I did not really test - was reading https://medium.com/square-corner-blog/kotlins-a-great-language-for-json-fcd6ef99256b
|
@consp1racy Thanks for posting this. I didn't want to roll back to 2 current solutions:
I completely agree with @ligi, the
@swankjesse, though we may not retain most of these methods post proguard, it will most likely force many development builds to use multidex, which we do not want. |
The method count issue aside, all development builds should be using native
multidex.
…On Sun, Jul 2, 2017, 6:39 PM Jared Burrows ***@***.***> wrote:
@consp1racy <https://github.com/consp1racy> Thanks for posting this. I
didn't want to roll back to 1.4.0. I have tested 1.4.0 and it does work
out the box. Thanks for the @field:Json work around.
I completely agree with @ligi <https://github.com/ligi>, the moshi-kotlin
dependency is too large. We should not have to add another dependency that
includes transitive dependencies in order to solve this issue. See the
dependency count here:
http://www.methodscount.com/?lib=com.squareup.moshi%3Amoshi-kotlin%3A1.5.0
.
- com.squareup.moshi:moshi-kotlin:1.5.0 - 23043 (total)
- org.jetbrains.kotlin:kotlin-reflect:1.1.1 - 15095 (big one)
@swankjesse <https://github.com/swankjesse>, though we may not retain
most of these methods post proguard, it will most likely force many
development builds to use multidex, which we do not want.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#315 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAEEEZeWJTfdOHn0ecfG7TMesbqDxJ_hks5sKBwigaJpZM4NoAO4>
.
|
Same problem here even with Dependencies:
Moshi config:
And the attributes:
|
@dsdebastiani Can you make a minimum reproducible test case to show what's wrong? |
data class LoginSessionAuthDto( For me this works in debug mode, but in release it says accessToken can't be null. that class isn't being obfuscated, and i have the proper proguard rules (as stated in the main page). Only with @field:Json it works |
Some solution for this?
|
@dsdebastiani Are you using the KotlinJsonAdapterFactory? If so, can you provide a test case? The following works: @Test fun foo() {
class Foo {
@Json(name = "delivery_time")
var deliveryTime: Int? = null
}
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
val adapter = moshi.adapter(Foo::class.java)
assertThat(adapter.fromJson("{\"delivery_time\":60}")!!.deliveryTime).isEqualTo(60)
assertThat(adapter.fromJson("{}")!!.deliveryTime).isNull()
assertThat(adapter.toJson(Foo())).isEqualTo("{}")
assertThat(adapter.toJson(Foo().apply { deliveryTime = 60 })).isEqualTo("{\"delivery_time\":60}")
} |
I have the same problem when trying to convert API field (in snake case) to entity class field (in camel case). Some example cases:
Project info:
Retrofit & Moshi configuration in Dagger Di module:
Json example:
Entity classes for this Json parsing:
Is it a bug or I am doing something wrong? |
@Test fun f() {
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
val adapter = moshi.adapter(PhotosResponse::class.java)
val response = adapter.fromJson("""
{
"photos": {
"photo": [
{
"id": "1111",
"owner": "11111DD",
"title": "sss",
"ownername": "bob",
"iconserver": "0001",
"iconfarm": 1,
"url_z": "urlz",
"url_n": "urln",
"url_m": "urlm"
}
]
}
}
""".trimIndent())
assertThat(response).isEqualTo(PhotosResponse(PhotosEntity(listOf(PhotoEntity(
1111, "sss", "urlm", "urln", "urlz",
"bob","11111DD", 1, 1)))))
} passes. What specifically doesn't work (thrown exception, unexpected result)? Can you form it in a test case? |
@NightlyNexus , your test case clarifies everything. It was my side issue, thank you. |
@GediminasZukas what was the fix for the issue on your side? I believe I'm facing the same issue. |
@premnirmal bug in the |
I am not sure why this is closed. In Moshi 1.4 we did not need to do the |
Moshi 1.4 didn’t support Kotlin. It sort of worked because Moshi’s did Java reflection on the objects. But it was fragile, especially around nulls. |
@swankjesse Ok. Thanks for the explanation. Adding |
right, cuz
sounds like a ProGuard issue, not a bug here. adding the KotlinJsonAdapterFactory makes |
@NightlyNexus so is this a matter of setting the correct rules? |
@png6 Unsure. I don't use ProGuard, personally. Somebody on Stack Overflow or a ProGuard forum might know? |
Well, had the same issue ...
Maybe because it was read-only ... |
Is using |
Use moshi-kotlin. Nothing else is tested. |
@swankjesse, but that has the Kotlin reflect library and is huge right? Also a bit slow to initialize especially on older devices? We opted out of using it for these reasons. |
Then use the Kotlin codegen from the most recent release? |
Does that fix this issue? We were going to bring in codgen for just the null support. |
Yes, Kotlin support is the solution to mistargetted |
Ok thanks 👌
…On Sun, Jun 24, 2018 at 11:34 AM Jesse Wilson ***@***.***> wrote:
Yes, Kotlin support is the solution to mistargetted @JSON annotations.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#315 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAHH9l_EZ-0CZU2ARG9M-3X9II29JPwhks5t_9uZgaJpZM4NoAO4>
.
|
I just faced the same problem, I had some variables with underline like |
@flux87 I had to add a var and use a dummy value to my fields. using: |
Not working :
Working :
Do I have to use the field:Json anotation ? |
same problem |
Locking this as it's no longer constructive. Please use discussions to ask usage questions, and if you have a bug to report please do so with a minimally reproducible sample. |
package_prototype_id
is always 0,package_prototype
is alwaysnull
.Works out-of-the-box in Moshi 1.4.
Workaround:
@field:Json
The text was updated successfully, but these errors were encountered: