title: Play & Json: How to validate an array of integers
body: <p>The following validator checks whether or not incoming JSON is correct:</p>
  
  <pre><code>val validateAccount = (
    ((__ \ 'id).json.pickBranch(Reads.of[JsString] &lt;~ objectId) orEmpty) ~
    ((__ \ 'openingTime).json.pickBranch(Reads.of[JsString] &lt;~ utcDateTime) orEmpty) ~
    ((__ \ 'roles).json.pickBranch(Reads.of[JsArray]) orEmpty) ~
    ((__ \ 'permissions).json.pickBranch(Reads.of[JsArray]) orEmpty)
  ).reduce
  </code></pre>
  
  <p><code>roles</code> as well as <code>permissions</code> should be an array of integers... so how do I verify <code>JsArray</code> effectively wraps an array of integers?</p>
  
  <p><strong>EDIT</strong>
  As requested by Travis, here below is the implementation of <code>orEmpty</code>...</p>
  
  <pre><code>implicit class ReadsExtensions(val reads: Reads[JsObject]) extends AnyVal {
  
    import play.api.libs.functional.syntax._
  
    /**
      * Returns either a successful `Reads` or an empty object.
      * @return Either a successful `Reads` or an empty object.
      */
    def orEmpty = reads | __.json.put(Json.obj())
  
    /**
      * Returns either a successful `Reads` or an empty object if allowed.
      *
      * @param b  A Boolean value indicating  whether or not an empty
      *           object is allowed when a `Reads` fails.
      * @return   Either a successful `Reads` or an empty object if `b`
      *           is `true`.
      */
    def orEmptyIf(b: Boolean) = if (b) orEmpty else reads
  }
  </code></pre>
  
  <p>... and here an example of JSON to be validated:</p>
  
  <pre><code>{
    "id": "12d54f56cc456a2967e34a21",
    "openingTime": "2014-08-12T21:10:24Z",
    "roles": [ 0, 1, 3, 4],
    "permissions": [ 0, 1, 2 ,3 ]
  }
  </code></pre>