Added selective field getters for JsObject#63
Closed
cvrabie wants to merge 1 commit intospray:masterfrom
cvrabie:master
Closed
Added selective field getters for JsObject#63cvrabie wants to merge 1 commit intospray:masterfrom cvrabie:master
cvrabie wants to merge 1 commit intospray:masterfrom
cvrabie:master
Conversation
Author
|
This can be done better with the decorator pattern. I'll close and re-submit. |
Member
|
Actually, there's already a version 2 of spray-json in the work which has support for optional / default parameters. See 7352db2#L5R86 Unfortunately, we currently have no time to finish that work before spray 1.0 is released. |
Author
|
Is there any way I might help with that? It doesn't look like rocket science in itself though I'm not sure how many dependencies it has. |
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When writing a custom
JsonFormatthe currently proposed way of using pattern matching/extractors onJsObject.getFieldsis elegant and concise especially when all the fields are required.However we often meet the situation where, even though the fields in the domain object are required, we can provide default values for some of these fields, thus making some part of the incoming json optional.
For example, a
Userclass might require alanguageandtimezoneparameters but we we can use the system defaults if the incoming json does not specify these fields.I experimented with multiple options that would make easier the writing of
JsonFormats that allow optional fields and I think the best one is to add specialised field getters in the JsObject class. ThenflatMap/mapor a for comprehension can be used in theJsonFormatto extract the fields as demonstrated in the test.