Add an allowExtraKeys setting in ProductFormats#166
Add an allowExtraKeys setting in ProductFormats#166wsong wants to merge 2 commits intospray:masterfrom
Conversation
jrudolph
left a comment
There was a problem hiding this comment.
I like the general idea, I think the API could be improved a bit and documentation is still missing.
| 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.
Can we try to make the feature free when allowExtraKeys is false (default behavior). Maybe just make this a lazy val to make sure it is not initialized when not needed.
| 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.
Can we give more complete information when it fails? Missing set, given set and expected set of keys.
| * 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.
Do we actually need that trait? Seems a bit weird API to just change the flag from true to false. Maybe just document allowExtraKeys directly.
| import scala.collection.mutable.HashSet | ||
|
|
||
| trait ProductFormatsInstances { self: ProductFormats with StandardFormats => | ||
| def allowExtraKeys: Boolean = true |
There was a problem hiding this comment.
I wonder (but I'm not sure) if reversing the naming could be more clear? failOnExtraKeys or something like this?
Fixes issue #165 .