Skip to content

Commit

Permalink
Convenience methods for polymorphic JSON construction
Browse files Browse the repository at this point in the history
  • Loading branch information
bmjames committed Nov 13, 2013
1 parent 547db57 commit 7640b88
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion core/src/main/scala/com/gu/json/JsonLike.scala
Expand Up @@ -7,7 +7,7 @@ trait JsonLike[J] {
def array(elems: Seq[J]): J
def obj(fields: Seq[(String, J)]): J
def string(s: String): J
def int(s: BigInt): J
def int(i: BigInt): J
def double(d: Double): J
def bool(b: Boolean): J

Expand All @@ -19,3 +19,22 @@ trait JsonLike[J] {
def asBool(j: J): Option[Boolean]

}

object JsonLike {
def apply[J](implicit ev: JsonLike[J]): JsonLike[J] = ev

def nothing[J : JsonLike]: J = JsonLike[J].nothing
def array[J : JsonLike](elems: Seq[J]): J = JsonLike[J].array(elems)
def obj[J : JsonLike](fields: Seq[(String, J)]): J = JsonLike[J].obj(fields)
def string[J : JsonLike](s: String): J = JsonLike[J].string(s)
def int[J : JsonLike](i: BigInt): J = JsonLike[J].int(i)
def double[J : JsonLike](d: Double): J = JsonLike[J].double(d)
def bool[J : JsonLike](b: Boolean): J = JsonLike[J].bool(b)

def asArray[J : JsonLike](j: J): Option[List[J]] = JsonLike[J].asArray(j)
def asObj[J : JsonLike](j: J): Option[List[(String, J)]] = JsonLike[J].asObj(j)
def asString[J : JsonLike](j: J): Option[String] = JsonLike[J].asString(j)
def asInt[J : JsonLike](j: J): Option[BigInt] = JsonLike[J].asInt(j)
def asDouble[J : JsonLike](j: J): Option[Double] = JsonLike[J].asDouble(j)
def asBool[J : JsonLike](j: J): Option[Boolean] = JsonLike[J].asBool(j)
}

0 comments on commit 7640b88

Please sign in to comment.