json4s serialization of string to avro specific record class

本小妞迷上赌 提交于 2019-12-13 05:12:22

问题


I have json string which I am trying to serialize as an Avro Specific Record(scala case class extends org.apache.avro.specific.SpecificRecordBase ). Json4s throws exception rightfully for malformed json in case of normal scala case class but doesn't throw an exception for the case class which extends specific record . Instead it tries to contain with nulls(not sure if it's json4s doing this or having the specific record).
Say I have this json:

{
  "name":"Tom",
  "id":1,
  "address":{
    "houseNum": 88,
    "addresssss":"101 Hacker drive"
  }
}

And a case class,

case class Employee(id:Int , name:String , address:Address)
case class Address(houseNum: Int , address1:String)

It does throw an exception when I try to do:

def convertToEmployee(fileName:String):Employee={
    val json = readFromFile(fileName)
    implicit val formats = DefaultFormats
    val x = parse(json).extract[Employee]
    x
  }

But when I try to do the same thing for a case class that extends SpecificRecordBase, it initializes all the fields in address object to null and keeps name and id intact instead of throwing an error?

So I was wondering if there is an elegant way to handle this situation? I am using json4s though.

来源:https://stackoverflow.com/questions/55381927/json4s-serialization-of-string-to-avro-specific-record-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!