简体   繁体   中英

How to customize Json serialization using Scala and Play Framework?

I would like to serialize some Scala case classes to Json. Eg my case class looks like:

case class Item (
    id: Int, name: String, price: BigDecimal, created: java.util.Date)

and I would like to serialize it to Json like this:

{"id":3, "name": "apple", "price": 8.00, "created": "123424434"}

so I need a custom serilization for BigDecimal and for Date . Where I want the data in milliseconds since 1 jan 1970.

When using Scala and Play Framework, I can return Json using Json(myObject) , but how do I customize the serialization? Or is there any recommended Scala library?

For advanced json play-scala recommends using external json libraries, like lift-json .

Also there are some related topics here, you may want to have a look at them. I believe this one Customizing JSON serialization in Play has the answer to your question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM