1

i have a function which takes a list of objects as a parameter, takes out each object from the list and makes json object with its attributes and then add the json object to a mutable list and returns it

class foo {
  var jsonlist = new MutableList[JsValue]

  def makeJson(objlist : List[Student]) : MutableList[JsValue] = {

     for( obj <- objlist){
      var jsobj = Json.obj("name" -> obj.getName, "uuid" -> obj.getUuid)
      jsonlist += jsobj
    }
    jsonlist
  }

}

i need this fucntion in different classes with and different parameter type for List i.e List[Teacher], List[Admin] etc

Is there any way of writing this function so that i can use it for different classes or i have to write it separately for every class?

if it is not possible than, i have a User trait and some classes that extends User Trait . Can i write the above code in a way that can be used by all the child classes

M.Ahsen Taqi
  • 965
  • 11
  • 35
  • You need to make JSON object from those two fields, or from different fields in every class? – Forketyfork Jun 10 '15 at 12:59
  • @SergeyPetunin these two fields are common in every class , there are some other fields in those classes but these two are common and i only want these two fields in json – M.Ahsen Taqi Jun 10 '15 at 13:12

3 Answers3

2

Suppose you have the following

abstract class User {
  def id: UUID
  def name: String
}

case class Student(id: UUID, name: String, classRoom: String) extends User

Then you could write something like this

  var jsonList = new mutable.MutableList[JsValue]

  def makeJson[T <: User](objList: List[T]): mutable.MutableList[JsValue] = {
    objList.foreach(obj => jsonList += Json.obj("uuid" -> obj.id, "name" -> obj.name))
    jsonList
  }
Thiago Pereira
  • 1,724
  • 1
  • 17
  • 31
  • this is for the second part the Questions, Is there any thing for the first part? can i write the code generically for all classes? – M.Ahsen Taqi Jun 10 '15 at 14:02
  • If you don't care about the uuid and the name(I mean, specific fields) you can, doing a generic JsonSerializer like(@Gregor Rayman answer) however If you want to only create Json objects based on id and name from any class, then using your User class should help. – Thiago Pereira Jun 10 '15 at 14:25
  • these two fields are common in every class , there are some other fields in those classes but these two are common and i only want these two fields in json. and for JsonSerializer thing, i am a beginner and his code went way above my head :D – M.Ahsen Taqi Jun 10 '15 at 14:47
  • no problem :) as I said before, if you want only those fields, this code should help you out. You could also use ListBuffer instead of MutableList. Check out this http://stackoverflow.com/questions/5446744/difference-between-mutablelist-and-listbuffer – Thiago Pereira Jun 10 '15 at 17:26
2

You could also use type classes. So that you would provide a specific JSON serializer for every class you want to be able to serialize (e.g. Student or Admin)

trait JsonSerializer[T] {
  def toJson(x:T):JsValue
}

def toJson[T:JsonSerializer](xs:List[T]) = {
  val serializer = implicitly[JsonSerializer[T]]
  for (x <- xs) yield serializer.toJson(x)
}

implicit object StudentJsonSerializer extends JsonSerializer[Student] {
   // here you implement the serialization of the student
}

Similarly you can provide an implementation for Admin or whatever you want.

Gregor Raýman
  • 3,051
  • 13
  • 19
1

Use parent class or trait

class Foo {
  var jsonlist = new mutable.MutableList[JsValue]

  def makeJson(objlist : List[Jsonable]) : mutable.MutableList[JsValue] = {

    for( obj <- objlist){
      var jsobj = Json.obj("name" -> obj.getName, "uuid" -> obj.getUuid)
      jsonlist += jsobj
    }
    jsonlist
  }

}


trait Jsonable {
  protected val name: String
  protected val uuid: Long
  def getName = name
  def getUuid = uuid
}

case class Student(name: String, uuid: Long) extends Jsonable
case class Teacher(name: String, uuid: Long) extends Jsonable
Dmitry Meshkov
  • 921
  • 7
  • 20