Advertisement
NLinker

Serialize to Json using jackson ObjectMapper

Jun 22nd, 2015
417
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.49 KB | None | 0 0
  1. object Json {
  2.   val mapper = new ObjectMapper() with ScalaObjectMapper
  3.   mapper.enable(SerializationFeature.INDENT_OUTPUT)
  4.   mapper.registerModule(DefaultScalaModule)
  5.  
  6.   implicit class JsonOps[T](obj : T) extends scala.AnyRef {
  7.     def toJson: String = mapper.writeValueAsString(obj)
  8.   }
  9. }
  10.  
  11. case class Person(name: String, age: Int)
  12.  
  13. object Example {
  14.   import Json._
  15.  
  16.   def run = {
  17.     val x = Person("Frol", 123).toJson
  18.     println(x) // outputs {"name":"Frol","age":123}
  19.   }
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement