Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Skip constructor args

  • Loading branch information...
commit 144d4ea8682c379918c2a227f252a48bedd23092 1 parent a3581ee
Joni Freeman authored
Showing with 8 additions and 3 deletions.
  1. +8 −3 core/json/src/main/scala/net/liftweb/json/Extraction.scala
View
11 core/json/src/main/scala/net/liftweb/json/Extraction.scala
@@ -203,15 +203,20 @@ object Extraction {
}
}
- def setFields(a: AnyRef, json: JValue) = json match {
+ def setFields(a: AnyRef, json: JValue, constructor: JConstructor[_]) = json match {
case o: JObject =>
formats.fieldSerializer(a.getClass).map { serializer =>
+ val constructorArgNames =
+ Reflection.constructorArgs(constructor, formats.parameterNameReader).map(_._1).toSet
val jsonFields = o.obj.map { f =>
val JField(n, v) = (serializer.deserializer orElse Map(f -> f))(f)
(n, (n, v))
}.toMap
- Reflection.fields(a.getClass).foreach { case (name, typeInfo) =>
+ val fieldsToSet =
+ Reflection.fields(a.getClass).filterNot(f => constructorArgNames.contains(f._1))
+
+ fieldsToSet.foreach { case (name, typeInfo) =>
jsonFields.get(name).foreach { case (n, v) =>
val value = extract(v, typeInfo)
Reflection.setField(a, n, value)
@@ -231,7 +236,7 @@ object Extraction {
fail("No information known about type")
val instance = jconstructor.newInstance(args.map(_.asInstanceOf[AnyRef]).toArray: _*)
- setFields(instance.asInstanceOf[AnyRef], json)
+ setFields(instance.asInstanceOf[AnyRef], json, jconstructor)
} catch {
case e @ (_:IllegalArgumentException | _:InstantiationException) =>
fail("Parsed JSON values do not match with class constructor\nargs=" +
Please sign in to comment.
Something went wrong with that request. Please try again.