-
Notifications
You must be signed in to change notification settings - Fork 190
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How To Provide DefaultValues For Fields In JSON While Marshalling Or Unmarshalling? #257
Comments
#56 |
hi how can use default parameters for case class spray Json ? |
Here's a workaround for now: import spray.json._
/** JsonFormat that offers backwards compatibility for newly-added fields
* by splicing in a default value when the new field key is not present in the json.
*
* This is important because spray does not support reading case class default arguments in all case.
* Without using this, reading old-format messages (for example from an external queue), will throw an exception.
*
* Currently, spray-json *will* use `= None` default values, so this class does not need to be used with `Option` fields.
*
* @see [[https://github.com/spray/spray-json/pull/93 Unmerged spray-json PR adding support for default arguments]]
*
* @example {{{
* case class Person(name: String, age: Int, newTagsField: Map[String, String] = Map.empty)
* object Person {
* implicit val sprayFormatForPerson: RootJsonFormat[Person] = {
* import JsonProtocol._
* MissingFieldFormat(jsonFormat3(Person.apply),
* "newTagsField" -> Map.empty[String, String])
* }
* }
* }}}
*/
class MissingFieldFormat[A](realFormat: RootJsonFormat[A],
defaults: Map[String, JsValue])
extends RootJsonFormat[A] {
override def read(json: JsValue): A = {
val fixedJson = try {
JsObject(defaults ++ json.asJsObject.fields)
} catch {
case _: DeserializationException =>
// Was not a JsObject, return and let it fail later with better error
json
}
realFormat.read(fixedJson)
}
override def write(obj: A): JsValue = realFormat.write(obj)
}
object MissingFieldFormat {
def apply[A, B: JsonWriter](
realFormat: RootJsonFormat[A],
default: (String, B)
): MissingFieldFormat[A] =
new MissingFieldFormat[A](realFormat, Map(default._1 -> default._2.toJson))
def apply[A, B: JsonWriter, C: JsonWriter](
realFormat: RootJsonFormat[A],
defaultB: (String, B),
defaultC: (String, C)
): MissingFieldFormat[A] =
new MissingFieldFormat[A](realFormat,
Map(
defaultB._1 -> defaultB._2.toJson,
defaultC._1 -> defaultC._2.toJson
))
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I want to have default value for x2 which is optional in my request payload.
https://scastie.scala-lang.org/shankarshastri/kM9yeuDgSMqstJkqS91Lvw
Any suggestions?
@ktoso @jrudolph
The text was updated successfully, but these errors were encountered: