title: How to add a custom marshaller to akka http?
body: <p>As a beginner to both scala and akka-http, I am trying to hook into the serialization aka marshalling process.</p>
  
  <p>The project uses akka@2.5.2 and akka-http@10.0.10". Furthermore, it has the <code>akka-http-spray-json</code> dependency included.</p>
  
  <p>In the codebase, we use <code>Java.Util.Currency</code> (It may be deprecated which is not important as I'd still like to know how to add a custom marshaller.)</p>
  
  <p>Given this example controller:</p>
  
  <pre><code>def getCurrencyExample: Route = {
      path("currencyExample") {
        val currency: Currency = Currency.getInstance("EUR")
        val code: String = currency.getCurrencyCode
  
        val shouldBeFormated = collection.immutable.HashMap(
          "currencyCode" -&gt; code,
          "currencyObject" -&gt; currency
        )
  
        complete(shouldBeFormated)
      }
    }
  </code></pre>
  
  <p>I get a response like this where the currency object becomes empty:</p>
  
  <pre><code> {
    currencyObject: { },
    currencyCode: "EUR",
   }
  </code></pre>
  
  <p>I expect something like:</p>
  
  <pre><code> {
    currencyObject: "EUR",
    currencyCode: "EUR",
   }
  </code></pre>
  
  <p>The <code>currency</code> object should be transformed into a JSON string. And since I do not want to transform each response manually, I want to hook into marshalling process and have it done in the background.</p>
  
  <p>I want to add a custom marhaller only for <code>Java.Util.Currency</code> objects, yet even <a href="https://doc.akka.io/docs/akka-http/current/common/marshalling.html" rel="noreferrer">reading up on the docs</a> I am very unsure how to proceed. 
   There are multiple approaches described, and I am not sure which fits my need, or where to start.</p>
  
  <hr>
  
  <p>I tried creating my own <code>CurrencyJsonProtocol</code>:</p>
  
  <pre><code>package com.foo.api.marshallers
  
  import java.util.Currency
  
  import spray.json.{DefaultJsonProtocol, JsString, JsValue, RootJsonFormat}
  
  object CurrencyJsonProtocol extends DefaultJsonProtocol {
  
    implicit object CurrencyJsonFormat extends RootJsonFormat[Currency] {
      override def read(json: JsValue): Currency = {
        Currency.getInstance(json.toString)
      }
  
      override def write(obj: Currency): JsValue = {
        JsString(obj.getCurrencyCode)
      }
    }
  
  }
  </code></pre>
  
  <p>yet the mere existence of the file breaks my project:</p>
  
  <pre><code>[error] RouteDefinitons.scala:90:16: type mismatch;
  [error]  found   : scala.collection.immutable.HashMap[String,java.io.Serializable]
  [error]  required: akka.http.scaladsl.marshalling.ToResponseMarshallable
  [error]       complete(shouldBeFormated)
  [error]                ^
  [error] one error found
  [error] (Compile / compileIncremental) Compilation failed
  </code></pre>
  
  <p>and I have no idea why. (It was crashing due to my package name being called <code>marshaller</code>. That completely broke the compilation of the project.</p>