Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

on deserialization Option[Long] is converted to Option[BigDecimal] #27

Closed
stephanos opened this issue May 19, 2011 · 4 comments
Closed

Comments

@stephanos
Copy link

When I deserialize an object that has a Option[Long] field (or [int]) the resulting object suddenly has a Option[BigDecimal] field - even when I specify the correct @OptionTypeHint!

Is this a bug or did I do something wrong?

Example:

import org.specs2.mutable.Specification
import reflect.BeanInfo
import sjson.json._
import annotation.target._

@BeanInfo
case class Person(name: String,
                  @(OptionTypeHint@field)(value = classOf[Long]) age: Option[Long]) {

    private def this() = this ("", None)
}

class Test extends Specification {

    "SJSON" should {

        "deserialize Option[Long] fields" >> {
            // initialize
            val age = Some(12L)
            val p = new Person("Daniel", age)

            // serialize and deserialize
            val json = Serializer.SJSON.out(p)
            val obj = Serializer.SJSON.in[Person](json).asInstanceOf[Person]

            // check result
            obj.age match {
                case b: Option[BigDecimal] => // THROWN
                    throw new RuntimeException("deserialization resulted in Option[BigDecimal]") 
                case l: Option[Long] => println("works")
            }
            obj.age must beEqualTo(age)
        }
    }
}
@debasishg
Copy link
Owner

With the latest 0.12 release, the following works ..

describe("Serialization of Long in a bean") {
it("should serialize") {
val m = Person("alexander", Some(27l))
serializer.inPerson should equal(m)
}
}

You don't need the cast as in your example and since a valid instance of Person is obtained, it's always a long that I get back. And in your example, the pattern match erases the type of Option[] - hence I think it's always the first one that matches. What do you get if you reverse case b: and case l: in position ?

Thanks.

@stephanos
Copy link
Author

Well I couldn't come up with a better way to show this then pattern matching (reversing says "unreachable code").

In my real application I get an exception though, and my debugger shows my that it's really Option[BigDecimal] !
Here is a screenshot: http://imageshack.us/photo/my-images/199/capturehid.png/

Hm, what is the best way to check the runtime type? My skills are not that good yet.

PS: Using Scala 2.9.0 & sjson 0.12

@debasishg
Copy link
Owner

Let me send you another jar, which you can try and see if it solves your problem. Can u pls send me your email address where I can send it .. Thanks ..

@debasishg
Copy link
Owner

As confirmed in the mail, the new jar works. Fixed in the master. Will push 0.13 soon.

debasishg pushed a commit that referenced this issue Jul 1, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants