Guest User

Untitled

a guest
Feb 19th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. package object json {
  2.  
  3. implicit def stringToJString(s: String) = JString(s)
  4.  
  5. implicit def doubleToJNumber(n: Double) = JNumber(n)
  6.  
  7. implicit def booleanToJBoolean(b: Boolean) = JBoolean(b)
  8.  
  9. implicit def stringJValueAToJStringJValueA[A](t: (String, JValue[A])) = t match {
  10. case (s, v) => JString(s) -> v
  11. }
  12.  
  13. implicit def stringAToJStringJValueA[A](t: (String, A))(implicit f: A => JValue[A]) = t match {
  14. case (s, v) => JString(s) -> f(v)
  15. }
  16.  
  17. lazy val anyToJValue: PartialFunction[Any, JValue[_]] = {
  18. case m: Map[_, _] => mapToJObject(m)
  19. case l: List[_] => listToJArray(l)
  20. case d: Double => JNumber(d)
  21. case b: Boolean => JBoolean(b)
  22. case s: String => JString(s)
  23. case _ @ a => if (a == null) JNull else throw new MatchError
  24. }
  25.  
  26. implicit def listToJArray(l: List[_]) = JArray(l.map(anyToJValue): _*)
  27.  
  28. implicit def mapToJObject(m: Map[_, _]) = JObject(m.toSeq.map {
  29. case (s: String, a) => JString(s) -> anyToJValue(a)
  30. }: _*)
  31.  
  32. def parse(input: String) = JSON.parseFull(input).map {
  33. case m: Map[_, _] => mapToJObject(m)
  34. }
  35.  
  36. }
Add Comment
Please, Sign In to add comment