Skip to content

Instantly share code, notes, and snippets.

@boh717
Created February 24, 2020 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boh717/e428500c3f7ce54bf33b518b328a3208 to your computer and use it in GitHub Desktop.
Save boh717/e428500c3f7ce54bf33b518b328a3208 to your computer and use it in GitHub Desktop.
Akka http (de)serializer for Map[String, Any]
implicit object AnyJsonFormat extends JsonFormat[Any] {
def write(x: Any) = x match {
case n: Int => JsNumber(n)
case s: String => JsString(s)
case x: Seq[_] => seqFormat[Any].write(x)
case m: Map[String, _] @unchecked => mapFormat[String, Any].write(m)
case b: Boolean if b => JsTrue
case b: Boolean if !b => JsFalse
case x =>
serializationError(
"Can't serialize object of type " + x.getClass.getName
)
}
def read(value: JsValue): Any = value match {
case JsNumber(n) => n.intValue()
case JsString(s) => s
case _: JsArray => listFormat[Any].read(value)
case _: JsObject => mapFormat[String, Any].read(value)
case JsTrue => true
case JsFalse => false
case x =>
deserializationError("Can't deserialize " + x)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment