rapture-json 1.0.8 and Option

39 views
Skip to first unread message

Andrew Richards

unread,
Nov 24, 2014, 4:33:42 PM11/24/14
to raptur...@googlegroups.com
Hi, 

Would you expect the following to work? (Ie using Option types) From From the docs it would appear so but this fails:

import rapture.json.jsonBackends.play._
import rapture.json._
...

  case class Test(i: Option[Int])  
  val t = Test(Some(3))  
  val j = Json(t)

Given that other type constructors like List work I'd assume option would too. Maybe this is related to issue #23?

Thanks,

Andrew

Jon Pretty

unread,
Nov 25, 2014, 1:40:08 AM11/25/14
to raptur...@googlegroups.com
Hi Andrew,

That's a complicated case, actually. It's fine to serialize `Some(3)` to `3`, but what should `None` serialize to?

I think the two reasonable possibilities are `null` or simply not including that JSON object key or array value in the output. I think the latter would be more consistent with the extraction case (i.e. if you extract an `Option`, a missing value will extract to `None`), though unfortunately this doesn't generalize: for example, we can serialize `Json(Test(None))` to "{}", but how would be serialize `Json(None)`? It's not an empty object or array... it's just "nothing".

I wonder if the best solution would be to special-case `Option` types in case classes, and forbid them elsewhere. I'll look into that as a possibility for the next release.

https://github.com/propensive/rapture-json/issues/26

Cheers,
Jon

--
You received this message because you are subscribed to the Google Groups "Rapture users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rapture-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Jon Pretty | @propensive
Reply all
Reply to author
Forward
0 new messages