Skip to content

Instantly share code, notes, and snippets.

@farmdawgnation
Last active December 16, 2015 18:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save farmdawgnation/5477231 to your computer and use it in GitHub Desktop.
Save farmdawgnation/5477231 to your computer and use it in GitHub Desktop.
Examples for Lift and Mongo Post
import net.liftweb.mongodb._
import BsonDSL._
trait Animal {
def makeNoise(): String
}
case class Dog(name: String, breed: String) extends Animal {
def makeNoise() = {
"Woof"
}
}
case class Cat(name: String, furPattern: String) extends Animal {
def makeNoise() = {
"Meow"
}
}
// Define AnimalInfo as a mongo model.
case class AnimalInfo(animals: List[Animal], _id: ObjectId = ObjectId.get)
extends MongoDocument[AnimalInfo] {
val meta = AnimalInfo
}
object AnimalInfo extends MongoDocumentMeta[AnimalInfo] {
override def formats = allFormats ++ ShortTypeHints(
classOf[Dog] ::
classOf[Cat] ::
Nil
)
}
// Create and save an animal info.
AnimalInfo(
Dog("Shadow", "Collie") ::
Dog("Beamer", "Collie") ::
Cat("Mittens", "Zig-Zag") ::
Nil
).save
//And your data is in Mongo!
trait Animal {
def makeNoise(): String
}
case class Dog(name: String, breed: String) extends Animal {
def makeNoise() = {
"Woof"
}
}
case class Cat(name: String, furPattern: String) extends Animal {
def makeNoise() = {
"Meow"
}
}
case class AnimalInfo(animals: List[Animal])
scala> val jvalueAnimals: JValue = decompose(animals)
jvalueAnimals: net.liftweb.json.package.JValue = JObject(List(JField(animals,JArray(List(JObject(List(JField(name,JString(Shadow)), JField(breed,JString(Collie)))), JObject(List(JField(name,JString(Beamer)), JField(breed,JString(Collie)))), JObject(List(JField(name,JString(Mittens)), JField(furPattern,JString(Zig-Zag)))))))))
scala> val extractedAnimals = jvalueAnimals.extract[AnimalInfo]
net.liftweb.json.MappingException: No usable value for animals
No constructor for type interface Animal, JObject(List(JField(name,JString(Shadow)), JField(breed,JString(Collie))))
at net.liftweb.json.Meta$.fail(Meta.scala:191)
at net.liftweb.json.Extraction$.mkValue$1(Extraction.scala:357)
at net.liftweb.json.Extraction$.build$1(Extraction.scala:317)
at net.liftweb.json.Extraction$$anonfun$12.apply(Extraction.scala:253)
at net.liftweb.json.Extraction$$anonfun$12.apply(Extraction.scala:253)
at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:233)
at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:233)
at scala.collection.LinearSeqOptimized$class.foreach(LinearSeqOptimized.scala:59)
at scala.collection.immutable.List.foreach(List.scala:76)
at scala.collection.TraversableLike$class.map(TraversableLike.scala:233)
at scala.collection.immutable.List.map(List.scala:76)
at net.liftweb.json.Extraction$.instantiate$1(Extraction.scala:253)
// Import lift-json
import net.liftweb.json._
import Extraction._
// Declare a set of serializers and type hints to use.
implicit val formats = DefaultFormats
// Produce a JValue representation of a dog. This can
// be serialized to actual JSON and manipulated with
// queries and transforms.
val jvalueDog: JValue = decompose(Dog("Shadow", "Collie"))
// Import lift-json
import net.liftweb.json._
import Extraction._
// Declare a set of serializers and type hints to use.
implicit val formats = DefaultFormats
// Create an animal info instance with a list of animals.
val animals = AnimalInfo(
Dog("Shadow", "Collie") ::
Dog("Beamer", "Collie") ::
Cat("Mittens", "Zig-Zag") ::
Nil
)
// Produce a JValue representation of the animals.
// Would render to the following JSON:
// { animals: [ {name: "Shadow", breed: "Collie"}, ... ] }
val jvalueAnimals: JValue = decompose(animals)
// Attempt to extract the animals, which will fail with
// a no constructor error because Animals is a trait that
// can't be directly instantiated.
val extractedAnimals = jvalueAnimals.extract[AnimalInfo]
// Import lift-json
import net.liftweb.json._
import Extraction._
// Declare a set of serializers and type hints to use.
implicit val formats = DefaultFormats ++ ShortTypeHints(
classOf[Dog] ::
classOf[Cat] ::
Nil
)
// Create an animal info instance with a list of animals.
val animals = AnimalInfo(
Dog("Shadow", "Collie") ::
Dog("Beamer", "Collie") ::
Cat("Mittens", "Zig-Zag") ::
Nil
)
// Produce a JValue representation of the animals.
// Would render to the following JSON:
// { animals: [ {jsonClass: "Dog", name: "Shadow", breed: "Collie"}, ... ] }
val jvalueAnimals: JValue = decompose(animals)
// Attempt to extract the animals, which will succeed because the jsonClass
// parameter tells lift-json which actual instance fitting Animal it should
// instatiate.
val extractedAnimals = jvalueAnimals.extract[AnimalInfo]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment