-
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
Add an allowExtraKeys setting in ProductFormats #166
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,14 +16,21 @@ | |
|
||
package spray.json | ||
|
||
import scala.collection.mutable.HashSet | ||
|
||
trait ProductFormatsInstances { self: ProductFormats with StandardFormats => | ||
def allowExtraKeys: Boolean = true | ||
[# // Case classes with 1 parameters | ||
|
||
def jsonFormat1[[#P1 :JF#], T <: Product :ClassManifest](construct: ([#P1#]) => T): RootJsonFormat[T] = { | ||
val Array([#p1#]) = extractFieldNames(classManifest[T]) | ||
jsonFormat(construct, [#p1#]) | ||
} | ||
def jsonFormat[[#P1 :JF#], T <: Product](construct: ([#P1#]) => T, [#fieldName1: String#]): RootJsonFormat[T] = new RootJsonFormat[T]{ | ||
val knownFields = HashSet[String]() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we try to make the feature free when |
||
[# knownFields.add(fieldName1)# | ||
] | ||
|
||
def write(p: T) = { | ||
val fields = new collection.mutable.ListBuffer[(String, JsValue)] | ||
fields.sizeHint(1 * 2) | ||
|
@@ -34,6 +41,14 @@ trait ProductFormatsInstances { self: ProductFormats with StandardFormats => | |
def read(value: JsValue) = { | ||
[#val p1V = fromField[P1](value, fieldName1)# | ||
] | ||
if (!allowExtraKeys) { | ||
val jsObject = value.asJsObject() | ||
val keySet = jsObject.fields.keys.toSet | ||
val keySetDiff = keySet.diff(knownFields) | ||
if (!keySetDiff.isEmpty) { | ||
throw new DeserializationException(s"${keySetDiff.head} is not a known key", null, keySetDiff.toList) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we give more complete information when it fails? Missing set, given set and expected set of keys. |
||
} | ||
} | ||
construct([#p1V#]) | ||
} | ||
}# | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,7 +37,7 @@ trait ProductFormats extends ProductFormatsInstances { | |
} | ||
|
||
// helpers | ||
|
||
protected def productElement2Field[T](fieldName: String, p: Product, ix: Int, rest: List[JsField] = Nil) | ||
(implicit writer: JsonWriter[T]): List[JsField] = { | ||
val value = p.productElement(ix).asInstanceOf[T] | ||
|
@@ -152,3 +152,14 @@ trait NullOptions extends ProductFormats { | |
(fieldName, writer.write(value)) :: rest | ||
} | ||
} | ||
|
||
/** | ||
* This trait changes the behavior for reading JSON values. | ||
* If you mix in this trait into your custom JsonProtocol, JSON deserialization | ||
* will throw an error if the input contains keys that are not a field of the | ||
* target case class. | ||
*/ | ||
trait ExtraKeysOptions extends ProductFormats { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we actually need that trait? Seems a bit weird API to just change the flag from |
||
this: StandardFormats => | ||
override def allowExtraKeys = false | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder (but I'm not sure) if reversing the naming could be more clear?
failOnExtraKeys
or something like this?