问题
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