在某些情况下,对于案例类,默认值比可选参数有意义:
In some cases default values make more sense than optionals in case classes:
case class Car(numberOfWheels:Int = 4, color:String) case class Car(numbeOfWheels:Option[Int], color:String) //silly在第一种情况下,我希望能够轻松地将以下json转换为实例:
In the first case I'd expect to be able to easily convert the following json to an instance:
{"color":"red"}但是对于标准的jsonFormat2(Car),spray-json抱怨numberOfWheels的值缺失.
But with a standard jsonFormat2(Car), spray-json complains about missing value for numberOfWheels.
我如何最干净地解决这个问题?
How do I work around this most cleanly?
推荐答案我偶然发现了同样的问题.我已经创建了一个补丁来为我解决.它使具有默认值的字段为可选.
I stumbled upon the same problem. I've create a patch that solves it for me. It makes fields with a default value optional.
github/spray/spray-json/pull/56
更新:PR已更新,并且仍打开 github/spray/spray-json/pull/93
update: PR is updated and still open github/spray/spray-json/pull/93
更多推荐
用Spray
发布评论