Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Play has a lot of new features for JSON right now. This would be a nice showcase for the <code>Format[A]</code> trait (see <a href="http://www.playframework.com/documentation/2.1.x/ScalaJsonInception">Scala Json Inception</a>) which you could include implicitly as I will show, or explicitly to the methods that require an implicit <code>Format[A]/Reads[A]/Writes[A]</code>.</p> <p>Create a case class to represent your JSON objects,</p> <pre><code>case class Book(id: Int, name: String) case class BookDetail(id: Int, author: String, publicationDate: Int, pages: Int) </code></pre> <p>Create companion objects that contain the implicit <code>Format[A]</code> so that <code>Format/Reads/Writes</code> will automatically be in scope when you need them.</p> <pre><code>object Book { implicit val fmt: Format[Book] = Json.format[Book] } object BookDetail { implicit val fmt: Format[BookDetail] = Json.format[BookDetail] } </code></pre> <p>Now you could do something like this,</p> <pre><code>val bookJson = Json.toJson(Book(1, "A Brief History Of Time")) val bookDetailJson = Json.toJson(BookDetail(1, "Steven Hawking", 1988, 256)) bookJson.as[JsObject].deepMerge(bookDetailJson.as[JsObject]) </code></pre> <p>And you will have an object like this,</p> <pre><code>{ id: 1, name: "A Brief History Of Time", author: "Steven Hawking", publicationDate: 1988, pages: 256 } </code></pre> <p>I've tried this in the REPL but it does not work, in a Play application it does just fine though. Also in a production scenario we would likely use <code>asOpt[T]</code> in place of <code>as[T]</code>. </p> <p>Here is an example of why <code>asOpt[T]</code> may be better suited, suppose instead of a valid JSON object for book you get,</p> <pre><code>val bookJson = Json.toJson("not a book") </code></pre> <p>You will end up with a </p> <pre><code>[JsResultException: JsResultException(errors:List((,List(ValidationError(validate.error.expected.jsobject,WrappedArray())))))] </code></pre> <p>But suppose instead you change your method to use <code>asOpt[T]</code>,</p> <pre><code>bookJson.asOpt[JsObject].getOrElse(Json.obj()).deepMerge(bookDetailJson.asOpt[JsObject].getOrElse(Json.obj())) </code></pre> <p>Now you will end up with at least a partial JSON object,</p> <pre><code>{ id: 1, author: "Steven Hawking", publicationDate: 1988, pages: 256 } </code></pre> <p>So depending on how you would like to handle improperly formatted JSON you could choose either option.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload