https://kotlinlang.org logo
#android
Title
# android
g

gsala

12/12/2018, 10:31 AM
I have a problem with Kotlin enums and Moshi. It seems like when I annotate an enum member with
@Json(name = "xx")
, the serialized string has the name of the enum member itself instead of the value passed into the annotation. Has anyone run into this problem before?
i

ienoobong

12/12/2018, 11:12 AM
Have you tried with
@field:Json(name = "xx")
?
g

gsala

12/12/2018, 11:23 AM
Yes I have tried and it doesn't work either
i

ienoobong

12/12/2018, 12:00 PM
Strange, seems enums are treated differently, because actually they are not fields, neither are they getter 😕
e

eric

12/12/2018, 8:01 PM
#squarelibraries
are you using codegen or the reflective KotlinJsonAdapterFactory?
g

gsala

12/12/2018, 10:36 PM
Codegen
e

eric

12/13/2018, 7:31 PM
Copy code
@Test fun foo() {
  val moshi = Moshi.Builder().build()
  val adapter = moshi.adapter(HasFoo::class.java)
  assertThat(adapter.fromJson("{\"foo\":\"hello\"}")!!.foo).isEqualTo(Foo.WORLD)
  assertThat(adapter.toJson(HasFoo(Foo.WORLD))).isEqualTo("{\"foo\":\"hello\"}")
}

@JsonClass(generateAdapter = true)
class HasFoo(val foo: Foo)

enum class Foo {
  @Json(name = "hello") WORLD
}
this passes for me. if you can make a reproducible sample, file an issue on GitHub.
g

gsala

12/13/2018, 7:34 PM
Interesting. I'll investigate what's going wrong then 🤔. Thank you for taking a look at it
134 Views