Re: JSON de-serialization: constructor memoization ignores type parameters

238 views
Skip to first unread message

Joni Freeman

unread,
Mar 11, 2013, 11:24:11 AM3/11/13
to lif...@googlegroups.com
Hi,

Looks like a bug and your diagnosis is correct. Could you please file an issue and we'll get this fixed.

Cheers Joni

On Friday, March 8, 2013 9:56:51 PM UTC+2, Justin Mullin wrote:

Was about to post this as an issue on GitHub, but I saw the note encouraging discussion here before opening issues, so here we are.  =)

My understanding of the problem is this: when de-serializing JSON, the memoization logic which maps classes to constructors in net.liftweb.json.Meta ignores type parameters. If a parameterized type is used multiple times during extraction, subsequent lookups for the same (erased) type will return the original constructor, even if different parameters were provided. The following code highlights the issue - the second call to 'read' tries to construct an instance of TypeA instead of TypeB, and throws an exception when it can't find the field 'foo':

import net.liftweb.json.Serialization.read
import net.liftweb.json.DefaultFormats

object JsonParseTest extends App {
  implicit val formats = DefaultFormats

  val jsonA = """ { "data": { "foo": "string" }, "success": true } """
  println(read[Container[TypeA]](jsonA))
  val jsonB = """ { "data": { "bar": "string" }, "success": true } """
  println(read[Container[TypeB]](jsonB))
}

case class TypeA(foo: String)
case class TypeB(bar: String)
case class Container[D](data: D)

And the output:

Container(TypeA(string))
Exception in thread "main" net.liftweb.json.MappingException: No usable value for data
No usable value for foo

Joni Freeman

unread,
Mar 12, 2013, 3:04:20 AM3/12/13
to lif...@googlegroups.com
Hi,

Fixed in master now. Thanks for reporting!

Cheers Joni

On Monday, March 11, 2013 5:47:06 PM UTC+2, Justin Mullin wrote:
Issue created: lift/framework/issues/1417.  Thanks Joni.

- Justin
Reply all
Reply to author
Forward
0 new messages