Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package object json {
- implicit def stringToJString(s: String) = JString(s)
- implicit def doubleToJNumber(n: Double) = JNumber(n)
- implicit def booleanToJBoolean(b: Boolean) = JBoolean(b)
- implicit def stringJValueAToJStringJValueA[A](t: (String, JValue[A])) = t match {
- case (s, v) => JString(s) -> v
- }
- implicit def stringAToJStringJValueA[A](t: (String, A))(implicit f: A => JValue[A]) = t match {
- case (s, v) => JString(s) -> f(v)
- }
- lazy val anyToJValue: PartialFunction[Any, JValue[_]] = {
- case m: Map[_, _] => mapToJObject(m)
- case l: List[_] => listToJArray(l)
- case d: Double => JNumber(d)
- case b: Boolean => JBoolean(b)
- case s: String => JString(s)
- case _ @ a => if (a == null) JNull else throw new MatchError
- }
- implicit def listToJArray(l: List[_]) = JArray(l.map(anyToJValue): _*)
- implicit def mapToJObject(m: Map[_, _]) = JObject(m.toSeq.map {
- case (s: String, a) => JString(s) -> anyToJValue(a)
- }: _*)
- def parse(input: String) = JSON.parseFull(input).map {
- case m: Map[_, _] => mapToJObject(m)
- }
- }
Add Comment
Please, Sign In to add comment