Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a good way to handle default values with 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

In the first case I'd expect to be able to easily convert the following json to an instance:

{"color":"red"}

But with a standard jsonFormat2(Car), spray-json complains about missing value for numberOfWheels.

How do I work around this most cleanly?

like image 888
iwein Avatar asked Apr 01 '13 09:04

iwein


1 Answers

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

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

like image 168
Ruud Diterwich Avatar answered Nov 15 '22 08:11

Ruud Diterwich