Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserializing Json array into Scala object

I have been having major problems trying to deserialize a JSON array to a Scala object

      [{"name":"Cool","city":"College Park","address":"806","id":1},{"name":"Mars ","city":"Durham","address":"12","id":2},{"name":"Something","city":"Raleigh 
","address":"","id":3},{"name":"test","city":"","address":"","id":5}]

I have tried gson, jerkson(jackson scala wrapper), sjson, flexjson. None of them have worked. What I have here is a List of Customers. List[Customer].

This is the closest I've got:

val array = new JsonParser().parse( customers ).getAsJsonArray()

This gave me an 4 arrays. It obviously didn't give me a customer object though. I tried Jerkson.

val array = parse[List[Customer]](customers)

But I get this.

GenericSignatureFormatError occured : null

I'm just trying to find a simple way like I would in Java.

Here is my Scala class.

    case class Customer(
    id : Pk[ Int ],
    name : String,
    address : Option[ String ],
    city : Option[ String ],
    state : Option[ String ],
    user_id : Int )

    object Customer extends Magic[ Customer ]( Option( "Customer" ) ) { 

    def apply( name : String, address : String, city : String, state : String, user_id : Int ) = {
        new Customer( NotAssigned, name, Some( address ), Some( city ), Some( state ), user_id )
    }

    def delete( id : Int ) = {
        SQL( "DELETE from Customer where id = {id}" ).onParams( id ).executeUpdate()
    }

}

Thanks for any help.

like image 563
Drew H Avatar asked Dec 09 '22 05:12

Drew H


1 Answers

With gson, you could write your own json reader:

case class Customer(id: Int, name: String, address: Option[String], 
  city: Option[String], state: Option[String], user_id: Int)

object CustomerJsonReader {

   def read(in: Reader) = readCustomers(new JsonReader(in))

   def readCustomers(reader: JsonReader) = {
     val customers = new ListBuffer[Customer]
     reader.beginArray()
     while (reader.hasNext()) {
       customers += readCustomer(reader)
     }
     reader.endArray()
     customers toList
   }

   def readCustomer(reader: JsonReader): Customer = {
     var id = 0
     var customerName = ""
     var address: Option[String] = None
     var city: Option[String] = None
     var state: Option[String] = None
     var userId = 0

     reader.beginObject()
     while (reader.hasNext()) {
       val name = reader.nextName()
       name match {
         case "id" => id = reader.nextInt()
         case "address" => address = Some(reader.nextString())
         case "state" => state = Some(reader.nextString())
         case "user_id" => userId = reader.nextInt()
         case "name" => customerName = reader.nextString()
         case "city" => city = Some(reader.nextString())
         case _ => reader.skipValue()
       }
     }
     reader.endObject()
     Customer(id, customerName, address, city, state, userId)
   }

}

val json = 
  """
  [{"name":"Cool","city":"College Park","address":"806","id":1},
  {"name":"Mars ","city":"Durham","address":"12","id":2},
  {"name":"Something","city":"Raleigh  ","address":"","id":3},
  {"name":"test","city":"","address":"","id":5}] 
  """

val customers: List[Customer] = 
  CustomerJsonReader.read(new StringReader(json))
like image 72
Marimuthu Madasamy Avatar answered Dec 14 '22 12:12

Marimuthu Madasamy