title: how to serialize Scala Array of types to JSON array in HTTP response using Spray?
body: <p>I am a Spray newby, somewhat proficient in Scala, and although the Spray documentation is overall very good, I can't find any relevant example for my problem.  I am trying to do something very simple:  serialize (or Marshall) a Scala Array of my type to a json array, using either json4s or spray-json.  In this example I use spray-json, with the following entry in build.sbt:</p>
  
  <pre><code>libraryDependencies ++= Seq(
    "io.spray" %%  "spray-json" % "1.3.2"
  )
  </code></pre>
  
  <p>The code below produces the following compiler error:</p>
  
  <pre><code>[error] /Users/scott/integration/src/main/scala/com/anomaly/rest/AnomalyService.scala:101: type mismatch;
  [error]  found   : Array[com.anomaly.model.KDEOutlier]
  [error]  required: spray.httpx.marshalling.ToResponseMarshallable
  [error]             complete(outlierService.getOutliersOverTime(startTime, endTime))
  
  /* request handling actor and service */
  
  package com.anomaly.rest
  
  import akka.actor.Actor
  import akka.event.slf4j.SLF4JLogging
  import spray.routing._
  import spray.http._
  import spray.json._
  import DefaultJsonProtocol._
  import MediaTypes._
  
  import anomaly.model.KDEOutlier
  import anomaly.dal.KDEOutliersDAO
  
  class AnomalyServiceActor extends Actor with AnomalyService {
    def actorRefFactory = context
    def receive = runRoute(anomaliesRoute)
  }
  
  trait AnomalyService extends HttpService with SLF4JLogging {
    val outlierService = KDEOutliersDAO
    val anomaliesRoute = respondWithMediaType(MediaTypes.`application/json`) {
      path("anomalies/overtime ") {
        get {
          parameters('startTime.as[Long], 'endTime.as[Long]) { (startTime, endTime) =&gt;
              complete(outlierService.getOutliersOverTime(startTime, endTime))
          }
        }
      }
    }
  }
  
  
  /* model */
  
  package com.anomaly.model
  import java.util.Date
  
  case class KDEOutlier(date: Long, score: Double)
  
  
  /* dao service called by request handler service */
  
  package com.anomaly.dal
  
  import java.util.Date
  import com.anomaly.model.KDEOutlier
  import com.anomaly.model.KDERawOutlier
  
  object KDEOutliersDAO {  
      def getOutliersOverTime(startTime: Long, endTime: Long): Array[KDEOutlier] = {
          Array(KDEOutlier(Date.UTC(2013,5,2,0,0,0),0.7695), KDEOutlier(Date.UTC(2013,5,3,0,0,0),0.7648),
                  KDEOutlier(Date.UTC(3,5,4,0,0,0),0.7645), KDEOutlier(Date.UTC(2013,5,5,0,0,0),0.7638),
                  KDEOutlier(Date.UTC(2013,5,6,0,0,0),0.8549), KDEOutlier(Date.UTC(2013,5,7,0,0,0),0.9562),
                  KDEOutlier(Date.UTC(2013,5,9,0,0,0),0.7574), KDEOutlier(Date.UTC(2013,5,10,0,0,0),0.7543))
      }
  }
  </code></pre>