I have a problem with Kotlin enums and Moshi. It s...
# android
g
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
Have you tried with
@field:Json(name = "xx")
?
g
Yes I have tried and it doesn't work either
i
Strange, seems enums are treated differently, because actually they are not fields, neither are they getter 😕
e
#squarelibraries
are you using codegen or the reflective KotlinJsonAdapterFactory?
g
Codegen
e
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
Interesting. I'll investigate what's going wrong then 🤔. Thank you for taking a look at it
181 Views