-
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?
Conversation
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 like the general idea, I think the API could be improved a bit and documentation is still missing.
[# // 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 comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The 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.
* 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 comment
The 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 true
to false
. Maybe just document allowExtraKeys
directly.
trait ProductFormatsInstances { self: ProductFormats with StandardFormats => | ||
def allowExtraKeys: Boolean = true |
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?
Fixes issue #165 .