I’m using json4s to serialize entities into JSON as my app is primarly REST based. json4s provides implicit serialization for case classes which is great as all of my domain objects are case classes.
I’m running into a problem though when I try to serialize an entity returned by my DB. When I try to serialize an entity returned from the DB (Game with SurrogateIntId) what I end up seeing:
{dao.GameEntity$$anon$2@12589}"Game(param a, param b, param c)"
{models.Game@12622}"Game(param a, param b, param c)"
json4s doesn’t know how to deserialize the returned entity because it isn’t a case class(or a class for that matter, it’s an object?). What I need is a way of unwrapping the companion object from GameEntity
so that json4s can read the correct object. Or a way to programatically return a copy of the case class from the persisted entity — otherwise I’ll need to write and explicitly call some kind of getOriginal
method in each of my case classes just so I can get the right object.
Is there any way to do this? Thanks
--
You received this message because you are subscribed to the Google Groups "mapperdao" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mapperdao+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
def write[A <: AnyRef](a: A)(implicit formats: Formats): String =
JsonMethods.mapper.writeValueAsString(Extraction.decompose(a)(formats))The problem is that even when explicitly providing the class it still won't work -- evaluating someGame.getClass still returns dao.GametEntity$$anon and so does cast.
The few other popular JSON libraries I've researched (spray-json, argonaut, jacks) all work the same way. They all include ways to implement custom serializers as well but that is a ton of boilerplate. I really just need a way to my classes back as POSOs -- I don't need the extra functionality mapperdao embues at the point I'm serializing them as the client app won't be using that information.
case class Game(name: String,publisher: String,website: String, gameType: GameType.Value, id: Int) {
def getClean:Game = new Game(name,publisher,website,gameType,id)
}
class LinkObjectEntitySerializer[T: Manifest] extends CustomSerializer[Entity[Int, Persisted, Class[T]]](formats =>(
{PartialFunction.empty},{
case tu: TeamUser =>
implicit val formats: Formats = DefaultFormats
("Team" ->
("name" -> tu.team.name) ~
("id" -> tu.team.id) ~
("resource" -> "/team/") ~
("isCaptain" -> tu.isCaptain)) ~
("User" ->
("name" -> tu.user.globalHandle) ~
("id" -> tu.user.id) ~
("resource" -> "/user/") ~
("isCaptain" -> tu.isCaptain))
}
))
class EntitySerializer[T: Manifest] extends CustomSerializer[Entity[Int, Persisted, Class[T]]](formats =>(
{PartialFunction.empty},{
case g: Game =>
implicit val formats: Formats = DefaultFormats + new org.json4s.ext.EnumNameSerializer(GameType)
Extraction.decompose(g.copy())
case u : User =>
implicit val formats: Formats = DefaultFormats + new LinkObjectEntitySerializer
Extraction.decompose(u.copy())
case t : Team =>
implicit val formats: Formats = DefaultFormats + new LinkObjectEntitySerializer
Extraction.decompose(t.copy())
...
}
"org.json4s" % "json4s-jackson_2.10" % "3.2.10",
"org.json4s" % "json4s-ext_2.10" % "3.2.10",
"org.json4s" % "json4s-core_2.10" % "3.2.10",
...
def getClean:Game = new <span
--
case class Game(name:String,players:Int)
//Using this in the test suite runs correctly
case class GameWithId(name: String, players: Int, id: Int) extends IntId
...