开发者

Salat not deserializing collections that map to Option[Set[A]]

开发者 https://www.devze.com 2023-04-05 09:43 出处:网络
I asked this question in scala-salat Google group, but no response there for a few days, hence trying here.

I asked this question in scala-salat Google group, but no response there for a few days, hence trying here.

I have a case class which has a collection wrapped in an Option.

case class SomeClass(
  var name:Option[String]=None,
  var addresses:Option[Set[Address]]=None
)

case class Address(
  var street:Option[String]=None,
  var zip:Option[String]=None
) 

The document gets stored in Mongo just fine. However, when I try to get 开发者_如何学编程this using findOne (with or without SalatDAO) the addresses field has JSON text representing addresses instead of the Address case class.

As a result, Lift-JSON skips any fields in my case class with Option[Collection[A]] in it. When I remove the Option and just go with addresses=Set[Address](), it works. However now I end up with empty sets in my JSON where there is no data, and I have no way to find out on an update if the consumer removed the entries, or I am getting an empty Set because the consumer did not send this field in the original request.

Using Option would have set the field to None, which would mean that I would not have to worry about it being empty or modified.

Any help will be greatly appreciated.


Sorry, Salat does not currently support an Option that contains a collection.


Now Salat offers JSON functionality backed by lift-json, so you can avoid this Option[Set[A]] mess in the first place.

Salat's JSON transformation supports default args, all the Salat annotations, and custom date and ObjectId serialization.

See https://github.com/novus/salat/wiki/JSON

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号