Created
February 24, 2020 16:38
-
-
Save boh717/e428500c3f7ce54bf33b518b328a3208 to your computer and use it in GitHub Desktop.
Akka http (de)serializer for Map[String, Any]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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