Problem in typeclass based serialization using Seq #37

Closed
debasishg opened this Issue Aug 29, 2011 · 0 comments

Comments

Projects
None yet
1 participant
@debasishg
Owner

debasishg commented Aug 29, 2011

from: Jeff Crilly jcrilly@vmware.com

I have some of the typeclass serialization working, but am running into a problem w/ a case class that contains a Seq field...

I'm trying to pattern this after the examples at https://github.com/debasishg/sjson/blob/master/src/test/scala/sjson/json/Protocols.scala

Here's what I've got, but its not compiling...

// the following is scalaxb generated...
case class User(val id : scala.Option[scala.Predef.String],
val username : scala.Predef.String,
val org : scala.Predef.String, val firstname : scala.Predef.String, val lastname : scala.Predef.String
// other fields removed for brevity
) extends java.lang.Object with scala.ScalaObject with scala.Product { ... }

// The following is not generated, and is a wrapper for the User list...
case class DataGridResult (totalCount: String, success: Boolean, results: Seq[User])
// Update! if use a List[User] instead of Seq[User] then it works.

// and the "Protocol" adapter...

object DataGridResultProtocol extends DefaultProtocol {
import dispatch.json._
import JsonSerialization._

implicit object UserFormat extends Format[User] {
def reads(json: JsValue): User = json match {
case JsObject(m) =>
User(
fromjsonOption[String],
fromjsonString,
fromjsonString,
fromjsonString
// other fields removed for brevity
)
case _ => throw new RuntimeException("JsObject expected")
}

def writes(p: User): JsValue =
  JsObject(List(
    (tojson("id").asInstanceOf[JsString], tojson(p.id.getOrElse(""))),
    (tojson("username").asInstanceOf[JsString], tojson(p.username)),
    (tojson("firstname").asInstanceOf[JsString], tojson(p.firstname)),
    (tojson("lastname").asInstanceOf[JsString], tojson(p.lastname))

// other fields to be added
))
}

implicit val DataGridFormat: Format[DataGridResult] =
asProduct3("totalCount", "success", "results")(DataGridResult)(DataGridResult.unapply(_).get) // <== there is obviously something wrong with this.

}

Any idea on who how to get the Seq[User] type to work? Do i need to use viaSeq()? (if so, a pointer would be helpful.)
Update: after i drafted this note, I changed the "results" to a List[User] instead of a Seq[User]... this works for classes that I control, but if the generated classes have Seq[] it will be a problem.

@ghost ghost assigned debasishg Aug 29, 2011

@debasishg debasishg closed this in 2ded0c4 Aug 29, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment