lift-mongo

47 views
Skip to first unread message

Guillaume Massé

unread,
Jul 13, 2013, 4:40:42 PM7/13/13
to lif...@googlegroups.com
Hi lifters,

I know it's possible to serialise a case class with lift-mongo (https://www.assembla.com/wiki/show/liftweb/Mongo_Record_Embedded_Objects)

Does it support something like this ?

sealed abstract class Z
case class Z1( x: Int, y: String ) extends Z
case class Z2( x: List[S], y: String ) extends Z
case class Z3( x: String ) extends Z

case class S( x: String, y: Int, z: Int )

val somez: Z = ???

Record.createRecord.myZ(somez)...

Matt Farmer

unread,
Jul 14, 2013, 7:13:27 PM7/14/13
to lif...@googlegroups.com
Hi there,

It sounds like you're asking if MongoRecord supports polymorphism. If so, then yes, I believe it does. However, I typically use the MongoDocument implementation, so I don't have exact code for you. What I can tell you is that you'll need to define a type hints object for those classes and hook it into a formats object somewhere. TypeHints tell lift-json (the component that serializes your class) to include the class name in the serialized json when it's writing out to disk. So, somewhere you'd define the following...

// With your imports
import net.liftweb.json._


// With your model code
object Z {
  val typeHints
= FullTypeHints(List(
    classOf
[Z1],
    classOf
[Z2],
    classOf
[Z3]
 
))
}


Then, you just need to make sure that typeHints instance is attached to your serialization formats for the model. Using the MongoDocument structure, I'd do that by overriding the formats def in MongoDocumentMeta with my own implementation that included those type hints. For example...

override val formats = super.formats + Z.typeHints

In theory, you should be able to define a similar override somewhere in the MongoRecord setup.

I probably did a pretty miserable job of explaining this, so let me know if you have any additional questions.

Matt
Reply all
Reply to author
Forward
0 new messages