I'd like to parse Json file to read values that maybe optionally provided. In case they aren't provided, I have default values to fall-back on.
Clearly in this case, the end result is that I will certainly have a value at hand: either the one read from Json or the default. However as per my current knowledge of ScalaJson (please correct me if I'm wrong), I'll still have to use an Option[T] to hold it (because it may not be available directly in the Json file). In other words, I believe that while I can supply a default value, it will still have to be wrapped in an Option[T]
Is there a way to read an optional value (with default) without having to wrap it inside Option[T]? I'd like to inform in advance that I have no requirement (in the foreseeable future) to write (serialize) my data into Json, I only have to read it (deserialize) a Json.
To further elaborate my question:-
I'm using automatic-conversion using case classes, so instead of having to use this case class with the given reads converter
case class MyCaseClass(optString: Option[String] = Some("None"))
implicit val reads = Json.reads[MyCaseClass]
I'd like to use this case class
case class MyCaseClass(optStringWithDefault: String = "None")
Is it possible to write a read converter for this case class given the same Json source? Alternatively, is there a better design choice that can overcome this problem altogether?
I'm on
Scala 2.11.11PlayFramework 2.6