用Spray

编程入门 行业动态 更新时间:2024-10-09 17:27:33
本文介绍了用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.

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

本文发布于:2023-11-25 06:06:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1628616.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Spray

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!