Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix parameter parsing in scala 2.9

  • Loading branch information...
commit a27e7c48afb01deb0f92e68bc97a014aa5df7d72 1 parent 3a110ba
Joni Freeman authored
5 core/json/src/main/scala/net/liftweb/json/ScalaSig.scala
View
@@ -33,7 +33,7 @@ private[json] object ScalaSigReader {
else
findField(findClass(current), name).getOrElse(read(current.getSuperclass))
}
- findArgTypeF(read(clazz), typeArgIndex)
+ findArgTypeForField(read(clazz), typeArgIndex)
}
private def findClass(clazz: Class[_]): ClassSymbol = {
@@ -64,6 +64,7 @@ private[json] object ScalaSigReader {
def findPrimitive(t: Type): Symbol = t match {
case TypeRefType(ThisType(_), symbol, _) if isPrimitive(symbol) => symbol
case TypeRefType(_, _, TypeRefType(ThisType(_), symbol, _) :: xs) => symbol
+ case TypeRefType(_, symbol, Nil) => symbol
case TypeRefType(_, _, args) =>
args(typeArgIndex) match {
case ref @ TypeRefType(_, _, _) => findPrimitive(ref)
@@ -74,7 +75,7 @@ private[json] object ScalaSigReader {
toClass(findPrimitive(s.children(argIdx).asInstanceOf[SymbolInfoSymbol].infoType))
}
- private def findArgTypeF(s: MethodSymbol, typeArgIdx: Int): Class[_] = {
+ private def findArgTypeForField(s: MethodSymbol, typeArgIdx: Int): Class[_] = {
// FIXME can be removed when 2.8 no longer needs to be supported.
// 2.8 does not have NullaryMethodType, work around that.
/*
30 core/json/src/test/scala/net/liftweb/json/SerializationBugs.scala
View
@@ -131,6 +131,30 @@ object SerializationBugs extends Specification {
val s = Serialization.write(x)
read[Eith](s) mustEqual x
}
+
+ "Custom serializer should work as Map key (scala 2.9) (issue #1077)" in {
+ class SingleOrVectorSerializer extends Serializer[SingleOrVector[Double]] {
+ private val singleOrVectorClass = classOf[SingleOrVector[Double]]
+
+ def deserialize(implicit format: Formats) = {
+ case (TypeInfo(`singleOrVectorClass`, _), json) => json match {
+ case JObject(List(JField("val", JDouble(x)))) => SingleValue(x)
+ case JObject(List(JField("val", JArray(xs: List[JDouble])))) => VectorValue(xs.map(_.num).toIndexedSeq)
+ case x => throw new MappingException("Can't convert " + x + " to SingleOrVector")
+ }
+ }
+
+ def serialize(implicit format: Formats) = {
+ case SingleValue(x: Double) => JObject(List(JField("val", JDouble(x))))
+ case VectorValue(x: Vector[Double]) => JObject(List(JField("val", JArray(x.toList.map(JDouble(_))))))
+ }
+ }
+
+ implicit val formats = DefaultFormats + new SingleOrVectorSerializer
+
+ val ser = swrite(MapHolder(Map("hello" -> SingleValue(2.0))))
+ read[MapHolder](ser) mustEqual MapHolder(Map("hello" -> SingleValue(2.0)))
+ }
}
case class Eith(x: Either[String, Int])
@@ -160,3 +184,9 @@ package plan2 {
}
case class Opaque(x: JValue)
+
+sealed trait SingleOrVector[A]
+case class SingleValue[A](value: A) extends SingleOrVector[A]
+case class VectorValue[A](value: IndexedSeq[A]) extends SingleOrVector[A]
+
+case class MapHolder(a: Map[String, SingleOrVector[Double]])
Please sign in to comment.
Something went wrong with that request. Please try again.