cbeust / klaxon

A JSON parser for Kotlin
Apache License 2.0
1.86k stars 121 forks source link

Can't get FieldRenamer to work #281

Open lpar opened 5 years ago

lpar commented 5 years ago

I've boiled it down to a pretty simple example, and can't see what I'm doing wrong:

  val downcase = object: FieldRenamer {
    override fun fromJson(fieldName: String): String {
      return fieldName.toLowerCase()
    }
    override fun toJson(fieldName: String): String {
      return fieldName.toLowerCase()
    }
  }

  val klaxon = Klaxon()
    .fieldRenamer(downcase)

  class Thing {
    var uniqueid : String? = null
    var name : String? = null
    var description : String? = null
    @Json(name="@form") var form : String? = null
    var type : String? = null
    var status : String? = null
    var iot : String? = null
    var imt : String? = null
    var region : String? = null
  }

  val json = """
  {
    "@created":"2015-07-02T14:21:24Z",
    "@modified":"2018-05-23T20:31:45Z",
    "@form":"Location",
    "IOT":"Value goes here",
    "Created":"2008-03-05T14:26:32Z",
    "UniqueID":"LAMG-7XZJXM",
    "IMT":"ASEAN",
    "Name":"Bangkok - Thailand",
    "Description":"Bangkok - Thailand",
    "Type":"A",
    "Modified":"2013-11-07T03:56:53Z",
    "Region":"AP - ASEAN",
    "Status":"Open"
  }
  """.trimIndent()
  val doc = klaxon.parse<Thing>(json)
  println(doc?.uniqueid)

I just get null as output.

zengineering commented 4 years ago

I have the same issue on v5.2. Even using something like the following has no effect, which makes me assume fromJson is never called.

  val downcase = object: FieldRenamer {
    override fun fromJson(fieldName: String): String {
      return "FOO"
    }
    override fun toJson(fieldName: String): String {
      return fieldName.toLowerCase()
    }
  }