klaxon icon indicating copy to clipboard operation
klaxon copied to clipboard

Can't get FieldRenamer to work

Open lpar opened this issue 6 years ago • 1 comments

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.

lpar avatar Aug 07 '19 14:08 lpar

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()
    }
  }

zengineering avatar Jan 30 '20 21:01 zengineering