0

I am not sure if this is achievable and I have a very basic understanding of how generics work in scala. But I was wondering if this is possible. Say I have a method:

case class Person(id:String,name:String)
case class Student(id:String,name:String, class:String)

    def convertToJson[A](fileName:String):{
     //read file
     parse[A]
    }

Is it possible to write this generic method which would parse the json based on the type of class I send when I call the convertToJson method? Something like:

convertToJson[Student](fileName)
convertToJson[Person](fileName)

BTW the above code gives me a :

No Manifest available for A. error.

Using json4s for parsing. Any help is appreciated.

1 Answer 1

3

This will convert a JSON string to a case class

import org.json4s._
import org.json4s.jackson.JsonMethods._

def convertToJson[T](json: String)(implicit fmt: Formats = DefaultFormats, mf: Manifest[T]): T =
  Extraction.extract(parse(json))

Once this is defined you can parse appropriate strings to the required type:

case class Person(id: String, name: String)
case class Student(id: String, name: String, `class`: String)

val person = convertToJson[Person]("""{"name":"Jane","id":45}""")
val student = convertToJson[Student]("""{"name":"John","id":63, "class": "101"}""")

Note that this will ignore JSON data that does not match fields in the case class. If a field is optional in the JSON, make it an Option in the case class and you will get None if the field is not there.

Sign up to request clarification or add additional context in comments.

7 Comments

That worked! Thanks. On a side note is there a way to deal with nulling out case when we have bad data? Say throw an exception or something?
@Achilleus If you have bad data the extract will throw an exception. You can let the caller handle that, or wrap the extract call in a Try and return a Try[T], which is perhaps more standard in Scala.
Actually, it won't throw an exception in the situation when we are dealing with nested json and an object in it say has a field spelled wrong. It will just null that object and move on. I was wondering if we can somehow specify it to fail or throw an exception instead of nulling it out.
Can you give an example of this? If I rename one of the fields from the JSON string I get MappingException: No usable value for <fieldname>.
Ah actually, it throws the exception for case classes. But I am using an Avro specific record case class. Its a case class that extends org.apache.avro.specific.SpecificRecordBase. But somehow it serializes to null in this case.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.