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

Inconsistent behavior when deserializing null #50

Open
jdanbrown opened this issue Mar 27, 2013 · 3 comments
Open

Inconsistent behavior when deserializing null #50

jdanbrown opened this issue Mar 27, 2013 · 3 comments

Comments

@jdanbrown
Copy link
Contributor

Over the last few months using spray-json I had learned the following (incorrect) rule:

null is allowed iff your type is of the form Option[X]

Today I unlearned this rule when I converted a null into Double.NaN (#8) instead triggering an exception, which is what I was expecting to happen. Trying out a bunch of standard formats, I observe these behaviors when deserializing from null:

JsNull.convertTo[Boolean]          -> error
JsNull.convertTo[Int]              -> error
JsNull.convertTo[Long]             -> error
JsNull.convertTo[Char]             -> error
JsNull.convertTo[String]           -> error
JsNull.convertTo[Seq[Int]]         -> error
JsNull.convertTo[Map[String, Int]] -> error
JsNull.convertTo[(Int, String)]    -> error

JsNull.convertTo[Float]            -> Float.NaN
JsNull.convertTo[Double]           -> Double.NaN
JsNull.convertTo[Unit]             -> ()

JsNull.convertTo[Option[Int]]      -> None

I think the library's behavior would be less surprising and thus easier to use if the rule above did hold. To that end, I propose:

  • Exposing an option for null <-> Float.NaN/Double.Nan, disabled by default
  • Rejecting null -> () for Unit

Thoughts? Are there more cases that I'm missing? Is the issue more subtle than this?

@sirthias
Copy link
Member

Dan,
thanks for this analysis. You definitely have a point here.
I think we should disallow null for anything but Options.
We'll discuss it on our side and come up with a solution.
Thanks again for taking the time to analyse and report!

@jrudolph jrudolph added the Bug label Mar 7, 2014
@jrudolph jrudolph added this to the Next bugfix release milestone Mar 7, 2014
@lwei-mdsol
Copy link

Any updates on this?

@kaarelnu
Copy link

Also, Some(Double.NaN).toJson.convertTo[Option[Double]] yields None.

@ktoso ktoso modified the milestones: Next bugfix release, Next next Oct 24, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants