用Spray-json处理默认值的好方法是什么 [英] What is a good way to handle default values with spray-json

查看:110
本文介绍了用Spray-json处理默认值的好方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在某些情况下,对于案例类,默认值比可选参数有意义:

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.

https://github.com/spray/spray-json/pull/56

更新:PR已更新,并且仍打开 https://github.com/spray/spray-json/pull/93

update: PR is updated and still open https://github.com/spray/spray-json/pull/93

这篇关于用Spray-json处理默认值的好方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆