Last active
December 21, 2015 15:08
-
-
Save rrmckinley/6324098 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* | |
- type mismatch; found : com.tagged.vor.toJSON.type required: ?{def apply(x$1: ? >: | |
A): ?} Note that implicit conversions are not applicable because they are ambiguous: | |
both method inst1 in trait PolyInst of type [A](fn: shapeless.Poly)(implicit cse: | |
fn.ProductCase[shapeless.::[A,shapeless.HNil]])A => cse.Result and macro method apply | |
in object Poly of type (f: Any)shapeless.Poly are possible conversion functions from | |
com.tagged.vor.toJSON.type to ?{def apply(x$1: ? >: A): ?} | |
- Unable to convert expression Expr[Nothing](toJSON) to a polymorphic function | |
value | |
*/ | |
import org.json4s._ | |
import org.json4s.native.JsonMethods._ | |
import shapeless._ | |
import poly._ | |
import ops.hlist._ | |
object Test { | |
def main(args: Array[String]) { | |
println(JSON.pretty("foo")) | |
// could not find implicit value for parameter st: | |
// com.tagged.vor.toJSON.Case.Aux[String,org.json4s.JValue] | |
} | |
} | |
object JSON { | |
def compact[A](a: A)(implicit st: toJSON.Case.Aux[A, JValue]): String = | |
org.json4s.native.JsonMethods.compact(render(toJSON(a))) | |
def pretty[A](a: A)(implicit st: toJSON.Case.Aux[A, JValue]): String = | |
org.json4s.native.JsonMethods.pretty(render(toJSON(a))) | |
} | |
object toJSON extends Poly1 { // Pullback1[JValue] { | |
implicit def nullToJSON = at[Null](_ => JNull) | |
implicit def doubleToJSON = at[Double](JDouble(_)) | |
implicit def bigIntToJSON = at[BigInt](JInt(_)) | |
implicit def bigDecimalToJSON = at[BigDecimal](JDecimal(_)) | |
implicit def numToJSON[V <% Long] = at[V](i => JInt(BigInt(i))) | |
implicit def stringToJSON = at[String](s => if (s == null) JNull else JString(s)) | |
implicit def boolToJSON = at[Boolean](JBool(_)) | |
implicit def jsonToJSON[V <: JValue] = at[V](identity) | |
implicit def dateToJSON[V <: java.util.Date](implicit f: Formats) = | |
at[V](s => if (s == null) JNull else JString(f.dateFormat.format(s))) | |
implicit def traversableToJSON[V, C[V] <: Traversable[V]](implicit st: Case.Aux[V, JValue]) = | |
at[C[V]](l => JArray(l.toList.map(v => toJSON(v)))) | |
implicit def recordToJSON[R <: HList](implicit foldMap: MapFolder[R, List[JField], fieldToJSON.type]) = { | |
at[R](r => JObject(r.foldMap(Nil: List[JField])(fieldToJSON)(_ ::: _))) | |
} | |
} | |
object fieldToJSON extends Poly1 { | |
implicit def value[K, V: toJSON.Case] = at[(K, V)] { | |
case (k, v) => (keyAsString(k), toJSON(v)) :: Nil | |
} | |
implicit def option[K, V: toJSON.Case] = at[(K, Option[V])] { | |
case (k, Some(v)) => (keyAsString(k), toJSON(v)) :: Nil | |
case (k, None) => Nil | |
} | |
def keyAsString(k: Any): String = { | |
def isNumeric(s: String) = s.toCharArray.forall(Character.isDigit) | |
if (k.getClass == classOf[String]) k.toString | |
else { | |
val parts = k.getClass.getName.split("\\$") | |
parts.reverse.dropWhile(isNumeric).head | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment