Warning: Trying to access array offset on value of type bool in /home/www/blog/wp-content/themes/catch-box/functions.php on line 1079

JSONContextResolver

Playing around with CouchDB (which is a great JSON document based database with REST access), Jersey (JAX-RS/JSR-311 implementation) and Scala I had to implement a custom ContextResolver to use JSON objects instead of XML.

The Context Resolver Interface in Java is:

package javax.ws.rs.ext;
public interface ContextResolver<T>   {
    T getContext(java.lang.Class<?> aClass);
}

I needed some time to get to the following Scala code implementing the Interface

(so I am still a newbie ;-)):

@Provider
class JSONContextResolver extends ContextResolver[JAXBContext] {
  val cTypes = Array(classOf[CUnit], classOf[UpdateResponse], classOf[DeleteResponse])

  val context = new JSONJAXBContext(JSONConfiguration.natural().build(), cTypes: _*)

  val types:Set[Class[_]] = Set(cTypes: _*)

  def getContext(objectType: Class[_]): JAXBContext = {
    if (types.contains(objectType))
      return context
    null
  }
}