Advertisement
Guest User

Untitled

a guest
Apr 19th, 2014
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. import org.json4s.native.Serialization._
  2. import org.json4s.JsonDSL.WithBigDecimal._
  3.  
  4. object WindowSerializer extends CustomSerializer[Window](format =>
  5. ( [omitted],
  6. {
  7. case Window(frame, size) =>
  8.  
  9. ( "size" -> size ) ~
  10. ( "frame" -> parse(write(frame)) )
  11. }))
  12.  
  13. import org.json4s._
  14. import org.json4s.jackson.JsonMethods._
  15. import org.json4s.JsonDSL._
  16. import java.util.UUID
  17.  
  18. case class Thing(name: String)
  19. case class Box(id: String, thing: Thing)
  20.  
  21. class BoxSerializer extends CustomSerializer[Box](format => ({
  22. case jv: JValue =>
  23. val id = (jv "id").extract[String]
  24. val thing = (jv "thing").extract[Thing]
  25. Box(id, thing)
  26. }, {
  27. case b: Box =>
  28. ("token" -> UUID.randomUUID().toString()) ~
  29. ("id" -> box.id) ~
  30. ("thing" -> Extraction.decompose(box.thing))
  31. }))
  32.  
  33. implicit val formats = DefaultFormats + new BoxSerializer
  34.  
  35. val box = Box("1000", Thing("a thing"))
  36.  
  37. // decompose the value to JSON
  38. val json = Extraction.decompose(box)
  39. println(pretty(json))
  40. // {
  41. // "token" : "d9bd49dc-11b4-4380-ab10-f6df005a384c",
  42. // "id" : "1000",
  43. // "thing" : {
  44. // "name" : "a thing"
  45. // }
  46. // }
  47.  
  48. // and read a value of type Box back from the JSON
  49. println(json.extract[Box])
  50. // Box(1000,Thing(a thing))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement