I am using parse method from circe library to cast my file records (records are in string format) to a case class and the output should be 2 lists, one with bad records which failed to be casted to case class (pre defined model) and the other one with records casted as case class. Here is the method:(https://circe.github.io/circe/parsing.html)

Below is the class

import io.circe._, io.circe.generic.auto._, io.circe.parser._, io.circe.syntax._

class LocalFileReader extends FileHandler {
    override def read[T](rootDir: String, fileName: String): (List[String], List[T]) = {

        // generate full path using rootDir and fileName

        val file = new File(rootDir, fileName)

        //Read content from the file path
        val fileContent = new FileInputStream(file);

        // zip data
        val zipFileContent = new GZIPInputStream(fileContent)

        //convert to json
        val jsonfileContent = Source.fromInputStream(zipFileContent).mkString

        //Deserialize jsonFileContent to the case class type
//use circe parse method to put bad records and good records which will be of case class type T

            val (br, gr):(List[String], List[T])= parse(jsonfileContent) match {
            case Left(failure) =>  br + "fail"
            case Right(json) => gr
            
    System.out.println(gr)
    System.out.println (json)
    System.out.println ("failure" + br)
        }
    }
}

I am struggling in this piece of code (which is incorrect)

val (br, gr):(List[String], List[T])= parse(jsonfileContent) match {
            case Left(failure) =>  br + "fail"
            case Right(json) => gr

Where I'm trying to achieve the following

(1) When records failed to parse to the case class which means case Left(failure), it should return a list of those bad records. This will be the first output of method read(list[String]) (2) When records successfully get casted to the case class it should return a list of those good records. This will be the second outout of method read list[T]

T is a generic case class Case class has attributes: name:String, age : int

0

There are 0 best solutions below