Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix List + Map support

  • Loading branch information...
commit e00bbf05891a8fe46704db59c9aa9f24a044d52b 1 parent 144d4ea
Joni Freeman authored
View
21 core/json/src/main/scala/net/liftweb/json/Extraction.scala
@@ -36,7 +36,7 @@ object Extraction {
*/
def extract[A](json: JValue)(implicit formats: Formats, mf: Manifest[A]): A =
try {
- extract0(json, mf)
+ extract0(json, mf.erasure, mf.typeArguments.map(_.erasure)).asInstanceOf[A]
} catch {
case e: MappingException => throw e
case e: Exception => throw new MappingException("unknown error", e)
@@ -176,14 +176,15 @@ object Extraction {
}
}
- private def extract0[A](json: JValue, mf: Manifest[A])(implicit formats: Formats): A = {
+ private def extract0(json: JValue, clazz: Class[_], typeArgs: Seq[Class[_]])
+ (implicit formats: Formats): Any = {
val mapping =
- if (mf.erasure == classOf[List[_]] || mf.erasure == classOf[Set[_]] || mf.erasure == classOf[Array[_]])
- Col(mf.erasure, mappingOf(mf.typeArguments(0).erasure))
- else if (mf.erasure == classOf[Map[_, _]])
- Dict(mappingOf(mf.typeArguments(1).erasure))
- else mappingOf(mf.erasure)
- extract0(json, mapping).asInstanceOf[A]
+ if (clazz == classOf[List[_]] || clazz == classOf[Set[_]] || clazz == classOf[Array[_]])
+ Col(clazz, mappingOf(typeArgs(0)))
+ else if (clazz == classOf[Map[_, _]])
+ Dict(mappingOf(typeArgs(1)))
+ else mappingOf(clazz)
+ extract0(json, mapping)
}
def extract(json: JValue, target: TypeInfo)(implicit formats: Formats): Any =
@@ -218,7 +219,9 @@ object Extraction {
fieldsToSet.foreach { case (name, typeInfo) =>
jsonFields.get(name).foreach { case (n, v) =>
- val value = extract(v, typeInfo)
+ val typeArgs = typeInfo.parameterizedType
+ .map(_.getActualTypeArguments.map(_.asInstanceOf[Class[_]]).toList)
+ val value = extract0(v, typeInfo.clazz, typeArgs.getOrElse(Nil))
Reflection.setField(a, n, value)
}
}
View
9 core/json/src/test/scala/net/liftweb/json/FieldSerializerExamples.scala
@@ -34,7 +34,7 @@ object FieldSerializerExamples extends Specification {
dog2.name mustEqual dog.name
dog2.color mustEqual dog.color
dog2.owner mustEqual dog.owner
-// dog2.size mustEqual dog.size
+ dog2.size mustEqual dog.size
}
"Fields can be ignored and renamed" in {
@@ -50,16 +50,15 @@ object FieldSerializerExamples extends Specification {
dog2.name mustEqual dog.name
dog2.color mustEqual dog.color
dog2.owner must beNull
-// dog2.size mustEqual dog.size
-// val size = parse(ser) \ "animalname"
-// size mustEqual JArray(List(JInt(10), JInt(15)))
+ dog2.size mustEqual dog.size
+ (parse(ser) \ "animalname") mustEqual JString("pluto")
}
}
abstract class Mammal {
var name: String = ""
var owner: Owner = null
-// val size = List(10, 15)
+ val size = List(10, 15)
}
class WildDog(val color: String) extends Mammal {
Please sign in to comment.
Something went wrong with that request. Please try again.