json4s allows user to convert a JsonAST object to a case class using extract
.
import org.json4s._
import org.json4s.jackson.JsonMethods._
implicit val formats = DefaultFormats
case class Item(name: String, price: Double)
val json = parse("""{"name": "phone", "price": 1000.0}""") // JObject(List((name,JString(phone)), (price,JDouble(1000.0))))
val item = json.extract[Item] // Item(phone,1000.0)
However, to convert a case class into a JsonAST object, the only way I can think of is:
write
extract
Like below:
parse(write(item)) // JObject(List((name,JString(phone)), (price,JDouble(1000.0))))
Is there any better way for the conversion? Thank you!
Extraction.decompose
converts a case class object into a JsonAST.
Extraction.decompose(item) // JObject(List((name,JString(phone)), (price,JDouble(1000.0))))